I'm trying to delete the last added entry of a table:
DELETE FROM notes ORDER BY created_at DESC LIMIT 1
This just causes the following error:
near "ORDER": syntax error
Why might I be getting this error? (notes exists and has records in it!)
Try this
DELETE FROM notes WHERE id = (SELECT MAX(id) FROM notes);
delete from notes where created_at = ( select max(created_at) from notes );
Watch out, this will not limit the number of rows deleted. If there are more than one row at max(created_at), this will delete all of them because the subject you specified does not exist (last added entry of a table).
Related
On the database open, I inserted 5 records into the Expense table. Then I removed all 5 records one by one.
And before I insert new entry I check max inserted id. For some reason it's null. What's wrong?
var result = await db.rawQuery("SELECT MAX(id) as last_inserted_id FROM Expense");
final maxID = result.first["last_inserted_id"];
print('maxID:$maxID'); //maxID:null
If there are no rows in the table, there is no maximum defined. What you rather should do is look for the ID sequence number of the table to find out what the next ID will be. That information is usually stored in some meta table of the DB.
SELECT seq FROM SQLITE_SEQUENCE WHERE name='Expense';
Hello i have an sqlite db with many records like 10540 record they are ordered by creation time , i want to shift like a record in the middle and like to do it automatically
for example :
select * from table1 where id >= 8521;
UPDATE Table1 SET id = id +1 ;
does not work i get Error: Result: UNIQUE constraint failed:
so i want to shift up all records from 8521 to the last record and get place in the 8520 place for example so i can insert my record in that place of table .
even the
id = select max(id)+1
does not work how can i increment the id from last record to the needed record so i can put a place in the records db
A simple update statement would fail, as it would try to create duplicate values in the primary key.
What you can do is this:
First update the column to the negatives of the values they should have:
update table1
set id = -(id + 1)
where id > 8520;
Now there are no duplicates and you just need to update again to the positive values:
update table1
set id = -id
where id < 0;
This will do the trick, but any kind of updating the primary key is not a recommended practice
I am running into a logical problem.My Trigger is:
create trigger Points1
on Posts
after insert, update
As
declare #value int
declare #postedby int
select #value= Count(Message) from Posts
select #postedby = PostedBy from Posts
update AspNetUsers set User_points = #value * 3
where ( AspNetUsers.Id = #postedby)
I dont know whether i am doing it right or not.
Two tables: AspNetUsers table with User_points column and Id Column as primary key
Posts table with PostId as primary key and PostedBy as foreign key referencing the AspNetUsers table.
Now, i want to compare PostedBy with Id column and if they both are same then update the User_Points column with +3 on every single message he posted.
Now, problem is:
1> It is inserting same number of points in every Row.It should check only currently inserted row and the PostedBy column of that row and then compare with Id column of other table and should Update user's Point of only that Id.
But same result nothing happens
Please tell me how to do it.
thanks in advance
change
select #postedby = PostedBy from Posts
to
select #postedby = PostedBy from INSERTED
'INSERTED' is a magic table that keep insert/updated data in this scope.
Same as this 'DELETED' table keep previous data in update a row
I want to have riggers that set the last_modified column automatically each time a row in updated or inserted.
Lets say I have an ID that is unique to each row.
This is my query:
CREATE TRIGGER insert_trigger
AFTER INSERT ON TABLE_NAME
BEGIN
update TABLE_NAME set last_modified =strftime('%Y-%m-%d %H:%M:%S:%s','now', 'localtime') where id = old.id;
END;
After creating this trigger, when I try to insert I get the error:
no such column: old.id
I can understand why I get this error, but how can I create a proper trigger?
When inserting, there is no old row.
To get the ID of the new row, use NEW.id.
I am executing the following query
DELETE FROM List,Tree WHERE List.CatID = Tree.CatID AND List.ID = '1' AND Tree.Cat = '332'
but I run into following error
near ",": syntax error
Correct syntax for delete statement is this
DELETE FROM table_name WHERE somecolumn=somevalue
So you cannot use 2 tables in a single delete query by separating them with a comma..
You need to do something like
DELETE something FROM table_name INNER JOIN...
You can delete only from one table at a time, and you have to rewrite the join as a subquery:
DELETE FROM List
WHERE ID = '1'
AND CatID IN (SELECT CatID
FROM Tree
WHERE Cat = '332')