what does the error "missing right parenthesis" in oracle sql means - sql

I'm trying to run this code and it seems correct to me but I'm getting an error stating that there's a right parenthesis missing.
The code is the following:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT NOT NULL DEFAULT 0.0,
PRIMARY KEY(CUSTOMER_ID)
);
Can anyone help me in solving my problem?

Since you tagged SQL Developer...
...the tool tries to give you a heads-up there will be a problem before you even hit the Execute button
The default value for the column is confusing the parser because it's not expected at that point.
Move it to after the data type and you'll be good
CREATE TABLE customer (
customer_id INT NOT NULL,
name VARCHAR2(30) NOT NULL,
date_of_birth DATE,
phone_nb CHAR(8) NOT NULL,
address VARCHAR(50),
total_spending FLOAT DEFAULT 0.0 NOT NULL,
PRIMARY KEY ( customer_id )
);
PS In oracle, use VARCHAR2, not VARCHAR. While VARCHAR will 'work', it's reserved and could mean something different in a future release.

You are using wrong order of column definition clauses: the constraint (NOT NULL) should follow the default value.
This is the right way:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT DEFAULT 0.0 NOT NULL ,
PRIMARY KEY(CUSTOMER_ID)
);

Related

Alternative to Postgresql BIGSERIAL data type in Azure Database?

I am learning Azure and data analytics with Azure. Recently finished learning Postgresql.
My question is if there is an alternative to BIGSERIAL data type for Azure Databases. I ran the query (below the error in the following) and had an error. Note that this datatype exists in Postgresql and hence I am getting confused in Azure. Any alternative to BIGSERIAL?
Failed to execute the query. Error: Column, parameter, or variable #1:
Cannot find data type BIGSERIAL.
create table person (
ID BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50),
gender VARCHAR(50) NOT NULL,
date_of_birth DATE NOT NULL,
Country_of_birth VARCHAR(50) NOT NULL
);
In PostgreSQL, the SERIAL keyword is used to setup an auto increment column, this works similar to auto increment in SQL. BIGSERIAL is an auto-incremented Bigint column of 8 bytes.
Closest, I could find "bigserial"in MS docs is as here
So...you can use BIGINT instead, below works fins for me.
create table person (
ID BIGINT NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50),
gender VARCHAR(50) NOT NULL,
date_of_birth DATE NOT NULL,
Country_of_birth VARCHAR(50) NOT NULL
);

I am creating a table in pgweb Heroku, and get this error "ERROR: pq: syntax error at or near "(""

Here is my exact query
CREATE Table Publisher
(Publisher_Id Int primary key not null,
Name varchar(20) not null,
Address varchar(50) not null,
Phone Int(10),
Isbn varchar(13) references books (Isbn) not null
)
Any help would be greatly appreciated.
datatype int does not take a length. So:
create table publisher (
publisher_id int primary key,
name varchar(20) not null,
address varchar(50) not null,
phone int,
isbn varchar(13) references books (isbn) not null
);
Notes:
not null is redondant on a primary key column
int does not seem like a good pick for a phone number; you would typically need to store leading 0s, or allow special characters such as + or () - int cannot do that. A string datatype would probably be a better pick

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

SQL server 2012 tables issue, sysdate and to_date issue

I have two issues while trying to create tables.
For **sysdate*** it says invalid column name
for *TO_DATE('01-JAN-2008','DD-MON-YYYY')));* it says TO_DATE is not a reconigized built-in function name.
^ both are in the Table Invoice.
This is using SQL SERVER 2012
CREATE TABLE VENDOR(
V_CODE INTEGER NOT NULL UNIQUE,
V_NAME VARCHAR(35) NOT NULL,
V_CONTACT VARCHAR(15) NOT NULL,
V_AREACODE CHAR(3) NOT NULL,
V_PHONE CHAR(8) NOT NULL,
V_STATE CHAR(2) NOT NULL,
v_ORDER CHAR(1) NOT NULL,
PRIMARY KEY (V_CODE));
CREATE TABLE PRODUCT(
P_CODE VARCHAR(10) NOT NULL,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE DECIMAL(8,2) NOT NULL,
P_DISCOUNT DECIMAL(5,2) NOT NULL,
V_CODE INTEGER,
PRIMARY KEY (P_CODE),
FOREIGN KEY(V_CODE) REFERENCES VENDOR ON UPDATE CASCADE);
CREATE TABLE CUSTOMER(
CUS_CODE DECIMAL PRIMARY KEY,
CUS_LNAME VARCHAR(15) NOT NULL,
CUS_FNAME VARCHAR(15) NOT NULL,
CUS_INITIAL CHAR(1),
CUS_AREACODE CHAR(3) DEFAULT '615' NOT NULL,
CHECK(CUS_AREACODE IN ('615','713','931')),
CUS_PHONE CHAR(8) NOT NULL,
CUS_BALANCE DECIMAL(9,2) DEFAULT 0.00,
CONSTRAINT CUS_UI1 UNIQUE (CUS_LNAME, CUS_FNAME));
CREATE TABLE INVOICE (
INV_NUMBER DECIMAL PRIMARY KEY,
CUS_CODE DECIMAL NOT NULL REFERENCES CUSTOMER(CUS_CODE),
INV_DATE DATE DEFAULT SYSDATE NOT NULL,
CONSTRAINT INV_CK1 CHECK (INV_DATE > TO_DATE('01-JAN-2008','DD-MON-YYYY')));
You have two issues:
the function to get the current system date and time is called SYSDATETIME() in T-SQL/SQL Server (not sysdate)
the way to convert a string to a date or datetime in T-SQL/SQL Server is using CAST or CONVERT (not TO_DATE - there is no such function in T-SQL)
Use something like
SELECT CAST('01-JAN-2008' AS DATE)
or something like that (it's highly dependent on your language/date format settings in SQL Server whether it'll work or not). If you need to specify a specific format, you can use CONVERT which allows you to use one of the many predefined formats (see relevant details in the MSDN documentation).
If that's still not enough - SQL Server 2012 has a new function called PARSE which allows you to specify any arbitrary date format that your string is formatted in. Again, see the relevant MSDN documentation for details.
The best thing is to avoid converting dates back and forth to and from strings if ever possible, and in your case, this should be easily doable! Just use:
INV_DATE DATE DEFAULT SYSDATETIME() NOT NULL,
CONSTRAINT INV_CK1 CHECK (INV_DATE > '20080101');