sql增加删除字段
2011-10-15 14:03
239 查看
增加字段
alter table docdsp add dspcode char(200)
删除字段
ALTER TABLE table_NAME DROP COLUMN column_NAME
修改字段类型
ALTER TABLE table_name ALTER COLUMN column_name new_data_type
改名
sp_rename
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
语法
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]
--假设要处理的表名为: tb
--判断要添加列的表中是否有主键
if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')
begin
print '表中已经有主键,列只能做为普通列添加'
--添加int类型的列,默认值为0
alter table tb add 列名 int default 0
end
else
begin
print '表中无主键,添加主键列'
--添加int类型的列,默认值为0
alter table tb add 列名 int primary key default 0
end
/**************************************************************************************/
判断table1中是否存在name字段
if exists(select * from syscolumns where id=object_id('table1') and name='name') begin
select * from people;
end
判断table1中是否存在name字段且删除字段
if exists(select * from syscolumns where id=object_id('table1') and name='name') begin
select * from people;
alter table table1 DROP COLUMN name
end
alter table docdsp add dspcode char(200)
删除字段
ALTER TABLE table_NAME DROP COLUMN column_NAME
修改字段类型
ALTER TABLE table_name ALTER COLUMN column_name new_data_type
改名
sp_rename
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
语法
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]
--假设要处理的表名为: tb
--判断要添加列的表中是否有主键
if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')
begin
print '表中已经有主键,列只能做为普通列添加'
--添加int类型的列,默认值为0
alter table tb add 列名 int default 0
end
else
begin
print '表中无主键,添加主键列'
--添加int类型的列,默认值为0
alter table tb add 列名 int primary key default 0
end
/**************************************************************************************/
判断table1中是否存在name字段
if exists(select * from syscolumns where id=object_id('table1') and name='name') begin
select * from people;
end
判断table1中是否存在name字段且删除字段
if exists(select * from syscolumns where id=object_id('table1') and name='name') begin
select * from people;
alter table table1 DROP COLUMN name
end
相关文章推荐
- sql 创建表、删除表 增加字段 删除字段操作
- Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结
- ORACLE中通过SQL语句(alter table)来增加、删除、修改字段
- SQL替换语句 批量修改、增加、删除字段内容
- SQL替换语句 批量修改、增加、删除字段内容
- sql 表字段增加删除
- SQL替换语句之批量修改、增加、删除字段内容
- ORACLE中通过SQL语句(alter table)来增加、删除、修改字段
- sql 创建表、删除表 增加字段 删除字段操作
- ORACLE中通过SQL语句(alter table)来增加、删除、修改字段
- ORACLE中通过SQL语句(alter table)来增加、删除、修改字段
- SQL增加、删除、更改表中的字段名
- SQL 增加,修改,删除字段
- sql增加字段、修改字段名、删除字段、修改表名、改存储过程名!!
- SQL增加、删除、更改表中的字段名
- sql 创建表、删除表 增加字段 删除字段操作
- SQL增加,删除,更改表中字段
- sql增加字段、删除字段、增加约束、删除约束、修改缺省值、修改字段数据类型、重命名字段、重命名表
- sql 创建表、删除表 增加字段 删除字段操作
- sql 创建表、删除表 增加字段 删除字段操作