day45 資料庫學習
今天學習了多表關聯、修改表、複製表和蠕蟲複製
一、多表關聯:
如何找出兩張表之間的關係
1、先站在左表的角度去找
是否左表的多條記錄可以對應右表的一條記錄,如果是,則證明左表的一個欄位foreign key 右表一個欄位(通常是id)
2、再站在右表的角度去找
是否右表的多條記錄可以對應左表的一條記錄,如果是,則證明右表的一個欄位 foreign key 左表一個欄位(通常是id)
3、總結:
多對一:
如果只有步驟1成立,則是左表多對一右表
如果只有步驟2成立,則是右表多對一左表
多對多:
如果步驟1和2同時成立,則證明這兩張表是一個雙向的多對一,即多對多,需要定義一個這兩張表的關係表來專門存放二者的關係
一對一:
如果1和2都不成立,而是左表的一條記錄唯一對應右表的一條記錄,反之亦然。這種情況很簡單,就是在左表foreign key右表的基礎上,將左表的外來鍵欄位設定成unique即可
建立表之間的關係
#一對多或稱為多對一
三張表:出版社,作者資訊,書
一對多(或多對一):一個出版社可以出版多本書
關聯方式:foreign key
create table press( id int primary key auto_increment, name varchar(20) ); create table book( id int primary key auto_increment, name varchar(20), press_id int not null, foreign key(press_id) references press(id) on delete cascade on update cascade ); insert into press(name) values ('北京工業地雷出版社'), ('人民音樂不好聽出版社'), ('智慧財產權沒有用出版社'); insert into book(name,press_id) values ('九陽神功',1), ('九陰真經',2), ('九陰白骨爪',2), ('獨孤九劍',3), ('降龍十巴掌'), ('葵花寶典',3);
多對多
三張表:出版社,作者資訊,書
多對多:一個作者可以寫多本書,一本書也可以用多個作者,雙向的一對多,即多對多
關聯方式:foreign key +一張新的表
create table auther( id int primary key auto_increment, name varchar(20) ); create table book( id int primary key auto_increment, name varchar(20) ); #這張表就存放作者表與書表的關係,即查詢二者的關係查這表就可以了 create table author2book( id int not null unique auto_increment, author_id int not null, book_id int not null, constraint fk_author foreign key(author_id) references author(id) on delete cascade on update cascade, constraint fk_book foreign key(book_id) references book(id) on delete cascade on update cascade, primary key(author_id,book_id) ); #插入四個作者,id依次排開 insert into author(name) values('egon'),('alex'), ('yuanhao'),('wpq'); #插入六本書,id依次排開 insert into book(name) values('九陽神功'), ('九陰真經'), ('九陰白骨爪'), ('獨孤九劍'), ('降龍十巴掌'), ('葵花寶典'); insert into author2book(author_id,book_id) values(1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (2,1), (2,6), (3,4), (3,5), (3,6), (4,1);
一對一:
兩張表:學生表和客戶表
一對一:一個學生是一個客戶,一個客戶有可能變成一個學校,即一對一的關係
關聯方式:foreign key +unique
#一定是student來foreign key表customer,這樣就保證了: #1 學生一定是一個客戶, #2 客戶不一定是學生,但有可能成為一個學生 create table customer( id int primary key auto_increment, name varchar(20) not null, qq varchar(10) not null, phone char(16) not null ); create table student( id int primary key auto_increment, class_name varchar(20) not null, customer_id int unique, #該欄位一定要是唯一的 foreign key(customer_id) references customer(id)#外來鍵的欄位一定要保證unique on delete cascade on update cascade ); #增加客戶 insert into customer(name,qq,phone) values ('李飛機','1224542',1234567891), ('王大炮','1224542',1234567891), ('守榴彈','1224542',1234567891), ('吳坦克','1224542',1234567891), ('戰地雷','1224542',1234567891); #增加學生 insert into student(class_name,customer_id) values ('脫產3期',3), ('週末19期',4), ('週末19期',5);
二、修改表、複製表和蠕蟲複製:
1.修改表
add 新增欄位
modify 修改欄位型別
change 修改欄位名稱 或 型別
drop 刪除欄位
rename 改表名
2.複製表
create table 新的表名 select *from 源表名;
資料 結構 約束不能複製
當條件不成立時 只複製表結構
create table 新的表名 select * from 源表名 where 1 = 2;
create table stu_copy2 select * from student1 where 1 = 2;
3.蠕蟲複製
即自我複製
insert into 表名稱 select *from 表名;
如果有主鍵 避開主鍵欄位
insert into 表名稱(其他欄位) select 其他欄位 from 表名;