对表操作--建表

建表

建表: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)
)

 

     

posted @ 2022-04-05 09:36  山上的树  阅读(160)  评论(0)    收藏  举报