I am a sql and mysql newbie, so I am not sure about how this works. I know that mysql doesn't recognise create assertion checks. But I am trying to do one for a boolean value to enforce a constraint for a particular Customer in my Customer Table.
I've been told that I can use CREATE TRIGGER for this, but haven't found a good enough example to see how this can be applied.
CREATE ASSERTION CHECK(
Contains_Nuts FROM Bagel
WHERE Contains_Nuts = TRUE
SELECT Name, DOB
FROM Customer WHERE Nut_Allergy = TRUE);
I know this question is quite vague and that the code is entirely wrong so let me know what I need to add to make it clearer.
As requested here are the Table designs that I want to apply,
CREATE TABLE Customer(
CustomerID INTEGER NOT NULL,
Name VARCHAR(255) NOT NULL,
Address_HouseNum VARCHAR(5),
Address_PostCode VARCHAR(8) NOT NULL,
Address_StreetName VARCHAR(255),
Title VARCHAR(6),
Email VARCHAR(255),
DOB DATE,
Order_History INTEGER,
Nut_Allergy Boolean,
PRIMARY KEY (CustomerID)
);
CREATE TABLE Bagel(
BagelID INTEGER NOT NULL,
Contains_Nuts BOOLEAN NOT NULL,
Price DECIMAL(4,2) NOT NULL,
Description VARCHAR(255),
PRIMARY KEY(BagelID)
);
CREATE TABLE `Order`(
OrderID INTEGER NOT NULL,
O_CustomerID INTEGER NOT NULL,
O_BagelCardID VARCHAR(16),
O_BagelID INTEGER,
O_BagelFillingID INTEGER NOT NULL,
O_DrinkID INTEGER,
Order_date DATE NOT NULL,
Order_Cost DECIMAL(4,2) NOT NULL, -- Assumption : Order_cost is the final price worked out after a discount has/hasnot been applied.
Discount_applied BOOLEAN,
PRIMARY KEY(OrderID,O_CustomerID,O_BagelCardID,O_BagelID,O_BagelFillingID,O_DrinkID),
FOREIGN KEY (O_CustomerID)
REFERENCES Customer(CustomerID)
ON DELETE RESTRICT
ON UPDATE CASCADE,
FOREIGN KEY (O_BagelCardID)
REFERENCES BagelCard(BagelCardID)
ON DELETE RESTRICT
ON UPDATE CASCADE,
FOREIGN KEY (O_BagelID)
REFERENCES Bagel(BagelID)
ON DELETE RESTRICT
ON UPDATE CASCADE,
FOREIGN KEY (O_BagelFillingID)
REFERENCES BagelFilling(BagelFillingID)
ON DELETE RESTRICT
ON UPDATE CASCADE,
FOREIGN KEY (O_DrinkID)
REFERENCES Drink(DrinkID)
ON DELETE RESTRICT
ON UPDATE CASCADE
);