zoukankan      html  css  js  c++  java
  • SQLServer创建约束

    --创建数据库
    create database students
    on primary
    (
        name=stu_data,
        filename='f:SQLstu_data.mdf',
        size=1,
        maxsize=10,
        filegrowth=1
    )
    log on
    (
        name=stu_log,
        filename='f:SQLstu_log.ldf',
        size=1,
        maxsize=10,
        filegrowth=1
    )
    --删除数据库
    drop database students
    --使用数据库
    use students
    --删除表
    drop table score
    drop table student
    drop table class
    --自定义类型
    Exec sp_addtype age,int ,'not null'
    --定义表时第一种方法创建主键外健

    create table student
    (
        fCode char(10) identity(1,2) constraint PK_fCode1 primary key,
        fName varchar(15) not null constraint DF_fName default 'sss',
        fClass varchar(5) not null default '',
        fSex varchar(2)  not null default ''constraint CK_fSex check (fSex in('男 ','女')),
        fAge age
    )
    create table score
    (
        fId char(10) constraint FK_fId foreign key (fId)references student,
        fSubject varchar(10) not null default '',
        fScore int not null default 0 constraint CK_fScore check(fScore>=0 and fScore<=100)
    )
    --定义表时第二种方法创建主键外键
    create table class
    (
        fClassId int not null,
        fTeacherName varchar(10) not null,
        fYear varchar(4)not null unique,
        constraint PK_fClassId primary key (fClassId),
        constraint FK_fClassId foreign key (fClassId) references student
        
    )
    --实例表
    drop table A
    create table A
    (
        fCode int not null,
        fClassId varchar(5)not null,
        fName varchar(4)not null,
        fSex varchar(2)not null,
        
    )
    --添加唯一性约束
    alter table A
    add constraint UQ_fClassId_A unique(fClassId)
    --添加主键,默认值,检查,外键约束
    alter table A
    add constraint  PK_fCode_A primary key(fCode)

    alter table A
    add constraint DF_fName_A default '' for fName

    alter table A
    add constraint CK_fSex_A check(fsex in('男','女'))

    alter table A
    add constraint FK_fCode_A foreign key(fCode) references student
    --删除约束
    alter table A
    drop constraint DF_fName_A
    --增加列
    alter table A
    add fAge int null
    --删除列
    alter table A
    drop column fAge

    /*注意
        1、主键表中主键列的类型和长度必须和外键表中列的类型和长度相同
        2、外键表中列的取值只能是主键表中主键列的子集*/
        
        
    /*
    --新增所有列数据
    insert into student values('李鸿','0201','女',23)
    insert into student values('John','0201','男',23)
    insert into score values(1,'英语',90)
    insert into class values(1,'逐月','2002')
    insert into score values(2,'英语',102)
    --新增数据部分列
    insert into student(fName,fSex) values('桐港','男')

    select * from student
    select * from score
    select * from class*/

  • 相关阅读:
    移动端屏幕旋转的事件和样式方案。
    active:移动端触摸按钮的效果。
    移动端字体单位该使用px还是rem?
    Cordova/Ionic Android 开发环境搭建
    JavaScript 深拷贝(deep copy)和浅拷贝(shallow copy)
    你不知道的JS之 this 和对象原型(一)this 是什么
    你不知道的JS之作用域和闭包 附录
    你不知道的JS之作用域和闭包(五)作用域闭包
    你不知道的JS之作用域和闭包(四)(声明)提升
    你不知道的JS之作用域和闭包(三)函数 vs. 块级作用域
  • 原文地址:https://www.cnblogs.com/zhangyongjian/p/3616897.html
Copyright © 2011-2022 走看看