SQL如何对一个“存在的表”的列进行操作?
2017-05-07 01:29
323 查看
SQL语句
范例:ALTER TABLE 表名 ADD 列名 nvarchar(50)
1、添加没有默认值:
2、添加没有默认值:
3、有默认值的添加列:
4、删除没有默认值的列:
5、删除有默认值的列:
先删除约束(默认值)
然后在删除列
系统自带的查询约束条件的存储过程:exec sp_helpconstraint 表名
6、添加字段:
允许空字符:
不允许空字符:
7、增加字段
8、删除字段
9、修改字段类型
10、改名
关键词:sp_rename
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
语法
–假设要处理的表名为: tb
–判断要添加列的表中是否有主键
范例:ALTER TABLE 表名 ADD 列名 nvarchar(50)
1、添加没有默认值:
alter table EXCEL导出数据到Word文档 add 姓名 nvarchar(50)
2、添加没有默认值:
alter table Test add BazaarType char(1)
3、有默认值的添加列:
alter table Test add BazaarType char(1) default(0)
4、删除没有默认值的列:
alter table Test drop COLUMN BazaarType
5、删除有默认值的列:
先删除约束(默认值)
alter table Test DROP CONSTRAINT DF__Test__BazaarType__3C4ACB5F,
然后在删除列
alter table Test DROP COLUMN BazaarType
系统自带的查询约束条件的存储过程:exec sp_helpconstraint 表名
6、添加字段:
允许空字符:
`alter table 表名 add 新字段 字段类型 NULL
不允许空字符:
alter table 表名 add 新字段 字段类型 not NULL
7、增加字段
alter table docdsp add dspcode char(200)
8、删除字段
ALTER TABLE table_NAME DROP COLUMN column_NAME
9、修改字段类型
ALTER TABLE table_name ALTER COLUMN column_name new_data_type
10、改名
关键词: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
相关文章推荐
- [SQL] 如何在SQL Server2005数据库中检查一个表是否存在,如存在就删除表记录,如不存在就建表.
- 如何判断数据库中1个表已经存在,这样不会重复创建一个新表(用sql语句)
- sql 如何判断一个表是否存在
- Oracle 在SQL语句中如何获取系统当前时间并进行操作
- 如何编写一条SQL实现根据某个字段的条件对某个字段进行不同的update操作
- 对同一个表中不同的字段进行升序蒋序的操作sql中排序的误区
- 一个表里有多个字段需要同时使用字典表进行关联显示,如何写sql查询语句
- SQL如何进行增加、删除、改写、查询等数据操作
- 在sqlserver中如何对text类型的数据进行replace操作。 MS-SQL Server / 基础类 - CSDN社区 community.csdn.net
- VB 中如何在已得到的记录集中进行排序/过滤等操作sql
- SQL中如何才能将查询的结果数据存在一个变量中???
- Sql : 如何判断一个表中,是否存在一个ID,如果存在不处理,如果不存在
- 页面中checkbox返回的是一个数组,如何对数组进行操作
- 链表的基本操作 (1)采用尾插法建立一个链表; (2)输入一个元素,在道表中查找该元素是否存在,请给出相关信息; (3)将链表中的元素进行就地逆置。
- 一个list,一个数组,如何在相互比较之后进行一些操作
- onethink如何在新增时一条数据的时候运行一个函数或进行某数据库的操作
- 知道一个文件名和位置如何对其进行打开/关闭操作
- 【转载】sql 如何判断一个表是否存在
- sql 如何判断一个表是否存在
- Sql : 如何判断一个表中,是否存在一个ID,如果存在不处理,如果不存在