We've just completed a long-running migration on a large table, and ended up with the following constraint on our conversation_tags table:
CONSTRAINT `conversation_tags_ibfk_1` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
Unfortunately, there was a bug somewhere, because what we wanted was:
CONSTRAINT `fk_conversation_tags_tags` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
Dropping and re-adding the constraint would mean another two long queries. Is there any way to rename the constraint in a single query?
From the documentation:
Multiple ADD, ALTER, DROP, and CHANGE clauses are permitted in a
single ALTER TABLE statement, separated by commas. This is a MySQL
extension to standard SQL, which permits only one of each clause per
ALTER TABLE statement.
This way you can combine the drop and recreate into one query, and that should be faster than dropping the constraint and creating it in two queries:
ALTER TABLE conversation_tags
DROP FOREIGN KEY `conversation_tags_ibfk_1`,
ADD CONSTRAINT `fk_conversation_tags_tags` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`);
I'm sorry, but constraints can only be dropped and re-attacched in mySQL
The feature does not seems to be available in mysql ALTER TABLE syntax.
However it is supported for Oracle.
Please refer to my answer in MySQL terminology "constraints" vs "foreign keys" difference? to understand why the constraint name was different than what you desired.
However, MySQL doesnot have rename constraint feature and hence need to DROP and ADD FK with desired name .
https://dev.mysql.com/doc/refman/5.5/en/alter-table.html
Related
I'm trying to add barista_grade_id to the baristas table as a foreign key and I've looked every where on what syntax to use and it seems to be unanimous that this is the correct way.
ALTER TABLE baristas
ADD FOREIGN KEY barista_grade_id REFERENCES barista_grade(barista_grade_id)
I did it before this and it worked. but because of some mistakes I deleted it and redo it again, but for whatever reason it says that it's a syntax error.
The format is like this:
ALTER TABLE baristas
ADD CONSTRAINT FOREIGN KEY (barista_grade_id) REFERENCES barista_grade (barista_grade_id);
I have successfully added my first FK in heidisql using the foreign key tab and adding all the appropriate sections.
I have tried to do the same to my second related column both by using the FK tab and by running a query but I keep getting an error.
SQL Error (1005): Can't create table sprout.#sql-430_3 (errno: 150 "Foreign key constraint is incorrectly formed")
sprout is my db name so I have no idea why it is saying cant create table sprout (because I'm not referencing it in my query).
sql query for my first FK(generated via heidisql):
ALTER TABLE `purchase_history`
ADD CONSTRAINT `bus_id` FOREIGN KEY (`bus_id`) REFERENCES `business` (`bus_id`);
sql query for my second FK(generated via heidisql)
ALTER TABLE `purchase_history`
ADD CONSTRAINT `bus_name_fk` FOREIGN KEY (`bus_name`) REFERENCES `business` (`bus_name`);
sql query I wrote to try and add second FK
Alter table purchase_history
Add constraint bus_name_fk
Foreign key (bus_name)
references business(bus_name);
Can someone help explain to me how my constraint is incorrectly formed? To my understanding I was able to add another constraint to the the table.
This is too long for a comment.
Huh? Why are you adding two foreign constraints to the same table . . . but using different columns? That doesn't really make sense. In general, the foreign key should be referencing the primary key of the other table, which I presume is bus_id.
Then, if you want the business name, you can use a join to get the name.
I use MySQL with InnoDB engine. I double-checked type of columns. But always have:
Error Code: 1215. Cannot add foreign key constraint
I tried:
ALTER TABLE `mail`.`boxes`
ADD CONSTRAINT FK_id
FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
ON UPDATE NO ACTION
ON DELETE NO ACTION;
and
ALTER TABLE `mail`.`boxes`
ADD FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
Nothing works(((
Please, help, what I am doing wrong (except choosing MySQL :-) )?
If table contains data then you are not able to add foreign key you drop table object and recreate
use below reference for the same
Basics of Foreign Keys in MySQL?
To check what exactly the problem is, use:
SHOW ENGINE INNODB STATUS\G
There is section "last foreign key error". Look at: http://dev.mysql.com/doc/refman/5.0/en/innodb-monitors.html
My guess is that data type od mail.boxes (id) and mail.users (id) is not the same. (E.g. smallint in one table and integer in second one).
Data in table on which you're trying to create FK could possibly also be problem (are your mailbox ids the same as id of existing users?)
the command show create table myTable shows the foreign keys but without the parent table. Is there a way to also view the parent tables?
What I get is this:
KEY `FK_friend_id` (`friend_id`)
What I want to see is this (or something that shows customer):
CONSTRAINT `fk_friend_id` FOREIGN KEY (friend_id) REFERENCES customer (id) ON DELETE RESTRICT ON UPDATE CASCADE
From my understanding i could say that ,
Foreign Key have not created for it,
For Creating Foreign key Constraint ,Use Innodb Engine ,bcz if you have used MYISAM Engine it may fail to create the Foreign Keys , just we could see the primary keys alone as you mentioned
Correct me if 'm wrong ,
I have set up a Foreign Key in my mysql database with:
ALTER TABLE `gameplayers` ADD CONSTRAINT `FK_GAMENUMBER` FOREIGN KEY (`GameNumber`) REFERENCES `games`(`GameNumber`) ON UPDATE CASCADE ON DELETE CASCADE;
However, I am not sure I want the ON UPDATE and ON DELETE anymore.
So I go into my phpAdmin, click on the edit pencil icon in the Index section of the Structure Tab and I get this:
Warning: ("PRIMARY" must be the name of and only of a primary key!)
Do alterations just have to be done manually? Ie the pencil icon will just not work.
ALSO: Do foreign keys have the same speed bonus effect on mysql searches, similar to Indexes?
Foreign keys require indexes, so effectively, the foreign key constrain creates and index and it can be used to resolve queries just like normal indexes.
I'm not sure which version of phpMyAdmin you are using, I think foreign key constains are supported in newest versions, but it seems your does not list foreign key indexes, and the primary key is not what you are looking for. You can however modify the keys with plain SQL:
ALTER TABLE `gameplayers` DROP FOREIGN KEY FK_GAMENUMBER,
ADD CONSTRAINT `FK_GAMENUMBER` FOREIGN KEY (`GameNumber`) REFERENCES `games`(`GameNumber`) ON UPDATE NO ACTION ON DELETE NO ACTION;