How to use foreign keys in SQL Buddy? - mysql

I need to use foreign keys for update and cascade, etc.
ALTER TABLE topics
ADD FOREIGN KEY(topic_by) REFERENCES users(user_id)
ON DELETE RESTRICT ON UPDATE CASCADE;
but I am not able to make foreign keys in SQL Buddy.
Any way to do that?

did you try this :
ALTER TABLE topics
ADD CONSTRAINT topic_by FOREIGN KEY(user)
REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE

Try this query:
ALTER TABLE topics
ADD CONSTRAINT topic_by
FOREIGN KEY (user_id) REFERENCES users(user_id);

Related

How to refer primary key as Foreign to various table

I have a table called User Which contain user_id as primary key
I have other tables like Doctor ,patient,staff ...etc which contain column called created by which reffer to user_id of user table.
Here is my issue I cannot use same constrain name across different table for example
ALTER TABLE `emr#default`.`staff`
ADD CONSTRAINT `FK_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;
is created successfully
when I try with other table
ALTER TABLE `emr#default`.patient
ADD CONSTRAINT `FK_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;
generating error Error Code: 1022. Can't write; duplicate key in table '#sql-1_7a64'
if I try
ALTER TABLE `emr#default`.patient
ADD CONSTRAINT `FK_created_by1`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;
is creating fk
my Doubt is can we use same constrain name.if not can I create different constrain name that will not effect anythin
Constraint names can have conflicts between tables. That is why I include both the table and column names in the constraint name:
ALTER TABLE `emr#default`.patient
ADD CONSTRAINT `FK_patient_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;
ALTER TABLE `emr#default`.`staff`
ADD CONSTRAINT `FK_staff_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;

how can two foreign keys of one table refer to a primary key of another [duplicate]

I have a table user with userID as the primary key. I have another table called Friends. In the Friends table, I have two Users as friends represented by the columns UserID and FrndID where both UserID and FrndID should be a userID in table user.
I want to enforce data integrity. Could I use something like this?
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`, `friendId`)
REFERENCES `users` (`userId`, `userId`) ON DELETE CASCADE ON UPDATE CASCADE;
I want to know is REFERENCESusers(userId,userId) referencing a column multiple times correctly? The reason I am not creating 2 separate constraints, is that both users must exist in table user.
No, you should create two foreign keys:
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`)
REFERENCES `users` (`userId`)
ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`friendId`)
REFERENCES `users` (`userId`)
ON DELETE CASCADE ON UPDATE CASCADE;

mysql Multiple Foreign Keys in a Table to the Same Primary Key

I have a table user with userID as the primary key. I have another table called Friends. In the Friends table, I have two Users as friends represented by the columns UserID and FrndID where both UserID and FrndID should be a userID in table user.
I want to enforce data integrity. Could I use something like this?
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`, `friendId`)
REFERENCES `users` (`userId`, `userId`) ON DELETE CASCADE ON UPDATE CASCADE;
I want to know is REFERENCESusers(userId,userId) referencing a column multiple times correctly? The reason I am not creating 2 separate constraints, is that both users must exist in table user.
No, you should create two foreign keys:
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`)
REFERENCES `users` (`userId`)
ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`friendId`)
REFERENCES `users` (`userId`)
ON DELETE CASCADE ON UPDATE CASCADE;

Can I alter a foreign key constraint in mysql?

I have a foreign key in 3 tables and they are on cascade update . so I would like to add another cascade delete ,. Is that possible without dropping the constraint ? if yes please give me any example with alter .
To change your foreign key, you first have to drop it (using the name) and then create a new foreign key with the correct definition and your done!
ALTER TABLE `pets` DROP FOREIGN KEY `your_fk_name_here`;
ALTER TABLE `pets` ADD FOREIGN KEY (`owner_id`) REFERENCES `owners`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;
or
ALTER TABLE `pets` ADD CONSTRAINT fk_owner_pet FOREIGN KEY (`owner_id`) REFERENCES `owners`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;

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;