MySQL定义外键的方法_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1590 | 回复: 0   主题: MySQL定义外键的方法        下一篇 
huan.liu
注册用户
等级:新兵
经验:72
发帖:58
精华:0
注册:2011-11-23
状态:离线
发送短消息息给huan.liu 加好友    发送短消息息给huan.liu 发消息
发表于: IP:您无权察看 2015-5-27 11:09:57 | [全部帖] [楼主帖] 楼主

  1. mysql> CREATE TABLE categories (  
  2.       -> category_id tinyint(3) unsigned NOT NULL AUTO_INCREMENT,  
  3.       -> name varchar(30) NOT NULL,  
  4.       -> PRIMARY KEY(category_id)  
  5.       -> ) ENGINE=INNODB;  
  6. Query OK, 0 rows affected (0.36 sec)  
  7. mysql> INSERT INTO categories VALUES (1, ‘SQL Server’), (2, ‘Oracle’), (3, ‘PostgreSQL’), (4, ‘MySQL’), (5, ‘SQLite’);  
  8. Query OK, 5 rows affected (0.48 sec)  
  9. Records: 5    Duplicates: 0    Warnings: 0  
  10. mysql> CREATE TABLE members (  
  11.       -> member_id INT(11) UNSIGNED NOT NULL,  
  12.       -> name VARCHAR(20) NOT NULL,  
  13.       -> PRIMARY KEY(member_id)  
  14.       -> ) ENGINE=INNODB;  
  15. Query OK, 0 rows affected (0.55 sec)  
  16. mysql> INSERT INTO members VALUES (1, ‘test’), (2, ‘admin’);  
  17. Query OK, 2 rows affected (0.44 sec)  
  18. Records: 2    Duplicates: 0    Warnings: 0  
  19. mysql> CREATE TABLE articles (  
  20.       -> article_id INT(11) unsigned NOT NULL AUTO_INCREMENT,  
  21.       -> title varchar(255) NOT NULL,  
  22.       -> category_id tinyint(3) unsigned NOT NULL,  
  23.       -> member_id int(11) unsigned NOT NULL,  
  24.       -> INDEX (category_id),  
  25.       -> FOREIGN KEY (category_id) REFERENCES categories (category_id),  
  26.       -> CONSTRAINT fk_member FOREIGN KEY (member_id) REFERENCES members (member_id),  
  27.       -> PRIMARY KEY(article_id)  
  28.       -> ) ENGINE=INNODB;  
  29. Query OK, 0 rows affected (0.63 sec)  



注意:对于非InnoDB表,FOREIGN KEY子句会被忽略掉。
如果遇到如下错误:

 ERROR 1005: Can’t create table ‘./test/articles.frm’ (errno: 150)


请仔细检查以下定义语句,常见的错误一般都是表类型不是INNODB、相关联的字段写错了、缺少索引等等。

至此categories.category_id和articles.category_id、members.member_id和 articles.member_id已经建立外键关系,只有 articles.category_id 的值存在与 categories.category_id 表中并且articles.member_id的值存在与members.member_id表中才会允许被插入或修改。例如:

  1. mysql> INSERT INTO articles (category_id, member_id, title) VALUES (6, 1, ‘foo’);  
  2. ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test/articles`, CONSTRAINT `articles_ibfk_1` FOREIGN KEY (`category_id`)REFERENCES `categories` (`id`))  
  3. mysql> INSERT INTO articles (category_id, member_id, title) VALUES (3, 3, ‘foo’);  
  4. ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test/articles`, CONSTRAINT `fk_member` FOREIGN KEY (`member_id`) REFERENCES `members` (`member_id`))  

 
可见上面两条语句都会出现错误,因为在categories表中并没有category_id=6、members表中也没有member_id=3的记录,所以不能插入。而下面这条SQL语句就可以。

 mysql> INSERT INTO articles (category_id, member_id, title) VALUES (3, 2, ‘bar’);
Query OK, 1 row affected (0.03 sec)


以上就是MySQL定义外键的方法介绍。

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论