I got error on column with identity(1,1)? - sql

Sql developer gives an error of:
ORA-00907: missing right parenthesis, for next table :
create table customer (
CUSTOMER_ID number identity (1,1) not null,
CUSTOMER_PHONE char(13) not null
);
Also the 'identity is red underlined, have no idea why

Proper syntax for Oracle 12c and newer:
create table customer (
CUSTOMER_ID NUMBER GENERATED ALWAYS AS IDENTITY,
CUSTOMER_PHONE char(13) not null
);
-- explicit start and increment
create table customer (
CUSTOMER_ID NUMBER GENERATED ALWAYS AS IDENTITY(START WITH 1 INCREMENT BY 1),
CUSTOMER_PHONE char(13) not null
);
db<>fiddle demo

Related

Receiving endless errors with the insert - statement

I am trying to insert some values into customer and then i get the ora-00904 error.
After i research about this problem, instead of using '' i used "". But now im receiving the ora-00984.
INSERT INTO customer (c_id, name, age)
VALUES (1, 'Carl', 45)
OUTPUT:
SQL-ERROR: ORA-00904: "name": invalid ID
00904. 00000 - "%s: invalid identifier"
then i tried this way.
INSERT INTO customer (c_id, name, age)
VALUES (1, "Carl", 45)
OUTPUT:
00984. 00000 - "column not allowed here"
My DDL - Code:
CREATE TABLE adress (
adress_id INTEGER NOT NULL,
state VARCHAR2(60) NOT NULL,
country VARCHAR2(60) NOT NULL
);
ALTER TABLE adress ADD CONSTRAINT adress_pk PRIMARY KEY ( adress_id );
CREATE TABLE contract (
con_id INTEGER NOT NULL,
length DATE
);
ALTER TABLE contract ADD CONSTRAINT contract_pk PRIMARY KEY ( con_id );
CREATE TABLE customer (
c_id INTEGER NOT NULL,
name VARCHAR2(60) NOT NULL,
age CHAR(2) NOT NULL,
adress_adress_id INTEGER
);
ALTER TABLE customer ADD CONSTRAINT customer_pk PRIMARY KEY ( c_id );
CREATE TABLE relation_1 (
customer_c_id INTEGER NOT NULL,
contract_con_id INTEGER NOT NULL
);
ALTER TABLE relation_1 ADD CONSTRAINT relation_1_pk PRIMARY KEY ( customer_c_id,
contract_con_id);
ALTER TABLE customer
ADD CONSTRAINT customer_adress_fk FOREIGN KEY ( adress_adress_id )
REFERENCES adress ( adress_id );
ALTER TABLE relation_1
ADD CONSTRAINT relation_1_contract_fk FOREIGN KEY ( contract_con_id )
REFERENCES contract ( con_id );
ALTER TABLE relation_1
ADD CONSTRAINT relation_1_customer_fk FOREIGN KEY ( customer_c_id )
REFERENCES customer ( c_id );
The syntax uses VALUES() for the list of values:
INSERT INTO customer (c_id, name, age)
VALUES (1, 'Carl', 45);
You can also use a SELECT:
INSERT INTO customer (c_id, name, age)
SELECT 1, 'Carl', 45
FROM dual;
Note that in both cases, the delimiter for the string is single quotes not double quotes.
In SQL we use single quotes for string literals. Double quotes are used (optionally) for identifiers such as table and column names. You have used double quotes around the value of carl and that's why Oracle hurls ORA-00984.
Why you got ORA-00904 for the first statement is a mystery. Probably there is something awry with your definition of customer but unless you post the table structure we cannot be sure.

Unable to insert row into table with constraint

I have the following definition of table in my Database
CREATE TABLE Products
(
PartNo CHAR (6) NOT NULL ,
Description VARCHAR2 (16) NOT NULL ,
Weight SMALLINT NOT NULL
) ;
ALTER TABLE Products ADD CONSTRAINT CK_PartNo CHECK ( PartNo LIKE '[0-9][A-Z][0-9][0-9][0-9][0-9]') ;
ALTER TABLE Products ADD CONSTRAINT Products_PK PRIMARY KEY ( PartNo ) ;
When I try to insert a row I get an error of not meeting the constraint check.
insert into PRODUCTS(PARTNO, DESCRIPTION, WEIGHT)
values('1W1234', 'O-ring', 1);
*Action: do not insert values that violate the constraint.
Error starting at line : 1 in command -
insert into PRODUCTS(PARTNO, DESCRIPTION, WEIGHT)
values('1W1234', 'O-ring', 1)
Error report -
SQL Error: ORA-02290: check constraint (DEMO.CK_PARTNO) violated
02290. 00000 - "check constraint (%s.%s) violated"
*Cause: The values being inserted do not satisfy the named check
ALTER TABLE Products ADD CONSTRAINT CK_PartNo
CHECK ( regexp_like(PartNo, '\d[A-Z]\d{4}') ) ;
You can't use regular expression syntax in a LIKE, you need to use REGEXP_LIKE:
CHECK ( REGEXP_LIKE(PARTNO, '[0-9][A-Z][0-9][0-9][0-9][0-9]')
Demo:
CREATE TABLE Products
(
PartNo CHAR (6) NOT NULL ,
Description VARCHAR2 (16) NOT NULL ,
Weight SMALLINT NOT NULL
) ;
ALTER TABLE Products ADD CONSTRAINT CK_PartNo
CHECK ( REGEXP_LIKE(PARTNO, '[0-9][A-Z][0-9][0-9][0-9][0-9]') ) ;
ALTER TABLE Products ADD CONSTRAINT Products_PK PRIMARY KEY ( PartNo ) ;
insert into PRODUCTS(PARTNO, DESCRIPTION, WEIGHT)
values('1W1234', 'O-ring', 1);
1 rows inserted.

Auto increment number

Can you any one tell me how to generate auto number in SQL? I have tried but it shows the following error
"Incorrect sytnax near AUTO_INCREMENT".
What am I doing wrong?
create table auto1
(
Sno int NOT NULL AUTO_INCREMENT,
fname varchar(50)
)
I assume you are using SQL-Server where the syntax is different compared to other DB engines
create table auto1
(
Sno int NOT NULL IDENTITY(1,1),
fname varchar(50)
)
See here for details on table creation.
please try below one
CREATE TABLE test (
sno INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
fname VARCHAR( 20 ) NOT NULL
)

ORA-00904: : invalid identifier

I am trying to create a Table in Oracle and getting the error : ORA-00904: : invalid identifier
Here is my command. I really can't see any problem in it. Please help me to identify the error. Thanks.
CREATE TABLE Sale (
CustomerId INT NOT NULL ,
BarCode INT NOT NULL ,
SalesId INT NOT NULL ,
Date DATE NULL ,
CheckOut TINYINT(1) NULL ,
PRIMARY KEY (CustomerId, BarCode, SalesId) ,
CONSTRAINT fk_Customer_has_Product_Customer
FOREIGN KEY (CustomerId )
REFERENCES Customer (CustomerId )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT fk_Customer_has_Product_Product1
FOREIGN KEY (BarCode )
REFERENCES Product (BarCode )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
The maximum length for an Oracle identifier is 30 characters. These exceed that, are 32 chars long:
fk_Customer_has_Product_Customer
fk_Customer_has_Product_Product1
See Schema Object Naming Rules
As previously mentioned, change "DATE" to something more descriptive and not reserved. also, it seems TINYINT does not work in a table create so change that to NUMBER(1), as well as Tony's correct suggestion of reducing the name size (<=30 chrs)
CREATE TABLE Sale
(
CustomerId INT NOT NULL ,
BarCode INT NOT NULL ,
SalesId INT NOT NULL ,
SaleDate DATE NULL , --DATE is reserved, changed to SaleDate
CheckOut number(1) NULL , --tinyint(1) did not work so changed to number(1)
PRIMARY KEY( CustomerId, BarCode, SalesId ) ,
CONSTRAINT fk_SaleCustCusID FOREIGN KEY( CustomerId ) REFERENCES Customer( CustomerId ) ON
DELETE NO ACTION ON
UPDATE NO ACTION,
CONSTRAINT fk_SaleCustBarCode FOREIGN KEY( BarCode ) REFERENCES Product( BarCode ) ON
DELETE NO ACTION ON
UPDATE NO ACTION
);

Problem in oracle trigger query

Can anyone please tell me what is wrong with this query?
create trigger Test_trigger
before insert on Test for each row
begin select TestSequence.nextval into :new.id from dual;
end;/
When I run this query, I get the following error:
ERROR at line 1: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
;
The symbol ";" was substituted for "end-of-file" to continue.
I am using Oracle 10g Express Edition.
The trailing "/" is SQL*Plus syntax. You're probably using the SQL tool in Oracle Apex - in which case, omit the "/".
It should work if you remove the "/" at the end.
Here is the script I'm trying to execute:
drop index TestTableIdx1;
drop index TestIdx1;
drop index TestIdx2;
drop table Test;
drop sequence TestSequence;
drop table TestTable;
create table TestTable (
objId number NOT NULL,
type varchar(16) NOT NULL,
title varchar(192) ,
url varchar(192) ,
primary key(objId, type)
);
create table Test (
id number NOT NULL,
objId number NOT NULL,
type varchar(16) NOT NULL,
timeslot timestamp NOT NULL,
contextId number ,
pubId number NOT NULL,
category varchar(24),
meta varchar(32),
pageviews number NOT NULL,
aggrLevel number NOT NULL,
primary key(id)
);
create table Dummy (
id int NOT NULL,
primary key(id)
);
create sequence TestSequence
start with 1
increment by 1
nomaxvalue;
create trigger Test_trigger
before insert on Test
for each row
begin
select TestSequence.nextval into :new.id from dual;
end;
/
create index TestTableIdx1 on TestTable (
objId desc, type asc
);
create index TestIdx1 on Test (
timeslot desc, objId desc, type asc
);
create index TestIdx2 on Test (
timeslot desc
);
create index TestIdx3 on Test (
objId desc, type asc
);
create index TestIdx4 on Test (
contextId desc
);