Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

DateAdd Function - Age restriction of a student based on birth_date

User_LLY1OJun 3 2021 — edited Jun 3 2021

Hi, I am working on my seminar thesis and I cannot get through with this problem: I have a table of students with a column birth_date and I only want to allow stdents UNDER 18 years old to be in my database. I want to use DateAdd function to do this or a trigger. Can you help me with how the CONSTRAINT should look like? I have come up with this but it doesn't seem to be working ("Missing expression" error returns).

CREATE TABLE student (
id_student INTEGER NOT NULL,
name VARCHAR2(50) NOT NULL,
birth_date DATE NOT NULL
);

ALTER TABLE student
ADD CONSTRAINT CheckAge CHECK (birth_date > DATEADD(date(), 'DD.MM.YYYY', 0, 0, -18)) ;

This should be added into the table:
INSERT INTO student (id_student, name, birth_date) VALUES ('1', 'John', '01.01.2012');

This should not be added into the table:
INSERT INTO student (id_student, name, birth_date) VALUES ('1', 'John', '01.01.2000');

Thanks a lot in advance for any help! (I am a beginner so although this may be trivial to you, it is not for me.)

Comments
Post Details
Added on Jun 3 2021
4 comments
1,205 views