欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

mysql從表外鍵怎么寫 mysql數(shù)據(jù)庫建表外鍵

mysql怎么定義外鍵

定義外鍵的方法和詳細(xì)的操作步驟如下:

成都創(chuàng)新互聯(lián)公司:2013年至今為各行業(yè)開拓出企業(yè)自己的“網(wǎng)站建設(shè)”服務(wù),為1000多家公司企業(yè)提供了專業(yè)的網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)和網(wǎng)站推廣服務(wù), 定制網(wǎng)站制作由設(shè)計(jì)師親自精心設(shè)計(jì),設(shè)計(jì)的效果完全按照客戶的要求,并適當(dāng)?shù)奶岢龊侠淼慕ㄗh,擁有的視覺效果,策劃師分析客戶的同行競爭對手,根據(jù)客戶的實(shí)際情況給出合理的網(wǎng)站構(gòu)架,制作客戶同行業(yè)具有領(lǐng)先地位的。

1、第一步,創(chuàng)建一個(gè)主從表,如下圖所示,然后進(jìn)入下一步。

2、其次,完成上述步驟后,選擇主表,然后單擊設(shè)計(jì)表進(jìn)入表設(shè)計(jì)界面,如下圖所示,然后進(jìn)入下一步。

3、接著,完成上述步驟后,單擊外鍵進(jìn)入外鍵的設(shè)置界面,如下圖所示,然后進(jìn)入下一步。

4、然后,完成上述步驟后,設(shè)置外鍵名稱,然后選擇主表的外鍵字段,如下圖所示,然后進(jìn)入下一步。

5、隨后,完成上述步驟后,設(shè)置與數(shù)據(jù)庫,表名稱和從屬表的單詞相對應(yīng)的外鍵字段,如下圖所示,然后進(jìn)入下一步。

6、最后,完成上述步驟后,單擊保存即可,如下圖所示。這樣,問題就解決了。

mysql外鍵怎么寫

mysql添加外鍵:

為已經(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數(shù)據(jù)庫建表外鍵
本文路徑:http://chinadenli.net/article30/ddosdso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)域名注冊、全網(wǎng)營銷推廣、虛擬主機(jī)網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航

廣告

聲明:本網(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)站托管運(yùn)營