I keep getting errors on my code SQL Oracle - sql

So I've been working on this code for a few days now and I can't figure out why I'm getting errors. These are the errors I'm getting:
SQL Error: ORA-00904: "PATIENTID_FK": invalid identifier
00904. 00000 - "%s: invalid identifier"
and
SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint"
I've included the code that I wrote. The "..." symbolizes not important information from the table I was creating, such as "DoctorFirstName". Can someone help me figure out where I'm messing up? Thank you!
DROP TABLE HealthRecord;
DROP TABLE Patient;
DROP TABLE Insurance;
DROP TABLE Doctor;
DROP TABLE Hospital;
DROP TABLE Prescription;
CREATE TABLE Insurance (
InsuranceID number,
...
CONSTRAINT InsuranceID_pk
PRIMARY KEY (InsuranceID));
CREATE TABLE Prescription (
PrescriptionID number,
...
CONSTRAINT PrescriptionID_pk
PRIMARY KEY(PrescriptionID));
CREATE TABLE Hospital (
HospitalID number,
...
CONSTRAINT HospitalID_pk
PRIMARY KEY(HospitalID));
CREATE TABLE Doctor (
DoctorID number,
...
HospitalID number,
CONSTRAINT DoctorID_pk
PRIMARY KEY(DoctorID),
CONSTRAINT HospitalID_fk
FOREIGN KEY (HospitalID)
REFERENCES Hospital (HospitalID));
CREATE TABLE Patient (
PatientID number,
...
InsuranceID number,
CONSTRAINT PatientID_pk
PRIMARY KEY (PatientID),
CONSTRAINT InsuranceID_fk
FOREIGN KEY (InsuranceID)
REFERENCES Insurance(InsuranceID));
CREATE TABLE HealthRecord(
RecordID number,
...
DoctorID number,
PrescriptionID number,
PatientID number,
CONSTRAINT RecordID_pk
PRIMARY KEY(RecordID),
CONSTRAINT DoctorID_fk
FOREIGN KEY (DoctorID)
REFERENCES Doctor (DoctorID),
CONSTRAINT PrescriptionID_fk
FOREIGN KEY (PrescriptionID)
REFERENCES Prescription (PrescriptionID),
CONSTRAINT PatientID_fk
FOREIGN KEY (PatientID_fk)
REFERENCES Patient(PatientID));

Just dropping the table doesn't remove the constraints. So you constraints still exist. You should drop the constraints and indexes. Check this link out:
https://www.1keydata.com/sql/alter-table-drop-constraint.html
Reference the link below for dropping tables and the constraint in one statement (CASCADE CONSTRAINTS)
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9003.htm

Related

Create table with foreign key to an other table created below in sql file

My problem is that i have two tables with each table having a foreign key to the other table.
Each time , i execute the SQL file containing the creation of the two tables, it gives me an error that he doesn't find the other table. I'm working with sqlplus to execute the sql file.
Here's an example of SQL file i tried with :
create table A(
Age number(3),
name number(3) constraint A_FK references B(name))
/
create table B(
Age number(3) constraint B_FK references A(Age),
name number(3))
And even if i reverse the order, it gives the same error.
Thanks for help.
This is a problem of cycles in foreign keys. One method is to add all foreign keys after table creation (as I think the other answers propose).
You can also just do that for the first table:
create table A (
Age number(3) primary key,
name number(3)
);
create table B (
name number(3) primary key,
Age number(3),
constraint B_FK foreign key (age) references A(Age)
);
alter table B add constraint A_FK foreign key (name) references B(name);
Here is a db<>fiddle.
Notes:
Foreign keys should reference primary keys, so I added that declaration as well.
I recommend making the primary key the first column in the table.
You can also define the constraint inline for one of the tables (i.e. age number(3) constraint b_fk references a(age)).
The table column(s) that is referred by a foreign key must exist at the time when the constraint is created. Since you have some kind of cyclic reference between the tables, you need to do this in three steps:
first create one table without the foreign key
create the second table (with its foreign key)
finally add the foreign key to the first table with an alter table statement
You also need the referred column to have a unique or primary key constraint set up, otherwise you would get error ORA-02270: no matching unique or primary key for this column-list.
create table A(
age number(3) primary key,
name number(3)
);
create table B(
age number(3) constraint B_FK references A(Age),
name number(3) primary key
);
alter table A add constraint A_FK foreign key (name) references B(name);
Demo on DB Fiddle
Side note: I am quite suspicious about your sample structure, but this could be because your oversimplified it in the question.
It fails because the reference table doesn't exist yet.
Create the tables without the key first. Then drop one and recreated it with the reference. Then drop the 2nd and recreate it with the reference.
Create table first and then ADD the CONSTRAINT
ALTER TABLE A
ADD FOREIGN KEY (name) REFERENCES B(name);
ALTER TABLE B
ADD FOREIGN KEY (age) REFERENCES A(age);

(Oracle) Adding a constraint after a table has been made

I'm relatively new to Databases and Oracle and I'm atempting to make a database for a project involving 2 tables; Klant(customer) and Account_. These 2 both have each others PK as a foreign key. Since this is the case I had to make the foreign keys after the creation of the tables, resulting in this
DROP TABLE KLANT CASCADE CONSTRAINTS;
DROP TABLE ACCOUNT_ CASCADE CONSTRAINTS;
CREATE TABLE KLANT
(
"KlandId" INT PRIMARY KEY,
"AccountId" INT,
"Voornaam" VARCHAR2(64)NOT NULL,
"Achternaam" VARCHAR2(64) NOT NULL,
"GENDER" CHAR(1) DEFAULT 'M' CHECK (UPPER(GENDER) in ('M','F')),
"Tussenvoegsels" VARCHAR2(16),
"EmailAdres" VARCHAR2(64),
"Land" VARCHAR2(64) DEFAULT 'Nederland',
"Stad" VARCHAR2(64),
"Adres" VARCHAR2(64),
"Toevoeging" CHAR(1));
CREATE TABLE Account_
(
"AccountId" INT PRIMARY KEY,
"KlantId_" INT,
"GebruikersNaam" VARCHAR2(64)UNIQUE NOT NULL,
"Wachtwoord" VARCHAR2(64)
);
ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY (AccountId) REFERENCES "Account_"(AccountId);
ALTER TABLE Account_
ADD CONSTRAINT fk_klantId FOREIGN KEY (KlantId_) REFERENCES "Klant"(KlantId);
This yielded me the following error:
Error starting at line : 39 in command -
ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY (AccountId) REFERENCES "Account_"(AccountId)
Error report -
SQL Error: ORA-00904: "ACCOUNTID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Sorry for all the dutch words :(
My question being: Where did I mess up, because it probably is something riduculously stupid.
ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY ("AccountId") REFERENCES Account_("AccountId");
Name of the Account_ Table should not be in double quotes. Same for the other table .
Quote AccountId. In Oracle objects are generally uppercase. When you use AccountId without quoting it, Oracle will look for ACCOUNTID.

Oracle SQL Database error: "no matching unique or primary key for this column-list"

I'm trying to set up a database in Oracle sql developer, I've got these 3 tables.
I need the table "GuyAddress" to have 3 primary keys, which are all foreign keys as well. This is where I'm running into an error which I can't get my head around.
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
"number" NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number")
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
This is the error, hopefully someone can spot the mistake I made because I can't...
Error starting at line : 18 in command -
CREATE TABLE "GuyAddress"
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
)
Error report -
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
Thanks!
You don't need separate foreign keys for each column in the referenced table's primary key - you can have multiple columns in a foreign key, e.g.:
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
address_number NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, address_number)
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address FOREIGN KEY(Address_zipcode, Address_number) REFERENCES Address(zipcode,address_number),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
Note that I've updated your address.number column to be address.address_number, as it's not recommended that you use a column based on a keyword. Using doublequotes to get around this (and also enforce case sensitivity) is not recommended either; you'll have to remember to use them every time you reference that column!
As an aside, I assume that your address table has other columns? Because as things stand, the address table is pointless and could be skipped!

Primary & Foreign Key Constraints Confusion

currently trying to apply foreign keys to my created tables but I'm getting the SQL error:
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'm at a massive loss on where to go from here, I think everythings in order but I just can't point out what's wrong.
Here are the relavent create and alter scripts i'm using if anybody can point out where i'm going wrong:
Creation of Results & DopingTest:
CREATE TABLE Results
(
RaceID NUMBER,
HorseID NUMBER,
JockeyID NUMBER,
Position numeric(2)
);
CREATE TABLE DopingTest
(
RaceID NUMBER,
HorseID NUMBER,
TakenBy varchar2(60)
);
And the adding of constraints:
ALTER TABLE Results
ADD(
CONSTRAINT pk_ResultsID
PRIMARY KEY (RaceID,HorseID));
ALTER TABLE DopingTest
ADD(
CONSTRAINT pk_DopingTest
PRIMARY KEY (RaceID, HorseID));
ALTER TABLE Results
ADD(
CONSTRAINT fk_raceID
FOREIGN KEY (RaceID)
REFERENCES Race(RaceID),
CONSTRAINT fk_horseID
FOREIGN KEY (HorseID)
REFERENCES Horse(HorseID),
CONSTRAINT fk_JockeyID
FOREIGN KEY (JockeyID)
REFERENCES Jockey(JockeyID));
ALTER TABLE DopingTest
ADD(
CONSTRAINT fk_RaceIDDT
FOREIGN KEY (RaceID)
REFERENCES Results(RaceID),
CONSTRAINT fk_HorseIDDT
FOREIGN KEY (HorseID)
REFERENCES Results(HorseID));
Any help would be greatly appreciated, thanks.
If results has a composite primary key, the foreign key would have to reference both components of the key.
ALTER TABLE DopingTest
ADD(
CONSTRAINT fk_RaceIDDT
FOREIGN KEY (RaceID, HorseID)
REFERENCES Results(RaceID, HorseID)
);

Invalid Field Definition

I'm trying to create a table with a foreign key using SQL command but I keep getting this error
Invalid field definition 'CUS_CODE' in definition of index or relationship.
I'm using this command:
CREATE TABLE INVOICE(
INV_NUMBER CHAR(5) NOT NULL,
INV_DATE DateTime NOT NULL,
CONSTRAINT INV_PK PRIMARY KEY(INV_NUMBER),
CONSTRAINT INV_FK FOREIGN KEY(CUS_CODE) REFERENCES CUSTOMER(CUS_CODE)
);
It's because Cus_code is not a field in your invoice table.
It should be :
FOREIGN KEY(Your column name in your invoice table) REFERENCES Customer(Cus_Code)
Here's an tutorial about SQL FOREIGN KEY
It should help you to illustrate what it should looks like