ORA-00907 when trying to create a CHECK constraint - sql

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.

Related

ORA-00907: missing right parenthesis, and nothing is working

I've been sitting and looking at this code for a few hours and I don't understand where I went wrong, why it doesn't work.
ORA-00907: missing right parenthesis
I saw that this is a topic that is discussed alot but for some reason none of the examples I have seen has helped me. I'm pretty sure it's all right with the parentheses.
Here is the code:
CREATE TABLE ADT_FILIALA_AGENTIE (
id_f NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOT NULL ENABLE,
name varchar(30) NOT NULL,
telephone varchar(30) NOT NULL,
adress varchar(30) NOT NULL,
nr_an varchar(30) NOT NULL,
id_a int(11) NOT NULL,
PRIMARY KEY(id_f),
FOREIGN KEY(id_a) REFERENCES ADT_AGENTIE_PRINCIPALA(id_a)
);
You can't specify precision for the int data type in Oracle. You are using it for column id_a. int is shorthand for number(38,0).
To get the desired effect, replace that with number(11,0) - which means precision of 11 digits, of which zero decimal places.
Other than that, you would be well advised, in Oracle, to use varchar2(n) rather than varchar(n). For a very long time Oracle has warned us that in future releases varchar may be used for something else (even though that hasn't happened and is unlikely to happen).
To explain the error message: Right after the parser reads id_a int it expects (optional space and) a comma, or a constraint, etc. What it does not expect (since it makes no sense in that position) is an opening parenthesis, as you have in (11). The error handling is written to assume that at that point the create table statement should have ended - with a closing parenthesis. It doesn't find it, so it complains about that.
That's why you saw so many unrelated mentions of "missing right parenthesis" errors - they are often thrown by the parser when it finds something unexpected in a statement. The parser doesn't know what the real error is - it just sees something that doesn't make sense somewhere, and if at that point a closing parenthesis would have ended a valid statement, it throws that error.
You can't specify precision for the int data type in Oracle. You are using it for column id_a int.
CREATE TABLE ADT_FILIALA_AGENTIE ( id_f NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOT NULL ENABLE,
name varchar(30) NOT NULL,
telephone varchar(30) NOT NULL,
adress varchar(30) NOT NULL,
nr_an varchar(30) NOT NULL,
id_a int NOT NULL,
PRIMARY KEY(id_f),
FOREIGN KEY(id_a) REFERENCES ADT_AGENTIE_PRINCIPALA(id_a) );

Why does Diesel fail to migrate a PostgresSQL database when the columns specify a length? [duplicate]

I am experimenting with PostgreSQL coming from SQL using MySQL and I simply wish to create a table with this piece of code which is valid SQL:
CREATE TABLE flat_10
(
pk_flat_id INT(30) DEFAULT 1,
rooms INT(10) UNSIGNED NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (flat_id)
);
I get the error
ERROR: syntax error at or near "("
LINE 3: pk_flat_id integer(30) DEFAULT 1,
I have conducted searches on the web and found no answer and I cant seem to find an answer in the PostgreSQL manual. What am I doing wrong?
I explicitly want to set a limit to the number of digits that can be inserted into the "pk_flat_id" field
I explicitly want to set a limit to the number of digits that can be inserted into the "pk_flat_id" field
Your current table definition does not impose a "size limit" in any way. In MySQL the parameter for the intdata type is only a hint for applications on the display width of the column when displaying it.
You can store the value 2147483647 in an int(1) without any problems.
If you want to limit the values to be stored in an integer column you can use a check constraint:
CREATE TABLE flat_10
(
pk_flat_id bigint DEFAULT 1,
rooms integer NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (flat_id),
constraint valid_number
check (pk_flat_id <= 999999999)
);
The answer is that you use numeric or decimal types. These are documented here.
Note that these types can take an optional precision argument, but you don't want that. So:
CREATE TABLE flat_10
(
pk_flat_id DECIMAL(30) DEFAULT 1,
rooms DECIMAL(10) NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (pk_flat_id)
);
Here is a SQL Fiddle.
I don't think that Postgres supports unsigned decimals. And, it seems like you really want serial types for your keys and the long number of digits is superfluous.
Changing integer to numeric works.
CREATE TABLE flat_10
(
pk_flat_id bigint DEFAULT 1,
rooms numeric NOT NULL,
room_label CHAR(1) NOT NULL,
);

SQL Error: ORA-00907: missing right parenthesis when trying to creating table

This is the query I am trying to execute against an Oracle database which apparently has no parenthesis missing.
CREATE TABLE P_DOG(
DOG_ID CHAR(5) NOT NULL,
DOG_NAME VARCHAR2(30) NOT NULL DEFAULT 'UNKNOWN',
DOG_BIRTHDAY_MONTH NUMBER(2) NULL CHECK(DOG_BIRTH_MONTH>=1 AND DOG_BIRTH_MONTH<=12),
DOG_BIRTHDAY_YEAR NUMBER(4) NOT NULL CHECK(DOG_BIRTH_YEAR>=1980 AND DOG_BIRTH_YEAR<= 2030),
SEX CHAR(1) NOT NULL,
SPAYED_OR_NEUTERED CHAR(1) NOT NULL DEFAULT 'N',
CONSTRAINT DOG_PK PRIMARY KEY(DOG_ID),
);
Besides the extra comma at the end that Mat pointed out in a comment, NOT NULL should come after the defaults - and the check constraints should have the correct column names: the column names have BIRTHDAY but the conditions use BIRTH, change either the column name or what you have in the conditions so they match.
Specifically the "missing right parenthesis" error is caused by having a DEFAULT after NOT NULL. Wrong order.

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

How can I set a size limit for an "int" datatype in PostgreSQL 9.5

I am experimenting with PostgreSQL coming from SQL using MySQL and I simply wish to create a table with this piece of code which is valid SQL:
CREATE TABLE flat_10
(
pk_flat_id INT(30) DEFAULT 1,
rooms INT(10) UNSIGNED NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (flat_id)
);
I get the error
ERROR: syntax error at or near "("
LINE 3: pk_flat_id integer(30) DEFAULT 1,
I have conducted searches on the web and found no answer and I cant seem to find an answer in the PostgreSQL manual. What am I doing wrong?
I explicitly want to set a limit to the number of digits that can be inserted into the "pk_flat_id" field
I explicitly want to set a limit to the number of digits that can be inserted into the "pk_flat_id" field
Your current table definition does not impose a "size limit" in any way. In MySQL the parameter for the intdata type is only a hint for applications on the display width of the column when displaying it.
You can store the value 2147483647 in an int(1) without any problems.
If you want to limit the values to be stored in an integer column you can use a check constraint:
CREATE TABLE flat_10
(
pk_flat_id bigint DEFAULT 1,
rooms integer NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (flat_id),
constraint valid_number
check (pk_flat_id <= 999999999)
);
The answer is that you use numeric or decimal types. These are documented here.
Note that these types can take an optional precision argument, but you don't want that. So:
CREATE TABLE flat_10
(
pk_flat_id DECIMAL(30) DEFAULT 1,
rooms DECIMAL(10) NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (pk_flat_id)
);
Here is a SQL Fiddle.
I don't think that Postgres supports unsigned decimals. And, it seems like you really want serial types for your keys and the long number of digits is superfluous.
Changing integer to numeric works.
CREATE TABLE flat_10
(
pk_flat_id bigint DEFAULT 1,
rooms numeric NOT NULL,
room_label CHAR(1) NOT NULL,
);