Trouble adding Foreign Key Constraint (error ORA-02270: no matching unique or primary key for this column-list) - sql

I am learning how to create a small and simple database for class with Primary Key and Foreign Key restraints. I am getting ORA-02770 when attempting to run my ALTER TABLE statement, which as I understand is notifying me that the column I am referencing in the outside table is not listed as a primary key constraint. However, from what I can see my syntax is correct naming the primary keys in my CREATE TABLES statements. I searched inside the all_cons_columns table for the player_info table and it showed the primary keys listed as well. Could I get some guidance? Listed below is my current script:
CREATE TABLE Player_Game
( school varchar2(30),
player_number number(2,0),
game_number number(1,0),
CONSTRAINT playergame_pk PRIMARY KEY (school, player_number,game_number)
);
CREATE TABLE School
( school varchar2(30),
city varchar2(30),
coach varchar2(30),
team_name varchar2(30),
win_record number (2,0),
loss_record number (2,0),
CONSTRAINT school_pk PRIMARY KEY (school)
);
CREATE TABLE Game
( school varchar2(30),
game_number number(1,0),
game_date DATE,
game_score varchar2(15),
CONSTRAINT game_pk PRIMARY KEY (school, game_number)
);
CREATE TABLE player_info
( school varchar2(30),
player_number number(2,0),
player_name varchar2(25),
CONSTRAINT playerinfo_pk PRIMARY KEY (school, player_number)
);
CREATE TABLE city
( city varchar2(30),
population number(5,0),
CONSTRAINT city_pk PRIMARY KEY (city)
);
/*Here is the failing alter command */
ALTER TABLE Player_Game
ADD CONSTRAINT playergame_fk FOREIGN KEY (school) REFERENCES game(school);

You have incorrect column list in playergame_fk in the alter table statement. The column list of the foreign key must exactly match with the column list of the primary key it is referencing to.
Primary Key column list is school, game_number, therefore, your foreign key must have the same columns:
ALTER TABLE Player_Game
ADD CONSTRAINT playergame_fk FOREIGN KEY (school, game_number)
REFERENCES game(school, game_number);

Related

Foreign Key Reference

I addressed in class that the 2 foreign keys in the FlightData table are Depart_Code and Ariv_Code that there isn't any table to make references to them being a primary key in, in the relational schema we were given.
In class I was told that they reference Airport_Code in the Airport table. I was wondering I would go about doing that? I feel like I am missing something obvious. I appreciate any help offered I am still new to database in general and I am currently on Oracle 11g.
Airport table
CREATE TABLE Airport
(
Airport_Code VARCHAR2(7) CONSTRAINT pk_Airport Primary Key,
City_Code VARCHAR2(3),
CONSTRAINT fk_Airport_City_Code
FOREIGN KEY(City_Code) REFERENCES City,
Airport_Name VARCHAR2(30)
);
FlightData table:
CREATE TABLE FlightData
(
Flt_Nbr VARCHAR2(3) CONSTRAINT pk_FlightData Primary Key,
Depart_Code VARCHAR2(30),
Ariv_Code VARCHAR2(30)
);
To make sure Depart_Code and Ariv_Code always reference an airport in the Airport table you need to:
Make these columns NOT NULL.
Ensure they have the same data type as the key in Airport. Make them have a length of 7.
Add two foreign key constraints, each one based on each column.
For example, the second table could look like:
CREATE TABLE FlightData (
Flt_Nbr VARCHAR2(3) CONSTRAINT pk_FlightData Primary Key,
Depart_Code VARCHAR2(7) not null,
constraint fk1 foreign key (Depart_Code) references Airport (Airport_Code),
Ariv_Code VARCHAR2(7) not null,
constraint fk2 foreign key (Ariv_Code) references Airport (Airport_Code)
);

Creating a table with 2 primary keys and two foreign keys referring to 2 different tables with same constraint name

For this particular schema:
I have created the department student and subject tables now the problem right now arises in the creation of the mark table.
It is specified that there are 2 primary keys and 2 foreign keys , but as the title suggests creating 2 foreign keys referring to 2 different tables having the same constraint name seems impossible as per my understanding by surfing on the internet and reading on few threads here.
Is there any way to do this ? I want to have both the foreign key's constraint name to be "fk".
This code pops with a identifier error:
create table mark(
value number,
subject_id number,
student_id number,
constraint pk primary key(subject_id,student_id),
constraint fk foreign key(subject_id,student_id) references subject(subject_id,student_id));
But even if i create 2 constraints with different name test cases fail. Is there a solution?
This is department table
create table department(
department_id number(2),
department_name varchar(30),
department_block_number number,
constraint PK primary key(department_id));
This is student table
create table student(
student_id number,
student_name varchar(30),
address varchar(40),
city varchar(30),
department_id number,
constraint pk primary key(student_id),
constraint fk foreign key(department_id) references department(department_id));
This is staff table
create table staff(
staff_id number,
staff_name varchar(30),
department_id number,
constraint pk primary key(staff_id),
constraint fk foreign key(department_id) references department(department_id));
You have a composite primary key - on more than one column - which is fine; but you can't have a composite foreign key as the student table doesn't have a subject_id column - hence it giving you an invalid-identifier error.
You need two foreign keys, something like:
create table mark(
value number,
subject_id number,
student_id number,
constraint mark_pk primary key(subject_id,student_id),
constraint mark_fk_subject foreign key(subject_id) references subject(subject_id),
constraint mark_fk_student foreign key(student_id) references student(student_id));
The constraint names have to be unique, both within and across tables in the same schema, and something more descriptive than 'pk' or 'fk' would be sensible anyway.
db<>fiddle

How do I fix a ORA-00902: invalid datatype creating a table

I am creating a table in a command SQL sections into a script already populated I have created several tables already but in this one I get a message saying
ORA-00902: invalid datatype
CREATE TABLE Weapons
(
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2 (10),
CONSTRAINT pk_Weapons PRIMARY_KEY(id),
CONSTRAINT fk_Weapons_company
FOREIGN_KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo
FOREIGN_KEY(ammo_id) REFERENCES Ammo(id)
);
In the CONSTRAINT, it should be FOREIGN KEY and not FOREIGN_KEY. Also it should be PRIMARY KEY, not PRIMARY_KEY.
There is no underscore required as per syntax. So the query will be:
CREATE TABLE Weapons (
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2(10),
CONSTRAINT pk_Weapons PRIMARY KEY(id),
CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES Ammo(id)
);
About Foreign Keys: https://www.techonthenet.com/oracle/foreign_keys/foreign_keys.php
About Primary Keys: https://www.techonthenet.com/oracle/primary_keys.php
Here's a working example. I've created the AMMO table (whose description you didn't post, so I used only the ID column so that the foreign key constraint wouldn't fail). Pay attention to comments I wrote within the code.
SQL> create table ammo
2 ( id VARCHAR2(10),
3 CONSTRAINT pk_ammo PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Company
2 ( id VARCHAR(3),
3 name VARCHAR(30), --> switch from CHAR to VARCHAR2
4 CONSTRAINT pk_Company PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Weapons
2 ( id NUMBER(4),
3 name VARCHAR2(30),
4 damage NUMBER(4),
5 company_id VARCHAR2(3), --> should match COMPANY.ID datatype
6 ammo_id VARCHAR2(10), --> should match AMMO.ID datatype
7 CONSTRAINT pk_Weapons PRIMARY KEY(id),
8 CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
9 CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES ammo(id) );
Table created.
SQL>
In referential integrity constraint, you should match datatypes of the foreign and primary key columns. There's no sense in having a VARCHAR2(10) in the detail table which points to a VARCHAR2(3) column in the master table; you won't be able to put anything longer than 3 characters into the detail table's column anyway (foreign key constraint won't let you).

Creating a second table linked to my first one ORA-00904: : invalid identifier

i am trying to create a second table linked to the one i have just created name Company when i try to add a Foreign Key it doesnt let me
CREATE TABLE Weapons (
id NUMBER(11),
name VARCHAR2(20),
damage NUMBER(20),
company_id NUMBER(11),
CONSTRAINT pk_Weapons PRIMARY KEY(id),
CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
);

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!