Complete the code to create a foreign key constraint on the Orders table referencing Customers.
ALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (CustomerID) REFERENCES [1](CustomerID);The foreign key must reference the primary key of the Customers table to enforce referential integrity.
Complete the code to create a table with a foreign key that cascades on delete.
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
CONSTRAINT fk_customer FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE [1]
);ON DELETE CASCADE means when a customer is deleted, all their orders are also deleted automatically.
Fix the error in the foreign key constraint syntax.
ALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY [1] REFERENCES Customers(CustomerID);The foreign key column list must be enclosed in parentheses: (CustomerID).
Fill both blanks to enforce referential integrity with ON UPDATE CASCADE and ON DELETE SET NULL.
ALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON UPDATE [1] ON DELETE [2];
ON UPDATE CASCADE updates foreign keys when the referenced key changes. ON DELETE SET NULL sets the foreign key to NULL when the referenced row is deleted.
Fill all three blanks to create a table with a foreign key that restricts updates and deletes.
CREATE TABLE OrderDetails ( DetailID INT PRIMARY KEY, OrderID INT, ProductID INT, CONSTRAINT fk_order FOREIGN KEY (OrderID) REFERENCES Orders(OrderID) ON UPDATE [1] ON DELETE [2], CONSTRAINT fk_product FOREIGN KEY (ProductID) REFERENCES Products(ProductID) ON DELETE [3] );
ON UPDATE RESTRICT and ON DELETE RESTRICT prevent changes if dependent rows exist. ON DELETE NO ACTION also prevents deletion if related rows exist.