删除sql语句违反完整性约束

max*_*mus 2 java database eclipse hsqldb cascading-deletes

我通过delete sql语句获得了Integrity约束违规.发生这种情况是因为表的id在另一个表中用作主键.但是我想通过使用CASCADE删除它们.

但是什么是hsqldb的正确语法?

在此输入图像描述

a_h*_*ame 5

DELETE语句不支持"级联"关键字(在手册中明确记录)

您需要设置外键约束以级联删除:

create table playlist
(
   id integer primary key not null, 
   ... other columns ...
);

create table playlistmovies
(
   id integer primary key not null,
   playlist_id integer not null,
   ... other columns
);

alter table playlistmovies
   add constraint fk_plm_playlist
   foreign key (playlist_id) references playlist(id)
   on delete cascade;
Run Code Online (Sandbox Code Playgroud)

然后,当您删除播放列表时,也会删除引用该播放列表的所有行.