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

  • 相关阅读:
    利用百度轻松语音合成,语音识别
    python圆周率计算小程序(非常慢)
    python成语接龙小游戏
    在数组添加元素时报错:IndexError: list index out of range
    Redis-jedis的使用
    Shiro整合SpringMVC简单实例(一)
    容器
    防重提交功能(Token技术的引入)
    PageUtil
    单例设计模式
  • 原文地址:https://www.cnblogs.com/JiangLe/p/4005164.html
Copyright © 2011-2022 走看看