外键--foreign key reference(外键关联)
唯一值--unique
设置多个唯一值--constraint 名字 unique (属性:例如id,属性,属性,属性)
默认值--default
主键--primary key
限制--check
非空--not null
下面具体应用-------------------------------------------
代码示例---------------------------------------------
create database books
use books
drop table bookType
create table bookType(
tId int not null primary key,
tName varchar(20),
tNumber int,
constraint bt_name unique (tid,tname)
)
drop database test
create database test
use test
create table Type(
id int primary key,
name varchar(8) not null,
show char(2) default '是' check (show = '是' or show = '否')
)
create table PersonInformation(
id int primary key,
name varchar(12) not null,
sex char(2) default '男' check (sex = '男' or sex = '女'),
birthday date,
homeAddress varchar(100),
political varchar(20),
position
varchar(20),
nation varchar(20)default '汉族',
origin varchar(20),
phone varchar(11),
qq varchar(16),
homephone varchar(11)
)
create table InformationTable(
id int primary key,
typ int foreign key references type,
title varchar(50) not null,
content text not null,
stuId int foreign key references PersonInformation,
dat date default 'getdate()',
times int default '0'
)
单独代码示例-------------------与上面的 作业要求无关,注意红色字体
create table bookType(
tId int not null primary key,
tName varchar(20),
tNumber int,
constraint bt_name unique (tid,tname)
)
create table books(
bId int not null primary key ,
bName varchar(200)not null,
bWriter varchar(50),
bPublisher varchar(50),
bPublishdate date,
bPrice float,
bDbdate date,
bNumber int,
tId int foreign key references bookType------外键关联
)
create table s2(
sno char(2) primary key,
sn char(8) not null,
age numeric(2) not null,constraint age_chk check(age between 15 and 45),
sex char(2)
constraint sex_dft default'男' constraint sex_chk check (sex = '男' or sex = '女'),--限制
dept char(2) not null
)
create table dbUsers(
uId int not null primary key,
uName varchar(12),
uAge numeric(2) not null constraint ck_age check (uAge between 15 and 45),
uSex char(2) not null default '男' constraint ck_sex check (uSex = '男' or uSex = '女'),--限制
uDept varchar(10) not null
)