Oracle table error: table creating with constraints - sql

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

Related

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 shows invalid character on constraint check

here is my code
Create table Member(
Member_Id Number(5) primary key,
Member_Name varchar2(30),
Member_address varchar2(50),
Acc_Open_Date Date,
Membership_type varchar2(20),
Fees_paid Number(4),
Max_Book_Allowed Number(2),
Penalty_Amount Number(7,2),
CONSTRAINT mt CHECK (Membership_type IN(‘Lifetime’,’Annual’,’Half Yearly’,’Quarterly’))
);
now i want to set the check constraint with some specific string and i use for comparing the IN but it shows again and again invalid character.
now by using alter table it works..
but my query is how can i use in create table statement.
Too long for a comment.
This works for me.
Create table Member(
Member_Id Number(5) primary key,
Member_Name varchar2(30),
Member_address varchar2(50),
Acc_Open_Date Date,
Membership_type varchar2(20),
Fees_paid Number(4),
Max_Book_Allowed Number(2),
Penalty_Amount Number(7,2),
CONSTRAINT mt CHECK (Membership_type IN('Lifetime','Annual','Half Yearly','Quarterly'))
)

Cant create tables, sql error ORA-02270

Yeah, not too sure on this one. New to sql and I thought everything was done right, any ideas? If this is not the way to add foreign keys, could someone please explain to me how the correct way to do it is please? Would appreciate it, thanks.
CREATE TABLE customer (
reference NUMBER(5) PRIMARY KEY,
company_name VARCHAR2(30),
address VARCHAR2(30),
post_code VARCHAR2(10),
telephone VARCHAR(20),
contact_fname VARCHAR2(20),
contact_sname VARCHAR2(20),
contact_email VARCHAR2(30)
);
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1),
weight NUMBER(10)
);
CREATE TABLE category (
category NUMBER(1) PRIMARY KEY,
description VARCHAR2(15),
requirements VARCHAR2(30),
FOREIGN KEY (category) REFERENCES manifest(category)
);
CREATE TABLE trip (
trip_id NUMBER(10) PRIMARY KEY,
departure_date DATE,
return_date DATE,
vehicle_id VARCHAR2(10),
employee_no NUMBER(10),
FOREIGN KEY (trip_id) REFERENCES manifest(trip_id)
);
CREATE TABLE vehicle (
registration VARCHAR2(10) PRIMARY KEY,
vehicle_type_id VARCHAR2(10),
model VARCHAR2(15),
make VARCHAR2(15),
body VARCHAR2(15),
year NUMBER(4),
FOREIGN KEY (registration) REFERENCES trip(registration)
);
CREATE TABLE model (
vehicle_type_id VARCHAR(10) PRIMARY KEY,
make VARCHAR2(15),
model VARCHAR2(15),
FOREIGN KEY (vehicle_type_id) REFERENCES vehicle(vehicle_type_id)
);
CREATE TABLE driver (
employee_no NUMBER(10) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR(20),
ni_no VARCHAR2(15),
telephone VARCHAR2(20),
mobile VARCHAR2(12),
hazardous_goods VARCHAR2(1),
FOREIGN KEY (employee_no) REFERENCES trip(employee_no)
);
and the error message I get is
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I get this error for every table after manifest btw
The error message is pretty clear. The reference for a foreign key needs to be a unique or primary key in the other table. So this is wrong in the category table.
FOREIGN KEY (category) REFERENCES manifest(category)
I assume you intend for the manifest to look like this:
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10) REFERENCES trip(trip_id),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1) REFERENCES category(category),
weight NUMBER(10)
);
And so on for the other places where foreign keys are used. (This uses a short-hand notation for the foreign key reference; a separate clause in CREATE TABLE is also fine.)
Of course, the definition of manifest has to go after category and trip, in this example.
In other words, the reference goes in the other table, not where the primary key is defined.
Also, I would recommend being consistent with the names of the foreign keys. At the very least, they should generally have the same name as the primary key, where possible.
trip_id, vehicle_type_id, and employee_no all need to be marked as UNIQUE.

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.

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