码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server 对表的 12 种一般性操作

时间:2014-10-03 19:52:25      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   sp   c   on   cti   r   ad   

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

 

SQL Server 对表的 12 种一般性操作

标签:io   ar   for   sp   c   on   cti   r   ad   

原文地址:http://www.cnblogs.com/JiangLe/p/4005164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!