I am completely new to databases and am having some confusion with Oracle syntax. I am trying to create a table with integrity constraints to accompany it.
Here is my code:
CREATE TABLE Students
(
id INTEGER,
name CHAR(10) NOT NULL,
classification CHAR(10) NOT NULL,
hours INTEGER,
gpa NUMBER(3,2) NOT NULL,
mentor INTEGER,
CONSTRAINT IC1 CHECK (PRIMARY KEY (id))
CONSTRAINT IC2 CHECK (classification = 'freshman' or classification = 'sophomore' or classification = 'junior' or classification = 'senior')
CONSTRAINT IC3 CHECK (gpa >= 0 AND gpa <= 4)
CONSTRAINT IC4 CHECK (classification = 'junior' AND hours >= 55 AND hours <= 84) );
However whenever I run the code I get:
SQL> CONSTRAINT IC1 CHECK (PRIMARY KEY (id))
SP2-0734: unknown command beginning "CONSTRAINT..." - rest of line ignored.
SQL> CONSTRAINT IC2 CHECK (classification = 'freshman' or classification = 'sophomore' or classification = 'junior' or classification = 'senior')
SP2-0734: unknown command beginning "CONSTRAINT..." - rest of line ignored.
SQL> CONSTRAINT IC3 CHECK (gpa >= 0 AND gpa <= 4)
SP2-0734: unknown command beginning "CONSTRAINT..." - rest of line ignored.
SQL> CONSTRAINT IC4 CHECK (classification = 'junior' AND hours >= 55 AND hours <= 84) );
SP2-0734: unknown command beginning "CONSTRAINT..." - rest of line ignored.
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.
Can someone help me with my constraint syntax?
There is a syntax error in the declaration of the primary key constraint. A primary key constraint is not a check constraint so you don’t want the
CHECKkeyword. You also need commas after each inline constraint definitionDepending on the front-end tool you are using and the settings that tool has, you may want to avoid having completely blank lines in the middle of your SQL statements. It’s perfectly valid syntax from a SQL perspective. But some front ends will interpret the blank line as ending the prior statement.