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

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;

Related

error: You have an error in your SQL syntax when creating table in mysql [duplicate]

This question already has an answer here:
Create a table with a space in the name
(1 answer)
Closed 9 months ago.
I know what a syntax error is but cannot find any error when creating this table:
my code
CREATE TABLE branch supplier (
branch_id INT,
supplier_name VARCHAR(40),
supply_type VARCHAR(40),
PRIMARY KEY (branch_id, supplier_name),
FOREIGN KEY (branch_id) REFERENCES branch(branch_id) ON DELETE CASCADE
);
and the error I'm getting
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'supplier ( branch_id INT, supplier_name VARCHAR(40), supply_type ' at line 1
It is not allowed to use table names with space(s) unless you use `. This means you need to change this to...
CREATE TABLE `branch supplier`...
or to something without space, as example
CREATE TABLE branch_supplier...

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

i cant create a table because name is invalid [duplicate]

This question already has answers here:
ORA-00904: : invalid identifier Oracle sql [closed]
(2 answers)
Closed 1 year ago.
Im trying to create a table with the name " Order " and these datatypes (Note: i dont have a table named Order and I tried to create other tables and it worked ) and the names are:
create table Order(
Onum Number(8) PRIMARY KEY,
Odate Date,
Otime Time(7),
Delivered_on_time char(1));
and the error was:
invalid table name
so I tried to name it Ordeer and try again and it gave me this error:
ORA-00907: missing right parenthesis
Otime Time(7),
Delivered_on_time char(1)
);
what to do? help please
First, you can't call the table order because order is a reserved word. (There's a tricky way to get around this, but it's a bad idea to do so.)
Second, the DATE type also includes the time, so you don't need to have separate date and time columns.
CREATE TABLE orders (
onum NUMBER(8) PRIMARY KEY,
odate DATE,
delivered_on_time CHAR(1)
);

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