문제

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