目錄python
create table tf1( id int primary key auto_increment, x int, y int ); # 修改 mysql> alter table tf1 modify x char(4) default ''; mysql> alter table tf1 change y m char(4) default ''; # 增長 mysql> alter table 表名 add 新字段名 類型(長度) 約束; # 在全部字段末尾增長 eg> alter table tf1 add z int unsigned; mysql> alter table 表名 add 新字段名 類型(長度) 約束 first; # 在全部字段頭部增長 eg> alter table tf1 add a int unsigned first; mysql> alter table 表名 add 新字段名 類型(長度) 約束 after 舊字段名; # 在某個字段後面增長 eg> alter table tf1 add d int unsigned after x; mysql> alter table 表名 drop 字段名; # 刪除字段 eg> alter table tf1 drop a;
""" 一對一:丈夫-妻子,用戶-身份證,做者-做者詳情 一對多(多對一):部門-員工,班級-學生,書-出版社 多對多:老師-班級,課程-學生,出版社-做者 """ # 書-出版社-做者-做者詳情 外鍵分佈 # 外鍵是創建表與表關聯的字段,一般一個表的外鍵是另外一個表的主鍵(惟一鍵也能夠) 一、一對一:外鍵在任何一方均可以,此時外鍵要設置惟一鍵 """ 做者(author):id,name,sex,age,mobile 做者詳情(author_detail):id,info,address,author_id(外鍵) -------------------------------------------------- 做者(author):id,name,sex,age,mobile,detail_id(外鍵) 做者詳情(author_detail):id,info,address """ 二、一對多:外鍵必須放在多的一方,此時外鍵值不惟一 """ 書(book):id,name,price,publish_id(外鍵 ) 出版社(publish):id,name,address,phone """ 三、多對多:必定要建立第三張表(關係表),每個外鍵值不惟一,但能夠多個外鍵創建聯合惟一 """ 做者(auther):id,name,age 出版社(publish):id,name,address 做者與出版社關係表: id author_id publish_id 1 1 1 2 1 2 3 2 1 4 2 2 """
# 做者(author):id,name,sex,age,mobile,detail_id(外鍵) # 做者詳情(author_detail):id,info,address 一、外鍵的字段名能夠自定義(名字隨意),一般命名規範(關聯表_關聯字段) 二、外鍵要經過 foreign key 語法創建表與表之間的關聯 三、foreign key(所在表的外鍵字段) references 關聯表(關聯字段) # eg: foreign key(detail_id) references author_detail(id) 四、級聯關係 # 級聯更新:on update cascade # 級聯刪除:on delete cascade
重點:外鍵字段自己能夠惟一或不惟一,可是外鍵關聯的字段必須惟一。mysql
# 做者詳情表(被關聯表) create table author_detail( id int primary key auto_increment, info varchar(256), address varchar(256) ); # 做者表(關聯表) create table author( id int primary key auto_increment, name varchar(64) not null, mobile char(11) unique not null, # 惟一鍵,不能爲空 sex enum('男','女') default '男', age int default 0, detail_id int unique not null, foreign key(detail_id) references author_detail(id) ); # 必須先建立被關聯表數據,有關聯表外鍵關聯的記錄後,關聯表才能夠建立數據 mysql> insert into author_detail(info,address) values('Tom_info','Tom_address'); msyql> insert into author(name,mobile,detail_id) values('Tom','12233445566',1); mysql> insert into author_detail(info,address) values('Bob_info','Bob_address'); msyql> insert into author(name,mobile,detail_id) values('Bob','11223344556',2); # 修改關聯表author mysql> insert into author_detail(info,address) values('Tom_info_sup','Tom_address_sup'); mysql> update author set detail_id=3 where detail_id=2; # 有未被其餘數據關聯的數據,就能夠修改 # 刪除關聯表author mysql> delete from author where detail_id=3; # 直接刪除 # 修改被關聯表author_detail mysql> update author_detail set id=10 where id=1; # 沒法修改 # 刪除被關聯表 mysql> delete from author_detail where id=1; # 沒法刪除 """ 總結: 沒有級聯關係下: 增長:先增長被關聯表記錄,再增長關聯表記錄 刪除:先刪除關聯表記錄,再刪除被關聯表記錄 更新:關聯與被關聯表都沒法完成關聯的外鍵和主鍵的數據更新--(若是被關聯表記錄沒有被綁定,能夠修改) """
# 做者詳情表(被關聯表) create table author_detail( id int primary key auto_increment, info varchar(256), address varchar(256) ); # 做者表(關聯表) create table author( id int primary key auto_increment, name varchar(64) not null, mobile char(11) unique not null, # 惟一鍵,不能爲空 sex enum('男','女') default '男', age int default 0, detail_id int unique not null, foreign key(detail_id) references author_detail(id) on update cascade # 級聯更新 on delete cascade # 級聯刪除 ); # 增長數據,必須先建立被關聯表數據,有關聯表外鍵關聯的記錄後,關聯表才能夠建立數據 mysql> insert into author_detail(info,address) values('Tom_info','Tom_address'); msyql> insert into author(name,mobile,detail_id) values('Tom','12233445566',1); mysql> insert into author_detail(info,address) values('Bob_info','Bob_address'); msyql> insert into author(name,mobile,detail_id) values('Bob','11223344556',2); # 修改關聯表author mysql> update author set detail_id=3 where detail_id=2; # 失敗,3詳情不存在 mysql> insert into author_detail(info,address) values('Tom_info_sup','Tom_address_sup'); mysql> update author set detail_id=3 where detail_id=2; # 有未被其餘數據關聯的數據,就能夠修改 # 刪除關聯表author mysql> delete from author where detail_id=3; # 直接刪除 # 修改被關聯表author_detail mysql> update author_detail set id=10 where id=1; # 級聯修改,同步更新關聯表外鍵 # 刪除被關聯表 mysql> delete from author where detail_id=10; # 能夠刪除,對被關聯表無影響 msyql> insert into author(name,mobile,detail_id) values('Tom','12233445566',10); mysql> delete from author_detail where id=10; # 能夠刪除,將關聯表的記錄級聯刪除掉
# 一對多:外鍵必須放在多的一方,此時外鍵值不惟一 # 出版社(publish):id,name,address,phone create table publish( id int primary key auto_increment, name varchar(64), address varchar(256), phone char(20) ); # 書(book):id,name,price,publish_id(外鍵) create table book( id int primary key auto_increment, name varchar(64) not null, price decimal(5,2) default 0, publish_id int, # 一對多的外鍵不能設置惟一 foreign key(publish_id) references publish(id) on update cascade on delete cascade ); 一、增長數據 # 先增長被關聯表publish數據 mysql> insert into publish(name,address,phone) values('人民出版社','北京','010-110'), ('西交大出版社','西安','010-119'),('老男孩出版社','上海','010-120'); # 再增長關聯表book數據 mysql> insert into book(name,price,publish_id) values('西遊記',6.66,1), ('東遊記',8.66,1),('python從入門到放棄',2.66,2),('論程序員修養之道',3.66,3), ('好好活着',88.88,3); 二、更新數據: # 直接更新被關聯表(publish)的主鍵,關聯表(book)的外鍵會級聯更新 mysql> update publish set id=10 where id=1; # 直接更新關聯表(book)的外鍵,修改的值對應的被關聯表(publish)的主鍵若是存在,能夠更新成功,反之失敗 mysql> update book set publish_id=2 where id=4; # 成功 mysql> update book set publish_id=1 where id=4; # 失敗 三、刪除數據 # 刪除被關聯表,關聯表會被級聯刪除 mysql> delete from publish where id=2; # 刪除關聯表,被關聯表不會發生變化 mysql> delete from book where publish_id=3;
# 假設書與做者也是一對多關係,一個做者能夠出版多本書 create table book( id int primary key auto_increment, name varchar(64) not null, price decimal(5,2) default 0, publish_id int, # 一對多的外鍵不能設置惟一 foreign key(publish_id) references publish(id) on update cascade on delete cascade # 創建與做者一對多的外鍵關聯 author_id int, foreign key(author_id) references author(id) on update cascade on delete cascade );
# 多對多:必定要建立第三張表(關係表),每個外鍵值不惟一,但能夠多個外鍵創建聯合惟一 # 做者(auther):id,name,age create table author( id int primary key auto_increment, name varchar(64), age int unsigned default 0 ); # 出版社(publish):id,name,address create table publish( id int primary key auto_increment, name varchar(64), address varchar(256) ); # 做者與出版社關係表:id,author_id,publish_id create table author_publish( id int primary key auto_increment, # 關係表必定有多個外鍵關聯着多張表 # 關聯做者表 author_id int, foreign key(author_id) references author(id) on update cascade on delete cascade, # 關聯出版社表 publish_id int, foreign key(publish_id) references publish(id) on update cascade on delete cascade, # 創建兩個字段的聯合惟一 unique(author_id,publish_id) ); # 注意:關係表關聯着做者和出版社兩張表,在表結構上做者與出版社兩表間沒有任何關係 一、增:兩張被關聯表,沒有先後關係,但關係表必須在兩個表都提供數據後才能進行關係匹配 mysql> insert into author(name,age) values('ruakei',67),('egon',76),('nick',38); mysql> insert into publish(name,address) values('老男孩出版社','上海'),('小女孩出版社','北京'); # 增長關係表數據 mysql> insert into author_publish(author_id,publish_id) values(1,1),(1,2),(2,1),(2,2),(3,1); """ 一、操做關係表:增、刪、改,只要兩張被關係表有提供對應的操做數據,均可以操做成功,且對兩張被關係表沒有影響 二、操做兩張被關係表: 增:不會影響關係表 mysql> insert into publish(name,address) values('西交大出版社','西安'); 改:關係表都會級聯更新 mysql> update publish set id=10 where id=1; 刪:關係表都會級聯刪除 mysql> delete from author where name='ruakei'; """