zoukankan      html  css  js  c++  java
  • 转 sql server新增、修改字段语句(整理)

    通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数

    增加字段: alter table [表名] add 字段名 smallint default 0 增加数字字段,整型,缺省值为0
    alter table [表名] add 字段名 int default 0 增加数字字段,长整型,缺省值为0
    alter table [表名] add 字段名 single default 0 增加数字字段,单精度型,缺省值为0
    alter table [表名] add 字段名 double default 0 增加数字字段,双精度型,缺省值为0
    alter table [表名] add 字段名 Tinyint default 0 增加数字字段,字节型,缺省值为0
    alter table [表名] add 字段名 text [null] 增加备注型字段,[null]可选参数
    alter table [表名] add 字段名 memo [null] 增加备注型字段,[null]可选参数
    alter table [表名] add 字段名 varchar(N) [null] 增加变长文本型字段大小为N(1~255)
    alter table [表名] add 字段名 char [null] 增加定长文本型字段大小固定为255
    alter table [表名] add 字段名 Datetime default 函数增加日期型字段,其中函数可以是 now(),date()等,表示缺省值

    (上面都是最常用的,还有其他的属性,可以参考下面的数据类型描述)
    删除字段: alter table [表名] drop 字段名
    修改变长文本型字段的大小:alter table [表名] alter 字段名 varchar(N)
    删除表: drop table [表名]
    创建表:
    sql="CREATE TABLE [表名] ([字段1,并设置为主键] int IDENTITY
    (1, 1) NOT NULL CONSTRAINT PrimaryKey PRIMARY KEY,"&
    "[字段2] varchar(50),"&
    "[字段3] single default 0,"&
    "[字段4] varchar(100) null,"&
    "[字段5] smallint default 0,"&
    "[字段6] int default 0,"&
    "[字段7] date default date(),"&
    "[字段8] int default 1)"
    conn.execute sql
    有null 的表示字段允许零长

    2. 修改表:
    A. 重命名表:
    EXEC sp_rename 'oldname','newname'
    B. 修改列属性:
    ALTER TABLE 学生信息
    ALTER COLUMN 姓名 varchar(20) NOT NULL
    C. 添加列:
    ALTER TABLE 学生信息
    ADD 家庭住址 nvarchar(20) NULL
    D. 删除列:
    ALTER TABLE 学生信息
    DROP COLUMN 家庭住址

    D. 修改列名:
    exec sp_rename '表名.[字段原名]','字段新名','column'

    3. 复制表:
    A. 复制整张表:
    select * into new_table from old_table

    B. 复制表结构:
    select * into new_table from old_table where 1=2

    B. 复制表内容:
    insert into new_tab select * from old_table

    4. 修改identity列

    自增列不能直接修改,必须将原有ID列删除,然后重新添加一列具有identity属性的ID字段。比如你要修改的字段名为ID:
    alter table 表名 drop column ID
    alter table 表名 add ID int identity(1,1)

  • 相关阅读:
    Something I know about WebDynpro
    Details about support package implementation
    CRM Middleware Performance Topics
    Way to configure the logon navigaion layouts via Business Roles in CRM
    DOM 常用节点类型和方法
    第一届 xdef 会议日程
    去除百度音乐盒广告的chrome插件 持续更新
    从人人网抓取高校数据信息,包括,省份 高校 院系 (提供最终SQL文件下载)
    PHP 与 JSON
    解决HTTPS 发送请求走socket问题
  • 原文地址:https://www.cnblogs.com/duoe/p/12214503.html
Copyright © 2011-2022 走看看