Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to delete when not exists is not working. Multiple columns in primary key

I am currently trying to delete from Table A where a corresponding record is not being used in Table B. Table A has Section, SubSection, Code, Text as fields, where the first three are the Primary Key. Table B has ID, Section, SubSection, Code as fields, where all four are the Primary Key. There are more columns, but they are irrelevant to this question...just wanted to point that out before I get questioned on why all columns are part of the Primary Key for Table B. Pretty much Table A is a repository of all possible data that can be assigned to a entity, Table B is where they are assigned. I want to delete all records from table A that are not in use in Table B. I have tried the following with no success:

DELETE FROM Table A 
WHERE NOT EXISTS (SELECT * from Table B 
WHERE A.section = B.section
AND A.subsection = B.subsection
AND A.code = b.code)

If I do a Select instead of a delete, I get the subset I am looking for, but when I do a delete, I get an error saying that there is a syntax error at Table A. I would use a NOT IN statement, but with multiple columns being part of the Primary Key, I just don't see how that would work. Any help would be greatly appreciated.

like image 650
user2958704 Avatar asked Nov 06 '13 02:11

user2958704


People also ask

How do I DELETE multiple primary keys in SQL?

Use SQL Server Management Studio In Object Explorer, expand the table that contains the primary key and then expand Keys. Right-click the key and select Delete.

Can primary key be defined via multiple columns?

Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

Can DELETE work without WHERE clause?

Notice the WHERE clause in the DELETE statement. The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all records in the table will be deleted!

Can we DELETE row with primary key?

If you know the value of the primary key for the row that you want to delete, you can specify the value using the VIA clause. If there is more than one primary key column, the values must be specified in order and separated by commas (,). String values must be enclosed in single quotes (').


3 Answers

In sql server,when using not exists, you need to set an alias for the table to be connected, and in the delete statement, to specify the table to delete rows from.

DELETE a FROM Table_A a 
WHERE NOT EXISTS (SELECT * from Table_B b 
WHERE a.section = b.section
AND a.subsection = b.subsection
AND a.code = b.code)
like image 105
Susan Wang Avatar answered Nov 14 '22 05:11

Susan Wang


Please try :

DELETE FROM Table A 
WHERE NOT EXISTS (SELECT 1 from Table B 
WHERE A.section = B.section
AND A.subsection = B.subsection
AND A.code = b.code)

1 is just a placeholder, any constant/single non-null column will work.

like image 26
yswai1986 Avatar answered Nov 14 '22 05:11

yswai1986


Try something like this:

delete from Table_A
    where (section, subsection, code) not in (select section, 
                                                     subsection, 
                                                     code 
                                                from Table_B)
like image 30
RGPT Avatar answered Nov 14 '22 04:11

RGPT