Вопрос

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?

I add an ER-MODEL for being more speceific.

Это было полезно?

Решение

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).

Другие советы

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)
);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top