CREATE TABLE statement not working with CHECK constraint - ddl

Edit: I didn't have the database selected when running the statement.
CREATE TABLE animal (
AnimalID int NOT NULL,
AnimalName varchar(20) NOT NULL,
Category varchar(20) NOT NULL,
BestPlaceToSee varchar(100) NOT NULL,
PRIMARY KEY (AnimalID),
CHECK (
Category IN (
'Mammal',
'Bird of Prey',
'Sea Bird',
'Inland Bird',
'Wading and Ground Nesting Bird',
'Fish'
)
)
);
Is giving me the errors
A symbol name was expected! A reserved keyword can not be used as a column name without backquotes. (near CHECK)
Unexpected beginning of statement. (near Category)
Unrecognized statement type. (near IN)
Any help would be appreciated

Related

ORA-00903: invalid table name when Creating Table

Please help. I'm not sure what is happening. I even tried to drop table but still unsuccessful.
CREATE TABLE 'customer' (
'customer_id' int NOT NULL AUTO_INCREMENT,
'type' varchar(45) NOT NULL,
'name' varchar(45) NOT NULL,
'cut_off' int NOT NULL,
PRIMARY KEY ('customer_id')
)
Error starting at line : 1 in command -
CREATE TABLE 'customer' (
'customer_id' int NOT NULL AUTO_INCREMENT,
'type' varchar(45) NOT NULL,
'name' varchar(45) NOT NULL,
'cut_off' tinyint NOT NULL,
PRIMARY KEY ('customer_id')
)
Error report -
ORA-00903: invalid table name
00903. 00000 - "invalid table name"
*Cause:
*Action:
Column and table names should be without quotes.
Refer to create table query https://docs.oracle.com/cd/B28359_01/server.111/b28310/tables003.htm#ADMIN11004
There are multiple issues in your code.
Names of table and columns should not be wrapped in single quotes.
AUTO_INCREMENT keyword is not allowed. It should be implemented differently in oracle.
NOT NULL is not needed on PK column. It is implicit.
Corrected code:
CREATE TABLE customer (
customer_id int GENERATED always as IDENTITY,
type varchar(45) NOT NULL,
name varchar(45) NOT NULL,
cut_off int NOT NULL,
primary key(customer_id)
);

Oracle pushing me ORA-00902: invalid datatype

create table accountDetails(
accountNumber int unique,
customerId int unique,
balance int not null,
password varchar(255) not null,
type varchar(255) not null check(type in ('Savings','Current')),
primary key(accountNumber,customerId) )
create table statusDetails(
customerId int references accountDetails(customerId),
primarykey(customerId))
The last table resulted in an error
The last table resulted in an error
ORA-00902: invalid datatype error happens when we try to define a column using an invalid datatype. It's logical really.
Now, you think you haven't declared a column with an invalid datatype, because you think statusdetails table has only one column, customerid. But if you look at your actual statement you follow that column with this:
primarykey(customerId))
Because you mis-typed primary key Oracle treats that line as an attempt to create a second column. Hence the error, because (customerId) is an invalid datatype. So all you need do is pop in the missing space and Oracle will create the table for you.
create table statusDetails(
customerId int references accountDetails(customerId),
primary key(customerId))
You had a compilation error caused by a simple typo. A key skill for a developer is the ability to turn a cool eye on our own code. I urge you to work on acquiring that skill as soon as you can.
Your second table declaration is all wrong. Try this:
CREATE TABLE statusdetails (
customerid INT,
CONSTRAINT fk_cust FOREIGN KEY ( customerid )
REFERENCES accountdetails ( customerid )
)
Note: using "int" data type maps to a NUMBER(38), which may not be what you want. Use the proper oracle data type names.

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.

Oracle Unknown Command - CONSTRAINT

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

ORA-00907: missing right parenthesis Error while creating a table?

I am new to oracle,
I have created two tables using following queries,
CREATE TABLE employee
(
emp_name VARCHAR(20) NOT NULL,
street VARCHAR(50) NOT NULL,
city VARCHAR(20) NOT NULL,
PRIMARY KEY(emp_name)
)
and
CREATE TABLE company
(
comp_name VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
PRIMARY KEY(comp_name)
)
Now I am trying to create another table using some foreign keys,
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
FOREIGN KEY(emp_name) REFERENCES employee(emp_name),
FOREIGN KEY(comp_name) REFERENCES company(comp_name)
)
Getting ERROR : ORA-00907: missing right parenthesis
I have also tried with
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
constraint wemployee FOREIGN KEY(emp_name) REFERENCES employee(emp_name),
constraint wcompany FOREIGN KEY(comp_name) REFERENCES company(comp_name)
)
But getting same error.
Can any one tell me that where I am doing mistake?
I'm no expert in oracle, but are you allowed to specify the (10) in salary int(10) NOT NULL?
1: you should have a table called "test" with two columns, id and testdata. (This is just a dumb quick example, so I won't bother to specify any constraints on id.)
create table test (id number, testdata varchar2(255));
2: Next we'll create a sequence to use for the id numbers in our test table.
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
You could change "start with 1" to any number you want to begin with (e.g. if you already have 213 entries in a table and you want to begin using this for your 214th entry, replace with "start with 214"). The "increment by 1" clause is the default, so you could omit it. You could also replace it with "increment by n" if you want it to skip n-1 numbers between id numbers. The "nomaxvalue" tells it to keep incrementing forever as opposed to resetting at some point.i (I'm sure Oracle has some limitation on how big it can get, but I don't know what that limit is).
3: Now we're ready to create the trigger that will automatically insert the next number from the sequence into the id column.
create trigger test_trigger
before insert on test
for each row beginselect test_seq.nextval into :new.id from dual;
end;
/
There are two different ways to create a table with constraints:
1)
create table department(
deptno number(5) primary key,
deptname varchar2(30),
empno number(5) references emp(empno));
2)
create table department(
deptno number(5),
deptname varchar2(30),
empno number(5),
constraint pkey_deptno primary key(deptno),
constraint fkey_empno foreign key(empno) references Emp(empno));
When creating the index inline with the rest of the table creation statement try dropping the FOREIGN KEY part:
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
emp_name REFERENCES employee(emp_name),
comp_name REFERENCES company(comp_name)
)
See this question for more details:
ORA-00907: missing right parenthesis

Resources