SQL如何在已经存在的表里面添加字段?并设为主键?

爆小猴 2012-05-24 03:30:59
我的问题是这样的:
我知道添加字段用一种办法就是:alter table 表名 add 字段 字段类型
1、还有其他办法么?
2、如何在添加的时候顺便设为主键
3、如何删除字段?

我是新手,有懂的教我下哦,谢谢啦!~
...全文
5174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
以学习为目的 2012-05-26
  • 打赏
  • 举报
回复
/*添加主键列*/
alter table tb add col int identity primary key
/*删除主键*/
alter table tb drop constraint PK_col
/*删除表中字段*/
alter table tb drop column col
叶子 2012-05-24
  • 打赏
  • 举报
回复
直接在表名上右键,然后选择modify,然后可视化操作即可。
十三门徒 2012-05-24
  • 打赏
  • 举报
回复
alter table tb1 add row3 int identity(1000,1) primary key
alter table tb1 drop column row2
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test](
[工资项目] varchar(4),
[简写] varchar(2),
[金额] int,
[月分] int
)

--添加
alter table test add id int identity primary key
--删除主键
alter table test drop constraint PK__test__3213E83F687E5358
--删除id字段
alter table test drop column id

--主意,添加主键的时候主键字段必须有数据,所以最好是在空表的时候添加,或者这样默认自增

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧