Search code examples
sqloracleddlcheck-constraint

SQL: Check constraints syntax errors?


I'm trying to add a constraint to one of my columns, however i get this error message "missing right parenthesis". Not the first time I get this message, however I'm fairly new to SQL, so my syntax is not on par.

CREATE TABLE FAGFELT
(
bok varchar (255) PRIMARY KEY,
felt varchar (255) 
CREATE CONSTRAINT chk_felt CHECK (felt IN("databaser", "programmering", "matematikk", "statistikk", "kjemi", "fysikk"))
);

Solution

  • The create constraint is wrong, and string constants need to be supplied in single quotes '. Double quotes " are for identifiers

    CREATE TABLE FAGFELT
    (
       bok varchar (255) PRIMARY KEY,
       felt varchar (255), --<< you need a comma here
       CONSTRAINT chk_felt 
           CHECK (felt IN('databaser', 'programmering', 'matematikk', 'statistikk', 'kjemi', 'fysikk'))
    );