首页 > 解决方案 > Do i need to add the constraint line I used in a strong entity when I'm making a create statement for a weak entity?

问题描述

So if i have a strong entity like:

Book (bid, price)
PK (bid)
Constraint chk_book_price check (price >= 0)

And weak entity like:

Store (bid, storename)
FK (bid)

Do i need to add the constraint line I used in the strong entity when I'm making a create statement for the weak entity?

So like

Create Table Store
(
    bid number (4),
    storename varchar2 (20),
    Foreign Key (bid) references Book (bid),
    Constraint chk_book_price check (price >= 0)
);

标签: sqloracle

解决方案


No, you don't have to. you just need to have constraints for the STORE table in the definition.

Create Table Store
(
    bid number (4),
    storename varchar2 (20),
    Foreign Key (bid) references Book (bid),
);

So for table BOOK you only need to create constraints for itself like

CREATE TABLE BOOK (
BID NUMBER(5),
PRICE NUMBER(7, 2)
CONSTRAINT CHK_BOOK_PRICE CHECK (PRICE >= 0)
)

FOREIGN KEY CONSTRIANT refers to a unique or primary key of another table and before you can create FOREIGN KEY you must create a parent table first with PRIMARY OR UNIQUE KEY.

Either you create BOOK table first or use ALTER TABLE to create constraints when both tables are created.


推荐阅读