Created
August 15, 2012 19:09
-
-
Save stevenyxu/3362679 to your computer and use it in GitHub Desktop.
Unique keys in MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mysql> show create table foo; | |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |
| Table | Create Table | | |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |
| foo | CREATE TABLE `foo` ( | |
`bar` varchar(128) DEFAULT NULL, | |
`baz` varchar(128) DEFAULT NULL, | |
UNIQUE KEY `foo_unique` (`bar`,`baz`) | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1 | | |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |
1 row in set (0.00 sec) | |
mysql> insert into foo (bar, baz) values ('hello','world'); | |
Query OK, 1 row affected (0.00 sec) | |
mysql> insert into foo (bar, baz) values ('hello','world'); | |
ERROR 1062 (23000): Duplicate entry 'hello-world' for key 'foo_unique' | |
mysql> insert into foo (bar, baz) values (NULL, 'world'); | |
Query OK, 1 row affected (0.01 sec) | |
mysql> insert into foo (bar, baz) values (NULL, 'world'); | |
Query OK, 1 row affected (0.01 sec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment