3

Here is the sql, however, there is an error says "*#1061 - Duplicate key name 'unique_id'* ", what is the problem.

create table `users`(
   uid int(11) auto_increment,
   unique_id varchar(23) not null unique,
   name varchar(50) not null,
   email varchar(100) not null unique,
   encrypted_password varchar(80) not null,
   salt varchar(10) not null,
   created_at datetime,
   updated_at datetime null,
  PRIMARY KEY (`unique_id`),
  UNIQUE KEY `uid` (`uid`),
  UNIQUE KEY `unique_id` (`unique_id`),
  UNIQUE KEY `email` (`email`)
)ENGINE=InnoDB AUTO_INCREMENT=877888 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
John Woo
  • 258,903
  • 69
  • 498
  • 492
william007
  • 17,375
  • 25
  • 118
  • 194

1 Answers1

13

remove this line

UNIQUE KEY `unique_id` (`unique_id`),

since unique_id is already Primary Key. and Primary Keys are unique.

full CREATE TABLE statement

create table `users`
(
   uid int(11) auto_increment,
   unique_id varchar(23) not null,
   name varchar(50) not null,
   email varchar(100) not null unique,   -- specified here
   encrypted_password varchar(80) not null,
   salt varchar(10) not null,
   created_at datetime,
   updated_at datetime null,
   PRIMARY KEY (`unique_id`),
   UNIQUE KEY `uid` (`uid`)
)  ENGINE=InnoDB AUTO_INCREMENT=877888 
DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Thanks, I remove the statement, however, #1061 - Duplicate key name 'email' is occurred,why? – william007 Dec 19 '12 at 03:17
  • because `email` already exist. try `UNIQUE KEY email_uq (email)` see here for demo, http://sqlfiddle.com/#!2/ac712 – John Woo Dec 19 '12 at 03:20
  • Sorry but what do you mean email already exists? and why we can specify UNIQUE KEY `uid` (`uid`) – william007 Dec 19 '12 at 03:35
  • because as you can see you have already define that column as unique, see line number 5 of your `CREATE TABLE` statement. actually you can ignore this one, see this, http://sqlfiddle.com/#!2/cf941 – John Woo Dec 19 '12 at 05:15