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>
Related
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
I am getting the ORA-00936: missing expression error message when creating a table in SQL using the following command. Any ideas where I'm tripping up?
CREATE TABLE Workorders (
Wo# VARCHAR2(8) PRIMARY KEY,
Proj# VARCHAR2(8) REFERENCES Project (Proj#),
Wo_desc VARCHAR2(30) NOT NULL UNIQUE,
Wo_assigned CHAR(40),
Wo_hours NUMBER(6) NOT NULL CHECK(>0),
Wo_start DATE,
Wo_due DATE,
Wo_complete CHECK (Wo_complete IN ('Y','N')));
You can't define check constraints like that, you need the column name in the check:
CREATE TABLE Workorders (
Wo# VARCHAR2(8) PRIMARY KEY,
Proj# VARCHAR2(8) REFERENCES Project (Proj#),
Wo_desc VARCHAR2(30) NOT NULL UNIQUE,
Wo_assigned CHAR(40),
Wo_hours NUMBER(6,0) NOT NULL CHECK ( wo_hours > 0 ),
Wo_start DATE,
Wo_due DATE,
Wo_complete CHAR(1) CHECK (Wo_complete IN ('Y','N') )
);
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.
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 :)
I try to build this table
CREATE TABLE OFFICER
(
ID int(8) PRIMARY KEY,
FIRST_NAME varchar2(20) NOT NULL,
LAST_NAME varchar2(20) NOT NULL,
HIRE_DATE date NOT NULL,
UNHIRE_DATE date,
SALARY int(7),
PHONE_NUMBER int(10),
TYPE varchar2(15) NOT NULL
);
Do I have to use any constraint, reference? and What I lack for this code?
INT data type doesn't allow scale specification. Try either ID int primary key or Id NUMBER(8) primary key.
Try this,
CREATE TABLE OFFICER
(
ID NUMBER(8) PRIMARY KEY,
FIRST_NAME varchar2(20) NOT NULL,
LAST_NAME varchar2(20) NOT NULL,
HIRE_DATE date NOT NULL,
UNHIRE_DATE date,
SALARY NUMBER(7),
PHONE_NUMBER NUMBER(10),
TYPE varchar2(15) NOT NULL
);