I have a table with a primary key. I want to add a new column and make that one the primary key. However, I can't drop the primary key constraint on the old column, because it is referenced as a foreign key in another table.
So I dropped the foreign key constraint and the index:
ALTER TABLE experiments DROP FOREIGN KEY experiments_ibfk_1
ALTER TABLE experiments DROP INDEX `analysisfk`;
I checked with show create that both constraint and index are gone. Then I tried to drop the primary key again with
ALTER TABLE analysis DROP PRIMARY KEY;
However, I still got an Error 1025.
Am I overlooking something?
SOLVED: There was another key constraint which I overlooked because of a misleading name. Leaving this here anyway as it shows some useful stuff
Related
So real quick what I did:
Realisted didn't need unique key but still foreign key
First needed to drop the foreign key
ALTER TABLE groups DROP FOREIGN KEY group_information_id
Then dropped the unique key with
ALTER TABLE groups DROP INDEX group_information_id
Now I want to add the foreign key again with
ALTER TABLE groups ADD CONSTRAINT group_information_id FOREIGN KEY (group_id) REFERENCES group_information.group_id
Get the error (this is tanslated sorry if i'm not accurate, tell me if you don't understand):
Can not add foreign key
Why is this happening, and how can I fix this so I can add a foreign key?
Engine innodb used
I need to generate a Unique key by dropping the existing key on MySQL. My current version is MySQL 5.7
I dropped the existing key using the following query,
DROP INDEX `uk_bookid_bookname` ON Books;
where BookId is the foreign key.
Then,I added new unique key using the following query,
ALTER TABLE Books ADD UNIQUE uk_bookid_bookname (BookId, BookName);
I got the following error,
ERROR 1553 (HY000): Cannot drop index 'uk_bookid_bookname': needed in a foreign key constraint
I need to drop the existing key and then add a new unique key. But, it works vice-versa.
You have to drop the foreign key. Foreign keys in MySQL automatically create an index on the table
ALTER TABLE mytable DROP FOREIGN KEY mytable_ibfk_1 ;
then add another index key
I have a Mysql table having the following structure:
As you can see there is a composite primary key constraint between the fields: word_id and preposition_id.
I want to remove the Primary Key constraint from word_id without touching the preposition_id field, and without losing data from the linked tables (Foreign Key tables). How can I do it?
Regards.
There is no syntax available to modify a constraint and drop only "a half" of the primary key.
You must drop the whole primary key, and then recreate it from scrach.
Just:
ALTER TABLE tablename DROP PRIMARY KEY;
and then:
ALTER TABLE tablename ADD PRIMARY KEY ( preposition_id );
You need first to drop all foreign keys thar reference the primary key in this table.
Data in tables will be preserved.
I want to change primary key of table, initially it was id, now i want to change it to userid
smsusers(id,fname,lname,userid)
Here id is varchar type
adn userid is int type
for this i am trying following query
ALTER TABLE smsusers DROP PRIMARY KEY
which is showing this error
#1025 - Error on rename of '.\xrcwrn_sms\#sql-ae0_6f' to
'.\xrcwrn_sms\smsusers' (errno: 150)
id of smsusers is associated with many tables as foreign key.
How to change the primary key.
Here is an example:
ALTER TABLE `database`.`table`
DROP PRIMARY KEY,
ADD PRIMARY KEY (`userid`);
The message is telling you that you can't drop the primary key yet because it is referenced by one or more foreign keys. You need to identify and drop the foreign keys first, then drop the primary key.
ERROR NO:150 means Foreign key definition problem. I think that some other table has a foreign key constraint depending on this PK, so you need to drop that first and rebuild it later.
I tried this link. This is working properly.
My steps are
CREATE INDEX id_pk_unique ON smsusers (id)
ALTER TABLE parent DROP PRIMARY KEY;
ALTER TABLE parent ADD PRIMARY KEY (userid);
I have a table (DBC) with a primary key and unique constraint. The unique constraint consists of two fields, a foreign key to an other table (Client) and a date.
The unique constraint was created with the following statement.
alter table DBC
add constraint UNQ_DBC_CLIENT_STARTDATUM unique (FK_CLIENT, START_DATUM);
The unique constraint was just there to ensure that there where no duplicates in the database, it has never been used in a foreign key.
Now when I want to drop the unique constraint I always get the error
Cannot drop index 'UNQ_DBC_CLIENT_STARTDATUM':
needed in a foreign key constraint.
I tried all suggestions (drop key, drop index, alter table etc) I could find but without succes.
Any suggestions ?