Reputation: 105
How can I translate to the relational model a weak entity related to another weak entity which is related to the strong one?
I want to translate this ER MODEL to the relational database structure. So, what I'm wondering is whether this is correct:
BOOK_STORE(idBookStore, address);
SECTION(idBookStore, sectionNumber, surface)
WHERE idBookStore REFERS BOOK_STORE;
SHELL(shellNumber, sectionNumber, idBookStore)
WHERE sectionNumber
REFERS TO SECTION and
idBookStore REFERS TO BOOK_STORE.
So my question is: is this correct?
Upvotes: 1
Views: 5297
Reputation: 11
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)
);
Upvotes: 1
Reputation: 12983
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)
.
Upvotes: 2