日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL 主外键约束与标准SQL不同的地方

發布時間:2024/4/17 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL 主外键约束与标准SQL不同的地方 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

標準SQL的外鍵約束條件

  1): 子表引用父表的主鍵

drop table if exists child,parent;create table if not exists parent(id int not null auto_increment primary key,v int);create table if not exists child(id int not null auto_increment primary key,parent_id int not null,v int,constraint fk__child__parent_id foreign key (parent_id) references parent(id) );insert into parent(id,v) values(1,100); insert into child(parent_id,v) values(1,1000); insert into child(parent_id,v) values(1,1000);select * from parent; +----+------+ | id | v | +----+------+ | 1 | 100 | +----+------+select * from child; +----+-----------+------+ | id | parent_id | v | +----+-----------+------+ | 1 | 1 | 1000 | | 2 | 1 | 1000 | +----+-----------+------+

?

  2): 子表引用交表的唯一索引

create table if not exists parent(id int not null,v int,constraint unique index uix__parent_id (id));create table if not exists child(id int not null auto_increment primary key,parent_id int not null,v int,constraint fk__child__parent_id foreign key (parent_id) references parent(id) );insert into parent(id,v) values(1,100); insert into child(parent_id,v) values(1,1000); insert into child(parent_id,v) values(1,1000);select * from parent; +----+------+ | id | v | +----+------+ | 1 | 100 | +----+------+select * from child; +----+-----------+------+ | id | parent_id | v | +----+-----------+------+ | 1 | 1 | 1000 | | 2 | 1 | 1000 | +----+-----------+------+

?

innodb在標準SQL上做的擴展

  1): 只要在父表上有在對應的列上建索引,那么這個列就能在子表中引用

create table if not exists parent(id int not null auto_increment primary key,v int,index uix__parent_v (v) -- 只要父表上有索引就行 );create table if not exists child(id int not null auto_increment primary key,parent_v int not null,v int,constraint fk__child__parent_v foreign key (parent_v) references parent(v) -- 在子表中引用 );insert into parent(id,v) values(1,100); insert into parent(id,v) values(2,100);insert into child(parent_v,v) values(100,2000); insert into child(parent_v,v) values(100,2000);select * from parent; +----+------+ | id | v | +----+------+ | 1 | 100 | | 2 | 100 | +----+------+select * from child; +----+----------+------+ | id | parent_v | v | +----+----------+------+ | 1 | 100 | 2000 | | 2 | 100 | 2000 | +----+----------+------+

?

?

我的評介

  主外鍵約束在標準SQL下體現的是一種一對多的關系,但是經過MySQL的拓展之后可以表現出“多對多”的關系;雖然MySQL這樣

  的設計有一定的靈活性,個人覺得最好還是使用標準SQL的方式。

?

學習交流

?

?-----------------------------http://www.sqlpy.com-------------------------------------------------

?-----------------------------http://www.sqlpy.com-------------------------------------------------

?

總結

以上是生活随笔為你收集整理的MySQL 主外键约束与标准SQL不同的地方的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。