PSQL Error there is no unique constraint matching given keys for referenced table - sql

I have a baseball database. I'm getting the error Error there is no unique constraint matching given keys for referenced table pitcher.
Here's the schema
CREATE TABLE Teams(
Name varchar(30) NOT NULL Primary Key,
Record varchar(10)
);
CREATE TABLE Player(
Name varchar(30) NOT NULL,
Num int NOT NULL,
TeamName varchar(22) references Teams(Name),
PRIMARY KEY(Name, Num, TeamName),
Constraint NumCheck Check (Num < 100 and Num > -1)
);
CREATE TABLE Pitcher(
PHanded varchar(10),
PName varchar(30),
PTeamName varchar(30),
PNum int,
PRIMARY KEY(PName, PTeamName, PNum),
foreign key (PName, PTeamName, PNum) references Player(Name, TeamName, Num)
);
CREATE TABLE PosPlayer(
PPHanded varchar(10),
Position varchar(2),
PPName varchar(30),
PPNum int,
BTeamName varchar(30),
PRIMARY KEY(PPName, PPNum, BTeamName),
foreign key (PPName, PPNum, BTeamName) references Player(Name, Num, TeamName)
);
CREATE TABLE Games(
Id int Primary Key,
SchedDate DATE,
PlayedDate DATE,
HomeName varchar(30),
VisitName varchar(30),
Winner varchar(30)
);
CREATE TABLE Pitches
(
PID int,
Outcome varchar(10),
Bcount int,
Scount int,
Runners int,
Type varchar(10),
Speed int,
Pitchnum int,
PitName varchar(30) references Pitcher(PName),
PitNum int references Pitcher(PNum),
PitTeamName varchar(30) references Pitcher(PTeamName),
BatName varchar(30) references PosPlayer(PPName),
BatNum int references PosPlayer(PPNum),
BatTeamName varchar(30) references PosPlayer(BTeamName),
HomeTName varchar(30),
VisitTName varchar(30),
GId int references Games(Id),
foreign key (PitName, PitNum, PitTeamName) references Pitcher (PName, PNum, PTeamName),
foreign key (BatName, BatNum, BatTeamName) references PosPlayer (PPName, PPNum, BTeamName),
Primary Key(PID, PitName, PitNum, PitTeamName, BatName, BatNum, BatTeamName, GID),
Constraint Balls Check (Bcount > -1 and Bcount < 4),
Constraint Scount Check (Scount > -1 and Scount < 3)
);
In the other threads I looked up it was suggested to add a unique identifier, but in this schema it is possible to have the same name and be on the same team. How do I fix this?

The lines
CREATE TABLE Pitches
(
{...}
PitName varchar(30) references Pitcher(PName),
PitNum int references Pitcher(PNum),
PitTeamName varchar(30) references Pitcher(PTeamName),
BatName varchar(30) references PosPlayer(PPName),
BatNum int references PosPlayer(PPNum),
BatTeamName varchar(30) references PosPlayer(BTeamName),
{...}
)
are almost certainly the ones causing you grief. You're trying in each case to reference a nonunique column in the referenced table, and that just won't work.
The good news is that you don't need these references clauses, as they're taken care of by the table-level FOREIGN KEY clauses, each of which references a group of columns whose concatenated values are unique, due to being a composite primary key.

Related

ERROR :ORA-02264: name already used by an existing constraint

create table ward
(
wnum int primary key,
wname varchar(30),
phoneno int,
wloc varchar(50),
chnursename varchar(20) constraint ward_fk references charge_nurse
);
create table charge_nurse
(
chnurse varchar(20) constraint charge_nurse_pk primary key,
stnum int constraint charge_nurse_fk references staff
);
create table staff
(
stname varchar(20),
stnum int constraint staff_pk primary key,
addr varchar(20),
phoneno int,
stposition varchar(30),
specality varchar(30) unique,
shift varchar(10),
noofhoursperweek int
);
create table generalsupplies
(
itnum int constraint generalsupplies_pk primary key,
itname varchar(20) unique,
quantityinstock int,
reorder varchar(10),
despt varchar(10),
costperunit int
);
create table pharmasupplies
(
dnum int constraint pharmasupplies_pk primary key,
dname varchar(30) unique,
despt varchar(20),
dosage_Mg int,
quantityinstock int,
reorder varchar(10),
costperunit int
);
While creating the below table I am facing problem:
ORA-02264: name already used by an existing constraint
create table centralsupplies
(
wardnum int constraint centralsupplies_fk references ward,
itemnum int constraint centralsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_fk references staff,
regnum int unique,
dateord date,
daterec date
);
How do I solve this problem?
You use 3 times the same constraint name centralsupplies_fk in your centralsupplies table.
3 constraints = 3 constraint names
Your create table statement has four foreign keys all called centralsupplies_fk. That is not allowed: constraint names must be unique within a schema. You must give each one a different name.
It is usual practice to include the referenced table in the key name. So
create table centralsupplies
(
wardnum int constraint centralsupplies_ward_fk references ward,
itemnum int constraint centralsupplies_generalsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_pharmasupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_staff_fk references staff,
regnum int unique,
dateord date,
daterec date
)
Also you have another foreign key constraint on STAFFNAME which you have not named. You do not need to name constraints, the system will generate a unique one for you, but it's generally a good idea to name them, not least because it is easier to diagnose relational integrity error messages with meaningfully named constraints.
However, in this case the correct solution is to drop the STAFFNAME column. You already have a foreign on the STAFF table, and you should join to that table whenever you need to display a value for STAFFNAME. Besides you do not have a unique constraint on staff.stname column, so the foreign key statement will fail: foreign keys can only reference primary or unique keys.

SQL ALTER TABLE foreign key

I have a problem with adding foreign keys with alter table command. I don't know how to make it so it works.
I need to add ISIK_ID and STAADION_ID to ISIK_STAADIONIL table as foreign key.
Here is my code:
CREATE TABLE ISIK(
ISIK_ID INT NOT NULL,
EESNIMI VARCHAR(25) NOT NULL,
PEREKONNANIMI VARCHAR(25) NOT NULL,
ISIKUKOOD VARCHAR(20),
KODAKONDSUS VARCHAR(30),
SUGU CHAR(1) NOT NULL,
HARIDUSTASE CHAR(1) NOT NULL,
TELEFONI_NR VARCHAR(20),
SYNNIPAEV DATE,
CONSTRAINT ISIK_ID_PK PRIMARY KEY (ISIK_ID)
);
CREATE TABLE ISIK_STAADIONIL(
ISIK_STAADIONIL_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_STAADIONIL_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP
);
CREATE TABLE STAADION(
STAADION_ID INT NOT NULL,
NIMETUS VARCHAR(20),
KIRJELDUS VARCHAR(100),
ASUKOHT VARCHAR(50),
SUURUS VARCHAR(20),
MAHUTAVUS INT,
EHITATUD VARCHAR(20),
EHITAJA VARCHAR(20),
CONSTRAINT STAADION_ID_PK PRIMARY KEY (STAADION_ID)
);
ALTER TABLE ISIK_STAADIONIL
ADD CONSTRAINT ISIK_ID_FK
FOREIGN KEY(ISIK_ID)
REFERENCES ISIK(ID);
You need to add the columns first (to the table) before you can create FK constraints using them.
CREATE TABLE ISIK_STAADIONIL(
ISIK_STAADIONIL_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_STAADIONIL_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP,
ISIK_ID INT,
STAADION_ID INT
);
And then your ALTER statement should work fine.
UPDATE
Changing the table-design slightly would make more sense, as per following. The logic is that ISIK_ID and STAADION_ID would together be sufficient to determine uniqueness in the ISIK_STAADIONIL table, so a separate ID is redundant:
CREATE TABLE ISIK_STAADIONIL(
ISIK_ID INT NOT NULL,
STAADION_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_ID, STAADION_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP
);

Error when creating table in Oracle Database 11g SQL

I'm new to SQL and I'm trying to create a new table however I get an error when I run my script in the SQL command line, the errors I'm getting are either ORA-00942 missing right parenthesis or ORA-00942 table or view does not exist.
Oh and yes I know I have probably written some terrible script but as mentioned earlier I'm learning so any meaningful criticism would be appreciated along with some help :).
CREATE TABLE Branch
(
Branch_ID varchar(5),
Branch_Name varchar(255),
Branch_Address varchar(255),
Branch_Town varchar(255),
Branch_Postcode varchar(10),
Branch_Phone varchar(50),
Branch_Fax varchar(50),
Branch_Email varchar(50),
Property_ID varchar(5),
Contract_ID varchar(5),
Staff_ID varchar(5),
PRIMARY KEY (Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id)
);
CREATE TABLE Staff
(
Staff_ID varchar(5),
Staff_Forename varchar(255),
Staff_Surname varchar(255),
Staff_Address varchar(255),
Staff_Town varchar(255)
Staff_Postcode varchar(10),
Staff_Phone varchar(50),
Staff_DOB varchar(50),
Staff_NIN varchar(10),
Staff_Salary varchar(50),
Staff_Date_Joined varchar(100),
Staff_Viewing_Arranged varchar(100),
Branch_ID varchar(5),
Sales_ID varchar(5),
PRIMARY KEY (Staff_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Sales_ID) REFERENCES Sales(Sales_Id)
);
CREATE TABLE Sales
(
Sales_ID varchar(5),
Property_Address varchar(255),
Property_Town varchar(255)
Property_Postcode varchar(10),
Property_Type varchar(255),
Num_Rooms varchar(50),
Date_of_Sale varchar(10),
Sales_Bonus varchar(100),
Branch_ID varchar(5),
Property_ID varchar(5),
Staff_ID varchar(5)
Seller_ID varchar(5),
PRIMARY KEY (Sales_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_Id)
);
CREATE TABLE Contract
(
Contract_ID varchar(5),
Contract_Signed_Date varchar(50),
Property_ID varchar(5),
Buyer_ID varchar(5),
Seller_ID varchar(5),
Branch_ID varchar(5),
PRIMARY KEY (Contract_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_Id),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_Id)
);
CREATE TABLE Buyer
(
Buyer_ID varchar(5),
Viewing_Data varchar(255),
Maximum_Budject varchar(255),
Purchase_Price varchar (50),
Buyer_Forename varchar(255),
Buyer_Surname varchar(255),
Buyer_Address varchar(255),
Buyer_Town varchar(255),
Buyer_Postcode varchar(10),
Property_ID varchar(5),
Contract_ID varchar(5),
Survey_ID varchar(5),
PRIMARY KEY (Buyer_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id),
FOREIGN KEY (Survey_ID) REFERENCES Survey(Survey_Id)
);
CREATE TABLE Seller
(
Seller_ID varchar(5),
Seller_Forename varchar(255),
Seller_Surname varchar(255),
Seller_Address varchar(255),
Seller_Town varchar(255),
Seller_Postcode varchar(10),
Seller_Property_ID varchar(5),
No_of_Bed varchar(5),
Contract_ID varchar(5),
Property_ID varchar(5),
Sales_ID varchar(5),
PRIMARY KEY (Seller_ID),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Sales_ID) REFERENCES Sales(Sales_Id)
);
CREATE TABLE Property
(
Property_ID varchar(5),
Property_Address varchar(255),
Property_Town varchar(255),
Property_Postcode varchar(10),
Asking_Price varchar(20),
Date_Registered varchar(50),
Property_Fixtures varchar(255),
Size_of_Rooms varchar(100),
Buyer_ID varchar(5),
Staff_ID varchar(5),
Contract_ID varchar(5),
Seller_ID varchar(5),
PRIMARY KEY (Property_ID),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_ID),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_ID),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id)
);
CREATE TABLE Survey
(
Survey_ID varchar(5),
No_of_Survey varchar(10),
Survey_Type varchar(255),
Organised_By varchar(255),
Property_ID varchar(5),
Staff_ID varchar(5),
Buyer_ID varchar(5),
PRIMARY KEY (Survey_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_Id)
);
CREATE TABLE Advert
(
Survey_ID Advert_ID varchar(5),
No_of_Adverts varchar(10),
Advert_Website varchar(255),
Advert_Newspaper varchar(255),
Property_ID varchar(5),
PRIMARY KEY (Advert_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID)
);
STAFF Table miss a , at the end
Staff_Town varchar(255)
Sales Table too
Property_Town varchar(255)
Staff_ID varchar(5)
Also you can't define constrains to table not created yet. I could found the error removing those constrain.
Can't define the foreign key references until the table being referred to is created. So, if the foreign key references are circular, in that A => B => C => A, they you will first have to create the tables, then use ALTER TABLE to define the foreign keys. Otherwise, create the tables in order, where first you create a table, then create the table that is referencing it.
Your script references tables that haven't been created yet. I.e., look at your first create table. by that time, STAFF and other tables don't exist yet, but you're setting them as foreign key references.
You should create your tables first, and then apply constraints after the dependencies are in place, using alter:
ALTER TABLE BRANCH
ADD CONSTRAINT fk_staff_id
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id);
because you have tables that references each other you need create the references after the tables are created
--create table Buyer
CREATE TABLE Buyer
(
Buyer_ID varchar(5),
Property_ID varchar(5),
PRIMARY KEY (Buyer_ID)
);
--create table Property
CREATE TABLE Property
(
Property_ID varchar(5),
Buyer_ID varchar(5),
PRIMARY KEY (Property_ID)
);
--now you can set your reference constraints
ALTER TABLE Property Add FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_ID);
ALTER TABLE Buyer Add FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID);
Your syntax is not clear on your keys.
Here is an example of what a create table with a primary key and a foreign key (fk) would look like:
CREATE TABLE animals (
animal_id NUMBER(10) PRIMARY KEY,
animal_name VARCHAR2(50) NOT NULL,
animal_species_code NUMBER(50) NOT NULL
CONSTRAINT fk_animal_species REFERENCES animal_species
(species_code));
It appears that the modeling in this set of tables might benefit from some additions and changes.
As an example, the STAFF table references the BRANCH table - but BRANCH also references STAFF. This means that BRANCH can only reference one STAFF, and vice versa, which I suspect is not what's wanted. It appears that you want a many-to-many relationship between BRANCH and STAFF, and the standard solution for enabling this is a junction table - one which contains the primary keys of both tables. Such a junction table between BRANCH and STAFF might look like:
CREATE TABLE BRANCH_STAFF
(BRANCH_ID VARCHAR(5)
CONSTRAINT BRANCH_STAFF_FK1
REFERENCES BRANCH(BRANCH_ID),
STAFF_ID VARCHAR(5)
CONSTRAINT BRANCH_STAFF_FK2
REFERENCES STAFF(STAFF_ID),
CONSTRAINT PK_BRANCH_STAFF
PRIMARY KEY (BRANCH_ID, STAFF_ID)
USING INDEX);
Best of luck.

ORA-00904: : invalid identifier two references in Oracle

I cant find a good anwser too my question, I want to create a table that have references too one table but i use it twice in the code..
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer)
But then i get the error:
ORA-02256: number of referencing columns must match referenced columns
I understand that my reference is wrong some how but can´t figure it out...
Sorry for the swedish words!!
Create table Ärende(
Ärendenr VARCHAR(50) NOT NULL,
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
In Datum VARCHAR(50),
Ömmande Skäl VARCHAR(5),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
PRIMARY KEY (Ärendenr)
);
I have used Person(Personnumer) as a reference before just not twice in createing a table..
I created this script which does work. Probably there is an issue with your indexes:
create table handläggare
( anställningsnr varchar(50) primary key
)
create table person
( personnummer varchar(11) primary key
)
;
create table förskola
( idnr varchar(50) primary key
);
create table ärende
( ärendenr varchar(50) not null
, handläggare varchar(50)
, foreign key (handläggare) references handläggare(anställningsnr)
, vårdnadshavare varchar(11)
, foreign key (vårdnadshavare) references person(personnummer)
, barn varchar(11)
, foreign key (barn) references person(personnummer)
, indatum varchar(50)
, ömmandeskäl varchar(5)
, förskola varchar(50)
, foreign key (förskola) references förskola(idnr)
, primary key (ärendenr)
);
There are two other issues in your script above which I edited:
In Datum should be one name, not separated by a space;
Ömmande Skäl, same as above.
In you create statement you have provided 4 referenes:
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
Please check that all 3 distinct columns are of same data type.
That means:
Handläggare(Anställningsnr) should be VARCHAR(50)
Person(Personnummer) should be VARCHAR(11)
Förskola(IDnr) should be VARCHAR(50).
As per me i think you are making a mistake in
Förskola(IDnr) should be VARCHAR(50)
which should have been a Number

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

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)
);