Oracle SQL Auto Generated Identity Primary Key [duplicate] - sql

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

Related

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

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

Sequence in Oracle/PostgreSQL with no ID in insert statement

I'm try to create table with clever sequence generator for using this insert-strucure:
insert into SOMEUSERS (SOMEUSERS_NAME, SOMEUSERS_PASSWORD)
values ('Artem', 'PracTimPatie');
instead of this:
insert into SOMEUSERS (SOMEUSERS_ID, SOMEUSERS_NAME, SOMEUSERS_PASSWORD)
values (2, 'Artem', 'PracTimPatie');
or this structure:
insert into SOMEUSERS (SOMEUSERS_ID, SOMEUSERS_NAME, SOMEUSERS_PASSWORD)
values (GEN_ID_SOMEUSERS.nextval, 'Artem', 'PracTimPatie');
When I executing the following sql script:
create sequence gen_id_someUsers START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;
CREATE TABLE loc_db.someUsers
( someUsers_id number(10) DEFAULT gen_id_someUsers.NEXTVAL NOT NULL, --because of this row
someUsers_name varchar2(50) NOT NULL,
someUsers_password varchar2(50),
CONSTRAINT someUsers_pk PRIMARY KEY (someUsers_id)
);
the following notice is given to me:
Error report - SQL Error: ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
For clarity, said that in this case:
...
CREATE TABLE loc_db.someUsers
( someUsers_id number(10) NOT NULL, --correct this row
...
Sequence GEN_ID_SOMEUSERS created.
Table LOC_DB.SOMEUSERS created.
How can I configure comfortable sequence generator?
(in case of PostgreSQL too. If possible with no trigger(as easily as possible)
Oracle 12c introduces Identity columns:
CREATE TABLE SOMEUSERS (
SOMEUSERS_ID NUMBER(10) GENERATED ALWAYS AS IDENTITY
CONSTRAINT SOMEUSERS__SOMEUSERS_ID__PK PRIMARY KEY,
SOMEUSERS_NAME VARCHAR2(50)
CONSTRAINT SOMEUSERS__SOMEUSERS_NAME__NN NOT NULL,
SOMEUSERS_PASSWORD VARCHAR2(50)
);
If you want to do it in earlier versions then you will need a trigger and a sequence:
CREATE TABLE SOMEUSERS (
SOMEUSERS_ID NUMBER(10)
CONSTRAINT SOMEUSERS__SOMEUSERS_ID__PK PRIMARY KEY,
SOMEUSERS_NAME VARCHAR2(50)
CONSTRAINT SOMEUSERS__SOMEUSERS_NAME__NN NOT NULL,
SOMEUSERS_PASSWORD VARCHAR2(50)
);
/
CREATE SEQUENCE gen_id_someUsers START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;
/
CREATE OR REPLACE TRIGGER SOMEUSERS__ID__TRG
BEFORE INSERT ON SOMEUSERS
FOR EACH ROW
BEGIN
:new.SOMEUSERS_ID := gen_id_someUsers.NEXTVAL;
END;
/
You can then just do (either with the identity column or the trigger combined with your sequence):
INSERT INTO SOMEUSERS (
SOMEUSERS_NAME,
SOMEUSERS_PASSWORD
) VALUES (
'Name',
'Password'
);
In postgres just use a serial like this:
CREATE TABLE SOMEUSERS (
SOMEUSERS_ID serial NOT NULL,
SOMEUSERS_NAME text,
SOMEUSERS_PASSWORD text
);
Your insert statement is then easy as:
INSERT INTO SOMEUSERS (SOMEUSERS_NAME, SOMEUSERS_PASSWORD)
values ('Artem', 'PracTimPatie');
If you wanna query the sequence you can just query it like any other relation.
Other answers have addressed postgreSQL and Oracle 12c, so I'll address Oracle 11.2 or earlier here.
From the 11.1 SQL Reference Manual:
DEFAULT
The DEFAULT clause lets you specify a value to be assigned to the column if a subsequent INSERT statement omits a value for the column. The datatype of the expression must match the datatype of the column. The column must also be long enough to hold this expression.
The DEFAULT expression can include any SQL function as long as the function does not return a literal argument, a column reference, or a nested function invocation.
Restriction on Default Column Values
A DEFAULT expression cannot contain references to PL/SQL functions or to other columns, the pseudocolumns CURRVAL, NEXTVAL, LEVEL, PRIOR, and ROWNUM, or date constants that are not fully specified.
(Emphasis mine)
So since you can't put sequence.NEXTVAL in as a DEFAULT value you're basically going to have to use a trigger:
CREATE OR REPLACE TRIGGER SOMEUSERS_BI
BEFORE INSERT
ON LOC_DB.SOMEUSERS
FOR EACH ROW
BEGIN
IF :NEW.SOMEUSERS_ID THEN
:NEW.SOMEUSERS_ID := GEN_ID_SOMEUSERS.NEXTVAL;
END IF;
END SOMEUSERS_BI;
In my experience there is no reliable alternative to using a trigger such as this in Oracle 11.2 or earlier.
Best of luck.

Creating table in Oracle yields a "Missing right parenthesis" error [duplicate]

This question already has answers here:
How to create id with AUTO_INCREMENT on Oracle?
(18 answers)
Closed 8 years ago.
I am trying to create a table, but keep getting the following error message: Warning: oci_execute(): ORA-00907: missing right parenthesis in ... on line 14
The following is the code that deals with this issue:
$stid = oci_parse($conn, 'CREATE TABLE tags (
id INT NOT NULL auto_increment,
PRIMARY KEY(id),
name VARCHAR2(64) NOT NULL)')
or die(oci_error($conn));
oci_execute($stid) or die(oci_error($conn));
Line 14 is oci_execute($stid) or die(oci_error($conn));.
I am new to Oracle and don't understand this error. I used Google and found numerous posts on StackOverflow too, but none of those answers were able to solve this problem and properly create a new table.
What am I doing wrong here?
I believe your used to creating table in MySQL.
Oracle don't have an auto_increment as MySQL.
$stid = oci_parse($conn, 'CREATE TABLE tags (
id INT NOT NULL,
name VARCHAR2(64) NOT NULL),
PRIMARY KEY(id)')
Oracle used object Sequence to create an auto_increment value.
Example on how to create sequence assuming you have the right permission.
CREATE SEQUENCE "CCAD"."AUTH_GROUP_SQ"
MINVALUE 1
MAXVALUE 999999999999
INCREMENT BY 1
START WITH 91
CACHE 20
NOORDER NOCYCLE;
Together with Insert trigger.
create or replace TRIGGER "AUTH_GROUP_TR"
BEFORE INSERT ON "AUTH_GROUP"
FOR EACH ROW
WHEN (new."ID" IS NULL)
BEGIN
SELECT "AUTH_GROUP_SQ".nextval
INTO :new."ID" FROM dual;
END;