Can anyone explain about the purpose of PRIMARY KEY
, UNIQUE KEY
and KEY
, if it is put together in a single CREATE TABLE
statement in MySQL?
CREATE TABLE IF NOT EXISTS `tmp` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `tag` int(1) NOT NULL DEFAULT '0', `description` varchar(255), PRIMARY KEY (`id`), UNIQUE KEY `uid` (`uid`), KEY `name` (`name`), KEY `tag` (`tag`) ) ENGINE=InnoDB AUTO_INCREMENT=1 ;
How do I convert this query to MSSQL?
Both Primary key and Unique Key are used to uniquely define of a row in a table. Primary Key creates a clustered index of the column whereas a Unique creates an unclustered index of the column . A Primary Key doesn't allow NULL value , however a Unique Key does allow one NULL value .
A table can have only one primary key whereas there can be multiple unique key on a table.
A primary key can constitute one or more fields of a table to identify records in a table uniquely. On the other hand, a unique key prevents two rows from having duplicate entries in a column. A table cannot have more than one primary key in a relational database, while there can be multiple unique keys per table.
Purpose: Primary Key is used to uniquely identify a row but a unique key is used to prevent duplicate values in a column. Existence: A table can have only one primary key but it can have multiple unique keys.
Just to add to the other answers, the documentation gives this explanation:
KEY
is normally a synonym forINDEX
. The key attributePRIMARY KEY
can also be specified as justKEY
when given in a column definition. This was implemented for compatibility with other database systems.A
UNIQUE
index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, aUNIQUE
index permits multipleNULL
values for columns that can containNULL
.A
PRIMARY KEY
is a unique index where all key columns must be defined asNOT NULL
. If they are not explicitly declared asNOT NULL
, MySQL declares them so implicitly (and silently). A table can have only onePRIMARY KEY
. The name of aPRIMARY KEY
is alwaysPRIMARY
, which thus cannot be used as the name for any other kind of index.
A key is just a normal index. A way over simplification is to think of it like a card catalog at a library. It points MySQL in the right direction.
A unique key is also used for improved searching speed, but it has the constraint that there can be no duplicated items (there are no two x and y where x is not y and x == y).
The manual explains it as follows:
A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. This constraint does not apply to NULL values except for the BDB storage engine. For other engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL. If you specify a prefix value for a column in a UNIQUE index, the column values must be unique within the prefix.
A primary key is a 'special' unique key. It basically is a unique key, except that it's used to identify something.
The manual explains how indexes are used in general: here.
In MSSQL, the concepts are similar. There are indexes, unique constraints and primary keys.
Untested, but I believe the MSSQL equivalent is:
CREATE TABLE tmp ( id int NOT NULL PRIMARY KEY IDENTITY, uid varchar(255) NOT NULL CONSTRAINT uid_unique UNIQUE, name varchar(255) NOT NULL, tag int NOT NULL DEFAULT 0, description varchar(255), ); CREATE INDEX idx_name ON tmp (name); CREATE INDEX idx_tag ON tmp (tag);
Edit: the code above is tested to be correct; however, I suspect that there's a much better syntax for doing it. Been a while since I've used SQL server, and apparently I've forgotten quite a bit :).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With