1

how to set particular column as primary key with auto increment in mysql.am using alter statement . but am getting error when i excecute the alter statement in mysql.

alter statement
---------------

 ALTER TABLE tbl_travelplan add COLUMN TRREQNO id INT(11) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (id);    
3
  • what is the error message? Commented Dec 18, 2012 at 10:39
  • @sashikant Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id INT(11) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (id)' at line 1 Commented Dec 18, 2012 at 10:41
  • Ask for help but don't provide the error, errors are usually not needed. Commented Dec 18, 2012 at 10:41

5 Answers 5

3

I think the real problem is the space between TRREQNO and id in your query. Try the query i have placed below:

ALTER TABLE tbl_travelplan add TRREQNO_id INT(11) NOT NULL primary KEY AUTO_INCREMENT;
Sign up to request clarification or add additional context in comments.

1 Comment

You mean to say, you are facing the same issue with error that you have mentioned in your above comment?
3

You need to DROP THE PRIMARY KEY FIRST: Try this ::

ALTER TABLE tbl_travelplan add TRREQNO_id INT(11) NOT NULL AUTO_INCREMENT, DROP PRIMARY KEY,PRIMARY KEY;    

ALTER TABLE tbl_travelplan add TRREQNO_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY;    

1 Comment

Uhm, wtf? Why is this answer accepted, and why did it get upvotes? It shouldn't even work?
0

You have to drop the existing index first using

DROP_INDEX [your_existing_index] ON tbl_travelplan

Comments

0

You can use this query

alter table tbl_travelplan 
    add TRREQNO_id int(11) NOT NULL AUTO_INCREMET PRIMARY KEY

Comments

-1

I think in that table already having primary key. So only these kind of error came. Please remove the primary key and run your query.

Please run this query

ALTER TABLE `table_name` add TRREQNO id INT(11) NOT NULL AUTO_INCREMENT DROP PRIMARY KEY ,
ADD PRIMARY KEY ( `id` ) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.