Question

How to create a table for a weak entity using microsoft sql server ?

The weak entity contains a partial key, and it is denoted in the ER diagram using a dotted line.

My issue is, how to create a table to this weak entity including it's partial key.

Était-ce utile?

La solution

Example:

First entity: Book(BookId - PK, Titlle)

Second entity (the weak entity): Chapter(No, Title) (at this point Chapter.No isn't a primary key because one or more books may have chapters with the same No).

In order to create the weak table I would add BookId to Chapter table and, also, I would create a compound unique key and a simple PK:

create table dbo.Chapter(

Id int identity primary key,

BookId int not null referenced dbo.Book(BookId),

No tinyint not null,

unique(BoolId, No),

Title nvarchar(100) not null

);

The reasons for defining a simple PK for dbo.Chapter are:

1) a compound PK could force me to define, also, compound FKs and

2) FK join elimination requires a simple FK.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top