SQL: Check constraints syntax errors? - sql

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

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

Related

SQL unterminated quoted string \i: missing required argument

I'm trying to run this script with the command "\ i c: /users/public/giro_d'italia2014.sql" as I did with other scripts only that this time sql seems not to do something right ...
this is what it says: unterminated quoted string
\ i: missing required argument ..... I tried to look for this problem but I could not find anything .... can you help me please?
this is the script ...
DROP SCHEMA IF EXISTS giro_d'italia2014 CASCADE;
CREATE SCHEMA giro_d'italia2014;
SET search_path TO giro_d'italia2014;
CREATE TABLE ciclista(
id_ciclista INTEGER PRIMARY KEY,
nome ciclista VARCHAR ,
squadra CHAR(3),
nazione CHAR(3));
CREATE TABLE tappa(
nome_tappa VARCHAR PRIMARY KEY,
km INT,
tipologia {'pianeggiante'|'alta montagna'|'media montagna'|'cronometro a squadre'|'cronometro individuale'|'cronoscalata'} );
CREATE TABLE ordine_arrivo(
id_ciclista INT,
nome_tappa VARCHAR ,
ordine INT,
PRIMARY KEY (id_ciclista, nome_tappa));
You can't include a single quote ' in an identifier unless you use double quotes:
DROP SCHEMA IF EXISTS "giro_d'italia2014" CASCADE;
CREATE SCHEMA "giro_d'italia2014";
SET search_path TO "giro_d'italia2014";
But I strongly recommend to use identifiers that do not need to be quoted.
this
tipologia {'pianeggiante'|'alta montagna'|'media montagna'|'cronometro a squadre'|'cronometro individuale'|'cronoscalata'} );
is also invalid SQL. I don't know what you are trying to achieve with that. if you are trying to limit the values that are allowed, use a check constraint:
(
...
tipologia text not null,
check tipologia in ('pianeggiante', 'alta montagna', 'media montagna', 'cronometro a squadre', 'cronometro individuale', 'cronoscalata')
);
The use of the char data type is also discouraged

Missing Right Parenthesis in SQL

I'm new to learning SQL. When I create this table, it has an Asterix (*) under the first parenthesis of the "(dbClassID)" and says "missing right parenthesis"
Does anyone know why it does that and how I can fix it?
CREATE TABLE vod_classification (
dbClassId CHAR(4) NOT NULL,
dbDescription VARCHAR2(100)
CONSTRAINT vod_classification_PK PRIMARY KEY (dbClassId)
);
CONSTRAINT is part of table creation and need to be comma delimited as other column:
CREATE TABLE zz_classification (
dbClassId CHAR(4) NOT NULL,
dbDescription VARCHAR2(100),
CONSTRAINT vod_classification_PK PRIMARY KEY( dbClassId)
);
Tables contain columns and constraints
you are missing , here try this VARCHAR2(100),
For a single-column constraint, it's neater to define it inline as part of the column:
create table vod_classification
( dbclassid varchar2(4) not null constraint vod_classification_pk primary key
, dbdescription varchar2(100) not null constraint vod_classification_uk unique
);
I have corrected the CHAR column to the standard string type which is VARCHAR2 in Oracle.
(PK columns will be not null automatically, but I've left it in for completeness and in case you later create table as select.)
When using the "Create" code, you must use a comma in the line where you define each column of the table. Except the last column. You can read the oracle sql syntax link as follows: https://docs.oracle.com/cd/E11882_01/server.112/e41085/sqlqr01001.htm#SQLQR110

Literal does not match format string when creating a table

I keep trying to create a basic table. Every time I try to create the table I get the error
literal does not match format string
I'm trying to limit the years of the tournament between 2005 and 2100. The error is between DATE '2005'.
This is my code:
Create table Tournament_T
(Tournament_name VARCHAR (50) PRIMARY KEY NOT NULL,
Tournament_year INTEGER NOT NULL,
CONSTRAINT RANGE CHECK (Tournament_year BETWEEN DATE '2005' AND '2100'),
Tournament_rules CLOB NOT NULL,
Tournament_fee VARCHAR(1000) NOT NULL,
Tournment_eligibility VARCHAR (1000) NOT NULL );
COMMIT;
Your constraint is using the date keyword, but you don't need it. Just do:
CONSTRAINT RANGE CHECK (Tournament_year BETWEEN 2005 AND 2100),
Your column is an integer, not a date.

Open Office Base: How to Create a INSERT Trigger

I am creating a database using OpenOffice Base and some homebrewed sql. Problems come in when trying to reference a foreign key that is numbers and possibly letters. I would like to force the table I am checking the key on to force the inserts to uppercase (or lower, I am not picky) and do the same to the table that is doing the checking. The tables:
DROP TABLE IF EXISTS Gun;
DROP TABLE IF EXISTS Model;
DROP TABLE IF EXISTS Value;
CREATE TABLE Model (
"Model Name" VARCHAR (25) NOT NULL PRIMARY KEY,
"Manufacuture" VARCHAR (15) NOT NULL,
"Manufactured Start Year" INTEGER NOT NULL,
"Manufactured End Year" INTEGER,
"Gun Type" VARCHAR (10),
"Value Mininum" DECIMAL (10, 2),
"Value Maximum" DECIMAL (10, 2) NOT NULL,
"Description" VARCHAR (500)
);
CREATE TABLE Gun (
"Serial Number" VARCHAR (25) NOT NULL,
"Model Name" VARCHAR (25),
"Caliber" VARCHAR (10),
"Unique Features" VARCHAR (200),
"Manufactured Date" INTEGER,
"Condition" INTEGER,
CONSTRAINT FK_MODEL FOREIGN KEY ("Model Name") REFERENCES Model ("Model Name"),
CONSTRAINT PK_GUN_KEY PRIMARY KEY ("Serial Number", "Model Name"),
CONSTRAINT CK_CONDITION CHECK( "Condition" <= 100 )
);
The Trigger (that doesn't work and is one of many I have tried):
CREATE TRIGGER ForceUpper BEFORE INSERT ON Model
SET Model."Model Name" = UPPER(Model."Model Name");
Which gives:
Unexpected end of command: SET in statement
Any tips on what I am doing wrong would be nice. The HyperSQL docs I looked at were of little help besides adding BEGIN ATOMIC which also had unexpected end of command (even with END). Thanks
Checking HSQLDB documentation, I think your syntax should be:
create trigger ForceUpper before insert on Model
for each row set new."Model Name" = UPPER(new."Model Name")
That new keyword refers to the new row that is being inserted
Note: I suggest you avoid using spaces in table and/or column names. If you need to separate words, either use _ instead of spaces or use CamelCase.

ORA-00907 when trying to create a CHECK constraint

I need your help with this error:
ORA-00907 on Check CONSTRAINT
Query:
CREATE TABLE S_NEWS.T_UTILISATEUR_USR (
USR_ID INTEGER NOT NULL PRIMARY KEY,
USR_MAIL VARCHAR(256) NOT NULL,
USR_TITRE CHAR(6) NULL DEFAULT 'M.'CHECK (USR_TITRE IN ('M.' , 'Mlle.','Mme.' )),
USR_NOM CHAR(32) NOT NULL,
USR_PRENOM VARCHAR(32) NULL,
USR_ORGANISATION VARCHAR(128) NULL
);
The error message is
ORA-00907: missing right parenthesis
It almost always points to a syntax error rather than a missing bracket. In this case the parser is objecting to the order of the elements in your column definition. Specifically, the DEFAULT clause must come before the CONSTRAINT clause, which includes the NULL/NOT NULL declaration. So try
USR_TITRE CHAR(6) DEFAULT 'M.'CHECK (USR_TITRE IN ('M.' , 'Mlle.','Mme.' )) NULL
Incidentally, you're going to a problem with that constraint. A CHAR datatype is always padded to the declared length. Thus if you enter 'M.' into the column it will pad out to 'M. ', which value will cause the constraint to hurl an exception. I suggest you use VARCHAR2(6) instead.
CHAR declarations are almost always a mistake, just a bug waiting to happen.