简体   繁体   中英

Cascade delete query on clients table

I am trying to configure the CLIENTS table on my database such that if an employee is deleted from the EMP table any associated clients are automatically deleted from the CLIENTS table.

This is what I've tried so far.

ALTER TABLE clients
MODIFY empno REFERENCES emp(empno) ON DELETE CASCADE

Unfortunately this returns a near "MODIFY": syntax error every time I try to run it. Any idea what is going wrong?

Thanks in advance.

Use this:

ALTER TABLE Clients
  ADD CONSTRAINT `Client_1`
    FOREIGN KEY empno REFERENCES emp(empno) ON DELETE CASCADE;

UPD:

According to this post , there is no way you can do that in SQLite. Therefore, my suggestion is this:

  • Create a temporary table.
  • Copy all the info there.
  • Drop the original table and create it with foreign key.
  • Copy all info from temporary table and remove the temporary table.

Of course, if the Clients table is empty right now, you can simply delete it and recreate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM