- 浏览: 243346 次
最新评论
mysql设置外键约束on delete cascade on update cascade
摘要: 当删除父节点时,由数据库来帮助删除子节点,这样就不用我们显示地写代码先删子节点,再删父节点了。
外键约束创建举例:
ALTER TABLE a
ADD CONSTRAINT `FK_Reference_1`
FOREIGN KEY (`parent_id` )
REFERENCES parent_table (`parent_id` )
ON DELETE CASCADE
ON UPDATE RESTRICT;
1)先看On Delete属性
可能取值为:No Action, Cascade,Set Null, Restrict属性。
当取值为No Action或者Restrict时,则当在父表(即外键的来源表)中删除对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除。
当取值为Cascade时,则当在父表(即外键的来源表)中删除对应记录时,首先检查该记录是否有对应外键,如果有则也删除外键在子表(即包含外键的表)中的记录。
当取值为Set Null时,则当在父表(即外键的来源表)中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null(不过这就要求该外键允许取null)。
2)看看on update属性:
当取值为No Action或者Restrict时,则当在父表(即外键的来源表)中更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许更新。
当取值为Cascade时,则当在父表(即外键的来源表)中更新对应记录时,首先检查该记录是否有对应外键,如果有则也更新外键在子表(即包含外键的表)中的记录。
当取值为Set Null时,则当在父表(即外键的来源表)中更新对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null(不过这就要求该外键允许取null)。
比如我有这样两张表:
tbluser用户表:
CREATE TABLE tbluser (
UserID varchar(50) NOT NULL primary key,
UserName varchar(40) NOT NULL,
UserMail varchar(50) NOT NULL,
UserPassword varchar(50) NOT NULL,
UserType tinyint(3) unsigned DEFAULT '0',
UserCreated datetime DEFAULT '0000-00-00 00:00:00'
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
tblfile文件(用户的文件)表:
CREATE TABLE tblfile (
FileID int(10) unsigned NOT NULL AUTO_INCREMENT,
FileOwner varchar(50) DEFAULT NULL,
FileName varchar(200) NOT NULL,
FilePath varchar(200) NOT NULL,
FileType varchar(10) NOT NULL,
FileSubject varchar(100) NOT NULL,
FileCreated datetime DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (FileID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create index ind_FileOwner on tblfile(FileOwner);
ALTER TABLE `tblfile` ADD CONSTRAINT `FK_tblfile_1` FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON DELETE SET NULL ON UPDATE CASCADE;
上面可以看出FileOwner是文件表的外键,引用的是用户表的UserID。且这里外键约束设置为"ON DELETE SET NULL ON UPDATE CASCADE "
现在用户表有记录(省略其他无关的字段):
mysql> select * from tbluser;
+--------+----------+------------------+--------------+----------+---------------------+
| UserID | UserName | UserMail | UserPassword | UserType | UserCreated |
+--------+----------+------------------+--------------+----------+---------------------+
| 1 | tina | 27145@qq.com | tina | 0 | 0000-00-00 00:00:00 |
| 2 | bobo | 6964@qq.com | bobo | 1 | 2016-12-27 16:07:51 |
| 3 | lio | 1649797en@qq.com | jiou | 2 | 2016-11-21 16:08:31 |
| 4 | kaka | 46416496@163.com | jiujiu | 0 | 2016-11-06 16:09:03 |
+--------+----------+------------------+--------------+----------+---------------------+
4 rows in set (0.00 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | 2 | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
我们来删除父表数据:
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint p1;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tbluser where UserId=2; 父表删除,子表相关联的字段就被set null
Query OK, 1 row affected (0.00 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | NULL | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
我们来更新父表数据:
mysql> update tbluser set UserId=5 where UserId=4; ---父表更新,子表更新
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 5 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 5 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | NULL | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
mysql> rollback to p1; ---回滚为后面测试做准备。
Query OK, 0 rows affected (0.01 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | 2 | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
删除之前的外键约束,改一下规则。
ALTER TABLE `tblfile` DROP FOREIGN KEY `FK_tblfile_1`;
ALTER TABLE `tblfile` ADD CONSTRAINT `FK_tblfile_1` FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON DELETE RESTRICT ON UPDATE NO ACTION;
再来一次上面的操作:
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint p1;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tbluser where UserId=2; --父表删除和更新都被限制,因为子表有这个用户的信息。
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`tina`.`tblfile`, CONSTRAINT `FK_tblfile_1`
FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON UPDATE NO ACTION)
mysql> update tbluser set UserId=5 where UserId=4;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`tina`.`tblfile`, CONSTRAINT `FK_tblfile_1`
FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON UPDATE NO ACTION)
no action和RESTRICT 是一样的,就是有记录就不允许操作。
再改一下规则:
ALTER TABLE `tblfile` DROP FOREIGN KEY `FK_tblfile_1`;
ALTER TABLE `tblfile` ADD CONSTRAINT `FK_tblfile_1` FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON DELETE CASCADE ON UPDATE CASCADE;
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint p1;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tbluser where UserId=2;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
3 rows in set (0.00 sec)
mysql> update tbluser set UserId=5 where UserId=4; --父表中删除和更新,子表就跟着删除和更新。
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 5 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 5 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
3 rows in set (0.00 sec)
mysql> rollback to p1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | 2 | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
外键的使用对于减少数据库冗余性,以及保证数据完整性和一致性有很大作用。
另外注意,如果两张表之间存在外键关系,则MySQL不能直接删除表(Drop Table),而应该先删除外键,之后才可以删除。
摘要: 当删除父节点时,由数据库来帮助删除子节点,这样就不用我们显示地写代码先删子节点,再删父节点了。
外键约束创建举例:
ALTER TABLE a
ADD CONSTRAINT `FK_Reference_1`
FOREIGN KEY (`parent_id` )
REFERENCES parent_table (`parent_id` )
ON DELETE CASCADE
ON UPDATE RESTRICT;
1)先看On Delete属性
可能取值为:No Action, Cascade,Set Null, Restrict属性。
当取值为No Action或者Restrict时,则当在父表(即外键的来源表)中删除对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除。
当取值为Cascade时,则当在父表(即外键的来源表)中删除对应记录时,首先检查该记录是否有对应外键,如果有则也删除外键在子表(即包含外键的表)中的记录。
当取值为Set Null时,则当在父表(即外键的来源表)中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null(不过这就要求该外键允许取null)。
2)看看on update属性:
当取值为No Action或者Restrict时,则当在父表(即外键的来源表)中更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许更新。
当取值为Cascade时,则当在父表(即外键的来源表)中更新对应记录时,首先检查该记录是否有对应外键,如果有则也更新外键在子表(即包含外键的表)中的记录。
当取值为Set Null时,则当在父表(即外键的来源表)中更新对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null(不过这就要求该外键允许取null)。
比如我有这样两张表:
tbluser用户表:
CREATE TABLE tbluser (
UserID varchar(50) NOT NULL primary key,
UserName varchar(40) NOT NULL,
UserMail varchar(50) NOT NULL,
UserPassword varchar(50) NOT NULL,
UserType tinyint(3) unsigned DEFAULT '0',
UserCreated datetime DEFAULT '0000-00-00 00:00:00'
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
tblfile文件(用户的文件)表:
CREATE TABLE tblfile (
FileID int(10) unsigned NOT NULL AUTO_INCREMENT,
FileOwner varchar(50) DEFAULT NULL,
FileName varchar(200) NOT NULL,
FilePath varchar(200) NOT NULL,
FileType varchar(10) NOT NULL,
FileSubject varchar(100) NOT NULL,
FileCreated datetime DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (FileID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create index ind_FileOwner on tblfile(FileOwner);
ALTER TABLE `tblfile` ADD CONSTRAINT `FK_tblfile_1` FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON DELETE SET NULL ON UPDATE CASCADE;
上面可以看出FileOwner是文件表的外键,引用的是用户表的UserID。且这里外键约束设置为"ON DELETE SET NULL ON UPDATE CASCADE "
现在用户表有记录(省略其他无关的字段):
mysql> select * from tbluser;
+--------+----------+------------------+--------------+----------+---------------------+
| UserID | UserName | UserMail | UserPassword | UserType | UserCreated |
+--------+----------+------------------+--------------+----------+---------------------+
| 1 | tina | 27145@qq.com | tina | 0 | 0000-00-00 00:00:00 |
| 2 | bobo | 6964@qq.com | bobo | 1 | 2016-12-27 16:07:51 |
| 3 | lio | 1649797en@qq.com | jiou | 2 | 2016-11-21 16:08:31 |
| 4 | kaka | 46416496@163.com | jiujiu | 0 | 2016-11-06 16:09:03 |
+--------+----------+------------------+--------------+----------+---------------------+
4 rows in set (0.00 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | 2 | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
我们来删除父表数据:
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint p1;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tbluser where UserId=2; 父表删除,子表相关联的字段就被set null
Query OK, 1 row affected (0.00 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | NULL | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
我们来更新父表数据:
mysql> update tbluser set UserId=5 where UserId=4; ---父表更新,子表更新
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 5 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 5 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | NULL | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
mysql> rollback to p1; ---回滚为后面测试做准备。
Query OK, 0 rows affected (0.01 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | 2 | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
删除之前的外键约束,改一下规则。
ALTER TABLE `tblfile` DROP FOREIGN KEY `FK_tblfile_1`;
ALTER TABLE `tblfile` ADD CONSTRAINT `FK_tblfile_1` FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON DELETE RESTRICT ON UPDATE NO ACTION;
再来一次上面的操作:
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint p1;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tbluser where UserId=2; --父表删除和更新都被限制,因为子表有这个用户的信息。
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`tina`.`tblfile`, CONSTRAINT `FK_tblfile_1`
FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON UPDATE NO ACTION)
mysql> update tbluser set UserId=5 where UserId=4;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`tina`.`tblfile`, CONSTRAINT `FK_tblfile_1`
FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON UPDATE NO ACTION)
no action和RESTRICT 是一样的,就是有记录就不允许操作。
再改一下规则:
ALTER TABLE `tblfile` DROP FOREIGN KEY `FK_tblfile_1`;
ALTER TABLE `tblfile` ADD CONSTRAINT `FK_tblfile_1` FOREIGN KEY (`FileOwner`) REFERENCES `tbluser` (`UserID`) ON DELETE CASCADE ON UPDATE CASCADE;
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint p1;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tbluser where UserId=2;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
3 rows in set (0.00 sec)
mysql> update tbluser set UserId=5 where UserId=4; --父表中删除和更新,子表就跟着删除和更新。
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 5 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 5 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
3 rows in set (0.00 sec)
mysql> rollback to p1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tblfile;
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| FileID | FileOwner | FileName | FilePath | FileType | FileSubject | FileCreated |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
| 1 | 1 | test | /tmp/a.txt | 1 | en | 2016-12-04 16:09:39 |
| 2 | 4 | jiujiu | /tmp/jiu.txt | 2 | jieng | 0000-00-00 00:00:00 |
| 3 | 4 | enogeh | /tmp/3hoge | 2 | eg | 0000-00-00 00:00:00 |
| 4 | 2 | egoe | /tmp/ghoeh | 3 | eighha | 2016-12-06 16:10:42 |
+--------+-----------+----------+--------------+----------+-------------+---------------------+
4 rows in set (0.00 sec)
外键的使用对于减少数据库冗余性,以及保证数据完整性和一致性有很大作用。
另外注意,如果两张表之间存在外键关系,则MySQL不能直接删除表(Drop Table),而应该先删除外键,之后才可以删除。
发表评论
-
mysql权限管理(实例)
2016-05-10 17:21 1514mysql权限管理实例 本文并没有很详细的介绍对具体的对象授 ... -
mysql简单的碎片清理脚本
2016-05-10 16:52 1496mysql简单的碎片清理脚本 #!/bin/bash date ... -
mysql qpress压缩备份恢复
2016-05-03 16:30 6962说明: 1.前面博客已经介绍过gzip压缩方法,备份正常,但后 ... -
mysql xtrabackup在线搭建主从
2016-04-11 14:59 1951使用xtrabackup进行在线的主从搭建: [root@m ... -
mysql xtrabackup在线备份还原(全备+增备)
2016-04-11 14:47 1053工具安装: [root@mysqlserver var]# t ... -
mysql主库清理数据,从库保留
2016-04-01 15:26 1298因为业务需要,想在mysql主库清理一些数据,但从库想要保留, ... -
oracle,postgresql,mysql一些使用上的区别记录
2015-12-16 11:38 01.限制行数: select * from ta where ... -
数据库调优分享-mysql
2015-12-16 10:02 951数据库调优分享------参考一本mysql资料书 日常的困 ... -
mysql 安装-tina
2015-12-08 17:32 0mysql安装-tina 1、准备安装程序(http://ww ... -
mysqldump 只导入数据或只导结构
2015-12-22 10:36 2719[size=small]mysqldump只导出数据或只导出表 ... -
mysql server has gone away
2015-12-10 09:26 877mysql server has gone away,他的意思 ... -
mysql optimize 清理碎片
2015-12-09 09:26 1206---定期清理脚本 0 1 * * 4 root /root ... -
mysql binlog
2015-12-10 09:26 1345mysqld在每个二进制日志 ... -
mysql远程连接设置
2015-12-10 09:25 1008远程连接mysql数据库: 连接上以后,通过这台跳转服务器远 ... -
Last_SQL_Error: Error 'Duplicate entry '1' for key 'PRIMARY''
2015-12-10 09:25 1720[size=small]-实际遇到的问题: Last_SQL ... -
[ERROR] Slave I/O: error connecting to master
2015-12-09 09:26 8210刚配置的MySQL主从,在从机上看到 点击(此处)折叠或打开 ... -
MySQL常用函数
2015-02-05 10:34 537一、字符串类 1、left(str, length) 从左开始 ... -
MySQL触发器简介
2015-02-05 10:33 899一、触发器基本语法 CREATE TRIGGER trigge ... -
MySQL主从切换
2015-02-05 10:32 505环境: 原主库:192.168.10.197 ---新 ... -
MySQL主从搭建
2015-02-05 10:31 795环境简介 master(主):192.168.12.101 s ...
相关推荐
常见的触发限制参数有ON DELETE和ON UPDATE,它们可以设置成多种参数,包括CASCADE(级联删除或更新)、RESTRICT(限制删除或更新)、SET NULL(将引用列置为空)和SET DEFAULT(设置默认值)。不加参数时,默认行为...
- **级联操作**:通过设置外键约束为`CASCADE`,可以实现级联删除或更新,这在维护数据一致性时非常有用。 - **ON DELETE 和 ON UPDATE 触发器**:可以指定当被参照记录被删除或更新时,如何处理外键字段。例如,`ON...
3. **简化数据维护**:通过设置级联操作(例如`ON DELETE CASCADE`和`ON UPDATE CASCADE`),可以在删除或更新主键所在表的记录时自动地维护引用表的一致性,从而大大简化了数据维护的工作量。 #### 创建外键约束 ...
在MySQL数据库中,外键约束是关系型数据库中用于维护数据完整性和一致性的重要工具。它确保了一张表中的数据与另一张表的数据之间的关联性,通常用于实现一对一、一对多或多对多的关系。本篇文章将详细讲解如何进行...
MySQL的MyISAM存储引擎不支持外键约束,因此在设置外键时,需要确保使用支持外键的存储引擎,如InnoDB。 8. **外键约束检查时机**: 默认情况下,MySQL在`DEFERRED`模式下,只在事务提交时检查外键约束。若需在...
FOREIGN KEY (groupid) REFERENCES t_group(id) ON DELETE CASCADE ON UPDATE CASCADE ); 在上面的示例中,t_user 表中的 groupid 字段是 t_group 表的外键,级联方式将被应用于删除和更新操作。 外键约束方式是...
- **简化查询操作**:例如,当需要查询某个产品的信息以及与之关联的订单信息时,如果设置了外键约束,则可以直接通过外键进行关联查询,提高查询效率。 ##### 2. 添加外键的格式 添加外键的基本语法如下: ```...
ON DELETE CASCADE ON UPDATE CASCADE; ``` 这样,当`blogs`表中的某篇文章被删除时,所有相关的评论也会自动删除,保持数据的一致性。 使用InnoDB的缺点主要是性能,特别是对于大量表连接查询,InnoDB可能会比...
本文将详细介绍MySQL中三种外键约束方式:级联(cascade)、置空(set null)和禁止(no action或restrict),并通过实例来展示它们在数据操作时的行为差异。 1. **级联(cascade)方式** 在级联约束中,当主表中...
此外,可以通过设置外键约束的行为来调整其在插入和删除时的行为,如ON DELETE CASCADE(级联删除)、ON DELETE SET NULL(设置外键为空)或ON DELETE NO ACTION(禁止删除)。这允许根据业务需求来定制外键的处理...
MySql 外键设置详解 MySql 外键设置详解 一、外键的使用 外键是数据库中的一种约束,用于确保数据的一致性和完整性。外键的作用主要有两个:一...但是,需要注意创建外键时的注意事项,并且正确地使用外键约束类型。
* 外键绑定关系这里使用了“ON DELETE CASCADE”和“ON UPDATE CASCADE”,意思是如果外键对应数据被删除或者更新时,将关联数据完全删除或者相应地更新。 MySQL 中的外键可以强制实施数据的一致性和完整性,使得...
并定义了一个外键约束`fk_students_teacherID`,它关联`teacherID`字段,当主表中的记录被删除或更新时,可以选择级联操作(如`ON DELETE NO ACTION`和`ON UPDATE CASCADE`)。 ```sql CREATE TABLE teachers ( ...
MySQL中的外键约束是数据库设计中的一个重要概念,用于维护数据库中的引用完整性,确保表与表之间的数据关联正确无误。本文将深入探讨外键的作用、使用条件、创建语法以及通过案例来演示其实际应用。 首先,外键的...
外键表名>(外键列名) 事件触发限制: on delete和on update , 可设参数cascade(跟随外键改动), restrict(限制外表中的外键改动),set Null(设空值),set Default(设默认值),[默认]no action 例如: ...
首先,当你尝试删除一个有外键约束的表或者表中的数据时,如果这个表被其他表引用,MySQL会抛出错误[Err] 1451,提示“Cannot delete or update a parent row: a foreign key constraint fails”。这是因为删除操作...
定义外键约束 在子表中定义外键时,需要指定外键的名称、引用的字段以及被引用的表和字段。 示例代码如下: ```sql ALTER TABLE pc ADD CONSTRAINT fk_cpu_model FOREIGN KEY (cpumodel) REFERENCES parts(model...
[ON UPDATE {CASCADE | SET NULL | NO ACTION | RESTRICT}]; ``` - **约束类型**: - **CASCADE**:当父表的外键关联列被更新或删除时,子表中的外键值也将被相应地更新或删除。 - **RESTRICT/NO ACTION**:不...