sql - 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)
);
解决方案
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.
推荐阅读
- yql - 如何在 Vespa 中 YQL 查询的搜索响应中获取结构类型映射的子字段?
- scala - 如何提高 Spark 中的查询性能?
- c# - 将多个 WPF 用户定义的组合框绑定到同一个 Observable 集合
- c# - 如何拒绝来自其他应用程序的 HTTP POST 请求?
- c# - 当同时提交两个或多个请求时,ActionMethodSelectorAttribute 中的 AmbiguousMatchException
- ajax - JSF 自定义异常处理程序不适用于 ajax
- python - 枕头图像打开保存未按预期工作
- javascript - 使用 Mocha 和 Vanilla Javascript 进行事件测试
- c# - 如何修复 POST 到机器人端点的 HTTP 状态为 417 失败?
- api - Google App Engine Search API 和 PhonenumberUtil 不起作用