SQL如何在已经存在的表里面添加字段?并设为主键?
我的问题是这样的:
我知道添加字段用一种办法就是:alter table 表名 add 字段 字段类型
1、还有其他办法么?
2、如何在添加的时候顺便设为主键
3、如何删除字段?
我是新手,有懂的教我下哦,谢谢啦!~
[解决办法]
- SQL code
--> 测试数据:[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--主意,添加主键的时候主键字段必须有数据,所以最好是在空表的时候添加,或者这样默认自增
[解决办法]
直接在表名上右键,然后选择modify,然后可视化操作即可。
[解决办法]
alter table tb1 add row3 int identity(1000,1) primary key
alter table tb1 drop column row2
[解决办法]
- SQL code
/*添加主键列*/alter table tb add col int identity primary key/*删除主键*/alter table tb drop constraint PK_col/*删除表中字段*/alter table tb drop column col