How can I fix this error? there is no unique constraint matching given keys for referenced table "products" - sql

CREATE TABLE order_items
(
order_id int,
product_id int,
quantity int,
CONSTRAINT order_items_pk
PRIMARY KEY(order_id,product_id),
CONSTRAINT order_items_order_id_fk
FOREIGN KEY(order_id) REFERENCES orders(id),
CONSTRAINT order_items_product_id_fk
FOREIGN KEY(product_id) REFERENCES products(id)
);

To create this table, you will first have to create the tables mentioned in the foreign key declarations. Those tables are orders and products.
If you don't create those tables first, the table server cannot handle those constraints.

Related

Foreign key 'fk_orders_SalesRep' references invalid column 'snum' in referencing table 'orders'

What am I doing wrong? I'm trying to add a foreign key to an existing table
ALTER TABLE orders
ADD CONSTRAINT fk_orders_SalesRep
FOREIGN KEY (snum)
REFERENCES SalesRep(snum);
Table information
create table SalesRep(
snum varchar(5) primary key,
sname varchar(10),
hours Int
)
create table orders(
odId varchar(5) primary key,
itId varchar(5) foreign key(itId) references items(itId)
)
Like Martin said add the column first
ALTER TABLE orders
ADD snum varchar(5);
And then add the fk
ALTER TABLE orders
ADD CONSTRAINT fk_orders_SalesRep
FOREIGN KEY (snum)
REFERENCES SalesRep(snum);

Alter Foreign Key Constraint Primary Key Error

I'm trying to drop and recreate a foreign key constraint, but I get an error
There are no primary or candidate keys in the referenced table 'inventory' that match the referencing column list in the foreign key 'fkInventory_VendorsInventory'.
I have already gone into the table design for both tables referenced in the code, and ensured that the column being referenced is a primary key.
ALTER TABLE inventory_vendors
DROP CONSTRAINT fkInventory_VendorsInventory;
ALTER TABLE inventory_vendors
ADD CONSTRAINT fkInventory_VendorsInventory
FOREIGN KEY(itemnum) REFERENCES inventory(itemnum)
ON UPDATE CASCADE
ON DELETE CASCADE
I have done such a drop and recreation before with no problems at all with another set of tables (unfortunately i don't remember which tables they were).
As you mentioned in comments, you have 2 primary key columns in the Inventory table:
one is itemnum, the other is store_id
I prepare a sample SQL here: 2 tables created
CREATE TABLE inventory
(
itemnum INT,
store_id INT,
inventoryDesc char(200),
primary key (itemnum, store_id)
);
CREATE TABLE inventory_vendors
(
inventory_vendors int,
itemnum INT,
store_id INT,
VendorDetails varchar(200),
primary key (inventory_vendors)
);
Create Unique constraint for one of the primary key. Here I am creating UNIQUE constraint for itemnum column
ALTER TABLE inventory
ADD CONSTRAINT [IX_inventory] UNIQUE ( [itemnum] )
GO
Then execute your script for creating the foreign key constraint on inventory_vendors for itemnum column and you can drop them as well.
ALTER TABLE inventory_vendors
ADD CONSTRAINT fk_Inventory_Vendors_Inventory
FOREIGN KEY(itemnum) REFERENCES inventory(itemnum)
ON UPDATE CASCADE
ON DELETE CASCADE
ALTER TABLE inventory_vendors
DROP CONSTRAINT fk_Inventory_Vendors_Inventory;
Hope this might help you..

Required to Declare Primary Keys on Junction Table?

In a junction table made up of only foreign keys (e.g. ShopSupplier in the example below), must we also declare those columns as primary keys in the DDL create statement? If yes, what would be the point of doing so? I would argue that these are NOT primary keys in the example below
CREATE TABLE Shop
(
ShopID TEXT PRIMARY KEY UNIQUE,
ShopName TEXT
);
CREATE TABLE Supplier
(
SupplierID TEXT PRIMARY KEY UNIQUE,
SupplierName TEXT
);
CREATE TABLE ShopSupplier
(
ShopID TEXT,
SupplierID TEXT,
FOREIGN KEY(ShopID) REFERENCES Shop(ShopID),
FOREIGN KEY(SupplierID) REFERENCES Supplier(SupplierID)
);
The question again: Should I also declare PRIMARY KEY(ShopID, SupplierID) for the ShopSupplier table. If so, why? I would argue that there should be no primary key for that table.
Both columns are foreign key, because they are referencing to other tables. In the case that only single connection between specific rows of Shopand Supplier may exit, you can use a unique constraint or create a combined primary key:
CREATE TABLE ShopSupplier(
ShopID TEXT,
SupplierID TEXT,
PRIMARY KEY(ShopID, SupplierID),
FOREIGN KEY(ShopID) REFERENCES Shop(ShopID),
FOREIGN KEY(SupplierID) REFERENCES Supplier(SupplierID));
Then, both columns are foreign keys and belonging to the primary key.
In general setting the columns of a (pure) junction table as primary key isn't necessary. You may use it as a constraint to prohibit duplicates.

two columns referencing a single column in another table

A similar question is asked here multiple foreign keys referencing single column in other table
but the syntax is not shown in the answer. I would like to know how this can be accomplished in SQL server. The following syntax gives error
ALTER TABLE ItemIssue ADD CONSTRAINT FK_ItemIssue_Person
FOREIGN KEY (PersonID, AdvisorID) REFERENCES Person (PersonID)
;
ERROR: Number of referencing columns in foreign key differs from number of referenced columns, table 'ItemIssue'.
-- Create Tables
CREATE TABLE ItemIssue (
ItemIssueID int identity(1,1) NOT NULL,
PersonID int,
AdvisorID int,
)
;
CREATE TABLE Person (
PersonID int NOT NULL,
Name nvarchar(500),
)
;
You need to define two foreign keys, one for each column:
ALTER TABLE ItemIssue ADD CONSTRAINT FK_ItemIssue_Person
FOREIGN KEY (PersonID) REFERENCES Person (PersonID)
;
ALTER TABLE ItemIssue ADD CONSTRAINT FK_ItemAdvisor_Person
FOREIGN KEY (AdvisorID) REFERENCES Person (PersonID)
;
It is impossible to create one foreign key for two columns referencing one column. Create them seperate:
ALTER TABLE ItemIssue
ADD CONSTRAINT FK_ItemIssue_Person_Person FOREIGN KEY (PersonID) REFERENCES Person (PersonID),
ADD CONSTRAINT FK_ItemIssue_Advisor_Person FOREIGN KEY (AdvisorID) REFERENCES Person (PersonID);
To define two foreign keys, one for each column-
Table
Contract - HospidPharmacyId Column
Hospice- HospiceID PK
Pharmacy PharmacyId Pk
Using Following Query we can apply 2 Foreign Key for 1 column.
Alter Table Contract
Add Constraint fk_pharmacyID Foreign Key ([HospIDPharmID]) references Pharmacy([PharmacyID])
Alter TAble contract
Add Constraint Fk_hospId Foreign key ([HospIDPharmID]) references Hospice(HospiceID)
In the Contract Table for column-HospidPharmacyId we can insert common value in both the
tables. those which are present in hospice & not in Pharmacy then we cant insert that value in
contract table & vice versa.

SQL exception due to foreign key constraint trying to create tables

I was trying to create a set of tables using the following script:
DROP TABLE ORDERS IF EXISTS;
DROP TABLE INVOICE IF EXISTS;
CREATE TABLE ORDERS(
ORDER_ID BIGINT NOT NULL PRIMARY KEY IDENTITY,
...
INVOICE_ID BIGINT,
...
);
CREATE TABLE INVOICE(
INVOICE_ID BIGINT NOT NULL PRIMARY KEY IDENTITY,
...
ORDER_ID BIGINT,
...
FOREIGN KEY(ORDER_ID) REFERENCES ORDERS(ORDER_ID)
);
ALTER TABLE ORDERS ADD FOREIGN KEY(INVOICE_ID) REFERENCES INVOICE(INVOICE_ID);
This causes sql exception
java.sql.SQLException: is referenced by FOREIGN KEY constraint:
PUBLIC.INVOICE.SYS_FK_10079 in statement [ DROP TABLE ORDERS IF
EXISTS]
I couldn't figure out how to solve this ..I need invoiceId in ORDERS table and OrderID in INVOICE table as foreign keys..That is why I used the ALTER TABLE statement..Still ,it seems wrong..
(btw I am using hsqldb)
I would like to know how to do this properly..Any help most welcome..
Drop the foreign key on orders first:
ALTER TABLE ORDERS DROP FOREIGN KEY(INVOICE_ID) REFERENCES INVOICE(INVOICE_ID);
Then run your commands.