Adding AUTO_INCREMENT using ALTER in sql [duplicate] - sql

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?

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

How to modify column to auto increment in PL SQL Developer?

I have created one table in PL SQL Developer.
CREATE TABLE Patient_List
(
Patient_ID number NOT NULL,
Patient_Name varchar(50) NOT NULL,
Patient_Address varchar(100) NULL,
App_Date date NULL,
Descr varchar(50),
CONSTRAINT patient_pk PRIMARY KEY(Patient_ID)
);
I want to auto increment Patient_ID, I tried altering the table and modifying the Patient_ID column but it's showing an error "invalid ALTER TABLE option"
ALTER TABLE Patient_List
MODIFY Patient_ID NUMBER NOT NULL GENERATED ALWAYS AS IDENTITY;
Please help, Thanks in advance.
This is not possible.
Oracle 10g didn't even have identity columns, they were introduced in Oracle 12.1
But even with a current Oracle version, you can't convert a regular column to an identity column. You would need to add a new one.
Before identity columns, the usual way was to create a sequence and a trigger to populate the column.
See here: How to create id with AUTO_INCREMENT on Oracle?
If anybody wants to modify existing column as auto_increment use this three lines
alter table Product drop column test_id;
create sequence Product_test_id_seq INCREMENT BY 1 nocache;
alter table Product add test_id Number default Product_test_id_seq.nextval not null;

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

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

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;