Oracle get id of inserted row with identity always [duplicate] - sql

This question already has answers here:
Inserting into Oracle and retrieving the generated sequence ID
(6 answers)
Closed 4 years ago.
Currently I have a table with this structure:
CREATE TABLE "DUMMY_SCHEMA"."NAMES"
(
"ID" NUMBER(10,0) GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 CACHE 20) NOT NULL
, "NAME" NVARCHAR2(1024) NOT NULL
, CONSTRAINT "NAMES_PK" PRIMARY KEY ("ID")
);
In SQL Server I only need to do following to get the Id of the inserted row.
INSERT INTO [NAMES]([NAME])VALUES('Random'); SELECT SCOPE_IDENTITY() Id
What would be the equivalent for Oracle 12c?

The equivalent is
INSERT INTO dummy_schema.names (name) VALUES ('Random')
RETURNING id INTO :myvalue;
The mechanism how to pick up the returned ID depends on the host language (Java, PL/SQL, SQL*Plus etc).

Related

Update Postgres SQL table with SERIAL from previous insert [duplicate]

This question already has answers here:
Insert a value from a table in another table as foreign key
(3 answers)
Closed 4 months ago.
Very new to SQL in general, working on creating 2 Tables, 1 for example representing appliances with a primary key, second representing a microwave for example with its FK referencing the primary tables PK.
I'm using SERIAL as the id for the primary table, but don't know how to update or insert into the second table using that specific generated value from the first.
I've created my tables using PSQL (Postgres15) like so:
CREATE TABLE Appliances (
id SERIAL NOT NULL,
field1 integer NOT NULL DEFAULT (0),
--
PRIMARY KEY (id),
UNIQUE(id)
);
CREATE TABLE Microwaves (
id integer NOT NULL,
field1 integer,
--
PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES Appliances(id)
);
Inserting my first row into the Appliance table:
INSERT INTO Appliances(field1) VALUES(1);
SELECT * FROM Appliances;
Yields:
And a query I found somewhere pulls the current increment of the SERIAL:
SELECT currval(pg_get_serial_sequence('Appliances', 'id'));
Yields:
I'm struggling to determine how to format the INSERT statement, have tried several variations around the below input:
INSERT INTO Microwaves VALUES(SELECT currval(pg_get_serial_sequence('Appliances', 'id'), 1));
Yields:
Appreciate feedback on solving the problem as represented, or a better way to tackle this in general.
Okay looks like I stumbled on at least one solution that works in my case as taken from https://stackoverflow.com/a/50004699/3564760
DO $$
DECLARE appliance_id integer;
BEGIN
INSERT INTO Appliances(field1) VALUES('appliance2') RETURNING id INTO appliance_id;
INSERT INTO Microwaves(id, field2) VALUES(appliance_id, 100);
END $$;
Still open to other answers if this isn't ideal.

Oracle Primary Key AUTO-INCREMENT [duplicate]

This question already has an answer here:
SQL: Oracle 19c unique ID
(1 answer)
Closed 1 year ago.
If I want to create a table on Oracle SQLDeveloper and put in the primary key a varchar which always starts with "c" followed by 9 digits which auto-increment, how could I?
Example: C0000000001
You could do this:
create table test_table (
id VARCHAR2(30)
constraint test_table_id_pk primary key
)
;
-- triggers
create or replace trigger test_table_biu
before insert
on test_table
for each row
begin
:id := 'c'||LPAD(test_table_seq.NEXTVAL,9,0);
end test_table_biu;
/
Note that it will start failing when the sequence reaches a 10 digit value because of duplicate values (it will cut off the extra digits).

create default value in oracle [duplicate]

This question already has answers here:
How to create id with AUTO_INCREMENT on Oracle?
(18 answers)
Closed 4 years ago.
How I can set default value in oracle? my query is like this :
alter table talend_job modify mtj_id varchar(10) not null default
('JASG'|| (nextval('mtj_id_seq')))
In Oracle 11g, you cannot specify a sequence number as a DEFAULT value for a column; however, you can emulate this functionality using a trigger. Even if a column is declared NOT NULL, you can still omit the column from INSERT statements to be populated in the trigger
You can refer to How to create id with AUTO_INCREMENT on Oracle?

How to have two auto increment columns in sql - entity framework mapping [duplicate]

This question already has answers here:
Entity Framework 6 GUID as primary key: Cannot insert the value NULL into column 'Id', table 'FileStore'; column does not allow nulls
(11 answers)
Closed 5 years ago.
My sql table has two columns that needs identity generated.
1) ID //its identity seeds trwo, identity(1,1)
2) pGuid //newid() as default values
Problem is when insert entry from UI then, it works for ID and not for pGuid field.
using entity framework, fluent API.
mapping added as
'this.property("pGuid").HasColumn("pGuid");`
what is missing with mapping and so, its not working.
You can create the table with pGuid column as 'uniqueidentifier' datatype with default value as newid() as shown below, then if you insert value using EF to other columns (e.g. Company) in below table, identity and newid() values will be automatically inserted in the new row for ID and pGuid columns.
CREATE TABLE MyTable
(
ID INT IDENTITY(1,1) PRIMARY KEY,
pGuid uniqueidentifier NOT NULL DEFAULT newid(),
Company varchar(30) NOT NULL,
..........
)

Oracle SQL Auto Generated Identity Primary Key [duplicate]

This question already has answers here:
Auto Increment for Oracle
(7 answers)
Closed 5 years ago.
I'm using Oracle's SQLDeveloper and trying to create some tables.
My goal is to create a simple table, for example, with an auto-generated id together with a description.
CREATE TABLE xpto (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
description VARCHAR2(200) NOT NULL
);
I'm getting:
Error report -
SQL Error: ORA-02000: missing ( keyword
02000. 00000 - "missing %s keyword"
Any idea what I might be doing wrong?
Thanks in advance!
Identity columns is a feature supported by Oracle from version 12c.
In Oracle 11 it does not work, you must use a sequence and a trigger, in this way:
CREATE TABLE xpto (
id NUMBER PRIMARY KEY,
description VARCHAR2(200) NOT NULL
);
CREATE SEQUENCE xpto_seq;
set define off
CREATE OR REPLACE TRIGGER xpto_id
BEFORE INSERT ON xpto
FOR EACH ROW
WHEN ( new.id IS NULL )
BEGIN
:new.id := xpto_seq.nextval;
END;
/