如何在不丢失数据的情况下将新列添加为主键 - SQL脚本 [英] How can add a new column as a Primary Key without Losing my Data - SQL Script

查看:65
本文介绍了如何在不丢失数据的情况下将新列添加为主键 - SQL脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的表'AccountMappingMaster'

已经有4个主键

现在我只想添加一个新列作为主键而不会丢失我的数据。



我尝试过Drop and Create但是如果我必须这样做,我必须使用Insert来恢复我的数据。但是我有很多数据需要时间才能完成。



请有人帮助我。谢谢

解决方案

尝试以下

1)删除表上的约束

2)添加列

3)在此栏中添加数据

4)用5个字段制作一个新的主键



要获取表格上的约束列表,请尝试

 选择  OBJECT_NAME  OBJECT_ID  AS  constrnt 
FROM sys.objects
其中​​ OBJECT_NAME (parent_object_id)= ' AccountMappingMaster'
type_desc LIKE ' %CONSTRAINT'


I have an existing table 'AccountMappingMaster'
with already 4 primary key
and now I just want to add a new column as primary key also without losing my data.

I've tried Drop and Create but If I have to do it, I have to use Insert to recover my data. But I have a lot of data and it would take time to make it done.

please someone help me. Thanks

解决方案

Try the following
1) drop the constraint on the table
2) add the column
3) add data in this column
4) make a new primary key with the 5 fields

To get a list of constraints on the table, try

select OBJECT_NAME(OBJECT_ID) AS constrnt
FROM sys.objects
where OBJECT_NAME(parent_object_id)='AccountMappingMaster'
and type_desc LIKE '%CONSTRAINT'


这篇关于如何在不丢失数据的情况下将新列添加为主键 - SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆