Skip to content

Instantly share code, notes, and snippets.

@stevenyxu
Created August 15, 2012 19:09
Show Gist options
  • Save stevenyxu/3362679 to your computer and use it in GitHub Desktop.
Save stevenyxu/3362679 to your computer and use it in GitHub Desktop.
Unique keys in MySQL
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