Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL sorting while insert possible?

In SQL, while inserting into a presorted table, is it possible to insert a row while sorting the row along with the entire table? Such as

create table positional_order (
  num number,
  txt varchar2(10)
);

existing table

row 1   (3, 'three' );
row 2   (4, 'four' );

insert a new row

row 3   (1, 'one');

after the insert

table becomes

row 1   (1, 'one');
row 2   (3, 'three' );
row 3   (4, 'four' );

Is this possible?

Or I have to insert the row into the table and then do select order by ?

Btw I am using sqlite.

Thanks,

like image 684
johnsam Avatar asked Dec 12 '22 08:12

johnsam


1 Answers

As far as I know inserting a row into a particular location is not part of any standard SQL database. Part of the database's job is to determine how to logically store rows. As you said, the time to order rows is when you do the SELECT, using ORDER BY.

If you are concerned about performance, create an index on the 'number' column. More on indexes here

like image 59
Josh Diehl Avatar answered Feb 12 '23 20:02

Josh Diehl