1、修改字段名:

  alter table 表名 rename column A to B

2、修改字段类型:

  alter table 表名 alter column 字段名 type not null

3、修改字段默认值
  alter table 表名 add default (0) for 字段名 with values

  如果字段有默认值,则需要先删除字段的约束,在添加新的默认值,

  select c.name from sysconstraints a 
  inner join syscolumns b on a.colid=b.colid 
  inner join sysobjects c on a.constid=c.id
  where a.id=object_id('表名') 
  and b.name='字段名'

  根据约束名称删除约束

  alter table 表名 drop constraint 约束名

  根据表名向字段中增加新的默认值

  alter table 表名 add default (0) for 字段名 with values

4、增加字段:

  alter table 表名 add 字段名 type not null default 0    --缺省值 default 是可选参数 ,eg:

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()等,表示缺省值

5、删除字段:

  alter table 表名 drop column 字段名;

6、删除表: drop table [表名]

7、创建表:       

CREATE TABLE 表名称
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
....
)

--使用T-SQL语句创建表
use ConstructionDB
go
---查询 库中是否存在 此表 存在则删除
if exists(select * from sysobjects where name = 'T_flow_step_def')
drop table T_flow_step_def
--- 方法二
IF OBJECT_ID (N'bas_CardType') IS NULL
BEGIN --如果不存在该表,则进行创建
--drop table com_CodeRecord

8、 修改表:

EXEC sp_rename 'CountProce','CountProce1'; ------重命名存储过程名;

EXEC sp_rename 'fruits.f_name','f_names','COLUMN';--重命名字段名;

EXEC sp_rename 'oldname','newname'; -----------重命名表名;

 

9、 复制表:

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

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

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

 

posted on 2018-10-22 15:42  田园牧歌2018  阅读(125)  评论(0编辑  收藏  举报