69pao国产精品视频-久久精品一区二区二三区-精品国产精品亚洲一本大道-99国产综合一区久久

mysql如何關(guān)聯(lián)表

mysql如何關(guān)聯(lián)表

在mysql中怎么進(jìn)行多表關(guān)聯(lián),在mysql里面可以創(chuàng)建多個(gè)表格,還可以讓每個(gè)表格互相關(guān)聯(lián),這里的關(guān)聯(lián)必須要有一個(gè)表頭和另一個(gè)表格的表頭來進(jìn)行關(guān)聯(lián)數(shù)據(jù),要用到foreign key的方法來進(jìn)行關(guān)聯(lián)。

一個(gè)表中的 foreign key 指向另一個(gè)表中的 primary key即可進(jìn)行進(jìn)行關(guān)聯(lián)。

示例:

創(chuàng)建dog表格并插入數(shù)據(jù):

create table dog(
id int primary key,
name varchar(10)
);

inset into dog values(1, 'uuu');
inset into dog values(2, 'ppp');

創(chuàng)建cat表并插入數(shù)據(jù):

create table cat(
id int primary key,
name varchar(10)
);

inset into cat values(1, 'ttt');
inset into cat values(2, 'vvv');

創(chuàng)建zoo表并與dog、cat表進(jìn)行關(guān)聯(lián):

create table zoo(
id int primay key,
dog_id int not null,
cat_id int not null,
foreign key(dog_id) references dog(id)
on delete cascade
on update cascade,
foreign key(cat_id) references cat(id)
on delete cascade
on update cascade);

 

下一節(jié):mysql怎么查詢記錄是否存在

數(shù)據(jù)庫技術(shù)

相關(guān)文章