when I execute my reservation table query, I get an error.
Any help?
This is my reservation table query
CREATE TABLE RESERVATION
(
NUMCHAMBRE INT FOREIGN KEY REFERENCES CHAMBRE (NUMCHAMBRE) ,
NUMHOTEL INT FOREIGN KEY REFERENCES HOTEL (NUMHOTEL),
NUMCLIENT INT FOREIGN KEY REFERENCES CLIENT (NUMCLIENT),
DATEARRIVE DATE,
DATEDEPART DATE,
PRIMARY KEY (NUMHOTEL, NUMCLIENT, DATEARRIVE)
);
This is the error I get:
Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table 'CHAMBRE' that match the referencing column list in the foreign key 'FK__RESERVATI__NUMCH__2BFE89A6'.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint or index. See previous errors.
According to your comment, the primary key on chambre is composite. So the foreign key reference needs to include all the columns:
CREATE TABLE RESERVATION (
NUMCHAMBRE int,
NUMHOTEL int Foreign Key REFERENCES HOTEL (NUMHOTEL),
NUMCLIENT int Foreign Key REFERENCES CLIENT (NUMCLIENT),
DATEARRIVE date,
DATEDEPART date,
foreign key (numhotel, numchambre) references chambre (numhotel, numchambre);
)
The column NUMCHAMBRE you're referencing in table CHAMBRE must be a primary key column, i.e. you can't just reference any column.
You can declare it as the primary key like this:
alter table CHAMBRE add primary key (NUMCHAMBRE);
Primary key columns need to be not null, so if NUMCHAMBRE is nullable, the above command will fail.
Update:
Based on your comment below, your table definition should be like this, i.e. you need to reference both columns of the primary key:
CREATE TABLE RESERVATION (
NUMCHAMBRE int,
NUMHOTEL int not null Foreign Key REFERENCES HOTEL (NUMHOTEL),
NUMCLIENT int not null Foreign Key REFERENCES CLIENT (NUMCLIENT),
DATEARRIVE date not null,
DATEDEPART date,
PRIMARY KEY (NUMHOTEL,NUMCLIENT,DATEARRIVE),
Foreign Key (NUMCHAMBRE,NUMHOTEL) REFERENCES CHAMBRE (NUMCHAMBRE,NUMHOTEL)
);
Note the additional not null constraints, as these will be necessary in order to create the primary key on this table using SQL Server.
Related
I'm creating a SQL table in VS that stores what rooms each client is, So the table has RoomId (int) and UserID (int).
Because I only want to add to the table only rooms and clients that exist they are both keys that have a foreign key to 2 tables, one that stores RoomID and Name and another that stores Client ID and Name.
Room and UserId tables:
CREATE TABLE [dbo].[UsersInRoomsTable]
(
RoomId INT NOT NULL,
UserId INT NOT NULL,
CONSTRAINT PK_RS PRIMARY KEY(RoomId, UserId),
CONSTRAINT [fk_room] FOREIGN KEY([RoomId]) REFERENCES [dbo].[RoomsTable]([RoomId]),
CONSTRAINT [fk_user] FOREIGN KEY ([UserId]) REFERENCES [dbo].[UserInfoTable] ([UserId])
);
Table that stores all the users:
CREATE TABLE [dbo].[UserInfoTable]
(
[UserName] NVARCHAR (50) NOT NULL,
[UserId] INT NOT NULL,
CONSTRAINT [PK_roomuser] PRIMARY KEY CLUSTERED ([UserName] ASC, [UserId] ASC)
);
Table that stores all the rooms
CREATE TABLE [dbo].[RoomsTable]
(
[RoomId] INT NOT NULL,
[RoomName] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([RoomId] ASC)
);
Everything works except the last line in the Rooms and users table:
CONSTRAINT [fk_user] FOREIGN KEY ([UserId]) REFERENCES [dbo].[UserInfoTable] ([UserId])
When I try to Update the table I get an error SQL71516:
SQL71516: The referenced table '[dbo].[UserInfoTable]' contains no primary or candidate keys that match the referencing column list in the foreign key.
If the referenced column is a computed column, it should be persisted
How can I solve this problem and what is causing it?
Edit: I think I know what is code is colliding: For some reason I can not have a foreign key connecting to a key that contains 2 indexes IE: fk_user is a Fk to table UserInfoTable that has 2 keys (UserID and UserName)
is there a way to pass this obstacle?
The columns of a foreign key have to match the columns they reference by number, type and order.
You have a primary key on userinfotable of (username, userid). But in usersinroomstable you are trying to let the foreign key (userid) to reference that. The number of columns doesn't match, so the foreign key cannot be added.
Presumably the username shouldn't really be part of the primary key of userinfotable and got there by accident. Remove it from the primary key constraint.
Or, if username has to be in the primary key, add such a column to the table usersinroomstable and add it to the foreign key constraint.
A very basic script is driving me crazy. It says it cant find a referenced key. Not idea what it's all about. I'm am using SQL SERVER 2014 and this script is for the creation of my database tables. I'm trying to make the id_TABLE_1 in the table TABLE_2 reference the id of the table TABLE_1.
CREATE TABLE TABLE_1
(
id int identity,
email varchar(50) not null,
constraint PK_TABLE_1 primary key (id,email)
)
GO
CREATE TABLE TABLE_2
(
id int identity,
id_TABLE_1 int not null,
constraint PK_TABLE_2 primary key (id),
constraint FK_TABLE_2 foreign key (id_TABLE_1)
references TABLE_1(id) on delete cascade
)
GO
The error is :
Msg 1776, Level 16, State 0, Line 32
There are no primary or candidate keys in the referenced table 'TABLE_1' that match the referencing column list in the foreign key 'FK_TABLE_2'.
Msg 1750, Level 16, State 0, Line 32
Could not create constraint or index. See previous errors.
Can you help me here ?
As per comment, you are trying to reference an index that doesn't exist. The primary key on your TABLE_1 table is a composite key containing two columns: id and email.
For this to compile, you can either alter your primary key to:
CONSTRAINT PK_TABLE_1 PRIMARY KEY (id)
or create a new index on just the id column:
CREATE INDEX IX_TABLE_1_id ON TABLE_1 (id);
It doesn't make sense to have a composite primary key that includes an identity column. The id column is already unique and non-NULL, so it should be the primary key.
If you want the email to be unique as well then remove it from the primary key and declare it as unique.
I have the following queries that I run to create tables in MS SQL Server:
CREATE TABLE menus
(
menu_id int NOT NULL PRIMARY KEY,
menu_name char,
other_details char
)
CREATE TABLE bookings
(
booking_Id int NOT NULL PRIMARY KEY,
date_booked DATE,
date_of_booking DATE,
other_details char,
staff_id int FOREIGN KEY REFERENCES staff(staff_id),
customer_id int FOREIGN KEY REFERENCES customers(customer_id)
)
CREATE TABLE menus_booked
(
menu_id INT NOT NULL,
booking_id INT NOT NULL,
CONSTRAINT PK_menus_booked PRIMARY KEY(menu_id,booking_id),
FOREIGN KEY (menu_id) REFERENCES menus(menu_id),
FOREIGN KEY (booking_id) REFERENCES bookings(booking_id)
)
CREATE TABLE menu_changes
(
change_id int NOT NULL PRIMARY KEY,
menu_id int NOT NULL,
booking_id int NOT NULL,
change_details char,
FOREIGN KEY (menu_id) REFERENCES menus_booked(menu_id),
FOREIGN KEY (booking_id) REFERENCES menus_booked(booking_id)
)
On running the last query I get the error:
There are no primary or candidate keys in the referenced table 'menus_booked' that match the referencing column list in the foreign key 'FK_menu_chan_menu'
I am unsure if my queries are correct and can't resolve this error.
The primary key of menus_booked is a unique combination of menu_id and booking_id. A foreign must point to that combination, not just one of its fields, which is not necessarily unique. Your query currently tries to define two foreign keys, one on each column, instead of one foreign key on the combination of the columns:
CREATE TABLE menu_changes
(
change_id int NOT NULL PRIMARY KEY,
menu_id int NOT NULL,
booking_id int NOT NULL,
change_details char,
FOREIGN KEY (menu_id, booking_id)
REFERENCES menus_booked(menu_id, booking_id) -- Here!
)
A foreign key has to reference a primary key (or unique key but here the PK is the problem), and it has to reference it in it's entirety.
FOREIGN KEY (menu_id) REFERENCES menus_booked(menu_id),
FOREIGN KEY (booking_id) REFERENCES menus_booked(booking_id)
You have two foreign key's referencing part of the primary key of menus_booked. You'll have to alter it to:
FOREIGN KEY (menu_id, booking_id) REFERENCES menus_booked(menu_id, booking_id)
This is my table 1:
CREATE TABLE PurchasedProducts
(
Purchase_Order_No int,
Purchase_Product_ID int FOREIGN KEY REFERENCES Inventory(In_Product_ID),
Purchase_Quantity int NOT NULL,
Purchase_Status varchar(7) NOT NULL,
PRIMARY KEY(Purchase_Order_No, Purchase_Product_ID)
);
This my table 2:
CREATE TABLE PurchasedDate
(
PD_PO_No int NOT NULL PRIMARY KEY FOREIGN KEY REFERENCES PurchasedProducts(Purchase_Order_No),
PD_Date date NOT NULL
);
I executed the first table successfully, but when I execute the second table It is showing this error message:
There are no primary or candidate keys in the referenced table 'PurchasedProducts' that match the referencing column list in the foreign key 'FK__Purchased__PD_PO__0B5CAFEA'.
I don't what is the problem is. Please help me!
The primary key in your PurchasedProducts table is made up of two columns:
PRIMARY KEY(Purchase_Order_No, Purchase_Product_ID)
So any child table that wants to reference that also must have these exact two columns:
CREATE TABLE PurchasedDate
(
PD_PO_No int NOT NULL PRIMARY KEY,
Purchase_Product_ID INT NOT NULL,
PD_Date date NOT NULL
);
ALTER TABLE dbo.PurchasedDate
ADD CONSTRAINT FK_PurchaseDate_PurchasedProducts
FOREIGN KEY(PD_PO_No, Purchase_Product_ID)
REFERENCES PurchasedProducts(Purchase_Order_No, Purchase_Product_ID)
A foreign key can only reference the whole primary key of a parent table - you cannot reference only one column out of 2 from the parent table's PK.
I'm using SQL Server 2008. I'm getting an error when I try to create the table 'Dossier_Financement':
create table Dossier_Financement
(
ID_Reunion integer foreign key references Reunion(ID_Reunion),
ID_Dossier integer foreign key references Dossier(ID_Dossier),
Decision varchar(20),
Motif text,
Montant_Retenu decimal(6,2),/* montant accorder */
Duree_Retenu smallint,/*nb jours accorder */
Nom_Giac varchar(50) foreign key references GIAC(Nom_Giac),
primary key(ID_Dossier,Nom_Giac,ID_Reunion)
)
GO
These are the two tables:
create table Reunion
(
ID_Reunion integer ,
Date_Reunion datetime,
ID_Membre integer,/*jquery*/
Type_Reunion varchar(20),
Nom_Giac varchar(50),
foreign key(ID_Membre,Nom_Giac) references Membre(ID_Membre,Nom_Giac),
primary key(ID_Reunion,Nom_Giac)
)
GO
create table Dossier_Financement
(
ID_Reunion integer foreign key references Reunion(ID_Reunion),
ID_Dossier integer foreign key references Dossier(ID_Dossier),
Decision varchar(20),
Motif text,
Montant_Retenu decimal(6,2),/* montant accorder */
Duree_Retenu smallint,/*nb jours accorder */
Nom_Giac varchar(50) foreign key references GIAC(Nom_Giac),
primary key(ID_Dossier,Nom_Giac,ID_Reunion)
)
GO
The 'Reunion' execute normally without any problem but I get this error when trying to create the second table:
Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'Reunion' that match the referencing column list in the foreign key 'FK__Dossier_F__ID_Re__5629CD9C'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
Depending upon the actual model needs/requirements, one solution is to reference the Key, which requires all parts (note that Nom_Giac is added to the FK definition):
create table Dossier_Financement
(
...
foreign key(ID_Reunion,Nom_Giac) references Reunion(ID_Reunion,Nom_Giac),
)
Another solution, as per Mark M's answer is to make the ID_Reunion column a Key (note that Nom_Giac is removed from the PK definition):
create table Reunion
(
...
primary key(ID_Reunion)
)
This will make ID_Reunion a Key (the Primary Key, in fact) which can then be referenced with foreign key references Reunion(ID_Reunion).
Happy coding!
You can only make a foreign key using the complete primary key of the table you are referencing. Your 2nd table tries to create a foreign key using only half of the Reunion table's primary key.
Create unique index on column you that you want to create index on:
CREATE UNIQUE NONCLUSTERED INDEX [UX_Index] ON dbo.[Table]([Column1],[Column2])