如何从表中删除标识列

发布于 2024-09-03 00:44:46 字数 98 浏览 1 评论 0原文

我创建了一个表,其中 employee id 作为身份列。现在,我想删除标识并将数据类型替换为 bigint。我使用的是 Sql 精简版。如何实现这一目标?

I have created a table with employee id as identity column. Now, I want to remove identity and replace datatype as bigint. I am using Sql Compact edition. How to achieve this?

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

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

发布评论

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

评论(2

感情洁癖 2024-09-10 00:44:47

我不相信您可以使用 TSQL 从列中删除 IDENTITY 属性。
相反:

  1. 将新的 BIGINT 列添加到表中
  2. 将数据从当前 IDENTITY 字段复制到新字段中
  3. 删除现有列 将
  4. 新列重命名为正确的名称

您可以在表的设计视图中的 SSMS 中执行此操作。我相信它在幕后做了类似上面的事情。

更新:
需要确认的是,在 SSMS 2K8 中,当您尝试从列中删除 IDENTITY 属性然后保存它时,它实际上会重新创建表(您可以通过在 SQL Profiler 中监视来查看它到底做了什么)。为了在 SSMS 中执行此操作,您需要确保在工具 -> 中关闭“防止保存需要重新创建表的更改”选项。选项->设计师->表和数据库设计器。我认为它默认为“开”,当您尝试以其他方式执行此操作时,这会导致错误消息。

I don't believe you can, using TSQL, remove the IDENTITY property from a column.
Instead:

  1. Add a new BIGINT column to the table
  2. Copy the data from the current IDENTITY field into the new field
  3. Drop the existing column
  4. Rename the new column to the correct name

You can do it in SSMS in the Design view for the table. I believe behind the scenes it does something like above.

Update:
To confirm, in SSMS 2K8 when you try to remove the IDENTITY property from a column and then save it, it will actually recreate the table (you can see what it does exactly by monitoring in SQL Profiler). In order to do it in SSMS, you need to ensure you have the "Prevent saving changes that require table re-creation" option turned OFF in Tools-> Options -> Designers -> Table and Database Designers. I think it defaults to ON, which would result in an error message when you try to do it otherwise.

自此以后,行同陌路 2024-09-10 00:44:47

在“真正的”SQL Server 中,您必须执行这些步骤 - 不确定 SQL Server CE 是否允许这样做,但请尝试一下!我假设您可能对该列也有 PRIMARY KEY 约束 - 对吗?如果没有,则无需执行第一步和最后一步。我假设您想再次在列上显示 IDENTITY,对吧?

-- DROP the primary key constraint (if you have that on your column)
ALTER TABLE dbo.Employees 
DROP CONSTRAINT PK__Employees__3214EC274222D4EF 

-- ALTER the datatype into BIGINT
ALTER TABLE dbo.Employees 
ALTER COLUMN Employee_ID BIGINT 

-- set PK constraint again  
ALTER TABLE dbo.Employees
ADD CONSTRAINT PK_Employees PRIMARY KEY(Employee_ID)

In "real" SQL Server, you'd have to do these steps - not sure if SQL Server CE allows the same, but give it a try! I'm assuming you probably have your PRIMARY KEY constraint on that column, too - right? If not, you don't need to do the first and last step. And I'm assuming you want to have the IDENTITY on the column again, right?

-- DROP the primary key constraint (if you have that on your column)
ALTER TABLE dbo.Employees 
DROP CONSTRAINT PK__Employees__3214EC274222D4EF 

-- ALTER the datatype into BIGINT
ALTER TABLE dbo.Employees 
ALTER COLUMN Employee_ID BIGINT 

-- set PK constraint again  
ALTER TABLE dbo.Employees
ADD CONSTRAINT PK_Employees PRIMARY KEY(Employee_ID)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文