pete
pete

Reputation: 135

how do I organize a table in postgres sql in ascending order?

I would like to organize my postgresql table in ascending order from the date it was created on. So I tried:

SELECT * 
FROM price
ORDER BY created_on; 

And it did show me the database in that order, however it did not save it.

Is there a way I can make it so it gets saved?

Upvotes: 1

Views: 1006

Answers (1)

user330315
user330315

Reputation:

Tables in a relational database represent unordered sets. There is no such thing as the "order of rows" in a table.

If you need a specific sort order, the only way is to use an order by in a select statement as you did.

If you don't want to type the order by each time, you can create a view that does that:

create view sorted_price
as
select *
from price
order by created_on;

But be warned: if you sort the rows from the view in a different way, e.g. select * from sorted_price order by created_on desc Postgres will actually apply two sorts. The query optimizer is unfortunately not smart enough to remove the one store in the view's definition.

Upvotes: 3

Related Questions