常见约束条件
NULL/NOT NULL 为空不为空
UNIQUE (唯一约束) 背此约束不能出现重复值,最多一次NULL
PRIMARY KEY (主键) 最多只能定义一个主键
FOREIGN KEY (外键) 连接两个数据表
约束
use test1;
create table S
( sNo varchar(10) primary key,
sN varchar(6) unique,
sex nchar(1),
age int,
dept nvarchar(5) )
create table C
( cNo varchar(6) primary key,
cN nvarchar(6),
cT int )
create table SC
( sNo varchar(6) not null,
cNo varchar(6) not null,
score numeric(4,1)
constraint SC_prim primary key(sNo, cNo) )
create table SC
( sNo varchar(10) not null foreign key references S(sNo),
cNo varchar(6) not null foreign key references C(cNo),
score numeric(4,1) check(score >=0 and score <= 100),
constraint S_C_prim primary key (sNo, cNo) )
-- complete student table
create table S
( sNo varchar(15) not null primary key ,
sN varchar(15) not null,
sex nvarchar(1) not null default '男' check(sex = '男' or sex = '女'),
age int not null check(age >= 15 and age <= 70),
dept nvarchar(10) not null )
无约束
-- create table in test database;
use test;
create table stuInfo1
(ID int primary key not null,
name varchar(10) not null,
age int null
)
删除数据表
使用 drop table 表名
-- drop table in test database;
use test;
drop table stuInfo1;