ALTER TABLE `rfid_reader` ADD FOREIGN KEY (`lattitude`, 'longitude') REFERENCES `ROBS`.`location`(`lattitude`, 'longitude') ON DELETE RESTRICT ON UPDATE RESTRICT;
The above command results in an error. Do i have to refence lat->lat and long->long seperately? I though a foreign key always had to reference a primary key.
Help would be much appreciated.
Related
I'm using MySQL 5.6 and I've read the MySql Reference guide regarding this but no where is it mentioned that the PK should be at the end of the list while creating a composite Foreign Key.
The only requirement in the guide that talks about columns is the following -
"In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order."
If so, then why doesn't the following work?
alter table table_1
add constraint "fk_key_1" FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2" ("pk_col", "col_2") ON DELETE NO ACTION;
But this works -
alter table table_1
add constraint "fk_key_1" FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2" ("col_2", "pk_col") ON DELETE NO ACTION;
The foreign key has to match the primary key (or some key, but preferably the primary one) -- both the types and order.
If this works:
FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2 ("col_2", "id_col") ON DELETE NO ACTION;
It is because the primary key on table_2 is defined as (col_2, id_col) rather than (id_col, col_2).
I am trying to add foreign key 'USERNAME' in tutorial table, but there was an error.
Executing:
ALTER TABLE `databse`.`tutorial`
ADD CONSTRAINT `USERNAME`
FOREIGN KEY (`USERNAME`)
REFERENCES `databse`.`register` (`USERNAME`)
ON DELETE CASCADE
ON UPDATE CASCADE;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1452: Cannot add or update a child row:
a foreign key constraint fails (`databse`.`#sql-e7c_5`, CONSTRAINT `USERNAME` FOREIGN KEY (`USERNAME`)
REFERENCES `register` (`USERNAME`) ON DELETE CASCADE ON UPDATE CASCADE)
SQL Statement:
ALTER TABLE `databse`.`tutorial`
ADD CONSTRAINT `USERNAME`
FOREIGN KEY (`USERNAME`)
REFERENCES `databse`.`register` (`USERNAME`)
ON DELETE CASCADE
ON UPDATE CASCADE
Foreign key setting:
Tutorial table setting:
Any ideas ? thank you
I solved it, i created a new 'tutorials' table replace 'tutorial' table, and use same way to add foreign key, it worked! = =
still thank you for your helps !!
as stated here :
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
In the code you show : you try to reference the "USERNAME" column, wich is not a primary key in your last capture
So you can either change your primary key in the register table to USERNAME, or you can change the foreign key to reference TutorialName
You have two table one is child table and second is parent table .So you would need to guarantee that each child column has NULL or has values that present in parent column.
This problem is normally caused by mismatching of values presented in the two columns constrained by the new foreign key.
That is, the value presented in the child table does not have a reference presented in the parent table.
when creating a foreign key, you need to make sure that:
the table type supports foreign key
there is a index on the foreign key column
the types of data of the two columns constrained by a foreign key are similar enough so that they can be converted to each other
the data presented in both columns constrained by a foreign key is consistent.
SETTING
I have a rails application that references an external mysql server. I am about to push some changes to the rails application. I made some changes to my local/test version of the application's database using rails migrations, but my production/live version of the application's database is not on a server that has the rails application, so I don't think I can just run the rails migration on the mysql database.
QUESTION
How do I safely add the following column, key and constraint:
`item_id` int(11) DEFAULT NULL,
KEY `index_notifications_on_item_id` (`item_id`),
CONSTRAINT `fk_rails_f395ae520f` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE CASCADE,
and safely change the unique key:
UNIQUE KEY `item_id_and_user_id_stand_id_unique_index` (`item_id`,`user_id`,`stand_id`)
to
UNIQUE KEY `item_id_and_user_id_stand_id_notified_user_id_unique_index` (`item_id`,`user_id`,`stand_id`,`notified_user_id`),`)
ATTEMPTS
CONCERNING THE CONSTRAINT AND KEY
I try running:
ALTER TABLE notifications ADD FOREIGN KEY fk_rails_f395ae520f(item_id) REFERENCES items(id) ON DELETE CASCADE;
But, I run into 2 issues:
1: It names the key what I want the constraint to be named and makes up its own constraint name.
OUTPUT:
KEY `fk_rails_f395ae520f` (`item_id`),
CONSTRAINT `notifications_ibfk_2` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE CASCADE,
It requires me to temporarily set: set foreign_key_checks = 0; or else I get the error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (DB_NAME.#sql-3c9_49, CONSTRAINT #sql-3c9_49_ibfk_2 FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE)
and I do not know if doing that is "safe" to do on a production/live server.
CONCERNING THE COLUMN AND UNIQE KEY
I believe I have the column and unique keys safely added with:
ALTER TABLE notifications add COLUMN item_id INT(11) DEFAULT NULL;
ALTER TABLE notifications DROP INDEX index_notifications_on_user_id_and_notified_user_id, ADD UNIQUE KEY `item_id_and_user_id_stand_id_notified_user_id_unique_index` (stand_id, user_id, notified_user_id, item_id);
CLARIFICATION
The reason I bring up the column and unique key even though I may have it working is the same reason I gave a setting: I am new to this and may be taking a flawed approach to the whole scenario. I am hoping this context allows someone to call me out on it if that's the case.
UPDATE 1
ALTER TABLE notifications ADD CONSTRAINT fk_rails_f395ae520f FOREIGN KEY index_notifications_on_item_id(item_id) REFERENCES items(id) ON DELETE CASCADE;
outputs:
CONSTRAINT `fk_rails_f395ae520f` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE CASCADE,
Which is what I want, but I am still working on the KEY section.
UPDATE 2
ALTER TABLE notifications add COLUMN item_id INT(11) DEFAULT NULL;
ALTER TABLE notifications DROP INDEX index_notifications_on_user_id_and_notified_user_id, ADD UNIQUE KEY `item_id_and_user_id_stand_id_notified_user_id_unique_index` (stand_id, user_id, notified_user_id, item_id);
CREATE INDEX index_notifications_on_item_id ON notifications (item_id);
ALTER TABLE notifications ADD CONSTRAINT fk_rails_f395ae520f FOREIGN KEY index_notifications_on_item_id(item_id) REFERENCES items(id) ON DELETE CASCADE;
I believe these four lines in sequence answers the question of how to manually handle what my rails migration does. But, I am going to leave this question here. The question that remains is: "is it a terrible idea to do what I just did? and if so, how to do proceed?"
I was truncating tables like this
TRUNCATE TABLE `enterprise_url_rewrite`; -- error
TRUNCATE TABLE `enterprise_catalog_category_rewrite`; -- works fine
in that order or reversed when I got this error.
Cannot truncate a table referenced in a foreign key constraint
(`magento`.`enterprise_catalog_category_rewrite`, CONSTRAINT
`FK_415B32DA3DF924D5C803CF24EB3AC1D9` FOREIGN KEY (`url_rewrite_id`) REFERENCES
`magento`.`enterprise_url_rewrite` (`url_rewrite_id`)
I thought foreign key constraints were enforced to prevent deletion when there are actual values being referenced. I recollect (I could be wrong) being able to TRUNCATE this table previously without disabling key checks. Note that DELETE FROM enterprise_url_rewrite works.
More info on the constraint definition on enterprise_catalog_category_rewrite.
KEY `FK_744D72D1D79D148B7C2542E53B0370B5` (`url_rewrite_id`),
CONSTRAINT `FK_744D72D1D79D148B7C2542E53B0370B5` FOREIGN KEY (`url_rewrite_id`)
REFERENCES `enterprise_url_rewrite` (`url_rewrite_id`) ON DELETE CASCADE ON UPDATE NO ACTION
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?)