Primary & Foreign Key Constraints Confusion - sql

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

Related

keep getting the error code no matching unique or primary key for this column-list

I am just starting out with SQL and I have been attepmting to see where the issue is with my SQL below. ive etaken out "Date_Reserved" and was allowed to create the table. However I have made sure to create a primary for "Date Reserved" and it still dosent work
create table reservation
( Booking_Number varchar(8) not null,
Room_Number number(1,50) not null,
Date_Reserved date not null,
primary key (Booking_Number, Room_Number, Date_Reserved),
foreign key (Booking_Number) references booking(Booking_Number),
foreign key (Room_Number) references room(Room_Number)
);
create table additional_extra
( Booking_Number varchar(8) not null,
Room_Number number(1,50) not null,
Extra_ID varchar(8) not null,
Date_Reserved date not null,
primary key (Booking_Number, Room_Number, Extra_ID),
foreign key (Booking_Number) references booking(Booking_Number),
foreign key (Date_Reserved) references reservation(Date_Reserved),
foreign key (Room_Number) references room(Room_Number),
foreign key (Extra_ID) references extra(Extra_ID)
);
It keeps resulting in the Error code ""
Error report -
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
The foreign key for the additional_extra table
foreign key (Date_Reserved) references reservation(Date_Reserved)
does not reference all of the columns in the reservation table
primary key (Booking_Number, Room_Number, Date_Reserved)
Foreign keys may be defined as multiple columns. However, a composite foreign key must reference a composite primary or unique key with the same number of columns and the same data types.

Why getting error: No matching unique or primary key for this column-list, if I have the same columns?

I need to add a Foreign key to my Date_Reserved attribute in the Additional_extra Table, (Maybe because the Date type can't be unique?
I keep getting the next error:
Error report - 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
For The next Code:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Date_Reserved)
REFERENCES Reservation(Date_Reserved)
);
This is the part of the task that I need to create. So it is given that Date_Reserved must be FK:
Any Suggestions on what is wrong?
I tried to add FK when creating the Table, however when I find out that just the Date_Reserved FK line is wrong I created without that, but the error is still the same.
I tried from the GUI to add a Foreign key but same error
date_reserved is part of a composite primary key consisting of many columns; there is no unique or primary key that is solely on the date_reserved column.
Your constraint:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Date_Reserved)
REFERENCES Reservation(Date_Reserved)
);
Is trying to refer to a unique constraint that is solely on the date_reserved column and that does not exist so the SQL engine (correctly) raises the exception that such a constraint does not exist.
What you need to do is refer to the entire composite key:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Booking_Number, Room_Number, Date_Reserved)
REFERENCES Reservation(Booking_Number, Room_Number, Date_Reserved)
);
fiddle

How to have more than one foreign key?

I'm having trouble adding more than 1 foreign key to my tables, I am getting the error:
"Error report -
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 tried making the table without the third foreign key and it worked so I think its something to do with the last key but not really sure. (I put spaces in between the SQL to make it easier to read.)
CREATE TABLE ORDER(
ORDERID CHAR(4) NOT NULL,
HOUSETYPE CHAR(2),
CHAIR CHAR(2),
PERSON CHAR(2),
PAYDAY DATE,
MENUPRICE NUMBER(4,2),
CONSTRAINT CONFERENCESESSION_PK PRIMARY KEY(ORDERID),
CONSTRAINT CONFERENCESESSION_FK1 FOREIGN KEY(HOUSETYPE) REFERENCES
HOUSE(HOUSETYPE),
CONSTRAINT CONFERENCESESSION_FK2 FOREIGN KEY(PAYDAY) REFERENCES PERSON(PAYDAY),
CONSTRAINT CONFERENCESESSION_FK3 FOREIGN KEY(MENUPRICE) REFERENCES
MENU(MENUPRICE)
);
A foreign key must reference another table primary key, my guess is that some of you foreign keys don't reference the primary key of the other table.
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and
the table containing the candidate key is called the referenced or
parent table.
https://www.w3schools.com/sql/sql_foreignkey.asp
A foreign key must reference another table primary key so make sure you are doing that with the right name of the primary key. For example:
CONSTRAINT CONFERENCESESSION_FK2 FOREIGN KEY(PAYDAY) REFERENCES PERSON(PAYDAY)
Make sure that in table PERSON you have PRIMARY KEY with name PAYDAY.

How to add foreign key to an existing column in SQL Server 2012

I am trying to add foreign key to my existing column using below query
ALTER TABLE Sub_Category_Master
ADD FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
but I'm getting an error
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Sub_Categ__Categ__5812160E". The conflict occurred in database "shaadikarbefikar_new", table "shaadikarbefikar.Category_Master", column 'Category_ID'.
Well, the error clearly tells you that Category_ID in your Sub_Category_Master table contains some values that are not present in Category_Master (column Category_ID). But that's exactly the point of having a foreign key constraint - making sure your child table (Sub_Category_Master) only uses defined values from its parent table.
Therefore, you must fix those "voodoo" values first, before you're able to establish this foreign key relationship. I would also strongly recommend to explicitly name that constraint yourself, to avoid those system-generated, but not really very useful constraint names like FK__Sub_Categ__Categ__5812160E:
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FK_SubCategoryMaster_CategoryMaster
FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FKSub_Category_Master_Category_ID FOREIGN KEY (Category_ID)
REFERENCES Category_Master(Category_ID);
CREATE TABLE Orders
(
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

There are no PK in the referenced table. Why?

I have the following T-SQL to create 3 SQL tables:
create table dbo.Posts
(
Id int identity not null
constraint PK_Posts_Id primary key clustered (Id),
Active bit not null
constraint DF_Posts_Active default (0)
);
create table dbo.PostsLocalized
(
Id int not null,
Culture int not null
constraint CK_PostsLocalized_Culture check ([Culture] in ('1', '2', '3')),
[Text] nvarchar (200) not null,
constraint PK_PostsLocalized_Id_Culture primary key clustered (Id, Culture)
);
create table dbo.Tags
(
Id int identity not null
constraint PK_Tags_Id primary key clustered (Id),
Name nvarchar not null
);
create table dbo.PostsLocalized_Tags
(
PostLocalizedId int not null,
TagId int not null,
constraint PK_PostsLocalized_Tags_Post_PostLocalizedId_TagId primary key clustered (PostLocalizedId, TagId)
);
Then I have added the following constraints:
alter table dbo.PostsLocalized
add constraint FK_PostsLocalized_Id foreign key (Id) references dbo.Posts(Id) on delete cascade on update cascade;
alter table dbo.PostsLocalized_Tags
add constraint FK_PostsLocalized_Tags_PostLocalizedId foreign key (PostLocalizedId) references PostsLocalized(Id) on delete cascade on update cascade,
constraint FK_PostsLocalized_Tags_TagId foreign key (TagId) references Tags(Id) on delete cascade on update cascade;
But I get the following error:
There are no primary or candidate keys in the referenced table 'PostsLocalized' that match the referencing column list in the foreign key 'FK_PostsLocalized_Tags_PostLocalizedId'.
How can I solve this?
Thank You,
Miguel
SQL Server mandates that foreign key references be to a primary key or unique key. The foreign key reference has to be to all the columns that constitute the primary/unique key. The documentation says:
In a foreign key reference, a link is created between two tables when
the column or columns that hold the primary key value for one table
are referenced by the column or columns in another table. This column
becomes a foreign key in the second table.
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY
KEY constraint in another table; it can also be defined to reference
the columns of a UNIQUE constraint in another table. A FOREIGN KEY
constraint can contain null values; however, if any column of a
composite FOREIGN KEY constraint contains null values, verification of
all values that make up the FOREIGN KEY constraint is skipped. To make
sure that all values of a composite FOREIGN KEY constraint are
verified, specify NOT NULL on all the participating columns.
The primary key in PostsLocalized contains the culture column, so you need to add it into the foreign key reference.
Your PK on PostsLocalized table is complex consisting of two columns - id and culture and you are trying to create FK on only one of those columns which is not possible.
You'll have to either add Culture column on PostsLocalized_Tags and use them both in foreign key or remove Culture from your PK on PostLocalized