MySQL Forums
Forum List  »  Newbie

How to implement this constraint
Posted by: Paul Reiser
Date: July 17, 2015 04:38PM

I'm wondering if there is a table-definition solution to this problem, rather than a programming one. I have a bunch of balls that are colored either red, green or blue. I have data on all of them (same fields), but I have extra data (different fields) on only the red balls. I want to assure that the Ball_BallId in table RedBall in fact only maps to a BallId in table Ball whose Color is "Red". These are example tables, but without the constraint being automatic.

CREATE TABLE `Ball` (
`BallId` INT NOT NULL,
`BallData` VARCHAR(45) NULL,
PRIMARY KEY (`BallId`))

CREATE TABLE `RedBall` (
`RedBallId` INT NOT NULL,
`Ball_BallId` INT NOT NULL,
`RedBallData` VARCHAR(45) NULL,
PRIMARY KEY (`RedBallId`, `Ball_BallId`),
INDEX `fk_RedBall_Ball_idx` (`Ball_BallId` ASC),
CONSTRAINT `fk_RedBall_Ball` FOREIGN KEY (`Ball_BallId`) REFERENCES `Ball` (`BallId`)

Options: ReplyQuote


Subject
Written By
Posted
How to implement this constraint
July 17, 2015 04:38PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.