mysql 唯一索引

来源:互联网 发布:淘宝页眉背景图素材 编辑:程序博客网 时间:2024/06/13 22:09
1.一个列的唯一索引mysql> create database pay;Query OK, 1 row affected (0.03 sec)mysql> use pay;Database changedmysql> show tables;Empty set (0.00 sec)CREATE TABLE `t1` (    `a1` int,  `a2` int,  `a3` int,  `a4` int,  UNIQUE KEY `t1_log` (`a1`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; mysql> show index from t1\G;*************************** 1. row ***************************        Table: t1   Non_unique: 0     Key_name: t1_log Seq_in_index: 1  Column_name: a1    Collation: A  Cardinality: 0     Sub_part: NULL       Packed: NULL         Null: YES   Index_type: BTREE      Comment: Index_comment: 1 row in set (0.00 sec)ERROR: No query specifiedmysql> insert into t1 values(1,2,3,4);Query OK, 1 row affected (0.05 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql>  insert into t1 values(1,2,3,4);ERROR 1062 (23000): Duplicate entry '1' for key 't1_log'2.2个列的唯一索引 ,a1和a2列不能相同CREATE TABLE `t2` (    `a1` int,  `a2` int,  `a3` int,  `a4` int,  UNIQUE KEY `t2_log` (`a1`,`a2`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; mysql> insert into t2 values(1,2,3,4);Query OK, 1 row affected (0.01 sec)mysql>  insert into t2 values(1,2,7,8);ERROR 1062 (23000): Duplicate entry '1-2' for key 't2_log'3.3个列的唯一索引,3个列不能重复CREATE TABLE `t3` (    `a1` int,  `a2` int,  `a3` int,  `a4` int,  UNIQUE KEY `t3_log` (`a1`,`a2`,`a3`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; mysql>  insert into t3 values(1,2,3,4);Query OK, 1 row affected (0.04 sec)mysql>  insert into t3 values(1,2,7,8);Query OK, 1 row affected (0.00 sec)

0 0
原创粉丝点击