The last inserted rowid of a specific table in SQLite3 - sql

Is there a way to get the last inserted rowid of a specific table in SQLite3? There is a function last_insert_rowid which returns the rowid of the most recent successful INSERT into a rowid table or virtual table. However, I want to know whether there's such an method to get the last inserted rowid of a specific table.

The last inserted rowids of all tables are stored in the internal table sqlite_sequence, which you can query like any other table. Make sure your primary key ID has the attribute autoincrement, else it isn't listed there.
sqlite> create table Test1 (id integer primary key autoincrement, text string);
sqlite> create table Test2 (id integer primary key autoincrement, text string);
[...] -- Insert rows here
sqlite> select rowid from Test1;
id
----------
1
2
sqlite> select rowid from Test2;
id
----------
1
2
3
sqlite> select * from sqlite_sequence;
name seq
---------- ----------
Test1 2
Test2 3

Related

How to update table with sequentional on table without primary key?

In DB2 on Linux v11.1 I have a table:
COL1 COL2 "COLn 50 more columns"
A A
A A
B A
B B
etc 3 million rows
There can be multiple rows with the same rows, like first two rows in my sample (so obvious there is no primary key on table).
Now I have to add new column ID and set for every row unique sequential number.
The result should be:
COL1 COL2 "COLn 50 more columns" ID
A A 1
A A 2
B A 3
B B 4
etc 3 million rows
How to write such an update statement to update ID column?
Regards
Here is one way to do it, using an identity column , and it assumes that there is not an existing Primary Key or identity column.
alter table myschema.mytab add column id integer not null default 0 ;
alter table myschema.mytab alter column id drop default ;
alter table myschema.mytab alter column id set generated always as identity ;
update myschema.mytab set id = default ;
-- optional, if you want the new ID column to be a surrogate primary key
alter table myschema.mytab add constraint pkey primary key(id) ;
reorg table myschema.mytab ;
runstats on table myschema.mytab with distribution and detailed indexes all;
Try this:
alter table myschema.mytab add column id integer not null default 0 ;
UPDATE (SELECT ID, ROWNUMBER() OVER() RN FROM myschema.mytab) SET ID = RN;
-- Or even simplier:
-- UPDATE myschema.mytab SET ID = ROWNUMBER() OVER();

How to UPDATE or INSERT in PostgreSQL

I want to UPDATE or INSERT a column in PostgreSQL instead of doing INSERT or UPDATE using INSERT ... ON CONFLICT ... because there will be more updates than more inserts and also I have an auto incrementing id column that's defined using SERIAL so it increments the id column everytime it tries to INSERT or UPDATE and that's not what I want, I want the id column to increase only if it's an INSERT so that all ids would be in an order instead
The table is created like this
CREATE TABLE IF NOT EXISTS table_name (
id SERIAL PRIMARY KEY,
user_id varchar(30) NOT NULL,
item_name varchar(50) NOT NULL,
code_uses bigint NOT NULL,
UNIQUE(user_id, item_name)
)
And the query I used was
INSERT INTO table_name
VALUES (DEFAULT, 'some_random_id', 'some_random_name', 1)
ON CONFLICT (user_id, item_name)
DO UPDATE SET code_uses = table_name.code_uses + 1;
Thanks :)
Upserts in PostgreSQL do exactly what you described.
Consider this table and records
CREATE TABLE t (id SERIAL PRIMARY KEY, txt TEXT);
INSERT INTO t (txt) VALUES ('foo'),('bar');
SELECT * FROM t ORDER BY id;
id | txt
----+-----
1 | foo
2 | bar
(2 Zeilen)
Using upserts the id will only increment if a new record is inserted
INSERT INTO t VALUES (1,'foo updated'),(3,'new record')
ON CONFLICT (id) DO UPDATE SET txt = EXCLUDED.txt;
SELECT * FROM t ORDER BY id;
id | txt
----+-------------
1 | foo updated
2 | bar
3 | new record
(3 Zeilen)
EDIT (see coments): this is the expected behaviour of a serial column, since they're nothing but a fancy way to use sequences. Long story short: using upserts the gaps will be inevitable. If you're worried the value might become too big, use bigserial instead and let PostgreSQL do its job.
Related thread: serial in postgres is being increased even though I added on conflict do nothing

Problem with alias in Sqliteman trying to make unique id´s

I´m trying to translate this function from SQL server to Sqlite, how can i do it? I know it´s different but I just couldn´t. Any information will be awesome. Thanks
This is the code:
CREATE TABLE "user" (
"numId" INT IDENTITY(1,1) NOT NULL,
"prefix" VARCHAR(50) NOT NULL,
"id" AS ("prefix"+RIGHT('000000'+CAST(ID AS VARCHAR(7)),7))PERSISTED,
Thanks.
Normal SQL tables are called rowid tables, and have a unique integer as their primary key (A INTEGER PRIMARY KEY column aliases this rowid). So there's the IDENTITY bit taken care of.
While Sqlite 3.31 (In beta as of this writing) supports generated columns, until versions with that feature become widely available over the next few years, you can either create a VIEW that adds your custom prefix, or do it in each individual SELECT where it matters. Something like:
sqlite> CREATE TABLE actual_user(numId INTEGER PRIMARY KEY, prefix TEXT NOT NULL);
sqlite> CREATE VIEW user(numId, prefix, id) AS
...> SELECT numId, prefix, printf('%s%07d', prefix, numId) FROM actual_user;
sqlite> INSERT INTO actual_user(prefix) VALUES ('TEST'), ('TEST'), ('TESTX');
sqlite> SELECT * FROM user;
numId prefix id
---------- ---------- ------------
1 TEST TEST0000001
2 TEST TEST0000002
3 TESTX TESTX0000003
You might also emulate it with triggers for your table that updates the id value on insertion and update. This more closely mimics a persist (stored in SQLite lingo) generated column, while the view mimics a virtual column:
sqlite> CREATE TABLE user(numId INTEGER PRIMARY KEY, prefix TEXT NOT NULL, id TEXT);
sqlite> CREATE TRIGGER user_ai AFTER INSERT ON user
...> BEGIN UPDATE user SET id = printf('%s%07d', new.prefix, new.numId) WHERE numId = new.numId; END;
sqlite> CREATE TRIGGER user_au AFTER UPDATE OF numId, prefix ON user
...> BEGIN UPDATE user SET id = printf('%s%07d', new.prefix, new.numId) WHERE numId = new.numId; END;
sqlite> INSERT INTO user(prefix) VALUES ('TEST');
sqlite> SELECT * FROM user;
numId prefix id
---------- ---------- ------------
1 TEST TEST0000001
sqlite> UPDATE user SET prefix='APPLE' WHERE numId = 1;
sqlite> SELECT * FROM user;
numId prefix id
---------- ---------- ------------
1 APPLE APPLE0000001

How to Insert Primary Value (PK) to Related Table (FK)

I'm having trouble from Inserting 1 Primary Value (Increment) of TABLE to another TABLE (Foreign Key)
Table 1 has the Primary key of Student Number; if i enter values for last and first name from TABLE 1 then the student number will automatically giving it's own value because of Increment, and else if i entered from TABLE 2, I want the value of Student Number from TABLE i will increment even the value of Last and First name if TABLE 1 is NULL
Table 1
(PK)Student_# | Last_Name | First_Name
...........1...........|........a..........|..........b.......
...........2...........|........c..........|..........b.......
Table 2
(FK)Student_# | Year_Level | Section
...........NULL................|..........2nd Year......|.....C1 .........
...........NULL................|..........3rd Year......|.....D1 .........
Needed
(FK)Student_# | Year_Level | Section
..............1...................|..........2nd Year......|.....C1 .........
..............2...................|..........3rd Year......|.....D1 .........
It sounds to me that you need a primary key with an identity seed on table2 and also a foreign key to the student table:
(PK/Identity) Table2ID | (FK)Student_# | Year_Level | Section
This way you can insert the student_# when you insert the record into table 2 and also be able to give each row in table2 a unique identifier
CREATE TABLE Table2
(
Table2ID INT IDENTITY(1,1) PRIMARY KEY
,Student_# INT NOT NULL FOREIGN KEY REFERENCES Table1(Student_#)
,Year_Level NVARCHAR(255) --Use whatever data type you need
,Section NVARCHAR(255) --Use whatever data type you need
)
I have assumed you are using sql server as you have not specified in your question. You may need to change this query for a different RDBMS.

PL/SQL developer how to get the row that made the insert fail?

I'm doing a method that inserts into the table which has a unique column. What I don't know is if I can access the insert value that made the insert fail.
For example:
table1(id,name, phone);
name is unique.
insert (1,a,123);
insert (2,a,1234);
What I want is when I do the second insert I to return the id value '1' without having to recur to a query.
Thank you in advance.
From oracle 10g r2 you can use log errors clause of insert command to log errors in a separate table. Here is an example:
SQL> create table test_table(
2 id number primary key,
3 col1 varchar2(7)
4 )
5 ;
Table created
-- creates a table for logging errors (table name will be prefaced with err$_)
SQL> begin dbms_errlog.create_error_log('TEST_TABLE'); end;
2 /
PL/SQL procedure successfully completed
-- violates primary key constraint
SQL> insert into test_table(id, col1)
2 ( select 1, level
3 from dual
4 connect by level <= 3)
5 log errors reject limit unlimited;
1 row inserted
SQL> commit;
SQL> select * from test_table;
ID COL1
---------- -------
1 1
SQL> select * from err$_test_table;
ORA_ERR_NUMBER$ ORA_ERR_MESG$ ORA_ERR_ROWID$ ORA_ERR_OPTYP$ ORA_ERR_TAG$ ID COL1
--------------- ------------------------------------------------------------------------------------------------------------
1 ORA-00001: unique constraint (HR.SYS_C008315) violated I 1 2
1 ORA-00001: unique constraint (HR.SYS_C008315) violated I 1 3
maybe you can write a trigger(before insert) on your table, on which insert about to happen. In this you can check if the column value(name) already exists in table.
In case it does you may insert this duplicate record in another table for further reference
Another approach is to write the insert in a procedure where the name may be checked and the duplicate name could be stored in a table.
Hope it helps