对表操作--建表

建表

建表: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 @   山上的树  阅读(149)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示