Question

Here is a very basic table to illustrate my question.

CREATE TABLE Customer (
CustID         INT,
CustLastName   VARCHAR (20),
ReferralID     INT,
ADD CONSTRAINT PRIMARY KEY (CustID),
ADD CONSTRAINT FOREIGN KEY (ReferralID) REFERENCES Customer(CustID)
);

My current code ensures that any only former customers who have CustID's can be in the ReferralID column (i.e. they told the customer about the store.) However, the problem is nothing is stopping CustID from equaling ReferralID in the same row, which is obviously impossible. A customer cannot tell themself about the store.

Basically, how do I stop CustID and ReferralID from having the same value in the same row?

Thank you, Andrew

Was it helpful?

Solution

To do this you would want a CHECK constraint. However MySQL hasn't implemented CHECK constraints yet, so you can use a trigger instead.

Related

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top