oracle创建外键约束的两种方式

转自:https://blog.csdn.net/baidu_37107022/article/details/74853467

1、创建表时直接创建外键约束

create table books(
    bookid number(10) not null primary key,
    bookName varchar2(20) not null,
    price number(10,2),
    categoryId number(10) not null references Category(id)  --外键约束
);

备注:必须先创建参照表,才能在创建外键约束,即必须现有表Category,再有book

2、先创建表,表创建成功后,单独添加外键约束

create table books(
    bookid number(10) not null primary key,
    bookName varchar2(20) not null,
    price number(10,2),
    categoryId number(10) not null
);

ALTER TABLE  books ADD CONSTRAINT FK_Book_categoryid FOREIGN KEY(categoryId ) REFERENCES Category(id);

猜你喜欢

转载自blog.csdn.net/qq_31279347/article/details/84032455