SQL CREATE TABLE missing left parentheses error - sql

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.

Related

Trying to run script through Oracle 11g

I am trying to run SQL statements in the Oracle 11g Express edition, where I am to create tables. Here is my SQL code:
CREATE TABLE STORE
(
StoreID INT PRIMARY KEY,
StoreName VARCHAR2(30) NOT NULL,
City VARCHAR2(30) NOT NULL,
Country VARCHAR2(30) NOT NULL CHECK Country in ('China','Egypt','United States','Spain','New Zealand','Mexico','Africa'),
Phone VARCHAR2(30) NOT NULL,
Fax VARCHAR2(30),
Email VARCHAR2(50) UNIQUE,
Contact VARCHAR2(30) NOT NULL,
UNIQUE (StoreName, City)
);
CREATE TABLE PURCHASE_ITEM
(
PurchaseItemID INT PRIMARY KEY,
StoreID INT NOT NULL REFERENCES STORE(StoreID) ON DELETE CASCADE ON UPDATE CASCADE,
"Date" DATE NOT NULL,
Description VARCHAR2(30) NOT NULL,
Category VARCHAR2(30),
PriceUsed NUMBER(15, 2)
);
CREATE SEQUENCE pur_seq
START WITH 500
INCREMENT BY 5;
CREATE OR REPLACE TRIGGER Purchase
BEFORE INSERT ON PURCHASE_ITEM
FOR EACH ROW
BEGIN
SELECT pur_seq.NEXTVAL
INTO :new.PurchaseItemID
FROM dual;
END;
CREATE TABLE SHIPPER
(
ShipperID INT PRIMARY KEY,
ShipperName VARCHAR2(30) NOT NULL,
Phone VARCHAR2(30) NOT NULL,
Fax VARCHAR2(30),
Email VARCHAR2(50) UNIQUE,
Contact VARCHAR2(30) NOT NULL
);
CREATE TABLE SHIPMENT
(
ShipmentID INT PRIMARY KEY Auto Increment,
ShipperID INT NOT NULL REFERENCES SHIPPER(ShipperID) ON DELETE CASCADE ON UPDATE CASCADE,
ShipperInvoiceNumber INT NOT NULL UNIQUE,
Origin VARCHAR2(30) NOT NULL,
Destination VARCHAR2(30) NOT NULL,
DepartureDate DATE,
ArrivalDate DATE
);
ALTER TABLE SHIPMENT AUTO_INCREMENT = 100;
CREATE TABLE SHIPMENT_ITEM
(
ShipmentID INT NOT NULL REFERENCES SHIPMENT(ShipmentID) ON DELETE CASCADE ON UPDATE CASCADE,
ShipmentItemID INT NOT NULL,
PurchaseItemID INT NOT NULL REFERENCES PURCHASE_ITEM(PurchaseItemID) ON DELETE CASCADE ON UPDATE CASCADE,
InsuredValue NUMBER(15, 2) NOT NULL defaut 100,
PRIMARY KEY (ShipmentID, ShipmentItemID)
);
It only ends up processing 4 statements and I keep getting these error messages:
CREATE TABLE STORE ( StoreID INT PRIMARY K - ORA-00906: missing left parenthesis
CREATE TABLE PURCHASE_ITEM ( PurchaseItemID INT - ORA-00907: missing right parenthesis
CREATE SEQUENCE pur_seq START WITH 500 INCREMENT BY 5 - ORA-00955: name is already used by an existing object
CREATE OR REPLACE TRIGGER Purchase BEFORE INSERT ON PURCHASE - ORA-00942: table or view does not exist
I am wholly unfamiliar with Oracle 11g. I am unsure if I am using the correct application in it for my assignment. I am only going by these instructions:
"For this assignment you are to write scripts to create tables and insert records. In oracle 11g I don’t want you to use the tools to generate the tables, you are required to write scripts to create the tables and records and will need to include the scripts for your submission."
Please, what am I doing wrong?
Let me summarize the problems with your scripts
First you have to enclose the check constraint with braces like below
CREATE TABLE STORE
(
StoreID INT PRIMARY KEY,
StoreName VARCHAR2(30) NOT NULL,
City VARCHAR2(30) NOT NULL,
Country VARCHAR2(30) NOT NULL CHECK (Country in ('China','Egypt','United States','Spain','New Zealand','Mexico','Africa')),
Phone VARCHAR2(30) NOT NULL,
Fax VARCHAR2(30),
Email VARCHAR2(50) UNIQUE,
Contact VARCHAR2(30) NOT NULL,
UNIQUE (StoreName, City)
);
Second, there is no ON UPDATE CASCADE in Oracle 11g so you need to remove it from the CREATE TABLE statement
Third, there is no Auto increment in Oracle 11G for columns So refer this SO for a workaround
Please let me know with these corrections whether your issue is resolved

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.

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.

create table in Oracle BD but gives error

CREATE TABLE employees (
id INT NOT NULL auto_increment PRIMARY KEY (ID),
first_name VARCHAR(20) DEFAULT NULL,
last_name VARCHAR(20) DEFAULT NULL,
salary INT DEFAULT NULL);
I think this is correct query to create table in Oracle database.. but it gives the following error:
ORA-00907: missing right parenthesis
How to correct the statement?
You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm
auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.
also, "id int not null auto_increment primary key (id)" does not need the last "(id)"
Using Oracle, you shoud try something like this
CREATE SEQUENCE seq;
CREATE TABLE employees
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR2(20) DEFAULT NULL,
last_name VARCHAR2(20) DEFAULT NULL,
salary INTEGER DEFAULT NULL
);
INSERT INTO employees
VALUES (seq.NEXTVAL,
'name',
'last name',
1);
Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.
Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)
EDITED : New feature 12c
CREATE TABLE employees(
id NUMBER GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR2(30)
etc.
);
Why would you do default null?
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.
Replace
id INT NOT NULL auto_increment PRIMARY KEY (ID),
with
id INT NOT NULL auto_increment PRIMARY KEY,
this is more efficient
CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);

Error at line 1: ORA-00907: missing right parenthesis

I believe I have the right syntax for SQL plus command, I have tried different ways to do it but I am getting the same error message. I don't know why i am getting this "missing right parenthesis error" any help will be appreciated thank you in advance.
here is my code:
create table PUBLISHERS (
NAME varchar2(50) primary key,
address varchar2(50), phone integer(10)
);
The integer data type does not use a length qualifier. integer is equivalent to number(38,0).
SQL> ed
Wrote file afiedt.buf
1 create table PUBLISHERS (
2 NAME varchar2(50) primary key,
3 address varchar2(50),
4 phone integer
5* )
SQL> /
Table created.
If you want to limit the size, use a number
SQL> ed
Wrote file afiedt.buf
1 create table PUBLISHERS (
2 NAME varchar2(50) primary key,
3 address varchar2(50),
4 phone number(10)
5* )
SQL> /
Table created.
Since you are never going to do numeric operations on a phone number, however, while it is generally likely that you will perform string manipulation on it to format phone numbers for display, it would generally make sense to store a phone number as a character string rather than as a number. You can add a CHECK constraint that ensures the format is correct.
SQL> ed
Wrote file afiedt.buf
1 create table PUBLISHERS (
2 NAME varchar2(50) primary key,
3 address varchar2(50),
4 phone varchar2(10)
5* )
SQL> /
Table created.
INTEGER is not a Oracle Built-In data type. It is just a ANSI format that is supported in oracle. The oracle representation of INTEGER is NUMBER (38). Use NUMBER datatype instead.
CREATE TABLE publishers(
name VARCHAR2(50) PRIMARY KEY,
address VARCHAR2(50),
phone NUMBER(10)
);
create table doctor_details(doctor_id number(10),
username varchar2(30) not null,
password varchar2(30) not null,
name varchar2(30) not null,
father_name varchar2(30) not null,
gender varchar2(6) not null check(gender in('male','female)),
email varchar2(50) not null,
contact_no number(10) not null,
qualification varchar2(50) not null,
specialization varchar2(5) not null,
date_of_birth date not null,
date_of_joining date not null,
primary key(doctor_id))
error is right parenthesis missing