简体   繁体   English

在Sybase的现有表中添加标识列的同时对列进行排序

[英]Ordering a column while adding an identity column in an existing table in Sybase

Thanks to this good forum, got a number of solutions to my past queries, thanks to Google too. 多亏了这个良好的论坛,我也对过去的查询提出了许多解决方案。 This is my first post in the forum although. 这是我在论坛上的第一篇文章。

I have a table order with columns pin, orderPath, quantity . 我有一个表订单 ,其列为pin,orderPath,quantity There are 5000 data in the table already. 该表中已经有5000个数据。 Now, I want to add an identy column, say OrderID , but with order by column pin . 现在,我想添加一个identity列,例如OrderID ,但要按列pin排序。

Using the following query helps but not in the order as required. 使用以下查询有帮助,但没有按要求的顺序排列。

ALTER TABLE order
ADD OrderID numeric(6,0) identity

Here's what I would like to see: 这是我想看到的:

Pin--OrderPath--Quantity--OrderID 

11   xyz/pop    200       1 
22   kl/pod     100       2 
33   djh/dd     200       3  
44   dj/po      300       4 
ALTER TABLE orderADD 
ADD Order_ID datatype

If any of your logic requires the data to be returned or displayed in a certain order, don't rely on the database. 如果您的任何逻辑要求按一定顺序返回或显示数据,请不要依赖数据库。 Explicitly put an order by clause in your queries to ensure consistency. 明确在查询中放置一个order by子句以确保一致性。

SELECT pin, OrderPath, Quantity, OrderID
ORDER BY pin

If you need data to be stored in a particular order, it's recommended to us a clustered index on the column you want orderd. 如果您需要按特定顺序存储数据,建议向我们推荐要排序的列上的聚集索引。 Clustered indexes keep data in order, making for faster retrieval, but keep in mind it does add cost to inserts. 聚簇索引可以使数据井井有条,从而可以更快地进行检索,但是请记住,确实会增加插入的成本。

create unique clustered index pin_idx on ORDER (pin)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM