sql添加、删除表中的列
2015-07-14 11:09
351 查看
添加没有默认值:alter table Test add BazaarType char(1)
有默认值的添加列:alter table Test add BazaarType char(1) default(0)
删除没有默认值的列:alter table Test drop COLUMN BazaarType
删除有默认值的列:先删除约束(默认值)alter table Test DROP CONSTRAINT DF__Test__BazaarType__3C4ACB5F,然后在删除列
alter table Test DROP COLUMN BazaarType
系统自带的查询约束条件的存储过程:exec sp_helpconstraint 表名
添加字段:
允许空字符: alter table 表名 add 新字段 字段类型 NULL
不允许空字符: alter table 表名 add 新字段 字段类型 not NULL
增加字段
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
收藏于 2009-07-09
来自于百度空间
有默认值的添加列:alter table Test add BazaarType char(1) default(0)
删除没有默认值的列:alter table Test drop COLUMN BazaarType
删除有默认值的列:先删除约束(默认值)alter table Test DROP CONSTRAINT DF__Test__BazaarType__3C4ACB5F,然后在删除列
alter table Test DROP COLUMN BazaarType
系统自带的查询约束条件的存储过程:exec sp_helpconstraint 表名
添加字段:
允许空字符: alter table 表名 add 新字段 字段类型 NULL
不允许空字符: alter table 表名 add 新字段 字段类型 not NULL
增加字段
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
收藏于 2009-07-09
来自于百度空间
相关文章推荐
- SQLite Expert 删除表数据并重置自动增长列
- SQL跨数据库复制表
- sql2005 卸载之后重装时报:对性能监视器计数器注册表值执行系统配置检查失败的解决方案
- 使用PLSql连接Oracle时报错ORA-12541: TNS: 无监听程序
- SQL_常用函数
- MySql数据库迁移图文展示
- 使用Mybatis时请注意这两个参数,否则会让你的数据库连接爆掉
- SQL如何在一个数据库中查询已知列名的所在表
- SQL_常用函数
- SQL基本语句
- sql:根据字段查询表名
- 【VB】SQL多条件查询(机房问题)
- SQL Server执行计划的理解
- [solaris]odbc-oracle,简单测试
- ruby redis使用
- D3+mysql 画Force图
- 当触碰一个陌生的数据库前,我要问些什么样问题
- mysql 主从不同步
- 数据库基础---数据库系统概论个人总结版
- mysql笔记