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 :)
Related
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>
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 have done this and it works.
CREATE TABLE Employee (
Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
Office_ID varchar2(7) NOT NULL,
Emp_FirstName varchar2(20) NOT NULL,
Emp_LastName varchar2(20) NOT NULL,
Emp_Gender varchar2(1) NOT NULL,
Emp_DateOfBirth Date NOT NULL,
Hire_Date date NOT NULL,
Emp_CurrentDate date default sysdate,
Emp_Telephone varchar2(11) NOT NULL,
Emp_Email varchar2(30) NOT NULL,
Emp_AddressLine varchar2(50) NOT NULL,
Emp_PostCode varchar2(8) NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Qualification varchar2(20) NOT NULL,
Emp_AwardingBody varchar2(30) NOT NULL,
Emp_Salary number(6) NOT NULL,
Emp_Supervised_By varchar2(10) NOT NULL,
Employment_History1 varchar2(50),
Employment_History2 varchar2(50),
Employment_History3 varchar2(50) ,
CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID),
CONSTRAINT Hire_Date_CK check (Hire_Date < Emp_CurrentDate AND (Hire_Date - Emp_DateOfBirth)/365 > 18),
CONSTRAINT Emp_DateOfBirth_CK check (Emp_DateOfBirth > TO_DATE('1900-01-01', 'YYYY-MM-DD')),
CONSTRAINT Emp_Salary_CK check (Emp_Salary > 0 AND Emp_Salary < 150000),
CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')),
CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_#__%._%'),
CONSTRAINT Emp_Telephone_CK CHECK (regexp_like(Emp_Telephone, '^[0123456789]{11}$') AND Emp_Telephone like '0%'),
CONSTRAINT Emp_FirstName_CK CHECK (regexp_like(Emp_FirstName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
CONSTRAINT Emp_LastName_CK CHECK (regexp_like(Emp_LastName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
CONSTRAINT Emp_PostCode_CK CHECK (regexp_like ( Emp_PostCode , '([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})')),
CONSTRAINT Emp_Speciality_CK CHECK (regexp_like(Emp_Speciality, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
CONSTRAINT Emp_Qualification_CK CHECK (regexp_like(Emp_Qualification, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
CONSTRAINT Emp_AwardingBody_CK CHECK (regexp_like(Emp_AwardingBody, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,30}$'))
);
What I want to do now is to create a constraint that enables me to set the value of the attribute Emp_Supervised_By automatically as the same value of the Employee_ID attribute when the Emp_Speciality is 'Manager'.
I tried to do it like this but it doesn't work:
CONSTRAINT Emp_Supervised_By_CK check (CASE WHEN Emp_Speciality = 'MANAGER' THEN Emp_Supervised_By = Employee_ID)
Is a requirement to use CONSTRAINTS NOT TRIGGERS.
Because you mentioned computed columns, maybe your business requirement can be satisfied with the below instead of a constraint.
CREATE TABLE Employee (
...
Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Supervised_By AS (CASE WHEN Emp_Speciality = 'Manager' THEN Employee_ID ELSE NULL END)
....
);
I did this and it works:
CREATE TABLE OrderLine (
Invoice_ID varchar2(10) PRIMARY KEY NOT NULL,
Item_ID varchar2(10) NOT NULL,
Unit_Price number(6,2) NOT NULL,
Quantity number(6) NOT NULL,
Total_Price number(38,2) AS (CASE WHEN Quantity >= 50 THEN
((Unit_Price*Quantity)-((Unit_Price*Quantity)*0.80)) ELSE Unit_Price*Quantity END) NOT NULL,
CONSTRAINT fk_item_invoice FOREIGN KEY(Invoice_ID) REFERENCES Invoice (Invoice_ID),
CONSTRAINT fk_item_item FOREIGN KEY(Item_ID) REFERENCES Item (Item_ID),
CONSTRAINT Unit_Price_CK CHECK (Unit_Price > 0),
CONSTRAINT Quantity_CK CHECK (Quantity > 0),
CONSTRAINT Total_Price_CK CHECK (Total_Price > 0)
);
CREATE TABLE Invoice (
Invoice_ID varchar2(10) PRIMARY KEY NOT NULL,
Office_ID varchar2(7) NOT NULL,
Invoice_Date Date NOT NULL,
Total_Cost number (9) NOT NULL,
CONSTRAINT fk_order_Office FOREIGN KEY(Office_ID) REFERENCES Office (Office_ID),
CONSTRAINT Total_Cost_CK CHECK (Total_Cost > 0)
);
I want to calculate The TotalCost like the sum of TotalPrice(s).
Is there a way to use the same approach ass before ?
I want to add a constraint that checks if the first value of a string (in this case Product_id) is a particular character. In this case I need the first character to be a 'P', while the rest can be any combination of numeric characters.
Thus far I have this when creating the table,
CREATE TABLE PRODUCTS
(
Product_id varchar2(6) CHECK (Product_id, SUBSTRING(1,1) = "P");
Description varchar2(20) NOT NULL,
Qty_on_hand number(10) NOT NULL,
Reorder_level number(5),
Price_per_unit number(8,2)
);
It doesn't seem to work though. I'm sure it's a very simple syntax error but I can't spot it.
It is syntax error
CREATE TABLE PRODUCTS
(
Product_id varchar2(6) CHECK ( SUBSTR(Product_id,1,1) = 'P'),
Description varchar2(20) NOT NULL,
Qty_on_hand number(10) NOT NULL,
Reorder_level number(5),
Price_per_unit number(8,2)
);
CREATE TABLE PRODUCTS
(
Product_id varchar2(6) CHECK (Product_id like 'P%');
Description varchar2(20) NOT NULL,
Qty_on_hand number(10) NOT NULL,
Reorder_level number(5),
Price_per_unit number(8,2)
);
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
);