Moving rows in sqlite database - qt

I have a table that is actually a ranking list. I want to give user a chance to rearrange that top the way he wants, ergo, allow him to move the rows in that table. Should I create a separate column that would hold the place, or can it be done using embedded order in table?

The documentation says:
If a SELECT statement that returns more than one row does not have an ORDER BY clause, the order in which the rows are returned is undefined.
(This is true for all SQL databases.)
So you cannot rely on the order that the rows happen to be stored in; you have to use some value in some table column.

Related

Preventing newly added column to become last in order

Is it possible to add a new column to an existing Kusto table somewhere in the middle , I don't want it to become last column in the table. Right now when I am issuing .create-merge table command on an existing table and no matter which order I specify for the new column, it always ends up becoming last column in the table.
You are right and it is also written in the docs:
If the motivation to re-order the column is in query scenarios, you can create a function (view) with the same name of the table that re-order the columns as you like, this will allow existing queries to work uninterrupted and the column order to be adjusted.
If the motivation is the ingestion scenario where column order is significant (such as in CSV ingestion or update policy), the new column should be specified at the end, no way around it.

SQLite query: get index of row after sort where

I need to find the index in a table of a specific row and also select it.
I need both, the index and the row.
Any ideas?
EDIT: by 'index' I refer to position in the table after being ordered.
This question could be a duplicate, but I don't know what to look for.
SQL database are fundamentally unordered. There is no index or row number in a table. Unless you explicitly order the results with ORDER BY in a SELECT clause there is no guarantee that the rows will be returned to you in the same order in two successive queries.
To uniquely identify a row, you must ensure that you know which column (or combination of column values) are uniquely identify a row and include these terms in the WHERE clause.

Insert into SQLite3 into "middle" of table

I just want to clarify: if you insert a row to a table in sqlite, it appends it to the table, but -- as I learned -- the table is unordered, so there is really not true way to insert a row into the middle of an "ordered table," right?
Is there even a way to make an ordered table without first created a table and then using '...ORDER BY name/id/etc' (i.e. when you insert something it puts itself in the right place)?
SQLite tables are actually stored in rowid order, but this is unlikely to help you because it is unlikely that there is a gap where you want it.
Furthermore, the order in which rows are stored does not matter because there is no guarantee that this is the order in which they are returned.
When you want to SELECT rows in a specific order, you must use ORDER BY.
If your query is too slow, an index on the sorting column might help.

How to use one sequence for all table in sqlite

When I'm creating tables in an SQLite database, separate sequences are automatically created for each table.
However I want to use one sequence for all tables in my SQLite database and also need to set min and max values (e.g. min=10000 and max=999999) of that sequence (min and max means start value of sequence and maximum value that sequence can increment).
I know this can be done in an Oracle database, but don't know how to do it in SQLite.
Is there any way to do this?
Unfortunately, you cannot do this: SQLite automatically creates sequences for each table in special sqlite_sequence service table.
And even if you somehow forced it to take single sequence as source for all your tables, it would not work the way you expect. For example, in PostgreSQL or Oracle, if you set sequence to value say 1 but table already has filled rows with values 1,2,..., any attempt to insert new rows using that sequence as a source would fail with unique constraint violation.
In SQLite, however, if you manually reset sequence using something like:
UPDATE sqlite_sequence SET seq = 1 WHERE name = 'mytable'
and you already have rows 1,2,3,..., new attempts to insert will NOT fail, but automatically assign this sequence maximum existing value for appropriate auto-increment column, so it can keep going up.
Note that you can use this hack to assign starting value for the sequence, however you cannot make it stop incrementing at some value (unless you watch it using other means).
First of all, this is a bad idea.
The performance of database queries depends on predictability, and by fiddling with the sequence of indexes you are introducing offsets which will confuse the database engine.
However, to achieve this you could try to determine the lowest sequence number which is higher than or equal to any existing sequence number:
SELECT MAX(seq) FROM sqlite_sequence
This needs to be done after each INSERT query, followed by an update of all sequences for all tables:
UPDATE sqlite_sequence SET seq=determined_max

How can I insert a new table row into every other row in an existing table?

Ok I have a sqlite db, that has roughly 100 rows. It is kind of a strange thing that I'm trying to do, but I need to insert a new row between each of the existing rows.
I have been trying to use the Insert statement as follows, but haven't had any luck:
insert into t1(column1) values("hello") where id%2 == 0
So I'm basically trying to use the %-operator to tell me if the id is even or odd. For every even id number, I'd like to insert a new row.
What am I missing? What can I do differently? How can I insert a new row into every other row and have the index updated as well?
Thanks
Your question assumes that the rows have some kind of built-in order to them, and that you can insert rows between other rows. That's not true.
It is true that rows have an order on disk, and that the id column is usually assigned in order, but that's an implementation detail. When you perform a query, the database is free to return the rows in any order it chooses, unless you specify what you want with an ORDER BY clause.
Now, I'm assuming what you really want is to insert rows between the existing rows in id order. One way to get what you want would look like this:
UPDATE t1 SET id = id * 2
INSERT INTO t1 (id, column) SELECT id+1, "hello" FROM t1
The UPDATE would double the ids of all the existing rows (so 1,2,3 becomes 2,4,6); then the INSERT would perform a query on t1 and use the result to insert a new set of rows with id values one more than the existing rows (so 2,4,6 becomes 3,5,7).
I haven't tested the above statements, so I don't know if they would work or if they require some extra trickery (like a temporary table) since we are querying and updating the same table in one statement. Also I may have made a syntax error.
Don't consider the rows as pre-ordered in the database. A database will store them as they come in, or according to an index. It's your task to order them on retrieval (i.e. when you query for data) according to your needs.

Resources