Oracle Primary Key AUTO-INCREMENT [duplicate] - sql

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).

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.

Adding AUTO_INCREMENT using ALTER in sql [duplicate]

This question already has answers here:
How to create id with AUTO_INCREMENT on Oracle?
(18 answers)
Closed 4 years ago.
I've been trying to add AUTO_INCREMENT on a created table , but the ALTER table query is not working
My Table:
Query and Error:
And also
ALTER TABLE professor ADD sno INT IDENTITY;
is not working
Can you please try:
alter table professor add sno integer generated by default on null as identity;
EDIT: Sorry, the OP is asking for 11g, as the code above works after 12.
Then you need to use sequence. Please see below:
ALTER TABLE professor ADD sno INT;
CREATE SEQUENCE dept_seq START WITH 1;
And you need to set a trigger for your sequence like:
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON departments
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
Reference: How to create id with AUTO_INCREMENT on Oracle?

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

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).

How to set up value for serial type in PostgreSQL? [duplicate]

This question already has answers here:
How to reset Postgres' primary key sequence when it falls out of sync?
(33 answers)
Closed 5 years ago.
I have a primery key in my table as follows:
CREATE TABLE a (
id serial NOT NULL,
valuea citext NOT NULL,
CONSTRAINT a_pkey PRIMARY KEY (id),
);
Table had the following rows:
id value
198 b
199 j
By accident I did this insert
Insert Into a(id,valuea) values (200,'hello');
Now, when I try to do another insert in the correct way:
Insert Into a(valuea) values ('b');
I expect it to insert (201,b) but the serial counter doesn't know 200 has been used because of the last manual insert.
I get:
ERROR: duplicate key value violates unique constraint
"a_pkey" DETAIL: Key (id)=(200) already exists.
I understand this error.. basically it happens because my last insert was not used the Serial and therefore it's counter didn't rise up.
What I don't know is how to fix it?
How do I tell the serial counter to start from 201?
You need to find the sequence name, normally something like <your table>_id_seq and do :
ALTER SEQUENCE a_id_seq INCREMENT BY 1;
When you create a serial key, Postgres creates a sequence that it uses to generate next values.
Just find that sequence and modify its START value to e.g 201 .
SELECT setval(<name of the sequence>, 201, true);

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;
/