图书信息表:图书编号,图书名称,出版社,出版日期,图书价格,图书作者,借出标识,读者编号,描述
①主键 constraint pk_name primary key <column_name>
②外键 constraint fk_name foreign key(column_name) reference table_name(column_name)
③检查 constraint ck_name check (condition)
④唯一 constraint un_name unique (column_name)
⑤非空 <column name> <data type> not null,
⑥默认值<column name> <data type> default <default value>,
--没有任何约束条件
create table bookinfo{
bookid int,
bookname char,
publish varchar2(20),
pubdata varchar2(20),
price decimal,
author char(40),
store int,
reader int,
remarks varchar2(50)
};
--指定了默认值:price store
create table bookinfo{
bookid int,
bookname char,
publish varchar2(20),
pubdata varchar2(20),
price decimal(7,2) default 0.0,
author char(40),
store int default 0 not null,
reader int,
remarks varchar2(50)
};
--检查约束:price
create table bookinfo{
bookid int,
bookname char,
publish varchar2(20),
pubdata varchar2(20),
price decimal,
author char,
store int,
reader int,
remarks varchar2(50),
constraint ck_price check (price>=10 and price<=100)
};
--非空约束:bookid bookname
create table bookinfo{
bookid int not null,
bookname char not null,
publish varchar2(20),
pubdata varchar2(20),
price decimal,
author char,
store int,
reader int,
remarks varchar2(50)
};
--唯一约束:bookname
create table bookinfo{
bookid int,
bookname char,
publish varchar2(20),
pubdata varchar2(20),
price decimal,
author char,
store int,
reader int,
remarks varchar2(50),
constraint un_bookname unique(bookname)
};
--外键约束 publish
create table publishinfo{
publishid varchar2(20) primary key,
publishname varchar(30)
}
create table bookinfo{
bookid int,
bookname char;
publish varchar2(20),
pubdata varchar2(20),
price decimal,
author char,
store int,
reader int,
remarks varchar2(50),
constraint fk_publish foreign key (publish) references publishinfo(publishid)
};
--创建一个含有多个约束的表
create table bookinfo{
bookid int,
bookname char,
publish varchar2(20) not null,
pubdata varchar2(20),
price decimal,
author char,
store int,
reader int,
remarks varchar2(50),
constraint ck_price check (price<=100),
constraint un_bookname unique (bookname)
};