向现有表添加新主键

发布于 2024-09-26 14:47:05 字数 612 浏览 0 评论 0原文

我有包含以下详细信息的表

表名称 EMPLOYEE 和列

EMPID (PK smallint not null)
EMPNAME (varchar 256 not null)
ORG (FK smallint not null)
FUNCTION (FK smallint not null)
EFF_DATE (datetime null)
AUDIT_ID (varchar null)

现在我必须向该表 ADD_UID 添加一个额外的 coulmn 并使其成为主键

我正在使用此查询但失败。

ALTER TABLE CVADMIN.EMPLOYEE
 ADD ADD_UID  varchar(32) NULL,
 CONSTRAINT PK_EMPLOYEE PRIMARY KEY [NON]CLUSTERED (ADD_UID)
go

表“EMPLOYEE”已定义主键。

编辑

这里的想法是新列应该是唯一的,这样如果失败我可以抛出 _KEY_VIOLATION 以便完成一些代码操作

I have table with following details

Table name EMPLOYEE and columns

EMPID (PK smallint not null)
EMPNAME (varchar 256 not null)
ORG (FK smallint not null)
FUNCTION (FK smallint not null)
EFF_DATE (datetime null)
AUDIT_ID (varchar null)

Now I have to add an extra coulmn to this table ADD_UID and make it also primary key

I am using this query but failing.

ALTER TABLE CVADMIN.EMPLOYEE
 ADD ADD_UID  varchar(32) NULL,
 CONSTRAINT PK_EMPLOYEE PRIMARY KEY [NON]CLUSTERED (ADD_UID)
go

Table 'EMPLOYEE' already has a primary key defined on it.

EDIT

The idea here is the new column should be unique so that if it fails I can throw _KEY_VIOLATION so that some code manipulation is done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许你一世情深 2024-10-03 14:47:05

要添加唯一约束(主键的附加约束),请执行以下操作:

ALTER TABLE EMPLOYEE ADD CONSTRAINT uc_UID UNIQUE (ADD_UID)

To add a unique constraint (which is additional to the primary key) do this:

ALTER TABLE EMPLOYEE ADD CONSTRAINT uc_UID UNIQUE (ADD_UID)
才能让你更想念 2024-10-03 14:47:05

我们可以通过 alter 语句在任何表上添加新列,但添加的列可以为 null 并且您知道主键在任何列上都不接受 null
因此我们不能通过alter语句在新添加的列上创建主键。

We can add new columns on any tables by alter statement but added column can be null and you know primary key does not accept null on any column.
Therefore we can not create primary key on the newly added column by alter statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文