当我尝试执行以下sql statemant时,MariaDB会出错: SQL:TRUNCATE $ table CASCADE;
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASCADE' at line 1 (SQL: TRUNCATE wortmann_products CASCADE;)
是在MariaDB中删除级联还是在sql语句中出错?
答案 0 :(得分:0)
如果您希望级联删除和更新,则必须在创建或更改表时存储定义,并将其应用于允许子记录的键(或关联表中的FK)删除父记录时在关联表中删除。这里有很多资源,我在这里列出了一个:https://mariadb.com/kb/en/the-mariadb-library/foreign-keys/ - 从这个资源中获取的是下面的例子:
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;