联合主键的各组成部分都不能为空
(2011-04-08 19:14:48)
标签:
数据库联合主键it |
分类: IT |
大部分时间用的都是单列主键,也知道主键不能空;就是没试过联合主键的情况下,某组成主键的子键是否能为空;今天正好碰到这个问题,写了段脚本测试了下,结果是:各个子键都不能为空。~
mysql> create table t2(id int(4),name
varchar(20), sex char, PRIMARY KEY(id,name));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| animal
|
| t2
|
| tab1
|
+----------------+
3 rows in set (0.00 sec)
mysql> insert into t2 values(1, 'henry',
null);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t2 values(2, 'henry',
null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(2, 'henry',
null);
ERROR 1062 (23000): Duplicate entry '2-henry' for key
1
mysql> insert into t2 values(2, '', null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(2, null,
null);
ERROR 1048
(23000): Column 'name' cannot be null
mysql> insert into t2 values(null, '',
null);
ERROR 1048
(23000): Column 'id' cannot be null
mysql> select * from t2;
+----+-------+------+
| id | name | sex
|
+----+-------+------+
| 1 | henry | NULL
|
| 2 | henry | NULL
|
| 2 |
| NULL
|
+----+-------+------+
3 rows in set (0.00 sec)
mysql>
前一篇:#ifndef 的作用 (续)
后一篇:extern C