I create a table whose query is
Create table unititem (ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (ID));
than alter it by following query
Alter table unititem AUTO_INCREMENT=1;
I also have a table tablename
and in want to alter it by adding a column unitItemId
which references table unititem
. The query which I want to execute is
Alter table tablename add column Unititemid INT NOT
NULL AUTO_INCREMENT ;
but when I execute this query it gives me this error:
Incorrect table definition; there can be only one auto column and it must be defined as a key
When I want to make it a foreign key then it gives 1005 error
on following query
Alter table tablename
add column Unititemid INT NOT NULL AUTO_INCREMENT,
ADD FOREIGN KEY (Unititemid) REFERENCES unititem (ID) ;
I don't know what I'm doing wrong. Any suggestions?
Update1
As suggested I'm not adding unititemid
as AUTO_INCREMENT
ALTER TABLE tablename ADD COLUMN unititemid INT NOT NULL;
Now the problem is: how can I make it a foreign key? When i use the following query it gives an error:
ALTER TABLE tablename ADD FOREIGN KEY (unititemid) REFERENCES unititem(ID);
Error: Error Code: 1005
Can't create table 'alnasar_inventory.#sql-a04_1' (errno: 150)
howto resolve this issue ? Any Suggestions.