truncate table with foreign key mysql code example

Example 1: cannot truncate a table referenced in a foreign key constraint

SET FOREIGN_KEY_CHECKS = 0; 
TRUNCATE table $table_name; 
SET FOREIGN_KEY_CHECKS = 1;

Example 2: mysql how to truncate table with foreign keys

SET FOREIGN_KEY_CHECKS = 0;

TRUNCATE table1;

SET FOREIGN_KEY_CHECKS = 1;

Example 3: alter table add foreign key mysql

ALTER TABLE orders
ADD 
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;

Example 4: truncate table mysql

TRUNCATE TABLE `fourniture`

Example 5: create table mysql with foreign key

CREATE TABLE Orders (
    OrderID int NOT NULL,
    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

Example 6: how to force truncate a table in mysql

DELETE FROM mytest.instance;
ALTER TABLE mytest.instance AUTO_INCREMENT = 1;

Tags:

Sql Example