Insert random row to table and then delete - sqlite

I've got two tables: tableA and tableB. I would like to insert random row from tableA to tableB and then delete this row from tableA. How can i do it? Is it possible at all? Below is my insert code but i dont know how to delete this row.
INSERT INTO tableB
SELECT * FROM tableA ORDER BY RANDOM() LIMIT 1;

You can use rowid:
DELETE from tableA
WHERE elementA = (
SELECT elementA from tableB
WHERE rowid = (SELECT MAX(rowid) FROM tableB)
)
Provided that elementA is unique in both tables, if this is executed after your INSERT statement it will find elementA from the inserted row and delete the row from TableA with that elementA.

Related

In teradata, can you insert a cte into a table?

I've looked at the other postings but I haven't seen a resolution for this.
CREATE TABLE newtable AS (SELECT * FROM yourtable) with no data ;
Then
with cte as (
select top 10* from yourtable
)
insert into newtable
select * from cte
select * from newtable
I get an error saying it expects a select and not an insert.
How would I accomplish inserting the cte into a table?

sqlite: insert a row using sqlite_sequence record

I have a table in my database... I want to insert a row to table1 by reading from a record on sqlite_sequence... I know the sqlite_sequence table is for AUTOINCREMENT... and also my table1 has AUTOINCREMENT for _id as well...
I tried this:
INSERT INTO table1 (column_1, column_2) VALUES ((SELECT seq FROM sqlite_sequence WHERE name=other_table),`other_column`);
but after inserting, inserted row will be deleted in table1...
I want to know the last row id in other_table... and i tried with last_insert_rowid() instead of (SELECT seq FROM sqlite_sequence WHERE name=other_table)... but the result was same...
Where is my mistake?? thanks!

Sequence in sql operation?

I am passing datatable as input parameter to stored procedure. Datatable contains id, Name,Lname,Mobileno,EmpId.
Employee table contains [Name],[Lname],[mobno],[Did] as columns.
When user is logged in, his Id come as DId. There are more than 1000 records. Instead of passing that id to datatable, I have created
separete parameter to sp. I want to add records to Employee table, which are not already exist. If combination of mobileno and Did already exists, then
don't insert into Employee table, else insert. Datatable may contain records, which can be duplicate. So I don't want to include that record. I want select only
distinct records and add them to table. I am intrested in mobile no. If there are 10 record having same moble no, I am fetching record, which comes first.
Following code is right or wrong. According to my knowledge, first from clause, then inner join, then where, then select execute. Record get fetched from datatable,
then inner join happens generate result, from that result not from datatable it will check record. So it will give me proper output.
Create Procedure Proc_InsertEmpDetails
#tblEmp EmpType READONLY,
#DId int
as
begin
INSERT INTO Employee
([Name],[Lname],[mobno],[Did])
SELECT [Name],[Lname],[mobno] #DId
FROM #tblEmp A
Inner join (
select min(Id) as minID, mobno from #tblEmp group by mobno
) MinIDTbl
on MinIDTbl.minID = A.ExcelId
WHERE NOT EXISTS (SELECT 1
FROM Employee B
WHERE B.[mobno] = A.[mobno]
AND B.[Did] = #DId )
end
or does I need to change like this
INSERT INTO Employee
([Name],[Lname],[mobno],[Did])
SELECT C.[Name],C.[Lname],C.[mobno], C.D_Id
from
(SELECT [Name],[Lname],[mobno] #DId as D_Id
FROM #tblEmp A
Inner join (
select min(Id) as minID, mobno from #tblEmp group by mobno
) MinIDTbl
on MinIDTbl.minID = A.ExcelId
)C
WHERE NOT EXISTS (SELECT 1
FROM Employee B
WHERE B.[mobno] = C.[mobno]
AND B.[Did] = #DId )

how to write before update trigger?

I have two tables namely table1 and table2.
When table1 is updated, I want to insert the row being updated to table2 so that table2 serves as a log.
table1 has column1,column2,column3,column4....column10
table2 has column1,column2,column3,column4
So while inserting into table2, I just want column1,column2,column3,column4 from table1.
This will be simple if you just want to add the record updated from table1 to table2 and you can use after update
delimiter //
create trigger update_table1 after update on table1
for each row
begin
insert into table2
(column1,column2,column3,column4)
values
(old.column1,old.column2,old.column3,old.column4);
end; //
delimiter;

SQLite - remove row with a lowest id

How can I remove a row from a table that has lowest rowid?
Thank you.
DELETE FROM
MyTable
WHERE
Id = (SELECT MIN(Id) FROM MyTable);
In most cases like this, rowid is an indexed column. If that is the case the much faster solution is:
DELETE FROM
tablename
WHERE
rowid= (SELECT rowid FROM tablename order by rowid limit 1)
If rowid is NOT indexed then:
DELETE FROM
tablename
WHERE
rowid= (SELECT MIN(rowid) FROM tablename)
I'd still test the first one as it will often be faster, even if rowid is not indexed.
DELETE FROM
tablename
WHERE
rowid= (SELECT MIN(rowid) FROM tablename)

Resources