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 ,
Related
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.
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?)
MySQL Workbench came up with the following SQL to create a table:
CREATE TABLE IF NOT EXISTS `mydb`.`errors_reports` (
`error_id` INT NOT NULL ,
`report_short` VARCHAR(15) NOT NULL ,
PRIMARY KEY (`error_id`, `report_short`) ,
INDEX `error_id_idx` (`error_id` ASC) ,
INDEX `report_short_idx` (`report_short` ASC) ,
CONSTRAINT `error_id`
FOREIGN KEY (`error_id` )
REFERENCES `mydb`.`errors` (`error_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `report_short`
FOREIGN KEY (`report_short` )
REFERENCES `mydb`.`reports` (`report_short` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
which looks fine to me, and there are a bunch of other very similar tables in my database which MySQL was perfectly happy to create.
But this one...
ERROR 1022 (23000): Can't write; duplicate key in table 'errors_reports'
I can't for the life of me see any duplicate keys here. There's only one key defined!
I'm running MySQL 5.6 with a fresh default install. There's nothing in the error log.
Ideas?
Edit: through a process of elimination (going back to the simplest possible definition of the table, then gradually adding bits back in) the problem appears to be this bit:
CONSTRAINT `error_id`
FOREIGN KEY (`error_id` )
REFERENCES `mydb`.`errors` (`error_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
which is particularly odd as there is identical code in several other table definitions and those are perfectly okay!
The problem is that the name of a foreign key can not be the same as another foreign key in the entire model.
Imagine this situation
Catalog --> Supplier
Product --> Supplier
if the name of the foreign key in table Catalog for supplier is "supplier" and you assigned the same name in product table then the foreign keys names will "collide".
You need to name them differently..
For example:
catalog_supplier
product_supplier
It seems you're creating an index on the foreign key columns. When creating a foreign key in InnoDb, one will be created automatically.
See this thread.
Try using INSERT IGNORE instead of INSERT where INSERT IGNORE will not insert a new row if a duplicate primary key is found. This should help resolve the problem temporary but I would recommend truncating the table.
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;
Okay, I understand what the problem is, I sort of understand constraints in that they need to be unique in the db (didn't really know that before now, so I learned something. Not sure what they are for at the moment, but can learn later. My problem is, I think a simple matter of not understanding how MySQL Workbench assigns constraints. Apparently, you can set it in options to do so automatically which I think I am setup to do; however, something that I did made many constraints the same because they used the same FK, here is an example of what I mean:
Table 1:
CONSTRAINT `dish_type`
FOREIGN KEY (`dish_type` )
REFERENCES `acs_operations_dev`.`dish_types` (`id` )
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `tech_number`
FOREIGN KEY (`tech_number` )
REFERENCES `acs_operations_dev`.`employees` (`tech_number` )
ON DELETE NO ACTION
ON UPDATE CASCADE)
Table 2
PRIMARY KEY (`id`) ,
INDEX `tech_number` (`tech_number` ASC) ,
INDEX `car` (`car` ASC) ,
INDEX `field_dev_coach` (`field_dev_coach` ASC) ,
UNIQUE INDEX `etad_user_UNIQUE` (`etad_user` ASC) ,
CONSTRAINT `tech_number`
FOREIGN KEY (`tech_number` )
REFERENCES `acs_operations_dev`.`employees` (`tech_number` )
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `car`
FOREIGN KEY (`car` )
REFERENCES `acs_operations_dev`.`cars` (`id` )
ON DELETE NO ACTION
ON UPDATE CASCADE,
I left out some parts - just trying to show the problem - i.e. on tech_number.
I created the tables manually, on the model page (i.e. I did not use the EER "drawing" style). When I created the FKs, here is what I have done:
What is it that I am doing here that is causing WB to not auto-assign constraints? Also, what can I do to go back and fix it within WB.
I hope that makes sense. I used this method for every table, btw - so it is consistent across the board. Thanks!
Mike
Check that the tables are InnoDB
Check that the columns for the foreign key have the same data type. If you are using integer columns check that both are either unsigned or signed.
Check the names of the foreign keys they have to be unqiue. I usually use the following fromat fk_currenttablename_foreignkeytablename