1

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"))
);
Peter
  • 27
  • 5
  • `ADD` is for `ALTER`. You do not need it in `CREATE`. See examples here: http://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13590.html#rrefsqlj13590 – PM 77-1 Nov 03 '16 at 18:04
  • Noted, thanks! I will edit my post. Still get this error message that I'm missing right parenthesis tho. – Peter Nov 03 '16 at 18:17
  • 3
    Strings are delimited by single quotes not double quotes. So "database" should be 'database` assuming you intend that to be a string literal. I suspect you want to create an out-of-line constraint so you'd want to have a comma after the `varchar(255)` and remove the `create` from `create constraint`. – Justin Cave Nov 03 '16 at 18:26
  • You're absolutely on point on everything. Solved, thank you! – Peter Nov 03 '16 at 18:40
  • 1
    @PM77-1 - Your link is actually to the documentation for JavaDB (another database that is owned by Oracle Corp) not to the Oracle documentation. It'll likely be similar but not identical. – Justin Cave Nov 03 '16 at 18:42
  • @JustinCave - right you are, it's Derby. – PM 77-1 Nov 03 '16 at 18:45
  • The very first example here: https://docs.oracle.com/cd/B28359_01/server.111/b28310/tables003.htm#ADMIN01503 – PM 77-1 Nov 03 '16 at 18:45
  • @Peter . . . You should probably delete the question. – Gordon Linoff Nov 03 '16 at 18:49
  • Unrelated, but: if you think there is some magic, hidden optimization for varchar columns with a length of 255 compared to one with e.g. 357 you are wrong. The maximum length for a varchar column has no influence whatsoever on the performance size on disk. The only time where this matters is when you using Oracle 12c's new "extended varchar" and go beyond 8000 bytes - that's stored as a CLOB internally and that is indeed much slower then a varchar that is defined with a shorter length –  Nov 03 '16 at 22:02

1 Answers1

0

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'))
);