[MDEV-26085] TRUNCATE on table does not take foreign keys in account Created: 2021-07-03  Updated: 2023-10-12  Resolved: 2023-10-12

Status: Closed
Project: MariaDB Server
Component/s: Storage Engine - InnoDB
Fix Version/s: N/A

Type: Task Priority: Major
Reporter: Jan Lindström (Inactive) Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-26053 TRUNCATE on table with Foreign Key Co... Closed

 Description   
  • Logically TRUNCATE is same as DELETE but currently ON DELETE actions are not executed.

CREATE TABLE author (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL
) ENGINE = InnoDB;
CREATE TABLE book (
id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
author_id SMALLINT UNSIGNED NOT NULL,
CONSTRAINT `fk_book_author`
    FOREIGN KEY (author_id) REFERENCES author (id)
ON DELETE CASCADE
ON UPDATE RESTRICT
) ENGINE = InnoDB;
INSERT INTO author (name) VALUES ('Abdul Alhazred');
INSERT INTO book (title, author_id) VALUES ('Necronomicon', LAST_INSERT_ID());
SELECT * FROM author;
id	name
1	Abdul Alhazred
SELECT * FROM book;
id	title	author_id
1	Necronomicon	1
TRUNCATE TABLE book;
SELECT * FROM author;
id	name
1	Abdul Alhazred
SELECT * FROM book;
id	title	author_id



 Comments   
Comment by Jan Lindström (Inactive) [ 2021-07-03 ]

serg Question is should we fix this or retain current implementation. See additional discussion on MDEV-26053. In Oracle there is special CASCADE option on TRUNCATE https://www.oracletutorial.com/oracle-basics/oracle-truncate-table/#:~:text=In%20this%20case%2C%20the%20TRUNCATE,DELETE%20CASCADE%20clause%20to%20work. My suggestion is to implement similar CASCADE option for TRUNCATE.

Generated at Thu Feb 08 09:42:37 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.