In an SQL Server database, I created a Unique Constraint to ensure that one of it’s tables contains only unique pairs of values.
The problem now is that the order of records I get is different. The records are sorted, but I want them to come in original order, just as they exist in the table, without any sorting.
I’ve checked everywhere, but couldn’t find a way to create a unique constraint without sort order. Is this supported at all?
Ah, the old sort issue – SQL for beginners.
TABLES have a sort order that is the order of the clustered index. Missing that the odder is undefined.
RESULTS have NO ORDER UNLESS DEFINED. SQL can change the order if it thinks it can process a query better. This is FUNDAMENTAL – you deal with data sets, and data sets per se are not ordered.
So, if you want an order, ASK FOR IT.
Why would you need an order for a unique constraint? A unique index should suffice, or? I would NOT make uniqueness a constraint but put – standard – a unique index on the fields. Especially as the index is good for – validating that they are unique and thus needed anyway.