How to alter Foreign Keys in phpMyAdmin - mysql

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;

Related

How to delete a foreign key thats references multiple columns in mySQL

I have a foreign key that references two columns in a child table. Id like to delete the foreign key but mySQL just hangs and nothing happens:
Here is the output from SHOW CREATE TABLE table_name:
KEY FK_animal_index (animal_type,food_index),
CONSTRAINT FK_animal_index FOREIGN KEY (animal_type, food_index)
REFERENCES animal_schedules (animal_type, food_index)
ENGINE=InnoDB
I have tried deleting this foreign key using:
`ALTER TABLE table_name DROP FOREIGN KEY FK_animal_index;`
`ALTER TABLE table_name DROP FOREIGN KEY animal_type;`
ALTER TABLE section_configuration DROP FOREIGN KEY FK_animal_index,
DROP KEY (animal_type, food_index);
AND
ALTER TABLE section_configuration DROP FOREIGN KEYFK_animal_index, ADD CONSTRAINT FK_animal_type FOREIGN KEY (animal_type) REFERENCESanimal_schedules(animal_type) ON DELETE SET NULL;
Others have mentioned that ON DELETE not being set can prevent you changing key values (mySQL will reject the change if ON DELETE is not set to anything)
But to no avail, mySQL just hangs and 30 minutes later still nothing. The database is very small at the moment so removing the FK should be fast.
The answer to this question was not about primary keys at all.
I had to stop my program from accessing the table in order to make modifications to it.
In mySQL db, you can add tables and add columns at any time, but if you want to change a column or remove a column, it must not be in use by any program or it will hang indefinitely.

How to add foreign key to MySQL table?

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?)

Get mysql to show table with references

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 ,

How to add a foreign key in MySQL on WAMPserver?

I can't figure how to indicate that a column is a foreign key in WAMPserver. I suppose I could write the MySQL query for that, but I would think that there is also a way to do that using the user interface (PHPMyAdmin)...?
Creating a foreign key constraint relies on your storage engine being set to something that can support it (such as InnoDB). In PHPMyAdmin, you can set this in "Operations" for the table with the "Storage Engine" option. Once that's complete:
Make sure you've assigned an index to the column you'll be assigning a foreign key to.
Click on "Relation view" under the table details on the "Structure" tab.
Assign your foreign key constraint and decide on the actions for DELETE and UPDATE.
you can add a foreign key for the existing table by the following query
ALTER TABLE sample.employee
ADD FOREIGN KEY (dno)
REFERENCES sample.department(dnumber)
here sample. employee is your current table and sample .department is existing table which has the value for your foreign key dno is current table forign key and dnumber is existing table primary key.

How do I rename a foreign key in mysql?

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