Oracle Unknown Command - CONSTRAINT - oracle

I've decided to completely put out the SQL file here.
CREATE TABLE Account
(
AccountNumber INTEGER NOT NULL PRIMARY KEY,
Name varchar(30) NOT NULL
);
CREATE SEQUENCE SEQ_ADDR START WITH 1 INCREMENT BY 1;
CREATE TABLE Address
(
AddressNumber INTEGER NOT NULL PRIMARY KEY,
AccountNumber INTEGER NOT NULL,
IsPrimary INTEGER NOT NULL,
StreetName varchar(50) NOT NULL,
ZipCode INTEGER NOT NULL
);
CREATE TABLE Bill
(
AccountNumber INTEGER NOT NULL,
EndDate DATE NOT NULL,
StartDate DATE NOT NULL,
DueDate DATE NOT NULL,
CONSTRAINT BillFK FOREIGN KEY (AccountNumber) REFERENCES Account(AccountNumber),
CONSTRAINT BillPK PRIMARY KEY (AccountNumber, EndDate)
);
Again, the error I'm getting begins with the first Constraint call (unknown command beginning "CONSTRAINT..." - rest of line ignored.). I'm also occasionally getting an 'unknown command ")" - rest of line ignored.' Any ideas?

Any empty lines will stop SQL*Plus from accepting the inputs blocks and put it in buffer.
So, when you started your CONSTRAINT keyword after an empty line, it treated it as a new command, and thrown an error.
Try this, before you run all your DDLs.
set sqlblanklines on
You need to instruct the sql*plus to ignore empty lines

Related

How can i drop a check constraint without deleting and recreating the table in sqlplus

I have this table and i added a check to the column duree
with this condition alter table operation modify Duree integer check(Duree>=2);
but in the homework it's specified that it needs to be like (duree>=44) isntead
how this be fixed.
create table Operation (
CodeOP varchar(20) not null ,
Duree integer ,
Chef integer ,
DateDeb Date ,
Budget float ,
CodeS varchar (20),
constraint pk_operation primary key (CodeOP),
constraint fk_operation foreign key(CodeS)REFERENCES Service(CodeS) on delete set null
i tried alter table operation modify Duree integer check(Duree>=44);
but i'm getting this message :
ERROR at line 1: ORA-02293: cannot validate (TPBDD2.SYS_C008292) - check constraint violated

SQL syntax change with H2 version update

I am having the following script for H2 DB used in SpringBoot application tests:
create TABLE PARAMETER (
ID long auto_increment,
TYPE VARCHAR(100) not null,
VALUE VARCHAR(100) not null,
SORT_ORDER int not null
);
CREATE SEQUENCE PARAMETER_ID_SEQ MINVALUE 1 START WITH 1;
This script executes with previous H2 version <h2.version>1.4.196</h2.version>, but when updating to <h2.version>2.1.210</h2.version> the following error ocures and I cannot understand what the problem. Is there a new syntax with the upper version?
ERROR:
Reason: liquibase.exception.DatabaseException: Syntax error in SQL statement "create TABLE PARAMETER (\000a ID long [*]auto_increment,\000a TYPE VARCHAR(100) not null,\000a VALUE VARCHAR(100) not null,\000a SORT_ORDER int not null\000a);\000a\000aCREATE SEQUENCE PARAMETER_ID_SEQ MINVALUE 1 START WITH 1;"; expected "RAW, ARRAY, INVISIBLE, VISIBLE, NOT, NULL, AS, DEFAULT, GENERATED, ON, NOT, NULL, DEFAULT, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY, UNIQUE, NOT, NULL, CHECK, REFERENCES, ,, )"; SQL statement:
create TABLE PARAMETER (
ID long auto_increment,
TYPE VARCHAR(100) not null,
VALUE VARCHAR(100) not null,
SORT_ORDER int not null
);
CREATE SEQUENCE PARAMETER_ID_SEQ MINVALUE 1 START WITH 1; [42001-210] [Failed SQL: (42001) create TABLE PARAMETER (
ID long auto_increment,
TYPE VARCHAR(100) not null,
VALUE VARCHAR(100) not null,
SORT_ORDER int not null
);
CREATE SEQUENCE PARAMETER_ID_SEQ MINVALUE 1 START WITH 1;]
There is no such data type as long in SQL, where did you find it? You need to use BIGINT. H2 accepts long too, but it depends on compatibility mode, for example, it isn't allowed in PostgreSQL compatibility mode.
AUTO_INCREMENT should also be used only in MySQL and MariaDB compatibility modes, H2 also accepts it in REGULAR and LEGACY modes, but normally you need to use GENERATED BY DEFAULT AS IDENTITY.
VALUE is a keyword in H2 and it also a reserved word in the SQL Standard (even in archaic SQL-92). You cannot use it as an identifier without quotes, you need to write it as "VALUE" or "value" depending on case you want (quoted identifiers are case-sensitive by default). Actually there is a compatibility setting, you can add ;NON_KEYWORDS=VALUE to JDBC URL of H2, but it would be better to quote it in your scripts and application.

H2 Schema initialization. Syntax error in SQL statement

I have a spring boot application and I trying to initialize some data on application startup.
This is my application properties:
#Database connection
spring.datasource.url=jdbc:h2:mem:test_db
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=schema.sql
#Hibernate configuration
#spring.jpa.hibernate.ddl-auto = none
This is schema.sql:
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL,
PRIMARY KEY(`id`)
);
and data.sql
INSERT INTO `Person` (
`id`,
`first_name`,
`age`
) VALUES (
1,
'John',
20
);
But I got 'Syntax error in SQL statement' on application startup:
19:08:45.642 6474 [main] INFO o.h.tool.hbm2ddl.SchemaExport - HHH000476: Executing import script '/import.sql'
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000388: Unsuccessful: CREATE TABLE Person (
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"
Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"; SQL statement:
I can't understand, what's wrong with this SQL.
Try this code. Remove PRIMARY KEY(id) and execute it.
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL
);
This error results from the structure of the CREATE TABLE declaration.
It will be the result when you have an extra comma in the end of your SQL declaration--no column declaration following the comma. For example:
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL, --note this line has a comma in the end
);
That's because CREATE TABLE expects a list of the columns that will be created along with the table, and the first parameter of the column is the identifier. As you check here, the column declaration follows the structure:
identifier datatype <constraints> <autoincrement> <functions>
Thus, in your case, as #budthapa and #Vishwanath Mataphati have mentioned, you could simply remove the PRIMARY KEY(id) line from the CREATE TABLE declaration. Moreover, you have already stated that id is a primary key on the first line of the column definitions.
In case you do not have a statement as the PRIMARY KEY declaration, be sure to check for the extra comma following your last column declaration.
Try this, as you have used Table_name
CREATE TABLE IF NOT EXISTS Person (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
age INTEGER NOT NULL
);
I was add below in to application.properties and it work for me
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
What helped in my case was removing single quotes from the table name in my insert query
I had to change this:
INSERT INTO 'translator' (name, email) VALUES ('John Smith', 'john#mail.com');
to this:
INSERT INTO translator (name, email) VALUES ('John Smith', 'john#mail.com');
You set auto increment id, so you can't insert new record with id.
Try INSERT INTO `Person` (
`first_name`,
`age`
) VALUES (
'John',
20
);
I ran into same issue. I fixed that with these application.properties:
spring.jpa.properties.hibernate.connection.charSet=UTF-8
spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
Some issue with multi-line and default encoding.

SQL Error: ORA-00906: missing left parenthesis

CREATE TABLE Customer_TBL
(CustomerID INTEGER NOT NULL PRIMARY KEY,
CustomerName VARCHAR NOT NULL,
JobPosition VARCHAR,
CompanyName VARCHAR NOT NULL,
USState VARCHAR NOT NULL,
ContactNo BIGINTEGER NOT NULL);
Error starting at line : 1 in command -
Error report - SQL Error: ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
Biginteger is not supported in Oracle, use number instead. And you need to use varchar2(number of char/bytes) or varchar(number of char/bytes).
Why the error missing left parenthesis?
Because Oracle was expecting ( after VARHCHAR but it was not there.
CREATE TABLE Customer_TBL (CustomerID INTEGER NOT NULL PRIMARY KEY,
CustomerName VARCHAR2(20) NOT NULL,
JobPosition VARCHAR2(20),
CompanyName VARCHAR2(20) NOT NULL,
USState VARCHAR2(20) NOT NULL,
ContactNo NUMBER NOT NULL);
You need to specify a maximum size for VARCHAR fields, e.g: field_name VARCHAR(40),

Error query oracle db in toad

I've executed following query in toad:
CREATE TABLE ACTWEB.usuarios
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
matricula INTEGER,
nome CHAR(50) NOT NULL,
senha CHAR(50),
nivel INTEGER,
maleta INTEGER,
email CHAR(50),
acessos INTEGER,
datacriacao DATE,
dataalteracao DATE
UNIQUE (id)
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL;
And got this ORA message:
ORA-00907 missing right parenthesis
Cause: A left parenthesis has been entered without a closing right parenthesis, or extra information was contained in the parentheses. All parentheses must be entered in pairs.
Action: Correct the syntax and retry the statement.
There are a couple of errors in your syntax. The NOT NULL has to occur after the IDENTITY clause. The range has to be specified without a , and the UNIQUE keyword has to appear at the column directly:
CREATE TABLE ACTWEB.usuarios
(
id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL UNIQUE,
matricula INTEGER,
nome CHAR(50) NOT NULL,
senha CHAR(50),
nivel INTEGER,
maleta INTEGER,
email CHAR(50),
acessos INTEGER,
datacriacao DATE,
dataalteracao DATE
)
LOGGING NOCOMPRESS NOCACHE NOPARALLEL;
You specified unique not right, to solve it you have several ways:
First, create unique key explicitly:
create table tab1(
id number(10),
CONSTRAINT id_uk UNIQUE (id)
)
Second, create unique key as an option of the column:
create table tab1(
id number(10) UNIQUE
)
Third, add the unique key constraint using alter table:
create table tab1(
id number(10)
);
alter table tab1 add constraint id_uk unique(id);
Forth, create unique key implicitly by creating unique index:
create table tab1(
id number(10)
);
CREATE UNIQUE INDEX id_uk ON tab1 (id);

Resources