-1

I have an existing product table which is already populated with 120 records and I have tried below SQL query:

ALTER TABLE product ADD product_id 
    INT PRIMARY KEY AUTO_INCREMENT

But it's given me the error:

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

StepUp
  • 36,391
  • 15
  • 88
  • 148
Ankit Sompura
  • 765
  • 6
  • 10

3 Answers3

1

Here is work around

  • create a simple column with autoincrement
  • browse the table and again make this newly added column as primary key

You are done !

:) Thanks to Guy who is not in Stackoverflow

Ankit Sompura
  • 765
  • 6
  • 10
0

Friend If you try to do this in MSSQL then your script will be this. suppose your table is below

create table product
(
 name varchar(20),
 product_type varchar(20)
)

and suppose you entered some record in this table after that you want to add a column that name product_id with auto increment and primary key. Then you use this script that is below

alter table product add product_ID int primary key identity(1,1)

suppose this will be help full for you.

-1

There can only be one auto increment field per table. But, you could have a calculated field based on the auto increment field. Or, you could have an int field where you manage the sequence by front end code or by a trigger. And also you could use a sequence in SQL Server.

CREATE SEQUENCE MySequence START WITH 100;

CREATE TABLE MyTable
(
    RealIdentity INT IDENTITY(1,1),
    RandomCol NVARCHAR(100),
    FakeIdentity INT DEFAULT NEXT VALUE FOR MySequence
);

UPDATE - MySQL Way

I just noticed that your question is taged for MySQL. Above answer is for MS SQL. Here's how you'd do the same in MySQL.

How do I create a sequence in MySQL?

MySQL equivalent of Oracle's SEQUENCE.NEXTVAL

Community
  • 1
  • 1
Sam
  • 2,917
  • 1
  • 15
  • 28