添加删除字段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 [表名]
相关文章推荐
- redis实现消息队列
- Python3之redis使用
- 基于SSH框架、Oracle数据库、easyui的分页显示
- mysql命令行参数(转)
- redis实现cache系统实践(二)
- 使用SQL Server 发送邮件
- redis实现cache系统原理(一)
- postgresql cast转换类型
- mysql日常运维与参数调优
- python_Memcached
- mysql配置PATH
- MySql 修改外键 支持级联删除
- Oracle Error:ORA-01465: 无效的十六进制数字【解决办法】
- python对缓存(memcached,redis)的操作
- 实战Zabbix-Server数据库MySQL的libdata1文件过大
- MySql总是弹出一个mySqlInstallerConsole的窗口
- MySql 纵表转横表
- 关于远程连接MySQL数据库的问题解决
- oracle数据字典和动态性能视图
- 8种Nosql数据库系统对比