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

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.

Related

Can I create a foreign key and primary key of the same column in SQL?

I want to create a composite primary key for the Enrolment table from the (STDNO & CORSNO) columns which they are also a foreign key from Student and Course tables but it keeps giving me errors. also, the same thing happened in student table I need to let each student belong to a department.
Here's what I did:
CREATE TABLE Student
(
STDNO number(8),
SNAME varchar2(30) NOT NULL,
DEPTNO varchar(2),
CONSTRAINT PK_Studnet PRIMARY KEY(STDNO,DEPTNO),
FOREIGN KEY(DEPTNO) REFERENCES Department(DEPTNO)
);
CREATE TABLE Department
(
DEPTNO varchar2(2) PRIMARY KEY,
DNAME varchar2(20)
);
CREATE TABLE Course
(
CORSNO number(3) PRIMARY KEY,
CNAME varchar2(30),
DETNO varchar2(30),
CMAX number(2)
);
CREATE TABLE Enrolment
(
STDNO number(8),
CORSNO number(3),
GRADE number(2),
EDATE date date default CURRENT_TIMESTAMP,
CONSTRAINT PK_Enrolment PRIMARY KEY (STDNO, CORSNO),
FOREIGN KEY(STDNO) REFERENCES Student(STDNO),
FOREIGN KEY(CORSNO) REFERENCES Course(CORSNO)
);
A PRIMARY KEY should uniquely identify a thing it represents; in the case of a Student, we can assume that the student number should be unique to that student and should be the primary key by itself (as, it is assumed that, you are not going to issue the same student number to two students in different departments).
If you change your code so that STUDNO alone is the PRIMARY KEY and rearrange the orders of the tables so that the referenced tables are created before the tables that references them then you get:
CREATE TABLE Department
(
DEPTNO varchar2(2) PRIMARY KEY,
DNAME varchar2(20)
);
CREATE TABLE Course
(
CORSNO number(3) PRIMARY KEY,
CNAME varchar2(30),
DETNO varchar2(30),
CMAX number(2)
);
CREATE TABLE Student
(
STDNO number(8),
SNAME varchar2(30) NOT NULL,
DEPTNO varchar(2),
CONSTRAINT PK_Studnet PRIMARY KEY(STDNO),
FOREIGN KEY(DEPTNO) REFERENCES Department(DEPTNO)
);
CREATE TABLE Enrolment
(
STDNO number(8),
CORSNO number(3),
GRADE number(2),
EDATE date default CURRENT_TIMESTAMP,
CONSTRAINT PK_Enrolment PRIMARY KEY (STDNO, CORSNO),
FOREIGN KEY(STDNO) REFERENCES Student(STDNO),
FOREIGN KEY(CORSNO) REFERENCES Course(CORSNO)
);
(And also fix the typo where you have date date in the Enrolment table.)
Then the constraints in the Enrolment table work because each of the referential constraints references something unique. Before, you had said that the combination of both student number and department together was unique but were trying to reference just the student number which, by itself, was not unique.
db<>fiddle here

query not working for foreign key as a composite primary key of another table oracle SQL

I am trying to add two foreign keys for an associative entity in SQL Oracle. The primary key that I am referring from another table is a composite primary key. When I try to enter the SQL it says
no matching unique or primary key for this column-list
The code that I used to create the tbl_customer
CREATE TABLE tbl_Customer(
customer_id NUMBER(4)
CONSTRAINT pk_customer PRIMARY KEY,
CustomerName VARCHAR2(50) NOT NULL,
Telephone VARCHAR2(10),
CusEmail VARCHAR2(20),
CONSTRAINT cus_email UNIQUE(CusEmail),
location_id NUMBER(4)
CONSTRAINT fk_location_id references tbl_Location(location_id));
SQL to create tbl_Vehicle
CREATE TABLE tbl_Vehicle(
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT pk_v PRIMARY KEY(vehicle_id,PlateNo),
Brand VARCHAR2(20),
Model VARCHAR2(10),
TotalNoSeats NUMBER(2),
Class VARCHAR2(4) NOT NULL,
CONSTRAINT no_seats CHECK (TotalNoSeats<100),
driver_id NUMBER(4)
CONSTRAINT fk_driveridV references tbl_Driver(driver_id),
location_id NUMBER(4)
CONSTRAINT fk_locationV references tbl_Location(location_id));
The associative table is
CREATE TABLE tbl_Customer_Vehicle(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4)
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
);
where the error is in this line
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
*
Is this error because vehicle_id is a composite primary key?
Please help!!
You need to add PlateNo column in tbl_Customer_Vehicle table and define foreign key on it.
CREATE TABLE tbl_Customer_Vehicle
(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT fk_vehicle_id_PlateNo FOREIGN KEY(vehicle_id,PlateNo)
references tbl_Vehicle(vehicle_id,PlateNo)
);

error ORA-02270: no matching unique or primary key for this column-list

I'm practicing a lab manual excercise in which I have to create 6 tables. Creation of 5 is
successful.
But one line is giving error
constraint GRADE_Designation_FK
FOREIGN KEY(Designation) References EMPLOYEE(Designation),
ERROR at line 7:
ORA-02270: no matching unique or primary key for this column-list
Queries of 2 linked tables are
create table EMPLOYEE
(
Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
Name varchar2(10) not null,
Designation varchar2(50),
Qualification varchar2(10),
Joindate date
);
create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
constraint GRADE_Designation_FK FOREIGN KEY(Designation) References EMPLOYEE(Designation)
);
Tried
create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts)
);
create table EMPLOYEE
(
Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
Name varchar2(10) not null,
Designation varchar2(50) NOT NULL UNIQUE,
Qualification varchar2(10),
Joindate date default sysdate
constraint EMPLOYEE_Designation_FK FOREIGN KEY(Designation) References GRADE(Designation),
);
Now new error
constraint EMPLOYEE_Designation_FK FOREIGN KEY(Designation) References GRADE(Designation)
*
ERROR at line 8:
ORA-02253: constraint specification not allowed here
You've got the constraint on the wrong table. You should create a foreign key on EMPLOYEE.DESIGNATION, referencing back to GRADE.DESIGNATION.
So your tables should look something like:
create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
);
create table EMPLOYEE
(
Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
Name varchar2(10) not null,
Designation varchar2(50)
constraint EMPLOYEE_FK1
REFERENCES GRADE(DESIGNATION),
Qualification varchar2(10),
Joindate date
);
Share and enjoy.
ORA-02270: no matching unique or primary key for this column-list
That error is very self explanatory and tells you what's wrong. In your case, you are trying to create foreign key on a non primary key column and so the error
constraint GRADE_Designation_FK FOREIGN KEY(Designation)
References EMPLOYEE(Designation)
<--Here
Designation in EMPLOYEE table is not a primary key and you can't create FK on a non primary key column. Your table creation rather should look like
create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
employee_Empno number(4),
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
constraint GRADE_Designation_FK FOREIGN KEY(employee_Empno)
References EMPLOYEE(Empno));
It was the order of execution which has causes us this issue, make sure you have tables created with NOT NULL Enabled before adding those constraints to avoid this error.

ORA-02270 error while creating a table

This is my create table command for creating table employee but I can't figure out why I get this error even though I have declared ssn as primary key
CREATE TABLE EMPLOYEE(
F_NAME VARCHAR2(15) NOT NULL,
M_NAME CHAR(2),
L_NAME VARCHAR2(15) NOT NULL,
SSN CHAR(9) PRIMARY KEY,
BIRTHDAY DATE,
ADDRESS VARCHAR2(50),
SEX CHAR(1) CHECK(SEX IN('M','F','m','f')),
SALARY NUMBER(7) DEFAULT 800,
SSSN CHAR(9),
DEPARTMENT_NUMBER NUMBER(5),
CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN)
REFERENCES EMPLOYEE(SSSN) ON DELETE SET NULL,
CONSTRAINT EMP_DEPTNO_FK FOREIGN KEY(DEPARTMENT_NUMBER)
REFERENCES DEPARTMENT(DEPT_NO) ON DELETE CASCADE);
but I am getting error:
ORA-02270: no matching unique or primary key for this column-list
Change CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN) to CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSN) like this:
CREATE TABLE EMPLOYEE(
F_NAME VARCHAR2(15) NOT NULL,
M_NAME CHAR(2),
L_NAME VARCHAR2(15) NOT NULL,
SSN CHAR(9) PRIMARY KEY,
BIRTHDAY DATE,
ADDRESS VARCHAR2(50),
SEX CHAR(1) CHECK(SEX IN('M','F','m','f')),
SALARY NUMBER(7) DEFAULT 800,
SSSN CHAR(9),
DEPARTMENT_NUMBER NUMBER(5),
CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN) REFERENCES EMPLOYEE(SSN) ON DELETE SET NULL,
CONSTRAINT EMP_DEPTNO_FK FOREIGN KEY(DEPARTMENT_NUMBER) REFERENCES DEPARTMENT(DEPT_NO) ON DELETE CASCADE);
A foreign key can be declared in a table if and only if it is a primary key in another table.
What you need to do immediately is ensure that SSN and DEPARTMENT_NUMBER are Primary key in their respective tables.
Visit this link and you will easily find your error.
http://www.techonthenet.com/oracle/errors/ora02270.php
In case you do not follow, learn from
http://www.w3schools.com/sql/sql_foreignkey.asp
Hope it helps

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