create table 表名
(A1 D1,
A2 D2,
……
An Dn,
<完整性约束>,
……
<完整性约束>,
);
Ai:属性名
Di:属性域(属性Ai的类型以及约束)
完整性约束也可直接在Di处定义
常见完整性约束:
primary key
foreign key from referrnces 表名
not null
null
default
例(环境:workbench):
create database test;
use test;
create table Students( #创建Students表
S_ID integer, #整型
S_NAME varchar(10) not null, #字符型,最长为10,不允许空值存在
S_AGE integer defult 1, #整型,缺省值为1
S_TEL varchar(11) null, #字符型,最长为11,允许空值存在
primary key(S_ID)); #将S_ID设为主键
insert into Students values(1,'TOM',14,'13223616367'); #插入元组
insert into Students values(2,'JACK',14,'13245277581'); #插入元组
select * from Students; #Students表展示
create table Mush_scores( #创建Mush_scores表
ID integer, #整型
SCORE integer, #整型
primary key(ID), #将ID设为主键
foreign key(ID) references Students); #将ID设为外键
结果如下: