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

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

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

一個表中的 foreign key 指向另一個表中的 primary key即可進行進行關(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表進行關(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)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频