怎样为已经创建好的表增加字段,或修改字段?
如何试用sql语句给数据表添加一个字段
2009-03-06 09:29
|
通用式: 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 [表名]
insert,update 触发器的编写
create trigger Insert_ResponseTable?
on ResponseTable ?for ?insert?
as
begin
? declare @currentId bigint
? select @currentId=id from inserted;
? update ResponseTable set createTime=getDate(),lastModifyTime=getDate() where id=@currentId;
? update ResponseTable set copyfield=question+' '+replay+' '+label where id =@currentId
end
create trigger update_ResponseTable?
on ResponseTable ?for ?update?
as
begin
declare @updateId bigint
select @updateId=id from deleted;
if update(question) or update(replay) or update(label) or update(isDeleted)
begin
update ResponseTable set lastModifyTime=getDate() where id=@updateId;
update ResponseTable set copyfield=question+' '+replay+' '+label where id =@updateId;
end
end
在一个已存在的表中插入一个字段并设置他的属性:
alter table test add id int identity(1,1)
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!