Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate key name 'unique_id'

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;
like image 866
william007 Avatar asked Dec 03 '25 11:12

william007


1 Answers

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;
  • SQLFiddle Demo
like image 80
John Woo Avatar answered Dec 06 '25 01:12

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!