新建数据库test ,然后新增表users ,向表中插入数据
create database test; use test go if exists(select name from sys.tables where name='users') drop table users go create table users( id int primary key IDENTITY(1,1), name nvarchar(50), age nvarchar(50) ); insert into users (name,age) values ('zhangsan',23), ('lisi',12), ('wusong',32), ('zhangliang',33);
1,增加列
alter table tableName add columnName varchar(30)
2,修改列类型
alter table tableName alter column columnName varchar(4000)
3,修改列名称
EXEC sp_rename 'tableName.column1' , 'column2' (把表名为tableName的column1列名修改为column2)
4,删除列
alter table tableName drop column columnName
有时更改数据库后提示列名无效;
原因是SQL Server的intellisense(智能感知功能)需要重新整理一下,用快捷键Ctrl+Shift+R即可
关于intellisense(智能感知功能)的主要功能,就是在用户具有权限的前提下,
在Query Editior中输入架构并加上"."符号后就可以看到架构下的表、视图和用户函数了,
当用户继续输入表名或视图名并加上"."符号后就可以看到表和视图中的字段列表
简单地说,就是自动提示当前框架(表、视图)的结构实例:操作表GameSingleRoomInfo
SELECT * FROM GameSingleRoomInfo order by WriteTime desc --where roomid=146875 UPDATE GameSingleRoomInfo SET SingleRoomInfo = 1,writetime = 2 WHERE RoomId = 099508 alter table GameSingleRoomInfo drop column WriteTime --删除列WriteTime alter table GameSingleRoomInfo add SingleRoomInfoN varbinary(MAX) --增加列SingleRoomInfoN alter table GameSingleRoomInfo add WriteTime datetime --增加列WriteTime EXEC sp_rename 'GameSingleRoomInfo.time' , 'WriteTime' --更改列名time为WriteTime alter table GameSingleRoomInfo alter column WriteTime datetime --smalldatetime --更改列类型