I keep getting this error when I attempt to create tables. This is what I have so far:
CREATE TABLE Club
( Club_ID NUMBER(7) NOT NULL
, Club_Name VARCHAR2(35) NOT NULL
, Street_Addr VARCHAR2(40) NOT NULL
, City VARCHAR2(20) NOT NULL
, State CHAR(2) NOT NULL
, Zipcode NUMBER(7) NOT NULL
, Phone NUMBER(10) NOT NULL
, CONSTRAINT club_pk PRIMARY KEY (Club_ID)
)
CREATE TABLE Member
( Member_ID NUMBER(10) NOT NULL
, First_Name VARCHAR2(20) NOT NULL
, Last_Name VARCHAR2(30) NOT NULL
, Street_Addr VARCHAR2(40) NOT NULL
, City VARCHAR2(20) NOT NULL
, State CHAR(2) NOT NULL
, Zipcode NUMBER(7) NOT NULL
, Phone NUMBER(10) NOT NULL
, CONSTRAINT member_pk PRIMARY KEY (Member_ID)
) ;
I don't get any errors from each individually, but as soon as I put them together like this I run into problems. Adding a semicolon between the two CREATE TABLE sections causes a different error ("ORA-00911: invalid character"). What should I be doing differently?
Thanks!
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 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 am having trouble identifying what is causing the following errors in my code:
2 0.00 CREATE TABLE BRANCH ( BRANCH_ID NUMBER(3) , BRANCH_NAME VARC ORA-00904: "LIBRARY_ID": invalid identifier -
3 0.00 CREATE TABLE COPY ( BOOK_ID NUMBER(3) , BRACH_ID NUMBER(3) , ORA-00904: "BRANCH_ID": invalid identifier -
4 0.00 CREATE TABLE PATRON ( PATRON_ID NUMBER(3) , FIRST_NAME VARCH ORA-02263: need to specify the datatype for this column -
5 0.01 CREATE TABLE CHECKOUT ( CHECKOUT_ID NUMBER(3) , COPY_ID NUMB ORA-00942: table or view does not exist -
6 0.00 CREATE TABLE WAITLIST ( BOOK_ID NUMBER(3) , PATRON_ID NUMBER ORA-00942: table or view does not exist
I cannot pinpoint where the lack of a datatype is or why my identifier is considered invalid. I am still learning and appreciate any help. Thank you guys in advance. Here is my SQL code:
CREATE TABLE LIBRARY_SYSTEM
( LIBRARY_ID NUMBER(3)
, LIBRARY_NAME VARCHAR2(50)
, STREET_ADDRESS VARCHAR2 (60)
, CITY VARCHAR2 (30)
, STATE CHAR (2)
, ZIPCODE NUMBER (5)
, PHONE NUMBER (10)
, MANAGER_NAME VARCHAR2(40)
, CONSTRAINT LIBARY_PK PRIMARY KEY (LIBRARY_ID)
);
CREATE TABLE BRANCH
( BRANCH_ID NUMBER(3)
, BRANCH_NAME VARCHAR2(50)
, STREET_ADDRESS VARCHAR2 (60)
, CITY VARCHAR2 (30)
, STATE CHAR (2)
, ZIPCODE NUMBER (5)
, PHONE NUMBER (10)
, BRANCH_MANAGER VARCHAR2(40)
, LIBARY_ID NUMBER(3)
, CONSTRAINT BRANCH_PK PRIMARY KEY (BRANCH_ID)
, CONSTRAINT BRANCH_LIBARY_FK FOREIGN KEY (LIBRARY_ID) REFERENCES LIBRARY_SYSTEM(LIBRARY_ID)
);
CREATE TABLE COPY
( BOOK_ID NUMBER(3)
, BRACH_ID NUMBER(3)
, CONSTRAINT COPY_BOOK_PK PRIMARY KEY (BOOK_ID, BRANCH_ID)
, CONSTRAINT COPY_BOOK_FK FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BOOK_ID)
, CONSTRAINT COPY_BRANCH_FK FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH(BRANCH_ID)
);
CREATE TABLE PATRON
( PATRON_ID NUMBER(3)
, FIRST_NAME VARCHAR2(40)
, LAST_NAME VARCHAR2(40)
, STREET_ADDRESS VARCHAR2 (60)
, CITY VARCHAR2 (30)
, STATE CHAR (2)
, ZIPCODE NUMBER (5)
, PHONE NUMBER (10)
, FEE_BALANCE
, CONSTRAINT PATRON_PK PRIMARY KEY (PATRON_ID)
);
CREATE TABLE CHECKOUT
( CHECKOUT_ID NUMBER(3)
, COPY_ID NUMBER(3)
, PATRON_ID NUMBER(3)
, OUT_DATE DATE
, DUE_DATE DATE
, RETURN_DATE DATE
, CONSTRAINT CHECKOUT_PK PRIMARY KEY (CHECKOUT_ID)
, CONSTRAINT CHECKOUT_COPY_FK FOREIGN KEY (COPY_ID) REFERENCES COPY(COPY_ID)
, CONSTRAINT CHECKOUT_PATRON_FK FOREIGN KEY (PATRON_ID) REFERENCES PATRON(PATRON_ID)
);
CREATE TABLE WAITLIST
( BOOK_ID NUMBER(3)
, PATRON_ID NUMBER(3)
, BRANCH_ID NUMBER(3)
, ON_DATE DATE
, OFF_DATE DATE
, RETURN_DATE DATE
, CONSTRAINT COPY_BOOK_FK FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BOOK_ID)
--, CONSTRAINT CHECKOUT_PATRON_FK FOREIGN KEY (PATRON_ID) REFERENCES PATRON(PATRON_ID)---
--, CONSTRAINT COPY_BRANCH_FK FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH(BRANCH_ID)
);
--sequences
CREATE SEQUENCE LIBRARY_SEQ INCREMENT BY 1 START WITH 1;
CREATE SEQUENCE BRANCH_SEQ INCREMENT BY 1 START WITH 1;
CREATE SEQUENCE PATRON_SEQ INCREMENT BY 1 START WITH 1;
--triggers
CREATE OR REPLACE TRIGGER INSERT_LIBRARY
BEFORE INSERT ON LIBRARY_SYSTEM
FOR EACH ROW
DECLARE
NEW_ID NUMBER;
BEGIN
SELECT LIBRARY_SEQ.NEXTVAL INTO NEW_ID FROM DUAL;
:NEW.LIBRARY_ID := NEW_ID;
END;
/
COMMIT;
CREATE OR REPLACE TRIGGER INSERT_BRANCH
BEFORE INSERT ON BRANCH
FOR EACH ROW
DECLARE
NEW_ID NUMBER;
BEGIN
SELECT BRANCH_SEQ.NEXTVAL INTO NEW_ID FROM DUAL;
:NEW.BRANCH_ID := NEW_ID;
END;
/
COMMIT;
CREATE OR REPLACE TRIGGER INSERT_PATRON
BEFORE INSERT ON PATRON
FOR EACH ROW
DECLARE
NEW_ID NUMBER;
BEGIN
SELECT PATRON_SEQ.NEXTVAL INTO NEW_ID FROM DUAL;
:NEW.PATRON_ID := NEW_ID;
END;
/
COMMIT;
There seem to be just a few misspellings:
CREATE TABLE BRANCH
( BRANCH_ID NUMBER(3)
, BRANCH_NAME VARCHAR2(50)
, STREET_ADDRESS VARCHAR2 (60)
, CITY VARCHAR2 (30)
, STATE CHAR (2)
, ZIPCODE NUMBER (5)
, PHONE NUMBER (10)
, BRANCH_MANAGER VARCHAR2(40)
**, LIBARY_ID NUMBER(3)**
, CONSTRAINT BRANCH_PK PRIMARY KEY (BRANCH_ID)
, CONSTRAINT BRANCH_LIBARY_FK FOREIGN KEY (LIBRARY_ID) REFERENCES LIBRARY_SYSTEM(LIBRARY_ID)
);
CREATE TABLE COPY
( BOOK_ID NUMBER(3)
, **BRACH_ID** NUMBER(3)
, CONSTRAINT COPY_BOOK_PK PRIMARY KEY (BOOK_ID, BRANCH_ID)
, CONSTRAINT COPY_BOOK_FK FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BOOK_ID)
, CONSTRAINT COPY_BRANCH_FK FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH(BRANCH_ID)
);
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
);