Missing right parenthesis? - sql

CREATE TABLE PERSON
(
PID Int Not Null,
FirstName Varchar2(15) Not Null,
LastName Varchar2(15) Not Null,
Email Varchar2(50) Not Null,
Type Varchar2(15) Not Null Default Customer,
Primary Key(PID)
);
I`m getting the following error:
ORA-00907: missing right parenthesis

type is a reserved word in SQL (or at least, in Oracle's flavor of it). You could either escape it using double quotes ("):
CREATE TABLE PERSON(
PID Int Not Null,
FirstName Varchar2(15) Not Null,
LastName Varchar2(15) Not Null,
Email Varchar2(50) Not Null,
"Type" Varchar2(15) Default 'Customer' Not Null,
Primary Key(PID)
);
Or just use a name that isn't a reserved word, such as person_type:
CREATE TABLE PERSON(
PID Int Not Null,
FirstName Varchar2(15) Not Null,
LastName Varchar2(15) Not Null,
Email Varchar2(50) Not Null,
Person_Type Varchar2(15) Default 'Customer' Not Null,
Primary Key(PID)
);
EDIT:
As #a_horse_with_no_name commented, the default value "Customer" is a string literal, so it has to be enclosed with single quotes (').
EDIT2:
The default value clause should come before the not null clause.

Related

SQL CREATE TABLE missing left parentheses error

CREATE TABLE residents
(
R_ID NUMBER(4), CONSTRAINTS pk_residents_R_ID PRIMARY KEY,
R_FN VARCHAR2(15), NOT NULL,
R_LN VARCHAR2(15), NOT NULL,
R_Contact NUMBER(10), NOT NULL,
DoB DATE, NOT NULL
);
Tried a few changes but I'm unable to resolve this error. Any help would be appreciated!
It works for Oracle in this way:
CREATE TABLE residents(
R_ID NUMBER(4),
R_FN VARCHAR2(15) NOT NULL,
R_LN VARCHAR2(15) NOT NULL,
R_Contact NUMBER(10) NOT NULL,
DoB DATE NOT NULL,
pk_residents_R_ID NUMBER(4) PRIMARY KEY
);
Code which is the closest to your attempt is:
SQL> CREATE TABLE residents
2 (
3 r_id NUMBER (4) CONSTRAINT pk_residents_r_id PRIMARY KEY,
4 r_fn VARCHAR2 (15) NOT NULL,
5 r_ln VARCHAR2 (15) NOT NULL,
6 r_contact NUMBER (10) NOT NULL,
7 dob DATE NOT NULL
8 );
Table created.
SQL>
keyword is constraint, not constraintS
don't separate NOT NULL (or any other constraint) from its column with a comma
Also, using mixed letter case is irrelevant as Oracle - by default - stores object names in UPPERCASE. That's so unless you decide to enclose names into double quotes, but then you'll always have to use double quotes and exactly match letter case so - that's not what anyone should do, not in Oracle.

Getting an error that I'm missing a right paren when I am not [duplicate]

This question already has answers here:
Missing right Paranthesis on Create Table command SQL
(2 answers)
Closed 3 years ago.
I'm getting an error when creating two tables that I'm missing a right paren on the second table although I am not.
I've tried different oracle variations of this code and I'm still getting the error.
(ORACLE LIVE SQL)
CREATE TABLE PET_OWNER
(OwnerID Integer Primary Key,
OwnerLastName Char(25) Not Null,
OwnerFirstName Char(25) Not Null,
OwnerPhone Char(25) Null,
OwnerEmail Char(50) Not Null);
CREATE TABLE PET_DATA
(PetID Integer Not Null,
PetName Char(50) Not Null,
PetType Char(25) Not Null,
PetBreed Char(50) Not Null,
PetDOB Varchar(50) Not Null,
Primary Key (PetID)
Constraint FK_PetOwner Foreign Key (OwnerID)
References Owner(OwnerID));
I expect the tables to be created but only the first table is being created successfully. The second table has a foreign key.
It looks like you are missing a comma after the primary-key definition on the second table.
The Oracle parser often complains about missing closing parentheses when the real issue is some other syntax error.
I would recommend:
CREATE TABLE PET_OWNER (
OwnerID Integer Primary Key,
OwnerLastName varchar2(25) Not Null,
OwnerFirstName varchar2(25) Not Null,
OwnerPhone varchar2(25) Null,
OwnerEmail varchar2(50) Not Null
);
CREATE TABLE PET_DATA (
PetID Integer Not Null,
OwnerID Integer,
PetName varchar2(50) Not Null,
PetType varchar2(25) Not Null,
PetBreed varchar2(50) Not Null,
PetDOB varchar2(50) Not Null,
Primary Key (PetID),
Constraint FK_PetOwner Foreign Key (OwnerID) References Pet_Owner(OwnerID)
);
This fixes small problems (missing OwnerId column in the second table, wrong table name). It also uses varchar2() for variable length strings rather than char() -- which are padded with spaces to the specified length.

SQL developer suggests "missing right parenthesis". But I couldn't find where's wrong

The code is as follows:
create table Person (
personId smallint not null,
title char(5) default null,
personName varchar2(50) not null,
institution varchar2(100),
country varchar2(30),
phoneNo varchar2(15),
personEmail varchar2(50),
primary key (personId),
check (title in ('Mr', 'Ms', 'Miss', 'Dr', 'Prof', null)),
check ( length(phoneNo) between ( 8 and 15)),
unique (personEmail));
I think you have two issues. Try this:
create table Person (
personId smallint not null,
title char(5) default null,
personName varchar2(50) not null,
institution varchar2(100),
country varchar2(30),
phoneNo varchar2(15),
personEmail varchar2(50),
primary key (personId),
check (title in ('Mr', 'Ms', 'Miss', 'Dr', 'Prof') or title is null),
check (length(phoneNo) between 8 and 15),
unique (personEmail)
);
The syntax error is the parentheses around (8 and 15). I would recommend that the title be varchar2() rather than a fixed length string.
Another issue is the NULL in in.
What is the issue? In most SQL expressions, NULL is treated as "false". This is true of conditions in WHERE clauses and CASE expressions, for example. CHECK constraints, for some reason, behave differently. A NULL value passes the CHECK constraint. I find all this confusing, so I prefer that my CHECK constraints evaluate to true or false.

Missing Right parenthesis error?

I'm trying to create a table in sqldeveloper however I keep getting a missing right parenthesis error when there are no missing right parenthises. Any fixes for this or am i just trying to create a table the wrong way?
CREATE TABLE Patient_T1(
PATIENT_ID INT(100) NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
DOB CHAR(10) NOT NULL,
P_STREET_ADRESS VARCHAR(50) NOT NULL,
PATIENT_CITY VARCHAR(30) NOT NULL,
PATIENT_STATE CHAR(2) NOT NULL,
PATIENT_ZIP CHAR(5) NOT NULL,
PATIENT_PHONE CHAR(12) NOT NULL,
PATIENT_ROOM INT(1000) NOT NULL,
CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID));
Not sure why Oracle gives that error message instead of something more helpful, but the cause is the precision applied to INT, switch from INT(100) and INT(1000) to just INT:
CREATE TABLE Patient_T1(
PATIENT_ID INT NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
DOB CHAR(10) NOT NULL,
P_STREET_ADRESS VARCHAR(50) NOT NULL,
PATIENT_CITY VARCHAR(30) NOT NULL,
PATIENT_STATE CHAR(2) NOT NULL,
PATIENT_ZIP CHAR(5) NOT NULL,
PATIENT_PHONE CHAR(12) NOT NULL,
PATIENT_ROOM INT NOT NULL,
CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID));
There are multiple issues with your table DDL:
INT(100) - In Oracle, an INTEGER is an ANSI SQL data type which refers to numeric values which have only an integer portion and no floating point or decimal part. That is, an INTEGER will only store whole numbers ONLY.
VARCHAR(20) - Oracle strongly recommends to use VARCHAR2.
From documentation,
VARCHAR Datatype
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To
avoid possible changes in behavior, always use the VARCHAR2 datatype
to store variable-length character strings.
CHAR(10) - better use VARCHAR2 as CHAR is blank-padded to the fixed length. That's a wastage of storage.
From documentation,
CHAR Datatype
The CHAR datatype stores fixed-length character strings. If you give a
shorter value, then the value is blank-padded to the fixed length.
Only the issue# 1 would throw an error, anyway fixing all the above issues would let you create the table.
For example,
SQL> CREATE TABLE Patient_T1
2 (
3 PATIENT_ID NUMBER NOT NULL,
4 FIRST_NAME VARCHAR2(20) NOT NULL,
5 LAST_NAME VARCHAR2(30) NOT NULL,
6 DOB DATE NOT NULL,
7 P_STREET_ADRESS VARCHAR2(50) NOT NULL,
8 PATIENT_CITY VARCHAR2(30) NOT NULL,
9 PATIENT_STATE VARCHAR2(2) NOT NULL,
10 PATIENT_ZIP VARCHAR2(5) NOT NULL,
11 PATIENT_PHONE VARCHAR2(12) NOT NULL,
12 PATIENT_ROOM NUMBER NOT NULL,
13 CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID)
14 );
Table created.

Oracle error sql

I have to create a table and insert values into the tables. The tables is being created but it wont insert values.
heres my code-
CREATE TABLE CUSTOMER(
CID number(20) not null primary key,
FNAME varchar2(50) not null,
MIDDLE varchar2(50) null,
LNAME varchar2(50) not null,
STREET varchar2(50) not null,
CITY varchar2(50) not null,
STATE char(2) not null,
ZIP varchar2(5) not null,
PHONE varchar2(20) not null,
BIRTH_YR date);
INSERT INTO CUSTOMER VALUES(
'1','john','paul','connor','Broad st','newark','nj','07103','2018643110','1992');
I keep getting an error
[Error] Script lines: 12-14 ------------------------
ORA-01861: literal does not match format string
Script line 14, statement line 2, column 85
Can somebody help me out? What am i doing wrong?
You must change the table definition to something like below to allow insertion of year in the format you need:
CREATE TABLE CUSTOMER(
CID number(20) not null primary key,
FNAME varchar2(50) not null,
MIDDLE varchar2(50) null,
LNAME varchar2(50) not null,
STREET varchar2(50) not null,
CITY varchar2(50) not null,
STATE char(2) not null,
ZIP varchar2(5) not null,
PHONE varchar2(20) not null,
BIRTH_YR char(4) not null);
Otherwise, you have to provide literal date values in the default format accepted by your Oracle database. You can use this query to find out what is the date format accepted by your system:
SELECT *
FROM nls_database_parameters
WHERE parameter LIKE '%DATE%'
For me, it returns:
NLS_DATE_FORMAT | DD-MON-RR
---------------------------------
NLS_DATE_LANGUAGE | AMERICAN
which means instead of '1992', I should insert values such as '01-Jan-1992' in the BIRTH_YR column:
INSERT INTO CUSTOMER
VALUES('1','john','paul','connor','Broad st','newark','nj','07103','2018643110', '01-Jan-1992');
BIRTH_YR being a DATE field should be given in 'yyyy-MM-dd' format, '1992-01-01'.