Author |
Topic |
WhiteDiablo
Starting Member
4 Posts |
Posted - 2011-03-31 : 07:26:14
|
Hello everyone, I am new in SQL...So I have a kind of problem about sorted primary keyAs we know, a table must have at least one primary key.Every primary will be sorted automaticallyI want to know, Is there any way to make it origin / unsorted ??If yes, how ??thanks for your effort... WDPlease Close the thread |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2011-03-31 : 11:36:47
|
quote: Every primary will be sorted automatically
That is not true. A clustered index will mean that the data is physically sorted on disk, however that doesn't mean your query results will be sorted. You must add an ORDER BY if you want a result set to be ordered.Clustered indexes do not need to be the primary key, although commonly they are. A non-clustered index is also sorted but logically not physically.If you don't want something sorted, then don't provide an ORDER BY.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
|
|
WhiteDiablo
Starting Member
4 Posts |
Posted - 2011-03-31 : 12:38:59
|
Thanks for the info about clustered index, but there is a problem... I am even don'/t use the ORDER BY but it still sorted automatically...When I remove the primary key, The order become to what I want...But I really need the primary key without ordered alphabetically ( usually ordered ascending ) and I really not using ORDER BYthanks for your effor... WD |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-03-31 : 12:41:26
|
quote: As we know, a table must have at least one primary key
A table can only have 1 primary key. It can have multiple unique keys, also known as candidate keys. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2011-03-31 : 12:50:59
|
quote: Originally posted by WhiteDiablo Thanks for the info about clustered index, but there is a problem... I am even don'/t use the ORDER BY but it still sorted automatically...When I remove the primary key, The order become to what I want...But I really need the primary key without ordered alphabetically ( usually ordered ascending ) and I really not using ORDER BYthanks for your effor... WD
It may be sorted according to the clustered index right now, but there are no guarantees that it will remain in that order if you don't specify an ORDER BY.If you want to get a random sort, then you can use ORDER BY NEWID().Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
|
|
WhiteDiablo
Starting Member
4 Posts |
Posted - 2011-03-31 : 14:29:00
|
It looks like I still don't get itHow if I show the problems ( I should do it from the first time, sorry )create database Propertycreate table ChainSupply( ChainId char(4)primary key , street varchar (20) not null, city varchar (10) not null,)insert into Branch ( branchNo, street, city)values ( '31B','321 Freedom', 'Monte')insert into Branch ( branchNo, street, city)values ( '42A','412 Local', 'Okhisa')insert into Branch ( branchNo, street, city)values ( '12D','476 Fury', 'Hongkong')SELECT * FROM ChainSupplyThe result should be:branchNo street city12 D 476 Fury Hongkong31B 321 Freedom Monte42A 412 Local OkhisaBut, the result I want is default ( this is important ) like this :branchNo street city31B 321 Freedom Monte42A 412 Local Okhisa12 D 476 Fury HongkongLike the order when I input the VALUES... As addition, I still use SQL Managment StudioAnyway thanks for your helpThanks for your effor... WD |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2011-04-01 : 05:03:01
|
quote: Originally posted by WhiteDiabloLike the order when I input the VALUES...
If you want the output to be in the same order as it's entered then you need another field such as a timestamp to indicate when you entered the data, you can then use that in your ORDER BYfor examplecreate table ChainSupply(ChainId char(4)primary key ,street varchar (20) not null,city varchar (10) not null,TimeEntered DateTime default getdate())Then your select would be something like SELECT ... from ChainSupply ORDER BY TimeEntered-----------If debugging is the process of removing software bugs, then programming must be the process of putting them in. |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-04-01 : 05:33:16
|
quote: Originally posted by tkizer A clustered index will mean that the data is physically sorted on disk,
It doesn't even mean that. The clustered index defines the logical order of the table, not the physical.If the cluster enforced the physical sort order, then the cluster would never have fragmentation (fragmentation measures the difference between logical and physical order)--Gail ShawSQL Server MVP |
|
|
WhiteDiablo
Starting Member
4 Posts |
Posted - 2011-04-01 : 07:55:51
|
quote: Originally posted by elwoos
quote: Originally posted by WhiteDiabloLike the order when I input the VALUES...
If you want the output to be in the same order as it's entered then you need another field such as a timestamp to indicate when you entered the data, you can then use that in your ORDER BYfor examplecreate table ChainSupply(ChainId char(4)primary key ,street varchar (20) not null,city varchar (10) not null,TimeEntered DateTime default getdate())Then your select would be something like SELECT ... from ChainSupply ORDER BY TimeEntered
Yeah, It can be done by that way... But Is there any way without adding the column into table ?? Like using array in another programing code ( java, C, etc ) ?Anyway, If I am using the addition column ( timestamp ), I would not using SELECT * FROM ChainSupply because addition column will be gather out too... that's exactly the problem Anyway thank for your effor... WD |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-04-02 : 16:10:38
|
I put the original text back, using a white color. You can highlight if you want to read it.I'm also locking the thread, as requested. |
|
|
|