Find error-ed column name in Oracle not null constraint - sql

I have a table with multiple NOT NULL columns. When I insert data with NULL value in NOT NULL columns, NOT NULL constraint error ORA-01400 is raised, but Column name in error message is blank.
ORA-01400: cannot insert NULL into ()
Why I cannot see error-ed column name and What can be work around to get column name in error message as below?
ORA-01400: cannot insert NULL into ("APPS"."MY_TABLE_NAME"."FIRST_NAME")
Table Example :
create table apps.my_table_data
(
id number,
first_name varchar2(50) not null,
last_name varchar2(50) not null,
district_name varchar2(50) not null
);
I also tried giving constraint explicit name:
create table apps.my_table_data
(
id number,
first_name varchar2(50) constraint my_table_date_first_name not null,
last_name varchar2(50) constraint my_table_date_last_name not null,
district_name varchar2(50) constraint my_table_date_district_nm not null
status varchar2(30 char),
)
But still I cannot see error-ed column name in error message.

Related

SQL error message: ORA-00907: missing right parenthesis

CREATE TABLE visitors
(
V_ID NUMBER(6)
CONSTRAINT pk_visitors_V_ID PRIMARY KEY,
V_FN VARCHAR2(15) NOT NULL,
V_LN VARCHAR2(15) NOT NULL,
V_Contact NUMBER(10) NOT NULL,
V_Address CHAR(10) NOT NULL,
DoB DATE NOT NULL,
Covid_Vaccine NUMBER(1)
CONSTRAINT ck_visitors_Covid_Vaccine CHECK (Covid_Vaccine IN ('1','2','3'))
R_ID NUMBER(4)
CONSTRAINT fk_residents_R_IDREFERENCES residents(R_ID),
Date DATE NOT NULL
);
I'm trying to create this table on Apex Oracle for SQL but I keep getting this error:
ORA-00907: missing right parenthesis.
That error is for missing comma after Covid_Vaccine column declaration. But after correcting this you will get invalid identifier error since date is reserve word . So I have changed the column name to ColDate.
You will face another error ORA-02253: constraint specification not allowed here for not putting reference key word in R_ID column declaration. Please try below create table statement (it's working now).
create table residents(R_ID NUMBER(4) PRIMARY KEY);
CREATE TABLE visitors (
V_ID NUMBER(6) CONSTRAINT pk_visitors_V_ID PRIMARY KEY,
V_FN VARCHAR2(15) NOT NULL,
V_LN VARCHAR2(15) NOT NULL,
V_Contact NUMBER(10) NOT NULL,
V_Address CHAR(10) NOT NULL,
DoB DATE NOT NULL,
Covid_Vaccine NUMBER(1) CONSTRAINT ck_visitors_Covid_Vaccine CHECK (Covid_Vaccine IN ('1','2','3')),
R_ID NUMBER(4) CONSTRAINT fk_residents_R_IDREFERENCES references residents(R_ID),
ColDate DATE NOT NULL
);
db<>fiddle here
Various errors (missing comma, invalid column name (date is reserved for datatype), missing references keyword). When fixed:
SQL> CREATE TABLE visitors (
2 v_id NUMBER(6) CONSTRAINT pk_visitors_v_id PRIMARY KEY,
3 v_fn VARCHAR2(15) NOT NULL,
4 v_ln VARCHAR2(15) NOT NULL,
5 v_contact NUMBER(10) NOT NULL,
6 v_address CHAR(10) NOT NULL,
7 dob DATE NOT NULL,
8 covid_vaccine NUMBER(1) CONSTRAINT ck_visitors_covid_vaccine CHECK
9 ( covid_vaccine IN ( '1', '2', '3' ) ),
10 r_id NUMBER(4) CONSTRAINT fk_residents_r_idreferences
11 REFERENCES residents(r_id),
12 c_date DATE NOT NULL );
Table created.
SQL>

How to rectify "missing right parenthesis" problem?

Following is a sample of query I am working on right now:
CREATE TABLE book (
book_id NUMBER(6) NOT NULL CHECK(book_id > 0),
isbn VARCHAR2(20) NOT NULL UNIQUE,
title VARCHAR2(100) NOT NULL,
shelf_letter VARCHAR2(2) NOT NULL CHECK(shelf_letter IN('A-Z')),
call_number NUMBER(3) NOT NULL CHECK(call_number IN (1-999)),
no_of_pages NUMBER(4),
no_of_copies NUMBER(3) NOT NULL CHECK(no_of_copies >= 1),
date_arrived DATE NOT NULL DEFAULT SYSDATE,
publisher_id NUMBER(4) NOT NULL,
cat_id NUMBER(2) NOT NULL,
CONSTRAINT shelf_call UNIQUE(shelf_letter,call_number)
);
On compiling, it is throwing a "missing right parenthesis" error. I checked, but all the parentheses are accounted for. Can somebody help me?
The actual error you are seeing is caused by the date_arrived column with NOT NULL being placed before the default value of SYSDATE, when it should be after:
date_arrived DATE DEFAULT SYSDATE NOT NULL,
In addition, the second and third check constraints, while technically valid syntax, look dubious:
shelf_letter VARCHAR2(2) NOT NULL CHECK(shelf_letter IN('A-Z')),
call_number NUMBER(3) NOT NULL CHECK(call_number IN (1-999)),
For the check on shelf_letter, presumably you want to enforce that it can only be the capital letters from A to Z. If so, then use REGEXP_LIKE:
shelf_letter VARCHAR2(2) NOT NULL CHECK(REGEXP_LIKE(shelf_letter, '^[A-Z]$')),
For the check on call_number, use a range comparison:
call_number NUMBER(3) NOT NULL CHECK(call_number BETWEEN 1 AND 999),
Putting all this together, use the following create table statement:
CREATE TABLE book (
book_id NUMBER(6) NOT NULL CHECK(book_id > 0),
isbn VARCHAR2(20) NOT NULL,
title VARCHAR2(100) NOT NULL,
shelf_letter VARCHAR2(2) NOT NULL CHECK(REGEXP_LIKE(shelf_letter, '^[A-Z]$')),
call_number NUMBER(6) NOT NULL CHECK(call_number BETWEEN 1 AND 999),
no_of_pages NUMBER(4),
no_of_copies NUMBER(3) NOT NULL CHECK(no_of_copies >= 1),
date_arrived DATE DEFAULT SYSDATE NOT NULL,
publisher_id NUMBER(4) NOT NULL,
cat_id NUMBER(2) NOT NULL,
CONSTRAINT shelf_call UNIQUE(shelf_letter, call_number)
);
Demo

Foreign key missing parenthesis

CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) FOREIGN KEY REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER FOREIGN KEY REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL,
);
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
in creation of the employee table
this is giving me an error of right parenthesis
and the department table is already created
You have an extra comma in the line
SALARY DECIMAL(7,2) NOT NULL,
Delete that comma and the Employee table should be created.
You need to Create the Department Table first to use one of its
Columns as FOREIGN KEY.
Also, check your database. There might already be a Department Table. To avoid getting that error when the table needed is already created, use the keyword IF NOT EXISTS
CREATE TABLE IF NOT EXISTS Department(
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
The error is caused by the trailing comma, but you have other issues:
The FOREIGN KEY is not needed for an inline reference.
You need to define the tables in the right order.
So . . .
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL
);
Here is an example of it working.

Check Constraint Violated Error, But Column Unspecified

I've made the following table below:
CREATE TABLE Employee (
Employee_ID NUMBER(10) NOT NULL PRIMARY KEY,
Company_ID NUMBER(10) NOT NULL REFERENCES
Penrhyn_Jet_Charter(Company_ID),
First_Name VARCHAR2(255) NOT NULL,
Last_Name VARCHAR2(255) NOT NULL,
Address VARCHAR2(300) NOT NULL,
Email_Address VARCHAR2(255) NOT NULL UNIQUE CHECK (Email_Address
LIKE '%_#__%.__%'),
Telephone_Number NUMBER(15) NOT NULL CHECK (Telephone_Number > 0),
Mobile_Number NUMBER(15) NOT NULL UNIQUE CHECK (Mobile_Number>0),
Date_Of_Birth DATE NOT NULL,
Gender CHAR(1) NOT NULL, CHECK (Gender = 'M' OR Gender
= 'F'),
NI_Number VARCHAR2(10) NOT NULL UNIQUE CHECK (NI_Number Like
'[a-z][a-z][0-9][0-9][0-9][0-9][0-9][0-9][a-z]'),
Job_Description VARCHAR2(255) NOT NULL,
Annual_Salary NUMBER(10) NOT NULL CHECK (Annual_Salary > 0)
);
CREATE OR REPLACE TRIGGER Check_Date_Of_Birth
BEFORE INSERT OR UPDATE ON Employee
FOR EACH ROW
BEGIN
IF( :NEW.Date_Of_Birth < DATE '1900-01-01' OR
:NEW.Date_Of_Birth > SYSDATE )
THEN
RAISE_APPLICATION_ERROR ( -20001,
'Date of birth must be later than Jan 1, 1900 and earlier than today'
);
END IF;
END;
The above table was created with no errors.
But when I'm trying to populate the table, It's gives me this error:
INSERT INTO Employee VALUES (1, 1, 'Gary', 'Turner', '5 Orpington Street, Crawley, CR18 25J',
'garyturner#mail.com', 087948743, 938468364, TO_DATE('1985-05-06','YYYY-MM-DD'),
'F', 'aa111111a', 'Pilot', 12500)
Error report -
ORA-02290: check constraint (K1651915.SYS_C0074079) violated
This is my insert statement:
INSERT INTO Employee VALUES (1, 1, 'Gary', 'Turner', '5 Orpington
Street, Crawley, CR18 25J',
'garyturner#mail.com', 087948743, 938468364, TO_DATE('1985-05-
06','YYYY-
MM-DD'),
'F', 'aa111111a', 'Pilot', 12500);
It doesn't specify which column's check constraint it violates, so I'm confused.
What am I doing wrong and how can I fix it?
In short, your CHECK CONSTRAINT uses a REGEXP check, but is actually expecting a WHERE check.
If you want to use regular expressions inside your check constraint, you'll need to use REGEXP_LIKE:
CONSTRAINT ck_ni_number
CHECK (REGEXP_LIKE(ni_number, '[a-z][a-z][0-9][0-9][0-9][0-9][0-9][0-9][a-z]'))
This code would go at the end of your CREATE TABLE statement. I've declared it this way (caleld and out-of-line constraint) as it allows you to name the constraint and easily see the error will relate to ni_number.
Your entire CREATE TABLE statement would then look like this:
CREATE TABLE Employee (
Employee_ID NUMBER(10) NOT NULL PRIMARY KEY,
Company_ID NUMBER(10) NOT NULL REFERENCES Penrhyn_Jet_Charter(Company_ID),
First_Name VARCHAR2(255) NOT NULL,
Last_Name VARCHAR2(255) NOT NULL,
Address VARCHAR2(300) NOT NULL,
Email_Address VARCHAR2(255) NOT NULL UNIQUE,
Telephone_Number NUMBER(15) NOT NULL,
Mobile_Number NUMBER(15) NOT NULL UNIQUE,
Date_Of_Birth DATE NOT NULL,
Gender CHAR(1) NOT NULL,
NI_Number VARCHAR2(10) NOT NULL UNIQUE,
Job_Description VARCHAR2(255) NOT NULL,
Annual_Salary NUMBER(10) NOT NULL,
CONSTRAINT ck_ni_number
CHECK (REGEXP_LIKE(ni_number, '[a-z][a-z][0-9][0-9][0-9][0-9][0-9][0-9][a-z]')),
CONSTRAINT ck_tel_num CHECK (Telephone_Number > 0),
CONSTRAINT ck_email CHECK (Email_Address LIKE '%_#__%.__%'),
CONSTRAINT ck_mob_num CHECK (Mobile_Number > 0),
CONSTRAINT ck_gender CHECK (Gender = 'M' OR Gender = 'F'),
CONSTRAINT ck_an_salary CHECK (Annual_Salary > 0)
);
Also, I would suggest specifying the columns in your INSERT statement. Without the column names specified, Oracle will assume an order of your values, which is not guaranteed.
To do this:
INSERT INTO Employee
(employee_id, company_id, first_name, last_name, address,
email_address, telephone_number, mobile_number, date_of_birth,
gender, ni_number, job_description, annual_salary)
VALUES (1, 1, 'Gary', 'Turner',
'5 Orpington Street, Crawley, CR18 25J',
'garyturner#mail.com', 087948743, 938468364,
TO_DATE('1985-05-06','YYYY-MM-DD'),
'F', 'aa111111a', 'Pilot', 12500);
One other suggestion I would make is that you may want to consider making the phone_number and mobile_number fields VARCHAR2 instead of numeric. This is because phone numbers may start with 0, and phone numbers don't have any arithmetic performed on them. But that's outside the scope of your question!
Hope that resolves your issue.

SQL Error: ORA-00907: missing right parenthesis - CANNOT FIND ERROR

Having started at the same statements over and over again, I still cannot find the missing right parenthesis. It's appeared when running both of these statements and in order to run the rest of my tables I need to be able to locate the right parenthesis.
I've selected the parenthesis used in the statements and all seemed to match up to the right ones. Can anyone suggest anything?
CREATE TABLE booking (
bookingNo NUMBER(8) PRIMARY KEY,
customerNo NUMBER(8) NOT NULL
REFERENCES customer(customerNo),
registrationNo VARCHAR2(10) NOT NULL
REFERENCES vehicle(registrationNo),
dateOfBooking DATE NOT NULL
DEFAULT SYSDATE,
pickupStreetAddressLine VARCHAR2(30) NOT NULL,
pickupTown VARCHAR2(30) NOT NULL,
pickupPostcode VARCHAR2(10) NOT NULL,
startTime NUMBER(4,2) NOT NULL,
startDate DATE NOT NULL
DEFAULT SYSDATE,
endTime NUMBER(4,2) NOT NULL,
endDate DATE NOT NULL
DEFAULTSYSDATE,
noOfPassengers NUMBER(3) NOT NULL
CONSTRAINT CHECK(noOfPassengers > 0 AND noOfPassengers <= 73)
price NUMBER(8,2) NOT NULL
);
CREATE TABLE employees (
nationalInsuranceNo VARCHAR2(10) PRIMARY KEY,
fullName VARCHAR2(50) NOT NULL,
streetAddress VARCHAR2(30) NOT NULL,
town VARCHAR2(30),
postcode VARCHAR2(10) NOT NULL,
homeNo NUMBER(11)
dateOfBirth DATE NOT NULL,
gender VARCHAR2(8) NOT NULL
CONSTRAINT CHECK(gender="Male" OR gender="Female"),
jobDescription VARCHAR2(30) NOT NULL,
currentSalary NUMBER(6) NOT NULL
CONSTRAINT CHECK(currentSalary>0)
);
Try using a standard indentation scheme to visually verify your formatting.
Here I moved each significant paren to a newline and indented each piece separately.
I put comments next to a few items that seem wrong:
CREATE TABLE
booking
(
bookingNo NUMBER(8) PRIMARY KEY,
customerNo NUMBER(8) NOT NULL
REFERENCES customer(customerNo),
registrationNo VARCHAR2(10) NOT NULL
REFERENCES vehicle(registrationNo),
dateOfBooking DATE NOT NULL
DEFAULT SYSDATE,
pickupStreetAddressLine VARCHAR2(30) NOT NULL,
pickupTown VARCHAR2(30) NOT NULL,
pickupPostcode VARCHAR2(10) NOT NULL,
startTime NUMBER(4,2) NOT NULL,
startDate DATE NOT NULL
DEFAULT SYSDATE,
endTime NUMBER(4,2) NOT NULL,
endDate DATE NOT NULL
DEFAULT SYSDATE, -- THERE WAS A MISSING SPACE HERE *********************
noOfPassengers NUMBER(3) NOT NULL, -- MISSING COMMA ********
CONSTRAINT Check_noOfPassengers CHECK -- ADD A CONSTRAINT NAME *********
(
noOfPassengers > 0
AND noOfPassengers <= 73
), -- THERE WAS A MISSING COMMA HERE *******************
price NUMBER(8,2) NOT NULL
);
CREATE TABLE
employees
(
nationalInsuranceNo VARCHAR2(10) PRIMARY KEY,
fullName VARCHAR2(50) NOT NULL,
streetAddress VARCHAR2(30) NOT NULL,
town VARCHAR2(30),
postcode VARCHAR2(10) NOT NULL,
homeNo NUMBER(11), -- THERE WAS A MISSING COMMA HERE ***************
dateOfBirth DATE NOT NULL,
gender VARCHAR2(8) NOT NULL, -- MISSING COMMA *****
CONSTRAINT Check_gender CHECK -- ADD A CONSTRAINT NAME *********
(
gender="Male"
OR gender="Female"
),
jobDescription VARCHAR2(30) NOT NULL,
currentSalary NUMBER(6) NOT NULL, -- MISSING COMMA
CONSTRAINT Check_currentSalary CHECK -- ADD A CONSTRAINT NAME *********
(
currentSalary>0
)
);
I don't have Oracle installed to test, but is there a missing comma for the column noOfPassengers above? More generally, it may not be a missing right parens, but a parens that is out of place because of another syntax error. Hope that helps.
Change this DEFAULTSYSDATE to this DEFAULT SYSDATE on the 17th line
Also, add the column name before each CHECK and after the CONSTRAINT words. And add a comma after the first CONSTRAINT statement.
Hope it helps :)