I have a table (multisite network) where I use blog_id as one of the columns which is also needed on some of my custom php functions. Until now, I haven't used FK which should be appropriate for my tables.
So, can I FK to wordpress wp_blogs table? if yes, if ever I delete that blog will it also delete the entries on my other tables that references wp_blogs?
CREATE TABLE wp_blogs (blog_id INT NOT NULL...)
FOREIGN KEY (blog_id)
REFERENCES wp_options(blog_id)
ON DELETE CASCADE;
If you created a table like above then every time when blog_id will remove from wp_options then all rows from wp_blogs will remove too.
When you remove rows from wp_blogs then no rows won't remove from wp_options
Read about ON DELETE CASCADE in MySQL documentation.
Related
Lets say I have two tables - User and Post
I introduced a customized Join table Vote to have a many-to-many relationship between User and Post.
The tables have following structure:
User (UseId, Name)
Post (PostId, UserId, Content)
Vote(Id, UserId, PostId, Value)
Notes:
The emphasized columns of each table is a PK.
The UserId in Post is a FK to User table.
The UserId and PostId columns in Vote table are FK to the
respective tables.
Other columns like Value, Content, Name, etc. are varchar.
Considering the above design is appropriate (if not, suggestions are welcomed :) .....
What I want is:
If a row in Post Table is deleted, the related row in Vote should also be deleted.
If a row in User Table is deleted, the related row in Vote should also be deleted.
If a row in User Table is deleted, the related row's UserId column in Post should be set to NULL.
Can I achieve such kind of relationships, without any Cyclic-Redundancy? If yes, How?
UPDATE:
Check out this awesome answer if you too have faced multiple cascade paths:
You are looking for cascading foreign key relationships. For the first two:
alter table vote
add constraint fk_vote_user
foreign key (userid) references user(userid) on delete cascade;
alter table vote
add constraint fk_vote_post
foreign key (postid) references post(postid) on delete cascade;
For the third:
alter table post
add constraint fk_post_user
foreign key (userid) references user(userid) on delete set null;
These are explained in the documentation.
One way is to add isdeleted bit, changed datetime columns to each table and use triggers to update column values on delete. In that case you will keep history of your votes, posts and users.
Or just on delete triggers.
Or to use cascade relationship as Gordon Linoff posted.
Hi I need to rename a table, and add some columns to it.
This table has a PK column (Id) and a Self-referencing column (ParentId). These constraints use the old table name in their names.
There are also other tables that use this tables PK as foreign keys.
So, what would be the correct way to do this, asssuming that I need to rename TableA->TableB?
Drop constraints in all tables that reference TableA.Id
sp_rename 'dbo.TableA', 'TableB'
Add constraints that were dropped with new names?
Or, there is some other way?
How to delete the data in a row in table 1 whose primary key is using as a foreign key in 2nd table.. I am facing the problem here
delete from ASSIGNMENT
where proj_num=18
delete from PROJECT
where proj_num=18
proj_num is the primary key in project and foreign key in assignment.
How to do this task in one delete query ?
You can create/edit the relationship between the two tables in order to set the delete behaviour to ON DELETE CASCADE, then you can just delete the proj_num=18 from the PROJECT table, and it will automatically delete the related records from the ASSIGNMENT table. Hope it works for you.
ALTER TABLE ASSIGNMENT
ADD CONSTRAINT fk_proj
FOREIGN KEY (proj_num)
REFERENCES PROJECT (proj_num)
ON DELETE CASCADE;
Or if you don't have access to the table design you may use this.
DELETE proj
FROM Project AS proj INNER JOIN Assignment AS assn
ON proj.proj_num = asn.proj_num
WHERE proj_num = 18
I have several tables with content. Is there a way how I can remove all the content without removing the keys (primary key, foreign key etc.) ?
When I say truncate or delete it obviously returns an error.
Cannot truncate table 'login' because it is being referenced by a FOREIGN KEY constraint.
You're right, you cannot run truncate table on an table which uses an foreign key. But you can run a normal delete on the table itself.
The only thing you should take care of is the order.
For example:
You have a table users and a table users_log where you store all users logins.
You cannot run the delete on the users table if still rows from users_log referencing them.
If you delete all rows from users_log and afterwards deleting the rows from users everything should be fine.
In this particular example, this code will work:
DELETE FROM users_log
DELETE FROM users
While this won work:
DELETE FROM users
DELETE FROM users_log -- which has a foreign key constraint on users
I need to create a script (SQL Server 2012) that deletes tables from one specific schema, I'm not using cascade delete. I'm for example getting all the tables from sys.tables for that specific schema. Is there any way of getting the tables in order that I delete first the ones with FK and after the main ones? like cascade delete but in a script. I know I can use "nocheck constraint all" but I prefer to do it directly.
Thank you.
Not possible sadly.
You can have a loop of FKs so that no sequence of deletes will work.
The simplest is a pair of tables where each references the other.
-- Pathological foreign keys
-- There is no order in which you can drop these tables
CREATE TABLE one(a INT PRIMARY KEY, b INT)
CREATE TABLE two(a INT PRIMARY KEY, b INT)
ALTER TABLE one ADD FOREIGN KEY (b) REFERENCES two(a)
ALTER TABLE two ADD FOREIGN KEY (b) REFERENCES one(a)