I would use this create table syntax to implement that ER diagram
CREATE TABLE bookStore (
idBookstore integer,
address text,
PRIMARY KEY(idBookstore)
);
CREATE TABLE section (
idBookstore integer,
sectionNumber integer,
surface text,
FOREIGN KEY (idBookstore) REFERENCES bookStore.idBookStore,
PRIMARY KEY (idBookstore, sectionNumber)
);
CREATE TABLE shell (
idBookstore integer,
sectionNumber integer,
shellNumber integer,
FOREIGN KEY (idBookstore) REFERENCES bookStore.idBookStore,
FOREIGN KEY (sectionNumber) REFERENCES section.sectionNumber,
PRIMARY KEY (idBookstore, sectionNumber, shellNumber)
);
The key point to think about here is what is the key that defines the weak entity. Then you can substitute the primary key of the related entity.
So in this case, section
has a primary key that's the tuple (idBookstore,sectionNumber)
. We'll substitute that with X
for this argument.
The shell
is related to the section and is a weak entity, so it's key should be (X, shellNumber)
. This is the same form as section
. And we can expand X
to get the correct foreign key (idBookstore, sectionNumber, shellNumber)
.