adding 2nd foreign keys in one table - MySql - mysql

I want to add 2nd foreign key on my existing table 'tbl_subcaste', its already having one 'fk caste caste_id' ass shown below:
now i want to add another 'fk religion religion_id' on this table as shown below :
but MySql giving me this error after doing this:
ALTER TABLE `tbl_subcaste`
ADD CONSTRAINT `fk religion religion_id`
FOREIGN KEY (`religion_id`) REFERE `sanskrut`.`tbl_religion`(`religion_id`)
ON DELETE NO ACTION ON UPDATE CASCADE;
Error: #1022 - Can't write; duplicate key in table '#sql-534_f7'
I can't understand, why this is not alloeing me to add another kf ?

It seems foreign key name already exist, so just change it and try as per below-
ALTER TABLE `tbl_subcaste`
ADD CONSTRAINT `fk religion religion_id100`
FOREIGN KEY (`religion_id`) REFERE `sanskrut`.`tbl_religion`(`religion_id`)
ON DELETE NO ACTION ON UPDATE CASCADE;
Even you should follow a standard to avoid this issue as you can keep name as "fk_tablename_columnname".
Further you can use below query to get all key names and other useful information.
SELECT constraint_Schema AS mydb, table_name AS child_table,constraint_name AS foreign_key_name, referenced_table_name AS master_table
FROM information_Schema.REFERENTIAL_CONSTRAINTS
WHERE constraint_Schema='mydb' AND table_name='mytable';

I think you have missed the syntax "REFERENCES". Try the below query
ALTER TABLE tbl_subcaste
ADD CONSTRAINT `f.k` FOREIGN KEY (`religion_id`) REFERENCES sanskrut.tbl_religion(`religion_id`) ON UPDATE CASCADE ON DELETE NO ACTION;

Related

Adding a foreign key to a table

I haven't practised SQL in a while and I forgot how to add a foreign key to my table
mysql> alter table students
add foreign key fk_unit(unitid)
references unit(unitid)
on delete no action
on update cascade;
ERROR 1072 (42000): Key column 'unitid' doesn't exist in table
I'm wondering why this is the case? My unit table has a primary key called unitid, why does this keep happening?
Try this one it should work....
ALTER TABLE students
ADD CONSTRAINT FK_UnitId FOREIGN KEY (unitid)
REFERENCES unit(unitid);
Try this
ALTER TABLE Students
ADD FOREIGN KEY (unitid)
REFERENCES unit(unitid)
Your query is correct. Looks like field 'unitid' is missing from 'students' table or it has a different name.

Alter Table add Foreign Key Reference

I'm doing a tutorial to learn perl/catalyst and it seems to be a little out of date. I'm trying to alter an already existing column, which was previously a primary key (Already dropped the primary key), into a foreign key. I've tried a bunch of different configurations of the syntax and can't seem to pin it down. This is my most recent attempt:
ALTER TABLE book_author (
MODIFY book_id INTEGER
ADD CONSTRAINT FOREIGN KEY book_id
REFERENCES book(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Any advice is appreciated.
You use parentheses like you are doing in a CREATE TABLE statement, but not in an ALTER TABLE statement.
You are also missing a comma between the MODIFY and the ADD CONSTRAINT lines.
And you are missing parentheses around the column book_id which is the subject of the constraint.
The following works:
ALTER TABLE book_author
MODIFY book_id INTEGER,
ADD CONSTRAINT FOREIGN KEY (book_id)
REFERENCES book(id)
ON DELETE CASCADE
ON UPDATE CASCADE;
This syntax is documented on the official MySQL site: http://dev.mysql.com/doc/refman/5.7/en/alter-table.html

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

how can i modify foreign key?

I'm wondering if it's possible to modify a Foreign Key?
FOREIGN KEY (member) REFERENCES scores (level) ON DELETE CASCADE,
And I would like to change it to:
FOREIGN KEY (member, subject) REFERENCES scores (level, subject) ON DELETE set null,
Is it possible?
You cannot modify the key in a single statement, see the ALTER TABLE syntax, in which there is no ALTER CONSTRAINT available.
You must use 2 ALTER TABLE statements to accomplish what you want.
Delete the key in the first one using an ALTER TABLE DROP FOREIGN KEY.
Re-create it with the new columns in the second, using an ALTER TABLE ADD CONSTRAINT FOREIGN KEY.
You can encapsulate both within a single transaction to make an atomic modification.
In MySql, you can accomplish that by following Tom Tresansky response, which will give us this syntax:
ALTER TABLE `table_name` DROP FOREIGN KEY `key_name`;
ALTER TABLE `table_name` ADD CONSTRAINT `constraint_name` FOREIGN KEY (`new_key_name`)
REFERENCES `other_table_name` ('other_table_id') ON UPDATE CASCADE ON DELETE CASCADE;
have you tried the alter table command?
http://www.w3schools.com/sql/sql_foreignkey.asp

MySQL InnoDB foreign key between different databases

I would like to know if it's possible in InnoDB in MySQL to have a table with foreign key that references another table in a different database ?
And if so, how this can be done ?
I do not see any limitation on https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html.
So just use otherdb.othertable and you will be good.
It's possible : Link to do it
Example (Table1 is in database1 and HelloTable is in database2) :
ALTER TABLE Table1
ADD foreign key FK_table1(ColumnNameFromTable1)
REFERENCES db2.HelloTable(ColumnNameFromHelloTable)
Below is how to add a foreign key on table t2, reference from table db1.historial(codh):
alter table t2
add foreign key FK_t2(micod2)
references db1.historial(codh)
on delete cascade
on update cascade;
ALTER TABLE `tablename1`
ADD CONSTRAINT `tablename1_student_id_foreign`
FOREIGN KEY (`tablename1`.`id`)
REFERENCES `db2`.`tablename2`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE;
if we have table calling answers in db1 and student in db2 calling ramiyusu_offline
we must type as below
ALTER TABLE `answers`
ADD CONSTRAINT `answers_student_id_foreign`
FOREIGN KEY (`id`)
REFERENCES `ramiyusu_offline`.`student`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE;