mysql用alter创建外键_MySQL入门(alter语法 与 外键)
MySQL入門(mén)(三)
字段的修改、添加、與刪除
修改表字段使用alter table語(yǔ)句,謹(jǐn)記!
create table tf1(
id int primary key auto_increment,
x int,
y int
);
# 修改
alter table tf1 modify x char(4) default '';
alter table tf1 change y m char(4) default '';
# 增加
mysql>: alter table 表名 add 字段名 類(lèi)型[(長(zhǎng)度) 約束]; # 末尾
eg>: alter table tf1 add z int unsigned;
mysql>: alter table 表名 add 字段名 類(lèi)型[(寬度) 約束] first; # 首位
eg>: alter table tf1 add a int unsigned first;
mysql>: alter table 表名 add 字段名 類(lèi)型[(寬度) 約束] after 舊字段名; # 某字段后
eg>: alter table tf1 add xx int unsigned after x;
mysql>: alter table 表名 drop 字段名; # 刪除字段
eg>: alter table tf1 drop a;
多表關(guān)系(外鍵)
外鍵基礎(chǔ)知識(shí)
"""
多表關(guān)系主要如下:
一對(duì)一:外鍵在任何一方都可以,此時(shí)外鍵要設(shè)置 唯一鍵
一對(duì)多:外鍵必須放在多的一方,此時(shí)外鍵值不唯一
多對(duì)多:一定要?jiǎng)?chuàng)建第三張表(關(guān)系表),每一個(gè)外鍵值不唯一,看可以多個(gè)外鍵建立聯(lián)合唯一
"""
# 1、外鍵的 字段名 可以自定義(名字隨意),通常命名規(guī)范(關(guān)聯(lián)表_關(guān)聯(lián)字段)
# 2、外鍵要通過(guò) foreign key 語(yǔ)法建立表與表之間的關(guān)聯(lián)
# 3、[constraint 外鍵名 ]foreign key(所在表的外鍵字段) references 關(guān)聯(lián)表(關(guān)聯(lián)字段)
# eg:foreign key(detail_id) references author_detail(id)
# 4、級(jí)聯(lián)關(guān)系
#級(jí)聯(lián)更新 on update cascade
# 級(jí)聯(lián)刪除 on delete cascade
# 重點(diǎn):外鍵字段本身可以唯一或不唯一,但是外鍵關(guān)聯(lián)的字段一定唯一
"""
非級(jí)聯(lián)的兩張關(guān)系表,若數(shù)據(jù)已經(jīng)被從表引用,則主表的那條數(shù)據(jù)無(wú)法更新或刪除,只有先刪了從表的數(shù)據(jù)才能操作主表的數(shù)據(jù);
有級(jí)聯(lián)的兩張關(guān)系表,主表的數(shù)據(jù)更新或刪除,會(huì)同時(shí)影響從表,會(huì)跟著一起更新或刪除。
"""
"""
了解內(nèi)容:(除級(jí)聯(lián)外的其他reference_option)
set null:從父表中刪除或更新該行,并將子表中的一個(gè)或多個(gè)外鍵列設(shè)置為NULL。ON DELETE SET NULLON UPDATE SET NULL。
restrict:拒絕父表的刪除或更新操作。指定 RESTRICT(或NO ACTION)與省略O(shè)N DELETEor ON UPDATE子句相同。
no action :標(biāo)準(zhǔn)SQL中的關(guān)鍵字。在MySQL中等效于RESTRICT。
set default:
"""
設(shè)置外鍵
# 建表語(yǔ)句
CREATE TABLE parent (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON DELETE CASCADE
);
# 升級(jí)版 product_order表具有其他兩個(gè)表的外鍵。一個(gè)引用product的兩個(gè)字段,一個(gè)引用customer的一列:
CREATE TABLE product (
category INT NOT NULL, id INT NOT NULL,
price DECIMAL,
PRIMARY KEY(category, id)
) ENGINE=INNODB;
CREATE TABLE customer (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE product_order (
no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
PRIMARY KEY(no),
INDEX (product_category, product_id),
INDEX (customer_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (customer_id)
REFERENCES customer(id)
) ENGINE=INNODB;
添加外鍵
如果我們忘記設(shè)置外鍵也可以后續(xù)添加(如果沒(méi)有那個(gè)字段就需要先手動(dòng)添加):
ALTER TABLE tbl_name
ADD [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (col_name, ...)
REFERENCES tbl_name (col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]
刪除外鍵
我們可以使用alter table語(yǔ)法來(lái)刪除外鍵:
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;
若是不知道外鍵名稱可以使用show create table語(yǔ)法來(lái)查看:
總結(jié)
以上是生活随笔為你收集整理的mysql用alter创建外键_MySQL入门(alter语法 与 外键)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java实现各种算法
- 下一篇: './mysql-bin.index'