最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MariaDB數(shù)據(jù)庫的外鍵約束實例詳解

 更新時間:2018年09月04日 11:54:14   作者:renpingsheng  
約束保證了數(shù)據(jù)的完整性和一致性。下面這篇文章主要給大家介紹了關于MariaDB數(shù)據(jù)庫的外鍵約束的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧

外鍵

外鍵的用途是確保數(shù)據(jù)的完整性。它通常包括以下幾種:

1 實體完整性,確保每個實體是唯一的(通過主鍵來實施)

2 域完整性,確保屬性值只從一套特定可選的集合里選擇

3 關聯(lián)完整性,確保每個外鍵或是NULL(如果允許的話)或含有與相關主鍵值相配的值

1.什么是外鍵約束

與主鍵約束不同,創(chuàng)建外鍵約束不會自動創(chuàng)建對應的索引。 但是由于以下原因,對外鍵手動創(chuàng)建索引通常是有用的:

  • 當在查詢中組合相關表中的數(shù)據(jù)時,經常在聯(lián)接條件中使用外鍵列,方法是將一個表的外鍵約束中的一列或多列與另一個表中的主鍵列或唯一鍵列匹配。 索引使 數(shù)據(jù)庫引擎 可以在外鍵表中快速查找相關數(shù)據(jù)。 但是,創(chuàng)建此索引并不是必需的。 即使沒有對兩個相關表定義主鍵或外鍵約束,也可以對來自這兩個表中的數(shù)據(jù)進行組合,但兩個表間的外鍵關系說明已用其鍵作為條件對其進行了優(yōu)化,以便組合到查詢中。
  • 對主鍵約束的更改可由相關表中的外鍵約束檢查。

外鍵約束(foreign key)就是表與表之間的某種約定的關系,由于這種關系的存在,我們能夠讓表與表之間的數(shù)據(jù),更加的完整,關連性更強。

關于數(shù)據(jù)表的完整性和關連性,可以舉個例子

有二張表,一張是用戶表,一張是訂單表:

1.如果我刪除了用戶表里的用戶,那么訂單表里面跟這個用戶有關的數(shù)據(jù),就成了無頭數(shù)據(jù)了,不完整了。
2.如果我在訂單表里面,隨便插入了一條數(shù)據(jù),這個訂單在用戶表里面,沒有與之對應的用戶。這樣數(shù)據(jù)也不完整了。

如果有外鍵的話,就方便多了,可以不讓用戶刪除數(shù)據(jù),或者刪除用戶的話,通過外鍵同樣刪除訂單表里面的數(shù)據(jù),這樣也能讓數(shù)據(jù)完整。

通過外鍵約束,每次插入或更新數(shù)據(jù)表時,都會檢查數(shù)據(jù)的完整性。

2.創(chuàng)建外鍵約束

2.1 方法一:通過create table創(chuàng)建外鍵

語法:

create table 數(shù)據(jù)表名稱(
...,
[CONSTRAINT [約束名稱]] FOREIGN KEY [外鍵字段] 
 REFERENCES [外鍵表名](外鍵字段,外鍵字段2…..)
 [ON DELETE CASCADE ]
 [ON UPDATE CASCADE ]
)

參數(shù)的解釋:

RESTRICT: 拒絕對父表的刪除或更新操作。
CASCADE: 從父表刪除或更新且自動刪除或更新子表中匹配的行。ON DELETE CASCADE和ON UPDATE CASCADE都可用

注意:on update cascade是級聯(lián)更新的意思,on delete cascade是級聯(lián)刪除的意思,意思就是說當你更新或刪除主鍵表,那外鍵表也會跟隨一起更新或刪除。

精簡化后的語法:

foreign key 當前表的字段 references 外部表名 (關聯(lián)的字段) type=innodb 

2.1.1 插入測試數(shù)據(jù)

例子:我們創(chuàng)建一個數(shù)據(jù)庫,包含用戶信息表和訂單表

MariaDB [book]> create database market;  # 創(chuàng)建market數(shù)據(jù)庫
Query OK, 1 row affected (0.00 sec)

MariaDB [book]> use market;    # 使用market數(shù)據(jù)庫
Database changed

MariaDB [market]> create table userprofile(id int(11) not null auto_increment, name varchar(50) not null default '', sex int(1) not null default '0', primary key(id))ENGINE=innodb; # 創(chuàng)建userprofile數(shù)據(jù)表,指定使用innodb引擎
Query OK, 0 rows affected (0.07 sec)

MariaDB [market]> create table user_order(o_id int(11) auto_increment, u_id int(11) default '0', username varchar(50), money int(11), primary key(o_id), index(u_id), foreign key order_f_key(u_id) references userprofile(id) on delete cascade on update cascade);  # 創(chuàng)建user_order數(shù)據(jù)表,同時為user_order表的u_id字段做外鍵約束,綁定userprofile表的id字段
Query OK, 0 rows affected (0.04 sec)

MariaDB [market]> insert into userprofile(name,sex)values('HA',1),('LB',2),('HPC',1); # 向userprofile數(shù)據(jù)表插入三條記錄
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

MariaDB [market]> select * from userprofile; # 查詢userprofile數(shù)據(jù)表的所有記錄
+----+------+-----+
| id | name | sex |
+----+------+-----+
| 1 | HA | 1 |
| 2 | LB | 2 |
| 3 | HPC | 1 |
+----+------+-----+
3 rows in set (0.00 sec)

MariaDB [market]> insert into user_order(u_id,username,money)values(1,'HA',234),(2,'LB',146),(3,'HPC',256);   # 向user_order數(shù)據(jù)表插入三條記錄
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0

MariaDB [market]> select * from user_order;  # 查詢user_order數(shù)據(jù)表的所有記錄
+------+------+----------+-------+
| o_id | u_id | username | money |
+------+------+----------+-------+
| 1 | 1 | HA | 234 |
| 2 | 2 | LB | 146 |
| 3 | 3 | HPC | 256 |
+------+------+----------+-------+
3 rows in set (0.00 sec)

MariaDB [market]> select id,name,sex,money,o_id from userprofile,user_order where id=u_id; # 聯(lián)表查詢
+----+------+-----+-------+------+
| id | name | sex | money | o_id |
+----+------+-----+-------+------+
| 1 | HA | 1 | 234 | 1 |
| 2 | LB | 2 | 146 | 2 |
| 3 | HPC | 1 | 256 | 3 |
+----+------+-----+-------+------+
3 rows in set (0.03 sec)

2.1.2 測試級聯(lián)刪除

MariaDB [market]> delete from userprofile where id=1; # 刪除user表中id為1的數(shù)據(jù)
Query OK, 1 row affected (0.01 sec)

MariaDB [market]> select id,name,sex,money,o_id from userprofile,user_order where id=u_id;
+----+------+-----+-------+------+
| id | name | sex | money | o_id |
+----+------+-----+-------+------+
| 2 | LB | 2 | 146 | 2 |
| 3 | HPC | 1 | 256 | 3 |
+----+------+-----+-------+------+
2 rows in set (0.00 sec)

MariaDB [market]> select * from user_order;   # 查看order表的數(shù)據(jù)
+------+------+----------+-------+
| o_id | u_id | username | money |
+------+------+----------+-------+
| 2 | 2 | LB | 146 |
| 3 | 3 | HPC | 256 |
+------+------+----------+-------+
3 rows in set (0.00 sec)

2.1.3 測試級聯(lián)更新

更新數(shù)據(jù)之前的狀態(tài)

MariaDB [market]> select * from userprofile;  # 查看userprofile表的數(shù)據(jù)
+----+------+-----+
| id | name | sex |
+----+------+-----+
| 2 | LB | 2 |
| 3 | HPC | 1 |
+----+------+-----+
3 rows in set (0.00 sec)

MariaDB [market]> select * from user_order;   # 查看order表的數(shù)據(jù)
+------+------+----------+-------+
| o_id | u_id | username | money |
+------+------+----------+-------+
| 2 | 2 | LB | 146 |
| 3 | 3 | HPC | 256 |
+------+------+----------+-------+
3 rows in set (0.00 sec)

更新數(shù)據(jù)

MariaDB [market]> update userprofile set id=6 where id=2; # 把userprofile數(shù)據(jù)表中id為2的用戶改為id為6
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0

更新數(shù)據(jù)后的狀態(tài)

MariaDB [market]> select id,name,sex,money,o_id from userprofile,user_order where id=u_id; # 聯(lián)表查詢,可以看出表中已經沒有id為2的用戶了
+----+------+-----+-------+------+
| id | name | sex | money | o_id |
+----+------+-----+-------+------+
| 6 | LB | 2 | 146 | 2 |
| 3 | HPC | 1 | 256 | 3 |
+----+------+-----+-------+------+
2 rows in set (0.00 sec)

MariaDB [market]> select * from userprofile;  # 查看userprofile表的數(shù)據(jù),id只剩下3和6
+----+------+-----+
| id | name | sex |
+----+------+-----+
| 3 | HPC | 1 |
| 6 | LB | 2 |
+----+------+-----+
2 rows in set (0.00 sec)

MariaDB [market]> select * from user_order;   # 查看user_order表的數(shù)據(jù),u_id也改為6
+------+------+----------+-------+
| o_id | u_id | username | money |
+------+------+----------+-------+
| 2 | 6 | LB | 146 |
| 3 | 3 | HPC | 256 |
+------+------+----------+-------+
2 rows in set (0.00 sec)

2.1.4 測試數(shù)據(jù)完整性

MariaDB [market]> insert into user_order(u_id,username,money)values(5,"XJ",345); # 單獨向user_order數(shù)據(jù)表中插入數(shù)據(jù),插入數(shù)據(jù)失敗
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`market`.`user_order`, CONSTRAINT `user_order_ibfk_1` FOREIGN KEY (`u_id`) REFERENCES `userprofile` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

在上面的例子中,user_order表的外鍵約束,user_order表受userprofile表的約束

在user_order里面插入一條數(shù)據(jù)u_id為5用戶,在userprofile表里面根本沒有,所以插入數(shù)據(jù)失敗

先向userprofile表中插入記錄,再向user_order表中插入記錄就可以了

MariaDB [market]> insert into userprofile values(5,"XJ",1);  # 先向userprofile數(shù)據(jù)表中插入id為5的記錄,插入數(shù)據(jù)成功
Query OK, 1 row affected (0.01 sec)

MariaDB [market]> insert into user_order(u_id,username,money) values(5,"XJ",345); # 再向user_order數(shù)據(jù)表中插入數(shù)據(jù),成功
Query OK, 1 row affected (0.00 sec)

MariaDB [market]> select * from userprofile;  # 查詢userprofile數(shù)據(jù)表中的所有記錄
+----+------+-----+
| id | name | sex |
+----+------+-----+
| 3 | HPC | 1 |
| 5 | XJ | 1 |
| 6 | LB | 2 |
+----+------+-----+
3 rows in set (0.00 sec)

MariaDB [market]> select * from user_order;   # 查詢user_order數(shù)據(jù)表中的所有記錄
+------+------+----------+-------+
| o_id | u_id | username | money |
+------+------+----------+-------+
| 2 | 6 | LB | 146 |
| 3 | 3 | HPC | 256 |
| 5 | 5 | XJ | 345 |
+------+------+----------+-------+
3 rows in set (0.01 sec)

2.2 方法二:通過alter table創(chuàng)建外鍵和級聯(lián)更新,級聯(lián)刪除

語法:

alter table 數(shù)據(jù)表名稱 add 
 [constraint [約束名稱] ] foreign key (外鍵字段,..) references 數(shù)據(jù)表(參照字段,...) 
 [on update cascade|set null|no action]
 [on delete cascade|set null|no action]
)

例子:

MariaDB [market]> create table user_order1(o_id int(11) auto_increment,u_id int(11) default "0",username varchar(50),money int(11),primary key(o_id),index(u_id));  # 創(chuàng)建user_order1數(shù)據(jù)表,創(chuàng)建表時不使用外鍵約束
Query OK, 0 rows affected (0.11 sec)

MariaDB [market]> show create table user_order1;  # 查看user_order1數(shù)據(jù)表的創(chuàng)建信息,沒有外鍵約束
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                     |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_order1 | CREATE TABLE `user_order1` (
 `o_id` int(11) NOT NULL AUTO_INCREMENT,
 `u_id` int(11) DEFAULT '0',
 `username` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
 `money` int(11) DEFAULT NULL,
 PRIMARY KEY (`o_id`),
 KEY `u_id` (`u_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

MariaDB [market]> alter table user_order1 add foreign key(u_id) references userprofile(id) on delete cascade on update cascade;  # 使用alter修改user_order1數(shù)據(jù)表,為user_order1數(shù)據(jù)表添加外鍵約束
Query OK, 0 rows affected (0.05 sec)  
Records: 0 Duplicates: 0 Warnings: 0

MariaDB [market]> show create table user_order1;  # 查看user_order1數(shù)據(jù)表的創(chuàng)建信息,已經添加了外鍵約束
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                    |
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_order1 | CREATE TABLE `user_order1` (
 `o_id` int(11) NOT NULL AUTO_INCREMENT,
 `u_id` int(11) DEFAULT '0',
 `username` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
 `money` int(11) DEFAULT NULL,
 PRIMARY KEY (`o_id`),
 KEY `u_id` (`u_id`),
 CONSTRAINT `user_order1_ibfk_1` FOREIGN KEY (`u_id`) REFERENCES `userprofile` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

3.刪除外鍵

語法

alter table 數(shù)據(jù)表名稱 drop foreign key 約束(外鍵)名稱

例子:

MariaDB [market]> show create table user_order1;  # 查看user_order1數(shù)據(jù)表的創(chuàng)建信息,包含外鍵約束
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                    |
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_order1 | CREATE TABLE `user_order1` (
 `o_id` int(11) NOT NULL AUTO_INCREMENT,
 `u_id` int(11) DEFAULT '0',
 `username` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
 `money` int(11) DEFAULT NULL,
 PRIMARY KEY (`o_id`),
 KEY `u_id` (`u_id`),
 CONSTRAINT `user_order1_ibfk_1` FOREIGN KEY (`u_id`) REFERENCES `userprofile` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [market]> alter table user_order1 drop foreign key user_order1_ibfk_1;  # 為user_order1數(shù)據(jù)表刪除外鍵約束,外鍵名稱必須與從`show create table user_order1`語句中查到的相同
Query OK, 0 rows affected (0.05 sec)  
Records: 0 Duplicates: 0 Warnings: 0

MariaDB [market]> show create table user_order1;  # 查看user_order1數(shù)據(jù)表的創(chuàng)建信息,外鍵約束已經被刪除了
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                     |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_order1 | CREATE TABLE `user_order1` (
 `o_id` int(11) NOT NULL AUTO_INCREMENT,
 `u_id` int(11) DEFAULT '0',
 `username` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
 `money` int(11) DEFAULT NULL,
 PRIMARY KEY (`o_id`),
 KEY `u_id` (`u_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

4.使用外鍵約束的條件

要想外鍵創(chuàng)建成功,必須滿足以下4個條件:

1、確保參照的表和字段存在。
2、組成外鍵的字段被索引。
3、必須使用type指定存儲引擎為:innodb.
4、外鍵字段和關聯(lián)字段,數(shù)據(jù)類型必須一致。

5.使用外鍵約束需要的注意事項

1.on delete cascade  on update cascade 添加級聯(lián)刪除和更新:
2.確保參照的表userprofile中id字段存在。
3.確保組成外鍵的字段u_id被索引
4.必須使用type指定存儲引擎為:innodb。
5.外鍵字段和關聯(lián)字段,數(shù)據(jù)類型必須一致。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • 淺談MySQL和mariadb區(qū)別

    淺談MySQL和mariadb區(qū)別

    MariaDB是MySQL源代碼的一個分支,在意識到Oracle會對MySQL許可做什么后分離了出來(MySQL先后被Sun、Oracle收購)。除了作為一個Mysql的“向下替代品”,MariaDB包括的一些新特性使它優(yōu)于MySQL。通過本篇文章給大家介紹MySQL和mariadb區(qū)別,需要的朋友可以參考下
    2015-09-09
  • 關于mongoose連接mongodb重復訪問報錯的解決辦法

    關于mongoose連接mongodb重復訪問報錯的解決辦法

    這篇文章主要介紹了關于mongoose連接mongodb重復訪問報錯的解決辦法的相關資料,需要的朋友可以參考下
    2016-01-01
  • Exchange在接收連接器上啟用匿名中繼的方法

    Exchange在接收連接器上啟用匿名中繼的方法

    這篇文章主要介紹了Exchange在接收連接器上啟用匿名中繼的方法,需要的朋友可以參考下
    2018-08-08
  • 詳解Centos 使用YUM安裝MariaDB

    詳解Centos 使用YUM安裝MariaDB

    這篇文章主要介紹了詳解Centos 使用YUM安裝MariaDB的相關資料,需要的朋友可以參考下
    2015-09-09
  • CentOS6.7系統(tǒng)中編譯安裝MariaDB數(shù)據(jù)庫

    CentOS6.7系統(tǒng)中編譯安裝MariaDB數(shù)據(jù)庫

    本文主要是給大家詳細講述了如何在CentOS6.7系統(tǒng)中編譯安裝MariaDB數(shù)據(jù)庫,有需要的小伙伴可以參考下
    2016-11-11
  • MariaDB配置雙主復制方案

    MariaDB配置雙主復制方案

    MySQL復制中較常見的復制架構有“一主一從”、“一主多從”、“雙主”、“多級復制”和“多主環(huán)形機構”等,今天我們來詳細探討下MariaDB配置雙主復制的方案
    2017-03-03
  • Mysql/MariaDB啟動時處于進度條狀態(tài)導致啟動失敗的原因及解決辦法

    Mysql/MariaDB啟動時處于進度條狀態(tài)導致啟動失敗的原因及解決辦法

    本文給大家介紹Mysql/MariaDB啟動時一直處于進度條狀態(tài),進度條結束后提示error。究竟是什么原因呢?該怎么解決呢?跟著小編一起看看該如何解決此問題呢。
    2015-09-09
  • MariaDB數(shù)據(jù)庫的外鍵約束實例詳解

    MariaDB數(shù)據(jù)庫的外鍵約束實例詳解

    約束保證了數(shù)據(jù)的完整性和一致性。下面這篇文章主要給大家介紹了關于MariaDB數(shù)據(jù)庫的外鍵約束的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧
    2018-09-09
  • centos編譯安裝mariadb的詳細過程

    centos編譯安裝mariadb的詳細過程

    這篇文章主要介紹了centos編譯安裝mariadb的方法,主要包括安裝cmake環(huán)境及安裝mariadb的詳細過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下的相關資料
    2022-08-08
  • MariaDB 數(shù)據(jù)類型詳細說明

    MariaDB 數(shù)據(jù)類型詳細說明

    這篇文章主要介紹了MariaDB 數(shù)據(jù)類型詳細說明,需要的朋友可以參考下
    2023-05-05

最新評論

莫力| 遂平县| 耒阳市| 西宁市| 鸡泽县| 霍林郭勒市| 蚌埠市| 晋江市| 东兰县| 从化市| 武夷山市| 肃宁县| 呼图壁县| 滨州市| 班玛县| 宜城市| 湘潭市| 东光县| 高要市| 获嘉县| 永新县| 洪洞县| 淅川县| 湾仔区| 闽侯县| 乡城县| 闻喜县| 拉萨市| 双江| 丰城市| 定边县| 黔西县| 海盐县| 体育| 剑阁县| 威宁| 句容市| 浏阳市| 珲春市| 佛冈县| 托克逊县|