Adding auto_increment after table creation

Adding auto_increment after table creation

Say the table is created like this,

mysql> CREATE TABLE customer (id int, fname varchar(20));

The above table would be created without having an index. If there is a need to make id primary key, then

mysql> ALTER TABLE customer ADD PRIMARY KEY (id);

Then adding auto_increment would be as:

mysql> ALTER TABLE customer CHANGE id id int NOT NULL AUTO_INCREMENT;

Trying to assign auto_increment before making it a primary key – if it is not already done during creation, might produce an error. Actually trying to drop the primary key before neutralizing the auto_increment would also might create an error.
The following query would take out the auto_increment part from the table

ALTER TABLE customer CHANGE id id int not null;

And, of course, after this it is possible to drop primary key.

See how you would solve these known algorithm problems

find longest word in the sentence

Implementing tokenizer and adding tokens to the linked list

Flatten nested javascript array

Changing decimal number to its binary equivalent

Kadane’s algorithm in C – Dynamic Programming

Check if there are three numbers a, b, c giving a total T from array A

Finding missing numbers from billion sequential number list file

Java solution for checking anagram strings – tell if phrases are anagrams

Array reversal in Recurrsion

Check if two strings are anagrams or not

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*