Retain The Order while Insert Rows - ssis

I put a sort component to sort my data.
and the data was sorted.
but my destination table is unordered!
How can retain the Order of sorted rows while Inserting them into sql Table with ssis?

There is no inherent ordering of rows in a SQL Server table. You'll need to either add a 'sort order' column or write your queries so that they produce properly sorted result sets.
You can use an IDENTITY column as your 'sort order' columns, since it will increment as things get inserted.
Understand that repeated executions of a given query against a sql database are specifically not guaranteed to return results in the same order, so your queries need to do it each and every time.

Rows in a relational database do not have any "order" - they are like water molecules in a bucket! If you need to have an order then you must include another column that you can use to order by - e.g. an autoincrement field, a timestamp, or some column from external data. You can then use that column to order your data when you query it - otherwise you won't get ordered data.

Related

MySQL database re-orders entries automatically

I have created a MySQL database using MySQL Workbench. In there, I have created a table, the first entries of which are:
id firstName lastName
1 John Smith
2 Emma Richards
Every column contains VARCHAR characters - even the id, it is NOT an integer.
For some reason, however, MySQL seems to re-order the entries:
id firstName lastName
1 John Smith
10 Karen Hill
I don't want MySQL to re-order my entries. When I retrieve the entries from the database, I want them to appear exactly in the order that I have inserted them into the database.
Previously to creating the MySQL database, I have used a SQLite database, which does not re-order the entries.
What might be the reason for this and how can I change it?
I don't want MySQL to re-order my entries. When I retrieve the entries from the database, I want them to appear exactly in the order that I have inserted them into the database.
SQL tables represent unordered (multi)sets. Period. When you query a table in any database, the ordering is not guaranteed unless you include an order by. This is even true in SQLite, as this DB fiddle demonstrates.
In most databases, if you want to capture the insertion order, then you need some sort of column that captures the ordering. There are two common methods:
An identity or auto_increment column that captures the insertion order.
A datetime/timestamp column that captures the date time. This does not always work, because there can be ties.
Then, when you query the table, you need to use order by on the column.
If your first column represents the ordering but happens to be stored as a string, then you can simply use:
order by (id + 0)

How to insert data in specific row in sql

So here is my problem, I want to add another entry to my database into a specific row.
eg. I have a table "example" then 2 column "name" "age", there are existing data to my table, I have 3 entry
jude,15,
joy,17,
john,16
respectively but I want to insert another entry
jason,18
in the row before "joy,17".
Is it possible or impossible?
Francis, the data in a SQL database has no implicit order. So, you can't count on the data coming out in the same order in which you inserted it. The order in which data comes out of a SQL database can only be guaranteed by using an 'ORDER BY' clause on the SQL statement that brings it out of the DB.
Thus, if you want a different sort order than sorting by the keys you've provided, you probably have to add a 'sort' column to your table and use that column in the ORDER BY portion of your SELECT statements.

store data ordered by a row in mysql?

Can I somehow make that mysql inserts data in a way that the database keeps an predefined order?
Let's say I make a highscore list. I could always use ORDER BY when selecting the stuff, but all the ordering in 1,000,000+ datasets takes alot of performance when a user browses the highscore list.
My idea is now that I want to insert the data in a way, that the table is always ordered by score desc, so all the ORDER BY work doesn't have to happen when users browse the list.
Tables have no inherent order that you should rely upon (they may, by coincidence, return rows in the same order that they were inserted, or by primary key sort order), so if you need a particular order, you should always use an ORDER BY clause.
However, the system may be able to perform the ordering more cheaply if it has an index available on the column on which you will have your ORDER BY clause - so add an index:
CREATE INDEX IX_Table_Scores ON `Table` (score desc);
Also there is another solution. If you store these much records in order by than there is tedious. Another way is you can create index on high score column so mysql internally create one index so when you try to fetch these record in order by than its performance is better than direct fetching query.
Please create index of high score column.

MySQL, ORDER BY insertion order, no sorting columns

How can I order values from a table, ascending from the time they were inserted. There is no special column for this matter, like a timestamp or autoincrement.
I know this is not recommended to do... Still I would like to know how to do this.
As I understand from the answers, if no sorting columns e.g: timestamp or autoincremental were added before the values were inserted, there is no way of sorting them by insertion.
There is no guarantee that rows will be returned in any particular order without an ORDER BY clause in the query.
Consider a simple query that returns all columns of all rows in a table. For example:
SELECT * FROM mytable ;
For that query, it is likely that MySQL will perform a full table scan, from the beginning of the table. So it is likely that the rows will be returned in the order they are found in physical storage.
This may roughly correspond to the order that rows were inserted, if there have been no deletes, no updates and no reorganization, where space for an inserted row was later reclaimed, and reused to store a newly inserted row.
But this behavior is NOT guaranteed.
To return the rows in the order that they were inserted, the query must specify the sequence that rows are to be returned, by including an ORDER BY clause.
For the rows to be returned in "insertion order", that means the query needs to be able to have that information available, or be able to derive that. For a simple query against a single table, that means the information needs to be stored in the row.
You can ORDER BY something you can get out of your table. If you do not have anything in there that can be used to find out the order you need, you cannot order by it.
Depending on the data in the table, you may be able to order by the id of the data - if the data has a single incremental integer to assure PK uniqueness. There is no other way to sort on insertion order unless the data is captured and recorded in the table.
I don't know of anything in MySQL that retains extra (meta) information on records that you have not specified at the table level.
There needs to be a column to order your query by. Usually this would be an insertion timestamp, or incrementing id/incrementing key. There is no way to guarantee the order otherwise, because there is no record of it.
relevant thread from MySQL forum

how to sort mysql

I want to sort mysql by latest logged in but I have to resort it everytime i log in, how do I sort and save the latest login?
You can't reliably keep the values stored in MySQL in alphabetical order. The best you can do is use the ORDER BY clause in your SELECT query.
Usually you sort things when you select by adding an "ORDER BY" clause. Why do you care what order they are actually stored in the table?
If you really care, I guess you could do an INSERT...SELECT to copy it to a new table while sorting the column, but whenever you add a new record, it will end up at the end of the list.
SQL doesn't work that way. It's not like a spreadsheet where the rows are meant to be read "as-is" and you can put them in a certain meaningful order. Nothing you do ever changes the order that SQL stores the rows in. SQL gives you data through queries, and the query tells it which rows you want to see, in what order.
So, you have to reuse the "order by" clause every time you want to see your data in a certain order. However, you shouldn't need to manually query SQL a lot. If you're writing any kind of script or application to work with it, you'll just write the query once and re-run it each time you want the data.
To get the last login from your user table, you could use a query like
SELECT user_name, login_datetime
FROM user_table
ORDER BY login_datetime DESC
LIMIT 1
Assuming that user_name is the field you are looking for and login_datetime is a timestamp you can order on. LIMIT 1 will show only the top most result and can be changed depending on your needs.
With MySQL's InnoDB engine, your rows are physically ordered according to the primary key. So, if the primary key is on the login column, then the rows will stay in physical order by the values in login.
The requirement of a primary key is that each value must be unique and NULL is not allowed. So, you may consider making the login column your primary key. If login were a datetime column, then the latest login datetime value would always be at the end of the table.
Many people make an auto-increment integer column their primary key. In this case, the last record to be inserted would always be at the end of the table.
You must know the value of having your rows in physical order (by a particular column) for certain circumstances, otherwise you wouldn't be asking.
For example, doing the following SELECT returns the rows in physical order (same as primary key) without having to specify a column to order the results by, and without forcing MySQL to later order the result set:
SELECT *
FROM test
However, if you need to order the results other than by the primary key, then you should utilize the ORDER BY clause. For example, if the rows were physically ordered by the column name, but you want the results returned in order by login, you would issue the following query:
SELECT *
FROM test
ORDER BY login ASC
The above query asks MySQL to order the rows according to the specified column. You specify ASC in the ORDER BY clause to order the values in ascending order, and DESC to order them in descending order.