mysql如何处理外码约束
http://hi.baidu.com/wangzhiqing999/item/e74b2be28be445a3c00d7528
MySQL 外键约束
-- 创建测试主表. ID 是主键.
CREATE TABLE test_main (
id INT,
value VARCHAR(10),
PRIMARY KEY(id)
);
-- 创建测试子表.
CREATE TABLE test_sub (
id INT,
main_id INT,
value VARCHAR(10),
PRIMARY KEY(id)
);
-- 插入测试主表数据.
INSERT INTO test_main(id, value) VALUES (1, 'ONE');
INSERT INTO test_main(id, value) VALUES (2, 'TWO');
-- 插入测试子表数据.
INSERT INTO test_sub(id, main_id, value) VALUES (1, 1, 'ONEONE');
INSERT INTO test_sub(id, main_id, value) VALUES (2, 2, 'TWOTWO');
默认外键约束方式
mysql> ALTER TABLE test_sub
-> ADD CONSTRAINT main_id_cons
-> FOREIGN KEY (main_id)
-> REFERENCES test_main(id);
-> //
Query OK, 2 rows affected (0.17 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> DELETE FROM
-> test_main
-> WHERE
-> id = 1;
-> //
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constrai
nt fails (`test`.`test_sub`, CONSTRAINT `main_id_cons` FOREIGN KEY (`main_id`) R
EFERENCES `test_main` (`id`))
MySQL使用下面这个语句删除外键约束
ALTER TABLE test_sub DROP FOREIGN KEY main_id_cons;
DELETE CASCADE 方式
mysql> ALTER TABLE test_sub
-> ADD CONSTRAINT main_id_cons
-> FOREIGN KEY (main_id)
-> REFERENCES test_main(id)
-> ON DELETE CASCADE//
Query OK, 2 rows affected (0.16 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> DELETE FROM
-> test_main
-> WHERE
-> id = 1;
-> //
Query OK, 1 row affected (0.02 sec)
mysql> SELECT
-> *
-> FROM
-> test_sub;
-> //
+----+---------+--------+
| id | main_id | value |
+----+---------+--------+
| 2 | 2 | TWOTWO |
+----+---------+--------+
1 row in set (0.00 sec)
MySQL使用下面这个语句删除外键约束
ALTER TABLE test_sub DROP FOREIGN KEY main_id_cons;
UPDATE CASCADE方式
mysql> ALTER TABLE test_sub
-> ADD CONSTRAINT main_id_cons
-> FOREIGN KEY (main_id)
-> REFERENCES test_main(id)
-> ON UPDATE CASCADE;
-> //
Query OK, 1 row affected (0.14 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> UPDATE test_main SET id = 5 where id = 2
-> //
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test_sub//
+----+---------+--------+
| id | main_id | value |
+----+---------+--------+
| 2 | 5 | TWOTWO |
+----+---------+--------+
1 row in set (0.00 sec)
MySQL使用下面这个语句删除外键约束
ALTER TABLE test_sub DROP FOREIGN KEY main_id_cons;
SET NULL方式
mysql> ALTER TABLE test_sub
-> ADD CONSTRAINT main_id_cons
-> FOREIGN KEY (main_id)
-> REFERENCES test_main(id)
-> ON DELETE SET NULL;
-> //
Query OK, 1 row affected (0.13 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> DELETE FROM
-> test_main
-> WHERE
-> id = 5;
-> //
Query OK, 1 row affected (0.05 sec)
mysql>
mysql> SELECT
-> *
-> FROM
-> test_sub;
-> //
+----+---------+--------+
| id | main_id | value |
+----+---------+--------+
| 2 | NULL | TWOTWO |
+----+---------+--------+
1 row in set (0.00 sec)
mysql如何处理外码约束的更多相关文章
- MySQL 建立外键约束
http://www.jzxue.com/shujuku/mysql/201109/06-8742.html MySQL 建立外键约束的语法太晦涩难懂了, 不得不记下笔记. 1. 在建表时建立外键 C ...
- 如果你发现mysql的外键约束不管用了
不知为何我机子上的mysql竟然默认关闭外键约束,导致我试了好多遍都可以插入非法值,以下语句可以开启约束 SET foreign_key_checks = 1; (0则关闭) 备忘
- mysql设置外键约束开启-关闭
在MySQL中删除一张表或一条数据的时候,出现 [Err] 1451 -Cannot delete or update a parent row: a foreign key constraint f ...
- MySQL数据库--外键约束及外键使用
什么是主键.外键关系型数据库中的一条记录中有若干个属性,若其中某一个属性组(注意是组)能唯一标识一条记录,该属性组就可以成为一个主键. 比如: 学生表(学号,姓名,性别,班级) 其中每个学生的学号是唯 ...
- MySQL 主外键约束与标准SQL不同的地方
[标准SQL的外键约束条件] 1): 子表引用父表的主键 drop table if exists child,parent; create table if not exists parent( i ...
- MySQL的外键约束:Cascade/Restrict/No action/SET NULL :级联操作
转自:http://blog.csdn.net/cnjsnt_s/article/details/5548280 具体使用时需要参考:http://blog.csdn.net/codeforme/ar ...
- mysql 设置外键约束时如何删除数据
Mysql中如果表和表之间建立的外键约束,则无法删除表及修改表结构 解决方法是在Mysql中取消外键约束: SET FOREIGN_KEY_CHECKS=0; 然后将原来表的数据导出到sql语句,重新 ...
- MySQL创建外键约束的报错Error : Can't create table '#sql-534_185' (errno: 150)
总得来说是因为两个表的字段类型不一致,例如: 两个字段的类型或大小不严格匹配,一个为tinyint,另一个为char:或一个为int(10)另一个为int(9)也是不行的,即使都为int(10),但一 ...
- MySQL删除外键约束问题
当我们在一个表中添加字段约束的时候: ALTER TABLE product ADD CONSTRAINT product_fk FOREIGN KEY(category_id) REFERENCES ...
随机推荐
- git status出现 fatal: Not a git repository (or any of the parent directories): .git
fatal: Not a git repository (or any of the parent directories): .git 提示说没有.git这样一个目录,解决办法如下: git ini ...
- Spark读取结构化数据
读取结构化数据 Spark可以从本地CSV,HDFS以及Hive读取结构化数据,直接解析为DataFrame,进行后续分析. 读取本地CSV 需要指定一些选项,比如留header,比如指定delimi ...
- 115. Distinct Subsequences *HARD* -- 字符串不连续匹配
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- poj1191 棋盘分割。 dp
连接:http://poj.org/problem?id=1191 思路:额,其实就是直接搞记录一下就可以了. #include <stdio.h> #include <string ...
- ansible常用套路(一)
一.SSH互信 1 配置/etc/ansible/hosts 文件 [zabbix_agent] 172.26.4.203 172.26.4.204 172.26.4.205 [zabbix_agen ...
- windows下搭建svn服务器及权限配置
服务器端VISUALSVN SERVER 3.3.1 下载地址 https://www.visualsvn.com/server/download/ 客户端TortoiseSVN 1.8.13下载地址 ...
- 如何导入数据到Mysql
有两种方法: 1.如果是.sql后缀的数据库文件,使用phpmyadmin中的导入功能导入即可,导入前需要新建数据库名. 2.如果导入的是文件夹(内含.frm,.myd,.myi,.opt类型文件), ...
- Class.getResource()方法的使用
我们之前使用路径总是有点不知道怎么用,发现别人使用Class.getResource()方法,好像挺不错的样子.于是看看博客,简单学习下. 参考链接:http://blog.csdn.net/lcj8 ...
- java的继承(编程思想)
继承语法: extends 初始化基类: 由于现在设计基类和导出类两个类,而不是一个类,所以要试着想像导出类锁产生的结果对象,会有些困惑.从外部看来,它就像是一个与基类具有相同接口的新类,或许还会有一 ...
- 解决eclipse maven install 造成JVM 内存溢出(java.lang.OutOfMemoryError: Java heap space)
maven install 报错信息: The system is out of resources.Consult the following stack trace for details.jav ...