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

  • 相关阅读:
    C# 空合并运算符 ??
    基于ASP.NET Core 创建 Web API
    使用 csc.exe 编译C#代码
    NPOI创建Excel批注
    http协议 put、delete请求asp.net mvc应用,报404错误
    visual studio清理nuget包缓存
    String.IsNullOrEmpty 与 String.IsNullOrWhiteSpace
    C# linq to xml 简单示例
    SQL Server修改表结构,不允许保存更改。
    SQL Server 分离与附加数据库
  • 原文地址:https://www.cnblogs.com/JiangLe/p/4005164.html
Copyright © 2011-2022 走看看