Search
 
SCRIPT & CODE EXAMPLE
 

SQL

drop foreign key

ALTER TABLE table_name
DROP CONSTRAINT fk_name;
Comment

drop foreign key

ALTER TABLEEmployee
DROP FOREIGN KEY FK_EmployeeDept;
Comment

delete from table if foreign key delete

-- Schema
CREATE TABLE T1 (
    `ID` int not null auto_increment,
    `Label` varchar(50),
    primary key (`ID`)
);

CREATE TABLE T2 (
    `ID` int not null auto_increment,
    `Label` varchar(50),
    primary key (`ID`)
);

CREATE TABLE TT (
    `IDT1` int not null,
    `IDT2` int not null,
    primary key (`IDT1`,`IDT2`)
);

ALTER TABLE `TT`
    ADD CONSTRAINT `fk_tt_t1` FOREIGN KEY (`IDT1`) REFERENCES `T1`(`ID`) ON DELETE CASCADE,
    ADD CONSTRAINT `fk_tt_t2` FOREIGN KEY (`IDT2`) REFERENCES `T2`(`ID`) ON DELETE CASCADE;

-- Data
INSERT INTO `T1` (`Label`) VALUES ('T1V1'),('T1V2'),('T1V3'),('T1V4');
INSERT INTO `T2` (`Label`) VALUES ('T2V1'),('T2V2'),('T2V3'),('T2V4');
INSERT INTO `TT` (`IDT1`,`IDT2`) VALUES
(1,1),(1,2),(1,3),(1,4),
(2,1),(2,2),(2,3),(2,4),
(3,1),(3,2),(3,3),(3,4),
(4,1),(4,2),(4,3),(4,4);

-- Delete
DELETE FROM `T2` WHERE `ID`=4; -- Delete one field, all the associated fields on tt, will be deleted, no change in T1
TRUNCATE `T2`; -- Can't truncate a table with a referenced field
DELETE FROM `T2`; -- This will do the job, delete all fields from T2, and all associations from TT, no change in T1
Comment

PREVIOUS NEXT
Code Example
Sql :: top 10 rows in mysql 
Sql :: mysql cannot delete or update a parent row 
Sql :: mysql your password does not satisfy the current policy requirements 
Sql :: postgres restart id 
Sql :: import database in mysql command line xampp 
Sql :: sql server alter column 
Sql :: install mysql server linux 
Sql :: postgresql if 0 then 1 
Sql :: how to get current mysql version 
Sql :: install mysql workbench ubuntu 20.04 terminal 
Sql :: mysql count grouped rows 
Sql :: oracle cpu per session 
Sql :: install mysql powershell 
Sql :: SQL Integer devision 
Sql :: mysql show column data types 
Sql :: current setting postgres timezone 
Sql :: mysql date between two dates 
Sql :: get the rows from two tables whose relation is in 3rd table 
Sql :: sql get user account 
Sql :: mysql subtract month from timestamp 
Sql :: check constraint to check if date greater than todays date 
Sql :: how to check nls format in oracle 
Sql :: capitalize 1st letter in sql server 
Sql :: mysql cli connect with password 
Sql :: change name of colum in sql table 
Sql :: oracle limit user tablespace 
Sql :: NOT LIKE sql laravel 
Sql :: remove all records from table mysql 
Sql :: sql fillna 
Sql :: having vs where sql 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =