SQL Server中如何维护插入顺序 [英] How to maintain the order of insertion in SQL Server

查看:28
本文介绍了SQL Server中如何维护插入顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否可以在 SQL Server 中保持数据插入的顺序?

I am just wondering if I can maintain the order of insertion of data in SQL Server?

我正在做我自己的项目,它是一种博客网站,有很多帖子.我会将我的帖子保存到我的 SQL Server,但我希望它们按插入顺序排列.

I am working on my own project and it is kind of blog site having a number of posts. I will save my posts to my SQL Server but I want them in the order of insertion.

问题:我知道如果我在 SQL Server 中使用自增整数作为主键,我可以保持插入的顺序.但我想使用Guid"作为主键,而不是身份.那么Guid"似乎并没有保持插入的顺序.

Question: I understand that If I use auto-incrementing integer in SQL Server as the primary key, I can maintain the order of insertion. But I want to use "Guid" for primary key, instead of identity. Then "Guid" does not seem to maintain the order of insertion.

是否应该同时使用自动递增整数作为插入顺序和 Guid 标识?

Should I use both auto-incrementing integer for order of insertion and Guid for identity?

或者有没有其他方法可以在 Guid 设置为主键的情况下保持插入顺序?

Or Is there any other way to maintain the order of insertion with Guid set to primary key?

相关问题:我想保持插入顺序的原因是这样,我不必使用导致额外排序过程的order by子句.

Related question: the reason why I want to maintain the order of insertion is that in that way, I don't have to use order by clause which causes extra sorting process.

但是我不应该相信从数据库返回的数据的顺序,而没有像 order by 这样的任何排序子句吗?

But should not I trust the order of data returned from database without any ordering clauses like order by?

我是否应该总是使用一些排序子句来对我的数据进行排序?那么如果我有大量的数据需要订购呢?那我该如何处理呢?

Should I always use some ordering clauses for ordering my data? Then what if I have a massive amount of data to order? Then how can I handle the situation?

抱歉,帖子中有太多问题,但我相信它们都是相关的.

Sorry for too many questions in a post but I believe they are all related.

推荐答案

当您在 INSERT...SELECT 语句中指定 ORDER BY 时,SQL Server 将分配 IDENTITY 值按指定的顺序排列.但是,这并不意味着行必须按该顺序插入.

When you specify ORDER BY in an INSERT...SELECT statement, SQL Server will assign IDENTITY values in the order specified. However, that does not mean rows are necessarily inserted in that order.

如果您需要以特定顺序返回的行,则必须使用 ORDER BY 来保证排序.索引可以根据查询细节有效地提供订单数据.

If you need rows returned in a specific order, you must use ORDER BY to guarantee ordering. Indexes can be leverage provide order data efficiently depending on the query particulars.

这篇关于SQL Server中如何维护插入顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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