zoukankan      html  css  js  c++  java
  • TSQL 创建表 修改表默认字段数值 SQL SERVER

    在一个SQL Server数据库中,可以创建多达两万亿个表

    输入一条Create Table 语句 指定下列内容

    1.包含表的数据库

    2.表的所有者

    3.表名,在同一个数据库中和同一个所有者下,改表名必须与任何其他基表或视图不同

    4.指定11024个列

    5.主键约束(可选)

    6.1250Uniquer约束(可选)

    7.1253个外键约束(可选)

    8.1个或者多个Check约束 ,限制插入表中的数据(可选)

    9.存储表的文件组(可选)

    /*创建表*/
    use databaseName
    go
    create Table tbName
    (
       tb_id 
    int Not Null check(tb_id>0),
       UserName 
    varchar(50)  NOT NULL CHECK(UserName<>'') ,
       Sex      
    int        not Null    Default 1 ,  
      price    
    Money    NOT NULL CHECK((price is NULL ) OR (price>=0)),
    constraint tbPriKey    Primary Key (tb_id)
    )

    ----修改表--
    --
    1.新增字段-
    Alter Table tbName
      
    add  tbNewColumn int  Null
    /*在为原来的表添加一条字段的时候需要注意的是 不允许指定该列为【 NOT NULL 】*/

    ---2.删除字段------
    Alter Table tbName  drop column tbNewColumn
       
    ---3.修改字段---
    Alter Table tbName Alter column tbNewColumn char(30null

    ----4.新建约束-------
    ALTER Table tbName ADD constraint tbNewRestrain  check(tb_id>0)

    -----5.删除约束---------
    Alter Table tbName Drop constraint tbNewRestrain

    -------6.新建默认值--------
    Alter Table tbName Add constraint tbNewDefault  Default  '10' for tb_id

    -------7.删除默认值----------
    Alter Table tbName drop constraint tbNewDefault 



    select * from tbName

    2.表约束
     

       在我们创建表的时候,可以有选择的制定四种类型的约束:

    1.主键

    2.唯一性

    3.外键

    4.检查



    create table student
    (
    s_id 
    int identity(1,1primary key,
    s_name 
    varchar(20not null,
    s_age 
    int
    )

    create table test
    (
    test_no 
    int identity(1,1primary key,
    test_name 
    varchar(30),
    nax_marks 
    int not null default(0),
    min_marks 
    int not null default(0)
    )
    create table marks
    (
    s_id 
    int not null,
    test_no 
    int not null,
    marks 
    int not null default(0),
    primary key(s_id,test_no),
    foreign key(s_id) references student(s_id),
    foreign key(test_no) references test(test_no)
    )





    3. 索引以及视图的创建

    -----视图的建立-------
    create view  视图名
    (
    字段1,
    字段2,
    ..
    )
    as select a.字段1 ,a.字段2,.. from tableName as a where 


    ------索引的创建--------
    create index indexName
    on TableName
    (字段1,字段2,字段3)

    ----  修改表默认字段数值 SQL SERVER
    IF EXISTS ( SELECT * FROM syscolumns WHERE id = OBJECT_ID('cg_CgProcReturnBid') AND name = 'WinBidPrice' )
    Begin

    DECLARE @tablename VARCHAR(100), @columnname VARCHAR(100), @tab VARCHAR(100)
    SET @tablename='cg_CgProcReturnBid'
    SET @columnname='WinBidPrice'
    declare @defname varchar(100)
    declare @cmd varchar(100)
    select @defname = name FROM sysobjects A JOIN sysconstraints sc ON A.id = sc.constid WHERE object_name(A.parent_obj) = @tablename AND A.xtype = 'D'AND sc.colid =(SELECT colid FROM syscolumns WHERE id = object_id(@tablename) AND name = @columnname)
    select @cmd='alter table '+ @tablename+ ' drop constraint '+ @defname if @cmd is null print ''exec (@cmd)

    end;
    GO

    一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
  • 相关阅读:
    SVG
    GML、SVG、VML的比较
    用VS.NET中的测试工具测试ASP.NET程序
    Ajax.NET 初步应用
    解决“未能创建 Mutex”的问题(转)
    .net捕获全局异常并且记录日志、多线程方式发送邮件提醒
    DataTable导出到Excel时身份证号信息自动转换为科学计数法的解决方案
    js三级联动地址选择
    .net通过一般处理程序模拟用户控件数据保持、Ispostback 【第二版将html与ashx文件分开】
    .net通过一般处理程序模拟用户控件数据保持、Ispostback
  • 原文地址:https://www.cnblogs.com/rhythmK/p/1544285.html
Copyright © 2011-2022 走看看