哪位高手能给发一些有关MySQL中建立外键约束的详细介绍资料,本人在table中建立外键约束时总是提示同一个错误“ERROR 1005 (HY000): Can't create table 'xxx' (errno: 121)”,不知该如何解决,因此在建table时,不敢加外键约束,在网上收了很久都没找到有关的详解,很是苦恼。
还请各位高人给予指教!
还请各位高人给予指教!
解决方案 »
- mysql 数据表主键自增,一定要命名为id吗?
- 这个SQL有没优化的余地呢
- 帮助编写一个简单mysql存储过程
- 问一个数据库设计问题 是不是一个表可以有且只有一个字段?
- postgresql中的数据文件是什么扩展名?
- 请教定义字段时int(10) 有什么好处
- sql中加法怎么用?
- 关于mysql文件的.frm文件的导入
- 如何将Mysql表结构导出到一个SQL脚本中?
- 在向mysql存储一2.7M的图片时,出现‘2006-MySQL server has gone away’,而存储几十K的图片时却正常,为什么?
- mysql_query(&zhqy,"select * from shop")时 报错Commands out of sync
- outport命令导出sql文件里面的问题??
用的什么存储引擎?
-> id int primary key,
-> col int
-> )engine=innodb;
Query OK, 0 rows affected (0.09 sec)mysql> create table t2 (
-> id int primary key,
-> tid int,
-> FOREIGN KEY (tid) REFERENCES t1(id)
-> )engine=innodb;
Query OK, 0 rows affected (0.09 sec)mysql> insert into t2 values (2,1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f
ails (`csdn`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`tid`) REFERENCES `t1` (`
id`))
mysql> insert into t2 values (1,null);
Query OK, 1 row affected (0.05 sec)mysql> insert into t1 values (1,91);
Query OK, 1 row affected (0.06 sec)mysql> insert into t2 values (2,1);
Query OK, 1 row affected (0.03 sec)mysql> select * from t2;
+----+------+
| id | tid |
+----+------+
| 1 | NULL |
| 2 | 1 |
+----+------+
2 rows in set (0.00 sec)mysql> select version();
+----------------------+
| version() |
+----------------------+
| 5.1.33-community-log |
+----------------------+
1 row in set (0.00 sec)mysql>
-> id int primary key,
-> tid int,
-> FOREIGN KEY (tid) REFERENCES t1(id)
-> )engine=innodb;
mysql innodb支持外键约束myisam不支持外键
show variables like 'foreign_key_checks' 看看支持不?