1

I am looking for a possibility to use two possibilities for one attribute. Here an example:

CREATE TABLE person (
 name VARCHAR(200), 
 age INTEGER CONSTRAINT adult 
 CHECK (age >= 18)
)

Here the code will call a Person adult if the age is over 18, but now I would like the to person is called child if he is under 18.

CREATE TABLE  Person (
Id INTEGER NOT NULL PRIMARY KEY,
power_level INTEGER NOT NULL,
evilness_level INTEGER NOT NULL,
name VARCHAR(256) NOT NULL UNIQUE,
)

or here if the person has an evilness_level of < 0 it's a super hero, otherwise it is a super villain.

have a good day!

1 Answer 1

1

You can try to create a TRIGGER like

CREATE TRIGGER trig_evilness_level_check BEFORE INSERT ON Person 
FOR EACH ROW 
BEGIN 
IF NEW.evilness_level < 0 THEN 
SET NEW.Name = 'super hero';
ELSE
SET NEW.Name = 'super villain';
END IF; 
END

Similarly you can create the Trigger for the age check also.

The reason why it is suggested to create a TRIGGER instead of a CHECK constraint is clear from the manual which says:

The CHECK clause is parsed but ignored by all storage engines.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.