Oracle table create error ORA-00904 invalid identifier - sql

It's very interesting I don't know why I'm getting ORA-00904 invalid identifier when I'm trying to create a table with oracle.
CREATE TABLE animals
(
CONSTRAINT animal_id NUMBER(6) PRIMARY_KEY,
name VARCHAR2(25),
CONSTRAINT license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);

When creating tables with CREATE TABLE in Oracle, you have at least four ways to specify constraints.
In-line specification
CREATE TABLE animals
(
animal_id NUMBER(6) PRIMARY KEY,
name VARCHAR2(25),
license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
In-line specification with explicit constraints' names
CREATE TABLE animals
(
animal_id NUMBER(6) CONSTRAINT animal_id_pk PRIMARY KEY,
name VARCHAR2(25),
license_tag_number NUMBER(10) CONSTRAINT animal_tag_no_uq UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
Out-line specification
CREATE TABLE animals
(
animal_id NUMBER(6) ,
name VARCHAR2(25),
license_tag_number NUMBER(10),
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL,
PRIMARY KEY (animal_id),
UNIQUE (license_tag_number)
);
Out-line specification with explicit constraints' names
CREATE TABLE animals
(
animal_id NUMBER(6) ,
name VARCHAR2(25),
license_tag_number NUMBER(10),
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL,
CONSTRAINT animal_id_pk PRIMARY KEY (animal_id),
CONSTRAINT animal_tag_no_uq UNIQUE (license_tag_number)
);
If you don't explicitly specify constraints names, they are generated automatically by the system, and read something like SYS_C0013321. I find the last way the most readable, because you see which constraints are created, and can manage them using user-friendly names (e. g. using view user_constraints).
By the way, there's a typo in your code: you should use PRIMARY KEY instead of PRIMARY_KEY.

This is correct code, you must remove CONSTRAINT word:
CREATE TABLE animals
(
animal_id NUMBER(6) PRIMARY KEY,
name VARCHAR2(25),
license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
but you can also use constraints for PK and UNIQUE as below:
CREATE TABLE animals
(
animal_id NUMBER(6) not null,
name VARCHAR2(25),
license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL,
CONSTRAINT animals_PK PRIMARY KEY (animal_id) ,
CONSTRAINT l_tag_number_uq UNIQUE (license_tag_number)
);
It is good practice to use constraints because they give you a friendly name/short description.
SQL Fiddle DEMO

I think there some mistake here :
Catch this example :
CREATE TABLE "name_of_table"
("column_1" "data_type",
"column_2" "data_type",
"column_3" "data_type",
CONSTRAINT column_name PRIMARY KEY (column_1, column_2)
... );
Your code suppose :
CREATE TABLE animals(
CREATE TABLE animals
(
animal_id NUMBER(6) PRIMARY_KEY,
name VARCHAR2(25),
license_tag_number NUMBER(10) UNIQUE,
admit_date DATE NOT NULL,
adoption_id NUMBER(5),
vaccination_date DATE NOT NULL
);
You may check the example here

I am creating a table of employee
Create table employee ( emp_id number(10), emp_name varchar(10), salary number(10));
I am getting an error of invalid identifier

Related

I'm getting this error message: `ORA-00933: SQL command not properly ended` and was hoping someone could point out what's causing it

Here's the code that's causing the issue:
DROP TABLE customers;
--1 create tables
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25),
first_name VARCHAR2(25),
home_phone VARCHAR2(12),
address VARCHAR2(100),
city VARCHAR2(30),
state VARCHAR2(2),
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id),
CONSTRAINT not_null_customer_last_name NOT NULL (last_name),
CONSTRAINT not_null_customer_first_name NOT NULL (first_name),
CONSTRAINT not_null_customer_home_phone NOT NULL (home_phone),
CONSTRAINT not_null_customer_address NOT NULL (address),
CONSTRAINT not_null_customer_city NOT NULL (city),
CONSTRAINT not_null_customer_state NOT NULL (state)
);
I've tried formatting the constraints at the column level and that didn't seem to help. I'm an absolute beginner and am doing this for a class so I'm sure it's something simple and silly but I couldn't figure it out for the life of me. After a few hours of frustration I figured I'd see if there was someone out there who could point me in the right direction.
I am using Oracle APEX.
NOT NULL constraints are defined in-line; there is no option to define them out-of-line (apart from using a CHECK constraint).
From the CREATE TABLE documentation:
inline_constraint::=
out_of_line_constraint::=
Therefore, if you want to use a NOT NULL constraint then you need to declare it inline:
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25)
CONSTRAINT not_null_customer_last_name NOT NULL,
first_name VARCHAR2(25)
CONSTRAINT not_null_customer_first_name NOT NULL,
home_phone VARCHAR2(12)
CONSTRAINT not_null_customer_home_phone NOT NULL,
address VARCHAR2(100)
CONSTRAINT not_null_customer_address NOT NULL,
city VARCHAR2(30)
CONSTRAINT not_null_customer_city NOT NULL,
state VARCHAR2(2)
CONSTRAINT not_null_customer_state NOT NULL,
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id)
);
I think the problem is the way you are defining the NOT NULL constraints, they can be CHECK constraints so you should use:
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25),
first_name VARCHAR2(25),
home_phone VARCHAR2(12),
address VARCHAR2(100),
city VARCHAR2(30),
state VARCHAR2(2),
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id),
CONSTRAINT not_null_customer_last_name CHECK (last_name IS NOT NULL),
CONSTRAINT not_null_customer_first_name CHECK (first_name IS NOT NULL),
CONSTRAINT not_null_customer_home_phone CHECK (home_phone IS NOT NULL),
CONSTRAINT not_null_customer_address CHECK (address IS NOT NULL),
CONSTRAINT not_null_customer_city CHECK (city IS NOT NULL),
CONSTRAINT not_null_customer_state CHECK (state IS NOT NULL)
);

Trying to do an assignment but stuck at ORA-00906: missing left parenthesis

Im doing a course on DB, this is one of the first assignments, I'm about to be done, but there is an issue somewhere in my REL_CURSOS_ALUMNOS query which ends up with the ORA-00906 error.
REL_CURSOS_ALUMNOS is a relationship table for "one to many" 1 ' CURSOS(Id_Curso) to many ALUMNOS(NIF_Alumno).
This is my query till now:
CREATE TABLE PROFESORES(
NIF_Profesor VARCHAR2(15) CONSTRAINT Prof_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Titulacion VARCHAR2(500),
Salario FLOAT(10) CONSTRAINT Prof_sal_NN NOT NULL
);
CREATE TABLE ALUMNOS(
NIF_Alumno VARCHAR2(15) CONSTRAINT Alum_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Sexo CHAR(1),
Fecha_Nacimiento DATE
);
CREATE TABLE CURSOS (
Id_Curso VARCHAR2(15) CONSTRAINT Curs_Id_PK PRIMARY KEY,
Nombre VARCHAR2(400) UNIQUE,
NIF_Profesor VARCHAR2(15) REFERENCES PROFESORES (NIF_Profesor),
Max_Alumnos NUMBER(5),
Inicio_Fecha DATE,
Final_Fecha DATE,
Num_Horas NUMBER(10) NOT NULL,
CONSTRAINT Curs_FechasIncorrectas CHECK (Final_Fecha > Inicio_Fecha));
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Cursos),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE KEY (Id_Curso, NIF_Alumno));
Thank you.
You have a couple of small errors in the final table definition:
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Curso),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE (Id_Curso, NIF_Alumno)
);
Your specific problem is caused by KEY. That is not needed for a unique constraint definition. Further, the reference for CURSOS is id_curso, not id_cursos.
I left both unique definitions. But if NIF_Alumno is unique, then the pair (Id_Curso, NIF_Alumno) is also unique, so that constraint is redundant.

Oracle DB can not create Table Showing "ORA-02270: no matching unique or primary key for this column-list"

I am trying to create tables in ORACLE with Foreign Key and Primary key but It is showing me error.
"
FOREIGN KEY(Branch_ID) REFERENCES Bank_Branchs(Branch_ID),
*
ERROR at line 10:
ORA-02270: no matching unique or primary key for this column-list
FOREIGN KEY(Branch_ID) REFERENCES Bank_Branchs(Branch_ID)
*
ERROR at line 10:
ORA-02270: no matching unique or primary key for this column-list
"
I don't know what is the reason of the error. Please take a look on my sql code.
drop table Employees;
drop table Bank_Branchs;
drop table Departments;
drop table Job_Titles;
drop table Accounts;
CREATE TABLE Bank_Branchs(
Branch_ID NUMBER(15) NOT NULL,
Branch_Name VARCHAR2(15),
Country VARCHAR2(35),
City VARCHAR2(35),
Phone VARCHAR2(15),
Manager_ID NUMBER(7) NOT NULL,
PRIMARY KEY (Branch_ID,Manager_ID)
);
CREATE TABLE Departments(
Dept_ID CHAR(3) NOT NULL,
Dept_Name VARCHAR2(25),
Head_of_Dept NUMBER(7),
PRIMARY KEY(Dept_ID)
);
CREATE TABLE Job_Titles(
Title_ID CHAR(3)NOT NULL,
Title_Name VARCHAR2(25),
Title_Desc VARCHAR2(250),
PRIMARY KEY(Title_ID)
);
CREATE TABLE Employees
(Emp_ID NUMBER(7) NOT NULL,
Branch_ID NUMBER(15) NOT NULL,
Title_ID CHAR(3),
Department_ID CHAR(3),
Manager_ID NUMBER(7),
Salary NUMBER(9),
Hourly_Rate NUMBER(9),
PRIMARY KEY(Emp_ID),
FOREIGN KEY(Branch_ID) REFERENCES Bank_Branchs(Branch_ID),
FOREIGN KEY(Title_ID) REFERENCES Job_Titles (Title_ID),
FOREIGN KEY(Department_ID) REFERENCES Departments (Dept_ID),
FOREIGN KEY(Manager_ID) REFERENCES Bank_Branchs (Manager_ID)
);
CREATE TABLE Accounts(
Account_ID NUMBER(7) NOT NULL,
Branch_ID NUMBER(15) NOT NULL,
Customer_ID NUMBER(7),
Acc_Type char(2),
Balance NUMBER(38),
Rate NUMBER(9),
Status VARCHAR(15),
PRIMARY KEY(Account_ID),
FOREIGN KEY(Branch_ID) REFERENCES Bank_Branchs(Branch_ID)
);
I tried to run this using start I:/SQLNAME.sql and the SQL command line is showing me the error.
Tested... And this will work, but please read below...
CREATE TABLE Bank_Branchs(
Branch_ID NUMBER(15) NOT NULL,
Branch_Name VARCHAR2(15),
Country VARCHAR2(35),
City VARCHAR2(35),
Phone VARCHAR2(15),
Manager_ID NUMBER(7) NOT NULL,
PRIMARY KEY (Branch_ID,Manager_ID)
);
CREATE TABLE Departments(
Dept_ID CHAR(3) NOT NULL,
Dept_Name VARCHAR2(25),
Head_of_Dept NUMBER(7),
PRIMARY KEY(Dept_ID)
);
CREATE TABLE Job_Titles(
Title_ID CHAR(3)NOT NULL,
Title_Name VARCHAR2(25),
Title_Desc VARCHAR2(250),
PRIMARY KEY(Title_ID)
);
CREATE TABLE Employees
(Emp_ID NUMBER(7) NOT NULL,
Branch_ID NUMBER(15) NOT NULL,
Title_ID CHAR(3),
Department_ID CHAR(3),
Manager_ID NUMBER(7),
Salary NUMBER(9),
Hourly_Rate NUMBER(9),
PRIMARY KEY(Emp_ID),
FOREIGN KEY(Branch_ID, Manager_ID) REFERENCES Bank_Branchs(Branch_ID, Manager_ID),
FOREIGN KEY(Title_ID) REFERENCES Job_Titles (Title_ID),
FOREIGN KEY(Department_ID) REFERENCES Departments (Dept_ID)
);
It seems like a manager_id would correspond to a employee_id...
It also seems like their may be more than one manager for an individual bank branch..
Bascically, i think manager_id should be removed from bank_branches and this should be created instead:
create table bank_branch_managers(
branch_id number(14),
manager_id number(7),
effective_date date,
activity_date date,
PRIMARY KEY (branch_id, manager_id),
FOREIGN KEY (branch_id) REFERENCES Bank_Branches(Brand_id),
FOREIGN KEY (manager_id) REFERENCED Employees( Emp_id)
);
But you'd need to adjust the setup of "Employees", probably adding a table for managers as well..
A FOREIGN KEY constraint have to be linked only to a PRIMARY KEY constraint in another table; or it can also be defined to reference the columns of a UNIQUE constraint in another table.
in your bank_branchs table, you have created a composite primary key ie a primary key consisting of more than one column.
PRIMARY KEY (Branch_ID,Manager_ID)
this statement does not make Brach_ID and Manager_ID as the primary keys, rather it makes their combination as a primary key. and since a foreign key can only refer to either a primary key or a column with UNIQUE constraint, it throws an error.
try adding UNIQUE constraint on both of these columns.
CREATE TABLE Bank_Branchs(
Branch_ID NUMBER(15) UNIQUE NOT NULL,
Branch_Name VARCHAR2(15),
Country VARCHAR2(35),
City VARCHAR2(35),
Phone VARCHAR2(15),
Manager_ID NUMBER(7) UNIQUE NOT NULL,
PRIMARY KEY (Branch_ID,Manager_ID)
);
this might solve your problem.

Oracle table error: table creating with constraints

create table prmr(
emp_id number(10) primary key,
name varchar2(10),
mob number(10) unique key,
id varchar2(10),
email varchar2(20)
);
When I create this table, it gives me the error "Missing Parenthesis". Any help?
Your syntax is wrong. Remove key from unique key
CREATE TABLE prmr(
emp_id number(10) PRIMARY KEY,
name varchar2(10),
mob number(10) UNIQUE,
id varchar2(10),
email varchar2(20)
);
There is also a way to specify constraints separately:
CREATE TABLE prmr(
emp_id number(10) NOT NULL,
name varchar2(10),
mob number(10),
id varchar2(10),
email varchar2(20),
CONSTRAINT mob_unique UNIQUE (mob),
CONSTRAINT emp_id_pk PRIMARY KEY (emp_id)
);
I also added NOT NULL to the emp_id (Although you don't need to specify it explicitly as far as emp_id is PK, that could, probably, help avoid the confusion anyway).
Try this :
create table prmr(
emp_id number(10),
name varchar2(10),
mob number(10),
id varchar2(10),
email varchar2(20),
CONSTRAINT PK_table PRIMARY KEY (emp_id),
CONSTRAINT unique_mob UNIQUE(mob)
);

Creating associative entity table

The following tables have already been implemented successfully:
CREATE TABLE Patient (
Patient_ID CHAR(5) CONSTRAINT Patient_PK PRIMARY KEY,
First_Name VARCHAR2(20) NOT NULL,
Last_Name VARCHAR2(20) NOT NULL,
DoB DATE,
Sex CHAR(1),
Phone NUMBER(10),
Address VARCHAR2(40)
);
CREATE TABLE Physician (
Physician_ID CHAR(5) CONSTRAINT Physician_PK PRIMARY KEY,
First_Name VARCHAR2(20) NOT NULL,
Last_Name VARCHAR2(20) NOT NULL,
Department_ID CHAR(5) ,
CONSTRAINT physician_FK_dept
FOREIGN KEY (Department_ID)
REFERENCES Department (Department_ID)
ON DELETE SET NULL
);
But when I tried creating the associative entity between the two tables:
CREATE TABLE Visit (
Visit_ID CHAR(5) CONSTRAINT Visit_PK PRIMARY KEY,
Visit_date DATE NOT NULL,
Patient_ID VARCHAR2(20) NOT NULL,
Physician_ID VARCHAR2(20),
CONSTRAINT visit_FK_patient
FOREIGN KEY (Patient_ID)
REFERENCES Patient (Patient_ID)
ON DELETE SET CASCADE,
CONSTRAINT visit_FK_physician
FOREIGN KEY (Physician_ID)
REFERENCES Physician (Physician_ID)
ON DELETE SET NULL
);
It failed because of an error:
ORA-00908: missing NULL keyword"
Normally it would be a simple matter of a missing NULL. But this time, no matter how I look at the SQL it doesn't look like that is the error. Can anyone help to see if they can find what the problem is from another perspective? Thank you
I think where you have
ON DELETE SET CASCADE
This should be
ON DELETE CASCADE
c.f. this