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?
This question is related to
mysql
sql-server
primary-key
unique-key
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.
MySQL unique and primary keys serve to identify rows. There can be only one Primary key in a table but one or more unique keys. Key is just index.
for more details you can check http://www.geeksww.com/tutorials/database_management_systems/mysql/tips_and_tricks/mysql_primary_key_vs_unique_key_constraints.php
to convert mysql to mssql try this and see http://gathadams.com/2008/02/07/convert-mysql-to-ms-sql-server/
Source: Stackoverflow.com