patternMinor
If an entity's primary key must contain a foreign key, does that make the entity weak?
Viewed 0 times
theprimarymustcontainmakeforeignweakthatentitydoes
Problem
I'm currently learning about strong and weak entity types in the context of RDBMS. While reading, I came across this statement:
"A weak entity is dependent on a strong entity to ensure its existence. Unlike a strong entity, a weak entity does not have any primary key"
I'm starting to grasp the differences between strong and weak entity types and this statement seems incorrect to me. Specifically, it states that a weak entity does not have a primary key. However, shouldn't every entity possess a primary key? i.e., there MUST be combination of attributes that will provide uniqueness. So saying that a weak entity type does not have a primary key makes no sense.
However, it seems like the real definition of a weak entity type is an entity who's primary key MUST require a foreign key.
Is my definition of a weak entity correct?
"A weak entity is dependent on a strong entity to ensure its existence. Unlike a strong entity, a weak entity does not have any primary key"
I'm starting to grasp the differences between strong and weak entity types and this statement seems incorrect to me. Specifically, it states that a weak entity does not have a primary key. However, shouldn't every entity possess a primary key? i.e., there MUST be combination of attributes that will provide uniqueness. So saying that a weak entity type does not have a primary key makes no sense.
However, it seems like the real definition of a weak entity type is an entity who's primary key MUST require a foreign key.
Is my definition of a weak entity correct?
Solution
Strong Entities and Weak Entities are not Relational Database ideas. They are more abstract data modeling ideas than can be implemented in a Relational Database, or a Document Database, or an OO object model, etc.
When you implement Weak Entities in a Relational Database the most correct (if not the most common) pattern is for the Weak Entity's primary key to include the Strong Entity's primary key columns as the leading columns. EG
In practice you often see
Which is just wrong and sloppy, but can be made right with
By which point you can see that it's easy to make mistakes with this pattern, and it requires two indexes instead of just one.
When you implement Weak Entities in a Relational Database the most correct (if not the most common) pattern is for the Weak Entity's primary key to include the Strong Entity's primary key columns as the leading columns. EG
create table SalesOrder(Id int primary key,...)
create table SalesOrderDetail
(
SalesOrderId int not null references SalesOrder,
Id int not null,
primary key pk_SalesOrderDetail(SalesOrderId, Id),
...
)In practice you often see
create table SalesOrder(Id int primary key,...)
create table SalesOrderDetail
(
Id int primary key,
SalesOrderId int references SalesOrder,
...
)Which is just wrong and sloppy, but can be made right with
create table SalesOrder(Id int primary key,...)
create table SalesOrderDetail
(
Id int primary key,
SalesOrderId int not null references SalesOrder,
index ix_SalesOrderDetail_SalesOrder (SalesOrderId),
...
)By which point you can see that it's easy to make mistakes with this pattern, and it requires two indexes instead of just one.
Code Snippets
create table SalesOrder(Id int primary key,...)
create table SalesOrderDetail
(
SalesOrderId int not null references SalesOrder,
Id int not null,
primary key pk_SalesOrderDetail(SalesOrderId, Id),
...
)create table SalesOrder(Id int primary key,...)
create table SalesOrderDetail
(
Id int primary key,
SalesOrderId int references SalesOrder,
...
)create table SalesOrder(Id int primary key,...)
create table SalesOrderDetail
(
Id int primary key,
SalesOrderId int not null references SalesOrder,
index ix_SalesOrderDetail_SalesOrder (SalesOrderId),
...
)Context
StackExchange Database Administrators Q#317037, answer score: 4
Revisions (0)
No revisions yet.