ORA-02270 error while creating a table - sql

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

Related

Oracle SQL: Receiving 'no matching unique or primary key' error and don't know why

I'm receiving this error when trying to create a table and I don't know why:
[2016-07-05 14:08:02] [42000][2270] ORA-02270: no matching unique or primary key for this column-list
This question seems different (to me) from a similar question, because in that question the OP is referencing a table with a composite PK, while I am not.
And while this other question has the same error code, it is because the OP is incorrectly references the primary key, which I don't think I did.
May someone more experienced in SQL educate me?
(A couple of things to note: 1) I know the table/column names have small errors/deviations from convention, but this is for homework, and the teacher requires I have the tables and rows declared exactly his way, even if it's non-conventional. 2) Yes, that's silly; but no, I can't change it or I get marked down.)
CREATE TABLE Student_Course
(
Stu_ID NUMBER(5) NOT NULL,
Course_ID VARCHAR2(8) NOT NULL,
Section# NUMBER(3),
CONSTRAINT pk_stu_crse PRIMARY KEY (Stu_ID, Course_ID),
CONSTRAINT fk_course_id FOREIGN KEY (Course_ID) REFERENCES course(Course_ID),
CONSTRAINT fk_stu_id FOREIGN KEY (Stu_ID) REFERENCES student(Stu_ID),
CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#)
)
There are only two, small, referenced tables, which are:
CREATE TABLE student
(
Stu_ID NUMBER(5) PRIMARY KEY ,
Lname VARCHAR2(20),
Fname VARCHAR2(20),
Mi CHAR(1),
Sex CHAR(1),
Major VARCHAR2(15),
Home_State CHAR(2)
);
CREATE TABLE course
(
Course_ID VARCHAR2(8) PRIMARY KEY ,
Section# NUMBER(3),
C_Name VARCHAR2(30),
C_Description VARCHAR2(30)
);
A foreign key is a reference to a primary key in another table.
The last constraint CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#) won't work - Section# isn't a primary key in that table
Section# Must be at least UNIQUE in course table.
If you want to use Section# as a reference for a foreign key, it must be a UNIQUE or a PRIMARY KEY
More information about FOREIGN KEY and constraints
Thanks to good answers, I'm posting my code which I corrected based on help here. Hope my corrections help others in the future.
CREATE TABLE student
(
Stu_ID NUMBER(5) PRIMARY KEY ,
Lname VARCHAR2(20),
Fname VARCHAR2(20),
Mi CHAR(1),
Sex CHAR(1),
Major VARCHAR2(15),
Home_State CHAR(2)
);
CREATE TABLE course
(
Course_ID VARCHAR2(8) ,
Section# NUMBER(3) ,
C_Name VARCHAR2(30),
C_Description VARCHAR2(30),
CONSTRAINT pk_course PRIMARY KEY (Course_ID, Section#)
);
CREATE TABLE Student_Course
(
Stu_ID NUMBER(5) ,
Course_ID VARCHAR2(8) ,
Section# NUMBER(3) ,
CONSTRAINT pk_stu_crse PRIMARY KEY (Stu_ID, Course_ID, Section#),
CONSTRAINT fk_stu FOREIGN KEY (Stu_ID) REFERENCES student(Stu_ID),
CONSTRAINT fk_course_id FOREIGN KEY (Course_ID, Section#) REFERENCES course(Course_ID, Section#)
);

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.

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.

SQL Error: ORA-00911: Invalid Character

I'm an new to Oracle and I'm trying to create a table but I keep getting an invalid character warning. I've tried deleting it and retyping it and have checked for any of the invalid characters and I can't seem to find any.
My table creation code is:
CREATE TABLE DEPARTMENT (
DNUMBER CHAR(1) PRIMARY KEY,
DNAME VARCHAR2(20),
MGRSSN CHAR(11),
MGRSTARTDATE CHAR(10),
CONSTRAINT DEPARTMENT_FK FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN));
CREATE TABLE EMPLOYEE (
SSN CHAR(11) PRIMARY KEY,
FNAME VARCHAR2(20),
MINIT CHAR(1),
LNAME VARCHAR2(20),
BIRTHDATE CHAR(10),
ADDRESS VARCHAR2(30),
SEX CHAR(1),
SALARY INTEGER,
SUPERSSN CHAR(11),
DNO CHAR(1),
CONSTRAINT EMPLOYEE_FK1 FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE (SSN),
CONSTRAINT EMPLOYEE_FK2 FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNUMBER));
I have faced the same problem in SQLplus.
I have to copy some create table sql from the other files which worked before and change it , the changed sql could work fine.
After compare this two files in Eclipse, there is difference only in white space . It seems that something wrong in the file character format.
As far as I can tell there's no "bad character" issue here, as I couldn't provoke the error as originally reported. However, a couple of the constraints on these tables - DEPARTMENT_FK on DEPARTMENT, and EMPLOYEE_FK1 on EMPLOYEE - are causing issues. The DEPARTMENT_FK constraint attempts to reference the EMPLOYEE table before it has been created. In the second case, the self-referential EMPLOYEE_FK1 constraint fails to be created because the EMPLOYEE table doesn't exist when the CREATE TABLE for EMPLOYEE is executed.
To work around this these constraints should be removed from the table definition. The tables should then be created, and the constraints added out-of-line:
CREATE TABLE DEPARTMENT (
DNUMBER CHAR(1) PRIMARY KEY,
DNAME VARCHAR2(20),
MGRSSN CHAR(11),
MGRSTARTDATE CHAR(10)
--CONSTRAINT DEPARTMENT_FK FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN)
);
CREATE TABLE EMPLOYEE (
SSN CHAR(11) PRIMARY KEY,
FNAME VARCHAR2(20),
MINIT CHAR(1),
LNAME VARCHAR2(20),
BIRTHDATE CHAR(10),
ADDRESS VARCHAR2(30),
SEX CHAR(1),
SALARY INTEGER,
SUPERSSN CHAR(11),
DNO CHAR(1),
--CONSTRAINT EMPLOYEE_FK1 FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE (SSN),
CONSTRAINT EMPLOYEE_FK2 FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNUMBER));
ALTER TABLE DEPARTMENT
ADD CONSTRAINT DEPARTMENT_FK FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN);
ALTER TABLE EMPLOYEE
ADD CONSTRAINT EMPLOYEE_FK1 FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE (SSN);
Done this way the tables will be created with the desired constraints.
dbfiddle here
Best of luck.

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