mysql外碼怎么寫 mysql 外表

mysql外鍵怎么寫

mysql添加外鍵:

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、秦都網(wǎng)站維護(hù)、網(wǎng)站推廣。

為已經(jīng)添加好的數(shù)據(jù)表添加外鍵:

語法:alter table 表名 add constraint FK_ID foreign key(你的外鍵字段名) REFERENCES 外表表名(對應(yīng)的表的主鍵字段名);

例: alter table tb_active add constraint FK_ID foreign key(user_id) REFERENCES tb_user(id)

//FK_ID是外鍵的名稱

/*

CREATE TABLE `tb_active` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,

`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,

`user_id` int(11) NOT NULL,

PRIMARY KEY (`id`),

KEY `user_id` (`user_id`),

KEY `user_id_2` (`user_id`),

CONSTRAINT `FK_ID` FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

*/

mysql外鍵約束怎么寫

你好朋友

1.簡介

外鍵表示一個(gè)表中的一個(gè)字段被另外一個(gè)表中的字段應(yīng)用.外鍵對相關(guān)表中的數(shù)據(jù)造成了限制,使MySQL 能夠保證參照完整性.

在MySQL 中,InnoDB 存儲(chǔ)引擎支持外鍵.在一張表中,可以存在多個(gè)外鍵.

外鍵的創(chuàng)建可以在創(chuàng)建表的時(shí)候創(chuàng)建,也可以在創(chuàng)建表之后增加(考慮數(shù)據(jù)的完整性問題).

父表:外鍵所指向的表.

字表:相對于父表,擁有外鍵的表.

2.語法

create 語法

create table table_name(

column_1,

column_2,

....

constraint constraint_name foreign key (column_name)

references parent_table(column_name)

on delete action

on update action

) engine=InnoDB default charset utf8;

constraint 子句允許為外鍵定義一個(gè)名稱,如果不寫,MySQL 自動(dòng)生成一個(gè)名稱

foreign key 子句指定子表中要應(yīng)用父表的列.注意:MySQL 會(huì)自動(dòng)創(chuàng)建一個(gè)基于外鍵的索引.

references 子句指定父表中的被引用字段.foreign key 和references 指定的列數(shù)必須相同.

on delete: 定義當(dāng)父表中的記錄被刪除時(shí),子表的記錄應(yīng)該執(zhí)行的動(dòng)作.action包括:

on delete restrict:(默認(rèn)),父表不能刪除一個(gè)已經(jīng)被子表引用的記錄.

on delete no action:等同與on delete restrict

on delete cascade: 級聯(lián)模式,父表刪除后,對應(yīng)子表關(guān)聯(lián)的數(shù)據(jù)也跟著被刪除

on delete set null:置空模式,父表刪除后,對應(yīng)子表關(guān)聯(lián)的外鍵值被設(shè)置為NULL,需要注意的是,如果子表的外鍵設(shè)置not null ,則不能使用這種模式,因?yàn)闀?huì)相互沖突.

on update:定義父表中的記錄更新時(shí),子表的記錄應(yīng)該執(zhí)行的動(dòng)作.action 包括:

on update restrict:(默認(rèn)),父表不能更新一個(gè)已經(jīng)被子表引用的記錄.

on update no action:等同與on delete restrict

on update cascade: 級聯(lián)模式,父表更新后,對應(yīng)子表關(guān)聯(lián)的數(shù)據(jù)也跟著被更新

on update set null:置空模式,父表更新后,對應(yīng)子表關(guān)聯(lián)的外鍵值被設(shè)置為NULL,需要注意的是,如果子表的外鍵設(shè)置not null ,則不能使用這種模式.

alter 語法

-- 添加外鍵

alter table table_name add constraint constraint_name

foreign key column_name

references parent_table(column_name)

on delete action

on update action

-- 刪除外鍵

alter table table_name drop constraint_name;

-- 如果沒有顯式的定義名字,可以使用如下命令獲取

show create table table_name;

3.演示

構(gòu)造兩張表categoryes 和products.每個(gè)類別有多種產(chǎn)品,而每個(gè)產(chǎn)品只屬于一個(gè)類別.

-- 設(shè)置 類別表 categoryes 和產(chǎn)品表 products

create table categoryes(

c_id int not null auto_increment,

c_name varchar(45) not null,

c_description text,

primary key (c_id)

) engine=InnoDB default charset utf8 comment '類別表';

create table products(

p_id int not null auto_increment,

p_name varchar(45) not null,

p_price decimal(8,4),

c_id int,

primary key (p_id),

constraint fk_products_categoryes

foreign key (c_id)

references categoryes(c_id)

on delete set null

on update cascade

) engine=InnoDB default charset utf8 comment '產(chǎn)品表';

在這兩張表的基礎(chǔ)上,新生成一張vendors 供應(yīng)商表,并更新products字段

-- 新生成一張表 供應(yīng)商 vendors ,并為 products 新添加字段 v_id 外鍵

-- 引用 vendors.v_id

create table vendors(

v_id int not null auto_increment,

v_name varchar(45),

primary key (v_id)

) engine=InnoDB default charset utf8 comment '供應(yīng)商';

alter table products add column v_id int not null;

alter table products add

constraint fk_products_vendors foreign key (v_id)

references vendors(v_id)

on delete no action

on update cascade;

望采納祝你好運(yùn)

mysql組合主碼外碼怎么寫

mysql通過一個(gè)主碼,多個(gè)外碼來組合。根據(jù)查詢相關(guān)資料信息,mysql選擇一個(gè)作為查詢、插入或刪除元組的操作變量,設(shè)置為主碼,其他數(shù)據(jù)設(shè)置為外碼。mysql中每個(gè)關(guān)系必定有且僅有一個(gè)主碼。

分享文章:mysql外碼怎么寫 mysql 外表
文章起源:http://bm7419.com/article32/dohgjpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、面包屑導(dǎo)航品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)