Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: #1075 - Incorrect table definition; autoincrement vs another key?

Here is a table in MySQL 5.3.X+ db:

CREATE TABLE members` (   `id` int(11)  UNSIGNED NOT NULL AUTO_INCREMENT,   `memberid` VARCHAR( 30 ) NOT NULL ,   `Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,   `firstname` VARCHAR( 50 ) NULL ,   `lastname` VARCHAR( 50 ) NULL ,   UNIQUE (memberid),   PRIMARY KEY (id)  ) ENGINE = MYISAM; 

Id column is never used in queries, it is just for visual convenience (so it's easy to see how the table grows). Memberid is an actual key, is unique, and memberid is used in queries to identify any member (WHERE memberid='abcde').

My question is: how to keep auto_increment, but make memberid as a primary key? Is that possible? When I try to create this table with PRIMARY KEY (memberid), I get an error:

1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

What is the best choice (Hopefully, there is a way to keep id column so performance is good and queries identify any user by memberid, not by id), if the performance is very important (although the disk space is not)?

like image 526
Haradzieniec Avatar asked Nov 13 '11 20:11

Haradzieniec


People also ask

What is MySQL used for?

MySQL is an open source relational database management system. For WordPress sites, that means it helps you store all your blog posts, users, plugin information, etc. It stores that information in separate “tables” and connects it with “keys”, which is why it's relational.

What is difference between SQL or MySQL?

SQL is a query programming language that manages RDBMS. MySQL is a relational database management system that uses SQL. SQL is primarily used to query and operate database systems. MySQL allows you to handle, store, modify and delete data and store data in an organized way.

Is MySQL a programming language?

Finally, it's an overview of MySQL and it is not a programming language rather it is software used for the database management system.

Is MySQL better than Microsoft SQL?

Both SQL Server and MySQL are built as binary collections. However, SQL server is more secure than MySQL. It does not let any process to access and manipulate the database files at run time. Users need to perform specific functions or manipulate files by executing an instance.


2 Answers

You can have an auto-Incrementing column that is not the PRIMARY KEY, as long as there is an index (key) on it:

CREATE TABLE members (    id int(11)  UNSIGNED NOT NULL AUTO_INCREMENT,   memberid VARCHAR( 30 ) NOT NULL ,    `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,    firstname VARCHAR( 50 ) NULL ,    lastname VARCHAR( 50 ) NULL ,    PRIMARY KEY (memberid) ,   KEY (id)                          --- or:    UNIQUE KEY (id) ) ENGINE = MYISAM;  
like image 85
ypercubeᵀᴹ Avatar answered Sep 28 '22 10:09

ypercubeᵀᴹ


First create table without auto_increment,

CREATE TABLE `members`(     `id` int(11) NOT NULL,     `memberid` VARCHAR( 30 ) NOT NULL ,     `Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,     `firstname` VARCHAR( 50 ) NULL ,     `lastname` VARCHAR( 50 ) NULL     PRIMARY KEY (memberid)  ) ENGINE = MYISAM; 

after set id as index,

ALTER TABLE `members` ADD INDEX(`id`); 

after set id as auto_increment,

ALTER TABLE `members` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT; 

Or

CREATE TABLE IF NOT EXISTS `members` (     `id` int(11) NOT NULL,     `memberid` VARCHAR( 30 ) NOT NULL ,     `Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,     `firstname` VARCHAR( 50 ) NULL ,     `lastname` VARCHAR( 50 ) NULL,       PRIMARY KEY (`memberid`),       KEY `id` (`id`) ) ENGINE=MYISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; 
like image 40
Thilina Sampath Avatar answered Sep 28 '22 09:09

Thilina Sampath