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.
Related
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
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#)
);
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.
CREATE TABLE Prescription (
pre_id NUMBER(10),
pssn CHAR(11),
phy_ssn CHAR(11),
date CHAR(11),
quantity INTEGER,
trade name CHAR(20),
pharm id CHAR(11),
drop_off_time CHAR(10),
pick_up_time CHAR(10),
status CHAR (15),
PRIMARY KEY(pre_id),
FOREIGN KEY (pssn) REFERENCES Pri_Phy_Patient,
FOREIGN KEY (phy_ssn) REFERENCES Doctor,
FOREIGN KEY (trade_name, pharm_id) REFERENCES Make_Drug);
I get ORA-00904:invalid identifier error on using the above sql statement.
The other sql statements related to the above one are:
CREATE TABLE Doctor (
phy_ssn CHAR(11),
name CHAR(20),
speciality CHAR(30),
exp_years CHAR(10),
PRIMARY KEY (phy_ssn));
CREATE TABLE Pri_Phy_Patient (
pssn CHAR(11),
name CHAR(20),
age INTEGER,
address CHAR(20),
phy ssn CHAR(11),
PRIMARY KEY (pssn),
FOREIGN KEY (phy_ssn) REFERENCES Doctor );
CREATE TABLE Make_Drug (
trade_name CHAR(20),
pharm_id CHAR(11),
formula VARCHAR(100)
PRIMARY KEY (trade_name, pharm_id),
FOREIGN KEY (pharm_id) REFERENCES Pharm_co);
Could anyone suggest on what I can do about this?!!
If you execute the statement in SQL*Plus, you can easily find the error and fix it.
SQL> CREATE TABLE Prescription (
2 pre_id NUMBER(10),
3 pssn CHAR(11),
4 phy_ssn CHAR(11),
5 date CHAR(11),
6 quantity INTEGER,
7 trade name CHAR(20),
8 pharm id CHAR(11),
9 drop_off_time CHAR(10),
10 pick_up_time CHAR(10),
11 status CHAR (15),
12 PRIMARY KEY(pre_id),
13 FOREIGN KEY (pssn) REFERENCES Pri_Phy_Patient,
14 FOREIGN KEY (phy_ssn) REFERENCES Doctor,
15 FOREIGN KEY (trade_name, pharm_id) REFERENCES Make_Drug);
date CHAR(11),
*
ERROR at line 5:
ORA-00904: : invalid identifier
You cannot use DATE as column name directly. Either keep it in "" double-quotation marks or better avoid using the keyword.
More issues with the create table script -
The column name cannot have two words, trade name is an invalid column name. Change it to trade_name.
Similarly, change pharm id to pharm_id.
Why do you use CHAR data type? It will always have blank padding to the right and consume space. Better use VARCHAR2 data type.
Change DATE column name with any other because you cannot use DATE as column name or table name. You can get more detail of reserved word of Oracle from following link http://www.petefreitag.com/tools/sql_reserved_words_checker/
This script works:
CREATE TABLE Doctor (
phy_ssn CHAR(11) not null,
name CHAR(20),
speciality CHAR(30),
exp_years CHAR(10),
constraint pk_phy_ssn PRIMARY KEY (phy_ssn));
CREATE TABLE Pri_Phy_Patient (
pssn CHAR(11) not null,
name CHAR(20),
age INTEGER,
address CHAR(20),
phy_ssn CHAR(11),
constraint pk_pssn PRIMARY KEY (pssn),
constraint fk_ppp_phy_ssn FOREIGN KEY (phy_ssn) REFERENCES Doctor(phy_ssn) );
CREATE TABLE Make_Drug (
trade_name CHAR(20) not null,
pharm_id CHAR(11) not null,
formula VARCHAR(100),
constraint pk_trname_phid PRIMARY KEY (trade_name, pharm_id));
CREATE TABLE Prescription (
pre_id NUMBER(10) not null,
pssn CHAR(11),
phy_ssn CHAR(11),
date_time CHAR(11),
quantity INTEGER,
trade_name CHAR(20),
pharm_id CHAR(11),
drop_off_time CHAR(10),
pick_up_time CHAR(10),
status CHAR(15),
constraint pk_pre_id PRIMARY KEY(pre_id),
constraint fk_pre_pssn FOREIGN KEY (pssn) REFERENCES Pri_Phy_Patient(pssn),
constraint fk_pre_phy_ssn FOREIGN KEY (phy_ssn) REFERENCES Doctor(phy_ssn),
constraint fk_pre_tr_nm_ph_id FOREIGN KEY (trade_name, pharm_id) REFERENCES Make_Drug(trade_name, pharm_id));
UPDATE
The issue is mostly due to spaces in the middle of column names, missing commas and the invalid identifier was raised because you tried to use date as a column name and Oracle didn't like it.
You can play around with this Demo.
date CHAR(11)
Date is a reserved word in SQL. It cannot be declared as a column in the way you did.
Please change the column name and it will work.
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