When I have an entity with another weak entity, how do I create a table in such a situation, where if I delete the primary entity, the weak entity will be deleted as well?
The term weak entity is used for a table where the rows cannot exist without referencing some "parent" entity. For example, you might have tables Users and Phones such that a user can have zero, one, or multiple phones. But a phone number cannot exist without referencing its owning user.
The meaning of a candidate key is that the columns are non-NULL and they can be used to uniquely identify any row in the table. The term weak entity is used for a table where the rows cannot exist without referencing some "parent" entity. For example, you might have tables Users and Phones such that a user can have zero, one, or multiple phones.
The second step is almost identical to the first but is used for weak entities. Here's what you do: For each weak entity in the model-there are three: inventory, order, and item -translate the entity directly to a CREATE TABLE statement as in Step 1.
The relation between one strong and one weak entity is represented by double diamond. Weak entities are represented with double rectangular box in the ER Diagram and the identifying relationships are represented with double diamond. Partial Key attributes are represented with dotted lines.
A foreign key with on delete cascade
should do the trick:
CREATE TABLE primary_entity (
id numeric PRIMARY KEY,
-- some data fields
);
CREATE TABLE weak_entity (
id numeric PRIMARY KEY
REFERENCES primary_entity(id)
ON DELETE CASCADE,
-- some data fields
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With