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.
Related
As the title says, for some reason it gives me the error that the sport variable cant reference to another column when I am not even referencing it, any reason for this?
CREATE TABLE club_rates
(club_id NUMBER(4)
CONSTRAINT rates_club_fk REFERENCES club_subscriptiontypes(club_id,subscriptiontype)
,sport CHAR(6)
,subscriptiontype CHAR(11)
,subscription_startdate DATE
,subscription_rate_existing NUMBER(2)
,subscription_rate_new NUMBER(2)
,subscription_enddate DATE
,registration_startdate DATE
,registration_enddate DATE,
CONSTRAINT rates_clubsportsub_pk PRIMARY KEY (club_id, sport, subscriptiontype, subscription_startdate)
);
Assuming you have the table:
CREATE TABLE club_subscriptiontypes(
club_id NUMBER(4),
subscriptiontype CHAR(11),
PRIMARY KEY (club_id,subscriptiontype)
);
Then you do not want to declare a composite primary key column inline against a single column or you get the error:
ORA-02256: number of referencing columns must match referenced columns
Instead, declare it out-of-line at the end of the statement:
CREATE TABLE club_rates(
club_id NUMBER(4)
, sport CHAR(6)
, subscriptiontype CHAR(11)
, subscription_startdate DATE
, subscription_rate_existing NUMBER(2)
, subscription_rate_new NUMBER(2)
, subscription_enddate DATE
, registration_startdate DATE
, registration_enddate DATE
, CONSTRAINT rates_club_fk
FOREIGN KEY (club_id, subscriptiontype)
REFERENCES club_subscriptiontypes(club_id,subscriptiontype)
, CONSTRAINT rates_clubsportsub_pk
PRIMARY KEY (club_id, sport, subscriptiontype, subscription_startdate)
);
As an aside, you probably don't want to use fixed-length CHAR strings and, instead, want variable-length VARCHAR2 strings.
db<>fiddle here
I'm in a bit of a mix right now, I have a Table called Avatars, which has a foreign key called Family. Now, in the family table, I have two Foreign Keys called Mother and Father - Now this is the confusing bit, in both the Mother and Father Tables, there is a Foreign key called Avatar_ID which is of course the Primary Key to the Avatars table. I'm not sure if that's even allowed in SQL PLUS.
Whenever I try and enter the tables 'Family, Mother or Father', I keep getting the error:ORA-02291: integrity constraint (SG304.FK_FATHER_ID) violated - parent key not found.
Is there any way around this? Or will I have to change my code completely? A sample of the code is below.
CREATE TABLE Avatars (
Avatar_ID VARCHAR(10) NOT NULL,
Avatar_Name VARCHAR(30),
AvA_DOB DATE,
Age VARCHAR(30),
Gender VARCHAR(30),
Strength_Indicated INT,
Hoard INT,
Avatar_Level VARCHAR(30),
Skill VARCHAR(30),
Original_Owner VARCHAR(30),
Family_ID VARCHAR(10) NOT NULL,
Species_ID VARCHAR(10) NOT NULL,
Inventory_ID VARCHAR(30) NOT NULL,
Weapon_ID VARCHAR(10),
Player_ID VARCHAR(10) NOT NULL,
PRIMARY KEY (Avatar_ID));
CREATE TABLE Family (
Family_ID VARCHAR(10) NOT NULL,
Mother_ID VARCHAR(10) NOT NULL,
Father_ID VARCHAR(10) NOT NULL,
primary key(Family_ID)
);
CREATE TABLE Mother (
Mother_ID VARCHAR(10) NOT NULL,
Avatar_ID VARCHAR(10) NOT NULL,
primary key(Mother_ID)
);
CREATE TABLE Father (
Father_ID VARCHAR(10) NOT NULL,
Avatar_ID VARCHAR(10) NOT NULL,
primary key(Father_ID)
);
ALTER TABLE Avatars
ADD CONSTRAINT fk_Family_ID
FOREIGN KEY (Family_ID)
REFERENCES Family(Family_ID);
ALTER TABLE Family
ADD CONSTRAINT fk_Mother_ID
FOREIGN KEY (Mother_ID)
REFERENCES Mother(Mother_ID);
ALTER TABLE Family
ADD CONSTRAINT fk_Father_ID
FOREIGN KEY (Father_ID)
REFERENCES Father(Father_ID);
ALTER TABLE Father
ADD CONSTRAINT fk_Avatar_ID
FOREIGN KEY (Avatar_ID)
REFERENCES Avatars(Avatar_ID);
ALTER TABLE Mother
ADD CONSTRAINT fk_Avatars_ID
FOREIGN KEY (Avatar_ID)
REFERENCES Avatars(Avatar_ID);
INSERT INTO Avatars (Avatar_ID,Avatar_Name,AvA_DOB,Age,Gender,Strength_Indicated,Hoard,Avatar_Level, Skill, Original_Owner, Family_ID,Species_ID,Inventory_ID,Player_ID) VALUES
('Ava01','Verda','20-JAN-2014','1 year 2 months','Female','100','20','Master','Leader',' - ',' - ','DRA1','MasterInventory','Player07');
Thanks in advance for any help given! (:
Foreign key refers to a record in parent table. In your INSERT statement you are inserting value ' - ' into a column parent_id. In this error message oracle informs you that there is no record with value ' - ' in a column family_id of a table family. As I can understand, you are trying to use ' - ' as 'absence of value'. There is special value for that - NULL. So you need to write your statement as:
INSERT INTO Avatars (Avatar_ID, Avatar_Name, AvA_DOB,
Age, Gender, Strength_Indicated, Hoard, Avatar_Level, Skill,
Original_Owner, Family_ID, Species_ID, Inventory_ID, Player_ID)
VALUES ('Ava01', 'Verda', '20-JAN-2014', '1 year 2 months' ,'Female',
'100', '20', 'Master', 'Leader', NULL, NULL, 'DRA1',
'MasterInventory', 'Player07');
Also I can recommend some changes to your schema. First of all, use number data type for primary keys - it allows you to use sequences to generate unique values. Also, I don't know details of the problem, but "family relations" in your tables look a bit complicated. You can describe it in a single table:
create table family_tree (
person_id number primary key,
father_id number,
mother_id number,
sex char(1),
name varchar2(50),
family_name varchar2(50));
add constraint fk_mother_id foreign key (mother_id)
references family_tree (person_id);
add constraint fk_father_id foreign key (father_id)
references family_tree (person_id);
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
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.
I have the following SQL code
CREATE TABLE EMPLOYEES
(
empID NUMBER NOT NULL,
ssn CHAR(10) NOT NULL,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(2),
email VARCHAR(40),
street VARCHAR(40),
postalCode NUMBER,
city VARCHAR(20),
country VARCHAR(20),
job VARCHAR(20),
salary NUMBER(*,2),
birthdate DATE,
specialization VARCHAR(30),
username VARCHAR(25),
password VARCHAR(25),
levelOfClearance VARCHAR(20),
PRIMARY KEY (empID),
UNIQUE (ssn)
);
CREATE TABLE PATIENTS
(
patientID NUMBER NOT NULL,
healthInsID NUMBER,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(1),
email VARCHAR(40),
street VARCHAR(40),
postalCode CHAR(4),
city VARCHAR(20),
country VARCHAR(20),
PRIMARY KEY (patientID),
FOREIGN KEY (healthInsID) REFERENCES HEALTH_INSURANCES (healthInsID)
ON DELETE SET NULL
);
CREATE TABLE APPOINTMENTS
(
appointmentID NUMBER NOT NULL,
empID NUMBER NOT NULL,
appointmentDate DATE,
cost NUMBER(*,2),
patientID NUMBER,
PRIMARY KEY (appointmentID),
FOREIGN KEY (empID) REFERENCES EMPLOYEES (empID)
ON DELETE SET NULL,
FOREIGN KEY (patientID) REFERENCES PATIENTS
ON DELETE SET NULL
);
CREATE TABLE SYMPTOMS
(
appointmentID NUMBER NOT NULL,
description VARCHAR(100),
PRIMARY KEY (appointmentID),
FOREIGN KEY (appointmentID) REFERENCES APPOINTMENTS (appointmentID)
ON DELETE SET NULL
);
The first 3 tables are created without a problem, but the last one give me the error "ORA-02270: no matching unique or primary key for this column-list". I have searched a lot but could not find any solution yet.
Not sure but I think the problem probably is with last line in your symptoms table
CREATE TABLE SYMPTOMS
(
appointmentID NUMBER NOT NULL, <-- it's NOT NULL
description VARCHAR(100),
PRIMARY KEY (appointmentID),
**FOREIGN KEY (appointmentID) REFERENCES APPOINTMENTS (appointmentID)
ON DELETE SET NULL** <-- here you are saying -- set to null on delete
);
You are trying to null the appointmentID which you have declared as non null. This doesn't looks correct. I tried creating the same table with on delete cascade and it worked fine.
(OR)
Try creating your SYMPTOMS table like below
CREATE TABLE SYMPTOMS
(
appointmentID number NOT NULL,
description VARCHAR(100),
FK_APT_APTID number null,
PRIMARY KEY (appointmentID),
FOREIGN KEY (FK_APT_APTID) REFERENCES APPOINTMENTS (appointmentID)
ON DELETE set null
);
See this sample fiddle
http://sqlfiddle.com/#!3/826dd
The problem is with the SYMPTOMS table, your PK can be set to null (which is not possible)
I suggest you'll do the following:
Add a symptom ID column as a PK (or set the combination of description and appointmentID as PK)
Set on delete cascade or don't set a delete policy: this will cause an error if you delete an appointment and will raise an exception that you'll need to handle in your code.
sql fiddle: http://sqlfiddle.com/#!4/67d4b
CREATE TABLE SYMPTOMS
(
SymptomID NUMBER NOT NULL,
appointmentID NUMBER NOT NULL,
description VARCHAR(100),
PRIMARY KEY (appointmentID),
FOREIGN KEY (appointmentID) REFERENCES APPOINTMENTS (appointmentID)
);