Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL: Complex Deletion
    primarykey
    data
    text
    <p>I basically created some tables to play around with: I have Two main tables, and a Many-Many join table. Here is the DDL: (I am using HSQLDB)</p> <pre><code>CREATE TABLE PERSON ( PERSON_ID INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, NAME VARCHAR(50), MAIN_PERSON_ID INTEGER ) CREATE TABLE JOB ( JOB_ID INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, NAME VARCHAR(50) ) CREATE TABLE JOB_PERSON ( PERSON_ID INTEGER, JOB_ID INTEGER ) ALTER TABLE JOB_PERSON ADD CONSTRAINT FK_PERSON_JOB FOREIGN KEY(PERSON_ID) REFERENCES PERSON ON DELETE CASCADE ON UPDATE CASCADE ALTER TABLE JOB_PERSON ADD CONSTRAINT FK_JOB_PERSON FOREIGN KEY(JOB_ID) REFERENCES JOB ON DELETE CASCADE ON UPDATE CASCADE ALTER TABLE PERSON ADD CONSTRAINT FK_PERSON_PERSON FOREIGN KEY(MAIN_PERSON_ID) REFERENCES PERSON ON DELETE CASCADE ON UPDATE CASCADE insert into person values(null,'Arthur', null); insert into person values(null,'James',0); insert into job values(null, 'Programmer') insert into job values(null, 'Manager') insert into job_person values(0,0); insert into job_person values(0,1); insert into job_person values(1,1); </code></pre> <p>I want to create a delete statement that deletes orphans from JOB (if there exists only one entry in the join table for a specific job) based on the PERSON.PERSON_ID. </p> <p>In pseudo language: </p> <pre><code>delete from job where job_person.job_id=job.job_id AND count(job_person.job_id)=1 AND job_person.person_id=X </code></pre> <p>Where X is some person_id. I have tried a lot of different ways; I think it is the "COUNT" part that is causing problems. I am an SQL rookie, so any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload