MySQL - Meaning of "PRIMARY KEY", "UNIQUE KEY" and "KEY" when used together while creating a table -
can explain purpose of primary key
, unique key
, key
if put in 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 convert query mysql?
a key normal index. way on simplification think of card catalog @ library. points mysql in right direction.
a unique key used improved searching speed, has constraint there can no duplicated items (there no 2 x , y x not y , x == y).
the manual explains follows:
a unique index creates constraint such values in index must distinct. error occurs if try add new row key value matches existing row. constraint not apply null values except bdb storage engine. other engines, unique index permits multiple null values columns can contain null. if specify prefix value column in unique index, column values must unique within prefix.
a primary key 'special' unique key. unique key, except it's used identify something.
the manual explains how indexes used in general: here.
in mssql, concepts similar. there indexes, unique constraints , primary keys.
untested, believe 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: code above tested correct; however, suspect there's better syntax doing it. been while since i've used sql server, , apparently i've forgotten quite bit :).
Comments
Post a Comment