Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL create weak entity table

Tags:

sql

ddl

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?

like image 744
NoFace Avatar asked Oct 19 '14 07:10

NoFace


People also ask

What is a weak entity in SQL?

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.

What is the difference between a weak entity and a candidate key?

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.

How do I use the second step for weak entities?

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.

What is the relation between one strong and one weak entity?

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.


1 Answers

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
);
like image 52
Mureinik Avatar answered Sep 25 '22 15:09

Mureinik