zoukankan      html  css  js  c++  java
  • SQL Server 对表的 12 种一般性操作

    01、

        创建

        create table Strings(ID int);
        go

    02、

        为表添加列

        alter table Strings
        add String nvarchar(32);
        go

    03、

        添加计算列

       alter table Strings
       add ID2 as (ID+1);
       go                                               --- 看到了没有这里不用指定type了。

    04、

        修改定义

        alter table Strings
        alter column ID int not null;
        go                                             --- 对于计算列要先drop再add

    05、

        删除列

       alter table Strings
       drop column ID2;
       go                                             --- 删除时要加column添加时不要column因为根据添加的内容就可以看出加的是什么东西。

    06、

        为表加主键

        alter table Strings
        add constraint PK_ID primary key(ID);
        go

    07、

        为表加外键

       alter table Strings
       add constraint FK_A
       foreign key (String) references T(S);
       go                                              --- create table T(S nvarchar(32)not null primary key);

       alter table Strings
       add constraint FK_A
       foreign key (String) references T(S) on delete cascade on update cascade;
       go                                             --- no action ,cascade,set null,set default

    08、为表加uniqu约束

          alter table Strings
          add constraint unique_A unique(String);
          go

    09、

        为表加check约束

        alter table Strings
        add constraint CK_A
        check(String != '007');
        go

    10、

        为表加default约束

        alter table Strings
        add constraint DF_A
        default '1234656' for String;
        go

    11、

        禁用约束

        alter table Strings
        nocheck constraint FK_A;
        go

        alter table Strings

        nocheck constraint all;                           ---禁用所有约束

        

         alter table Strings

         check constraint all;                              ---启用所有约束

    12、

        删除约束

       alter table Strings
       drop constraint FK_A;
       go

  • 相关阅读:
    实现MAXIMO7.5工作流任务箱任务颜色提示功能
    MAXIMO 快速查找实现
    DELPHI 通过方法名执行方法
    MAXIMO收件箱中,检修路线修改为其它名称
    在Linux 上手工创建 oracle 11g R2 数据库
    解决 maximo7.X 设备树子节点显示不全
    C++转换构造函数和隐式转换函数
    类或者结构体用无参构造函数创建对象时不需要带括号, 否则会当成函数声明
    今天我注册自己的博客啦,吼吼吼。。
    css3学习
  • 原文地址:https://www.cnblogs.com/JiangLe/p/4005164.html
Copyright © 2011-2022 走看看