I have 2 tables:
"customers" and "addresses". A customer can have several addresses, so they have an "n:m" relationship. For this reason, I also have the table "customer-addr".
This is how my tables look like:
+---------------+
+-----------+ | customer_addr |
| customers | +---------------+ +-----------+
+-----------+ | id | | addresses |
| id | <---> | cid | +-----------+
| name | | aid | <---> | id |
+-----------+ +---------------+ | address |
+-----------+
I need to update all customer-data incl. all addresses. For this reason I thought about deleting all existing addresses first, then updating the customer-table, and after that, I create every address new.
My question: How can I delete all existing addresses from one customer efficiently? (I have to remove rows from 2 tables).
Is there a single-statement I can use? (Without the cascade-method, this is too risky)
Or can I do it with 2 statements, without using subselects?
What's the best approach for this?
Notice that I'm using postgresql
Edit:
My whole database-design is more complex, and the address-table is not only a child from "customers" but also from "suppliers","bulkbuyers",..
Every address belongs to only one customer OR one supplier OR one bulkbuyer. (No address is used by more than one parent / no address-sharing)
Ever customer/supplier/.. can have multiple addresses.
For this reason, the edited solution from zebediah49 won't work, because it would also delete all addresses from every supplier/bulkbuyer/...