Oracle error sql - 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'.

Related

adding a min and max date and getting error: literal does not match format string

I am trying to create a table for bookings and want there to be a check constraint where the customer can only insert the D.O.B from a certain year to another certain year but keep getting the same error message
Any help would be very appreciated
`
create table guest
( Guest_ID varchar2(8) primary key,
Family_Name varchar2(20) not null,
Given_Name varchar2(20) not null,
Date_of_Birth date check (Date_of_Birth between date '01/01/1904' and
date '01/01/2004' ) not null,
Address varchar2(80) not null
);
`
Use the following code:
create table guest
( Guest_ID varchar2(8) primary key,
Family_Name varchar2(20) not null,
Given_Name varchar2(20) not null,
Date_of_Birth date check (Date_of_Birth between to_date('01/01/1904','DD/MM/YYYY') and
to_date('01/01/2004','DD/MM/YYYY')) not null,
Address varchar2(80) not null
);
The Tip from jarlh(comments) works. Problem might be your Date format.
create table guest
(Guest_ID varchar2(8) primary key,
Family_Name varchar2(20) not null,
Given_Name varchar2(20) not null,
Date_of_Birth date check (Date_of_Birth between date '1904-
01- 01' and date '2004-01-01' ) not null,
Address varchar2(80) not null
);
insert into guest values ('t','t','t','18-NOV-03','t')
works fine.
Fiddle
I would simplify that and prefer EXTRACT because you don't care about the day, but only want to check a range of years...
...CHECK (EXTRACT(YEAR FROM Date_of_Birth) BETWEEN 1904 and 2003 )...
...unless you really have to accept the 1st of January 2004 as valid date,too.

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.

Missing right parenthesis?

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.

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.

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