I was wondering if anyone knows if there is a way to sort the data that already resides in a database. That is, I want to sort what is there but NOT retrieve it in in query.
I am asking because I have a list of things in this database, that I would like to add to in future and would like to order it once I've added them.
So what I mean is, I would like to not have to download all the data; sort it; then put it back onto the database.
Thanks in advance.
If there is only one column in your table then this is fine and you can simply sort the data in that table.
But if there are more than one columns in your table then it would be dependent on the other columns as well(ie, you need to specify which column you are looking to sort.) Also if there is a primary key attached to the table then its not possible as primary key would be in ascending order by default. In that case you can only have it while selecting the data from the table.
(My suggestion is to sort the data while selecting your records from the table as that would be easy and will have less risk)
EDIT:
To make my point straight and clear, the best way to achieve what you are trying to simply use ORDER BY ASC or ORDER BY DESC while you are selecting the data from your table.
If you are creating a new table then you can create index organized table to ensure that the data is stored ordered by index.
I was wondering if anyone knows if there is a way to sort the data that already resides in a database.
Normal relational tables, called as heap-organized tables, store rows in any order i.e. unsorted. You sort the rows if required only when you fetch them, not when you store them. And the only way to guarantee the sorting while retrieving the rows is to use an ORDER BY clause.
Related
So I have a table in SQL that's in a database. I've performed sort operations as well as catching a certain amount of records, and now my objective is to input these values into a new database. How can I do this, considering the sort operations? Would it be potentially easier to export the whole table then perform the sort in the new database? I am using SQL 2005.
It doesn't matter what order you insert the data into the new table. If you put a clustered index on the table, the data will be stored in the order of the index, regardless of the order it was inserted. (And note that storing it in this order is no guarantee that your query results will come out in this order)
The only instance where the order of inserts might matter is if you have an IDENTITY column on the table, and are allowing it to auto-populate. Then the IDENTITY will increment in the order that the rows are inserted.
That said, treating your question as academic, if you did want to export data in a certain order from one database to another, you could use an SSIS dataflow and specify an ORDER BY in the query in the Source component.
How can I reorder rows in oracle sql database? I am using a 3rd party database-driven software and unfortunately I cannot change the call (or I would just add an order by), but can I change the order of the rows in the database?
Disclaimer: I know I should never depend on raw database order, and if it changes I understand, but can this be done?
Thank you!
You can't. If a query doesn't specify an ORDER BY, the order in which rows are returned is undefined.
If you are willing to accept a less-than-100% solution, you could try moving the data to a temporary table, truncate the table, and then insert the data back in the order you want it to appear. If the query is doing a table scan or some type of index scans and not doing anything complicated (like a join), it's likely that the rows would be returned in the order they are physically stored in the table. No guarantees, of course, but it might work most of the time.
I have a table with lots of columns, and I'd like to add two more (date and time) to the front of the existing table.
There is no data in the table right now, but I'm wondering what the best way is get the table in the format I need it.
I could just drop the table and create a new one with the correct configuration, but I'm wondering if there is a better way?
This is currently not possible. You have to drop and recreate the table.
Theoretically you could add the column, drop and re-add all other columns, but that's hardly practical.
It's an ongoing discussion and an open TODO-item of the Postgres project to allow reordering of columns. But a lot of dependencies and related considerations make that hard.
Quoting the Postgres project's ToDo List:
Allow column display reordering by recording a display, storage, and
permanent id for every column?
Contrary to what some believe, the order of columns in a table is not irrelevant, for multiple reasons.
The default order is used for statements like INSERT without column definition lists.
Or SELECT *, which returns columns in the predefined order.
The composite type of the table uses the same order of columns.
The order of columns is relevant for storage optimization (padding and alignment matter). More:
Calculating and saving space in PostgreSQL
People may be confusing this with the order of rows, which in undefined in a table.
In relational databases the order of columns in a table is irrelevant
Create a view that shows you the columns in the order you want
If you still want to, drop the table and recreate it
i would need to implement a LIMIT equivalent (from mysql) to MSSQL. I have found many topics here that answer this issue, but all require you to order by a specific column. I need to do this without having to order the table because my code is going through many tables automatically so the table structure/column names change from table to table and i dont know what table can be used to order properly.
the reason for this is that i am processing a HUGE table in vb.net and adapter.Fill(ds_values) causes to go out of memory so i would need to process the table in chunks of 1000 rows at the time..
Any help appreciated.
THanks
There is no way to know in which order the records where inserted if you don't have a column indicating that. That is not how DB engines work.
The DB engine's algorithm tries to fetch the data as fast as possible. And it does not care about an order if you don't specify one.
I have simple SSIS package where I import data from flat file into SQL Server table (SQL Server 005). File contains 70k rows and table has no primary key. Importing is sucessful but when I open SQL Server table the order of rows is different from the that of file. After observing closely I see that data in table is sorted by default by first column. Why this is happening? and how I can avoid default sort?
Thanks.
You cannot rely on ordering unless you specify order by in your SQL query. SQL is a relational algebra that works with sets. Those sets are unordered. Database tables do not have an intrinsic ordering.
It may well be that the sets are ordered due to the way the data is retrieved from the tables. This may be based on primary key, order of insertion, clustered key, seemingly random order based on the execution plan of the query or the actual data in the table or even the phase of the moon.
Bottom line, if you want a specific order, use order by. If you don't want a specific order, the DBMS is free to deliver your rows in any order, including one based on the first column.
If you really want them sorted depending on the position in the import file, you should add another column to the table to store an increasing number based on its position in that file. Then use order by using that column. But that's a pretty arbitrary sort order, you're generally better off choosing one that makes more sense to the data (transaction ID, date/time, customer number or whatever else you have).
If you want to avoid the default sort (however variable that may be), use a specific sort.
In general no order is applied if there is no ordering in the select query.
What I have noticed is that the table results might return in the order of the primary key, but this is not gaurenteed either.
So all in all, if you do not specify a ordering, no ordering can be assumed.