SQL 标识列

发布于 2024-08-30 18:27:47 字数 217 浏览 2 评论 0原文

我有一个 sql 表,它基本上是一条语句。

现在假设我的表中的记录有一个日期和一个标识列,该列是自动编号的,并定义了交易在前端向客户端显示的顺序。

问题是在插入期间,一些数据丢失了,并且两个日期之间的一些事务丢失了。

我需要将数据插入表中,但需要将它们插入日期之间而不是表末尾。如果我执行正常插入,数据将出现在表的末尾,而不是我指定的日期,因为标识列是自动编号的,并且无法更新。

I have an sql table which is basically a statement.

Now lets say the records I have in my table have a date and an identity column which is autonumbered and defines the order which the transactions are displayed in the front end to the client.

The issue is during an insert some of the data have gone missing and some transactions between two dates are missing.

I need to insert the data into the table, but I need to insert them between the dates and not at the end of the table. If I do a a normal insert, the data will appear at the end of the table and not at the date I specify, because the identity column is autonumbered, and cannot be updated.

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

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

发布评论

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

评论(3

江南月 2024-09-06 18:27:47

使用SET IDENTITY_INSERT (table) ON,您可以强制 SQL Server 允许您将任意值插入到 IDENTITY 列中,但无法更新 IDENTITY 列。

无论如何,有一些差距有什么大问题?是的,这可能有点“美容”问题 - 但是您真正愿意在美容问题上花费多少麻烦和精力?条目的顺序仍然是给定的 - 即使有间隙。

再说一遍:有什么大不了的? IDENTITY 列保证不断增加——这就是它们所保证的。对于 99% 的情况,这已经足够好了......

Using SET IDENTITY_INSERT (table) ON, you force SQL Server to let you insert any arbitrary value into an IDENTITY column - but there's no way to update an IDENTITY column.

What's the big problem with a few gaps anyway?? Yes, it might be a bit of a "cosmetic" problem - but how much hassle and effort do you really want to spend on cosmetic problems?? The order of the entries is still a given - even with gaps.

So again: what's the big deal?? IDENTITY columns are guaranteed to be ever increasing - that's all they guarantee. And for 99% of the cases, that's more than good enough....

眼藏柔 2024-09-06 18:27:47

为什么不在用户界面中只显示按日期排序的记录,而不是按主键排序?

好吧,如果你真的想这样做(就我个人而言,我认为更改 UI 中的排序日期比更新数据库中的主键值更容易,但无论如何......)。假设您没有在任何外键约束中使用主键值,这应该可行(如果是,那么您需要确保这些约束设置了 ON UPDATE CASCADE )

SET IDENTITY_INSERT tablename ON

UPDATE tablename SET
    primary_key = primay_key + 1
WHERE
    primary_key >= <the primary key where you want to insert the new date>

INSERT INTO tablename
    (primary_key, date, ...)
VALUES
    (<the primary key to insert>, <the date to insert>, ...)

SET IDENTITY_INSERT tablename OFF

但是,我强烈强烈建议您在尝试此操作之前备份数据库。

Why not just display the records in the user interface sorted by the date, rather than by the primary key?

OK, if you really want to do this (personally, I think changing the sort date in the UI is going to be easier than updating the primary key values in the database, but anyway...). This should work, assuming you're not using the primary key values in any foreign key constraints (if you are, then you'll need to make sure those constraints have ON UPDATE CASCADE set)

SET IDENTITY_INSERT tablename ON

UPDATE tablename SET
    primary_key = primay_key + 1
WHERE
    primary_key >= <the primary key where you want to insert the new date>

INSERT INTO tablename
    (primary_key, date, ...)
VALUES
    (<the primary key to insert>, <the date to insert>, ...)

SET IDENTITY_INSERT tablename OFF

However, I strongly, strongly suggest you backup your database before attempting this.

貪欢 2024-09-06 18:27:47

只是出于好奇,每个约会只有一个 ID 吗?您的答案暗示了这一点,所以如果是这样,请将 Identity 列替换为计算列,该计算列定义为距任意起点的日期差(以天为单位)?

DECLARE @EXAMPLE TABLE
(
    [Date] DATE,
    ID AS DATEDIFF(Day, '1 Jan 2010', [Date])
)

INSERT INTO @EXAMPLE([Date])
VALUES (GETDATE()), (GETDATE()+1), (GETDATE()+2)

SELECT * FROM @EXAMPLE

Just out of curiosity, is it one ID per date? Your answers imply this a little, so if so, replace the Identity column with a computed column that is defined as the date difference in days from an arbitrary starting point?

DECLARE @EXAMPLE TABLE
(
    [Date] DATE,
    ID AS DATEDIFF(Day, '1 Jan 2010', [Date])
)

INSERT INTO @EXAMPLE([Date])
VALUES (GETDATE()), (GETDATE()+1), (GETDATE()+2)

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