您的位置:首页 > 数据库

添加删除字段sql

2016-07-23 11:51 260 查看
1.把不是自增的主键列变成自增的主键列
USE Test1
alter table test2 drop constraint PK_test2 --删除主键约束
alter table test2 drop column id --删除列
alter table test2 add id int identity(1,1) not null --重新添加自增列
alter table test2 add constraint PK_test2 primary key (id)  --把列设为主键


2.插入列、列说明
ALTER TABLE dbo.TeacherMessage ADD MsgType INT NOT NULL DEFAULT 0

ALTER TABLE TeacherClass ADD CorrectServices INT
EXECUTE sp_addextendedproperty N'MS_Description', N'享受免费批改服务(0、不享受 1、享受)', N'user', N'dbo', N'table', N'TeacherClass', N'column', N'CorrectServices'

3.修改列
ALTER TABLE TeacherBankCard ALTER COLUMN CardNum varchar(50) NOT NULL


4.修改列说明
EXECUTE sp_updateextendedproperty N'MS_Description', N'版权信息', N'user', N'dbo', N'table', N'CourseSystem', N'column', N'Copyright'


/*添加主键列*/
alter table test1 add id int identity primary key
/*删除主键*/
alter table test1 drop constraint PK_test1
/*删除表中字段*/
alter table test drop column id


  

  

  

  

  

通用式: 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 tb add col int identity primary key
/*删除主键*/
alter table tb drop constraint PK_col
/*删除表中字段*/
alter table tb drop column col

修改变长文本型字段的大小:alter table [表名] alter 字段名 varchar(N)

删除表: drop table [表名]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: