对表操作--建表
建表
建表:create table 表名 create table Student
删表:drop table 表明 drop table Student
主键约束:列名 属性 primary key Sno int(4) primary key
列名 属性,primary key(列名) Sno int(4), primary key (Sno)
外键约束:列名 属性 foreign key references 来源表名(来源列名)
Sno int(4) foreign key references Student (Student Sno)
列名 属性,foreign key references 来源表名(来源列名)
Sno int(4), foreign key (Sno) references Student (Student Sno)
组合主键 primary key (Sno,Cno)
check约束:限制列的取值范围
Ssex char(2) check(Ssex in ('男','女')) Ssex char(2) check(Ssex = '男' or Ssex = '女')
default约束:设置默认值 Ssex char(2) default'男'
约束命名:constraint 名 约束 constraint PK_Student_Sno primary key
实例:
use PRO create table Student ( Sno char(9) not null constraint PK_Student_Sno primary key, Sname varchar(20) not null unique, Ssex char(2) default'男', ---Ssex char(2) check(Ssex in ('男','女')) Sage Smallint, ---Ssex char(2) check(Ssex = '男' or Ssex = '女') Sdept varchar(20) ) create table Course ( Cno char(4) , Cname varchar(40), Cpno char(4) not null foreign key references Course(Cno), Ccredit Smallint check(Ccredit>=1 and Ccredit<=4) constraint PK_Course_Cno primary key (Cno), unique (Cname), ) create table SC ( Sno char(9) not null, Cno char(4) not null, Grade smallint, primary key (Sno,Cno), foreign key (Sno) references Student(Sno), foreign key (Cno) references Course(Cno) )
11111111111111111111111111

浙公网安备 33010602011771号