Can't create table in Oracle Developer - sql

Hi I am very new to SQL and having a problem for creating the table.
I have been looking at this code for the past two days now and I can not seem to get it to work. It keeps giving me "ORA-00907: missing right parenthesis" I know that this is a topic that comes up a lot but for some reason none of the examples I have seen has helped me. Can someone please tell me why I got this error and how do I fix it. I am pretty sure that it has nothing to do with my parenthesis, maybe its my CONTRAINTS
Here is my Code
create TABLE Employee
(
EmployeeID VARCHAR(10) PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Phone INT(10) NOT NULL,
JobTitle VARCHAR(10) NOT NULL
);
create TABLE Airplane
(
AirplaneID VARCHAR(10) PRIMARY KEY,
Capacity INT(1000) NOT NULL,
Modle VARCHAR(10) NOT NULL
);
create TABLE Route
(
FlightID VARCHAR(10) PRIMARY KEY,
Origin VARCHAR(20) NOT NULL,
Destination VARCHAR(20) NOT NULL,
ETD INT(10) NOT NULL,
ETA INT(10) NOT NULL
);
create TABLE Customer
(
CustomerID VARCHAR(10) PRIMARY KEY,
Name VARCHAR(10) NOT NULL,
PhoneNumber INT(30) NOT NULL
);
create TABLE Maintenance
(
MaintenanceID VARCHAR(20) PRIMARY KEY,
MaintenanceDate date NOT NULL,
AirplaneID VARCHAR(10) NOT NULL,
EmployeeID VARCHAR(10) NOT NULL,
FOREIGN KEY (AirplaneID) REFERENCES Airplane(AirplaneID),
FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)
);
create TABLE Flight
(
FlightID VARCHAR(10),
FlightDate date,
AirplaneID VARCHAR(10),
ATD INT(10) NOT NULL,
ATA INT(10) NOT NULL,
FOREIGN KEY (FlightID) REFERENCES Flight(FlightID),
FOREIGN KEY (AirplaneID) REFERENCES Airplane(AirplaneID),
CONSTRAINT PK_FlightID PRIMARY KEY (FlightID,FlightDate)
);
create TABLE Reservation
(
ReservationID VARCHAR(20) PRIMARY KEY,
CustomerID VARCHAR(10) NOT NULL,
FlightID VARCHAR(10) NOT NULL,
FlightDate date NOT NULL,
Fare float,
PaymentMethod VARCHAR(20),
CardNumber INT(30) NOT NULL,
ExperationDate date,
check (PaymentMethod = "Cash" OR PaymentMethod ="Credit" OR PaymentMethod
="Cheque",
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID),
FORIGN KEY (FlightID,FlightDate) REFERENCES Flight(FlightID,FlightDate)
);
create TABLE CrewResveration
(
EmployeeID VARCHAR(10),
FlightID VARCHAR(10),
FlightDate date NOT NULL,
Role VARCHAR(20) NOT NULL,
ShiftHour INT(10) NOT NULL,
FORIGN KEY (FlightID,FlightDate) REFERENCES Flight(FlightID,FlightDate)
FORIGNKEY (EmployeeID) REFERENCES Employee(EmployeeID)
CONSTRAINT PK_CrewAssignment PRIMARY (FlightID,FlightDate,EmployeeID)
);
Here are the results I get when I run the code:
Error starting at line : 1 in command -
create TABLE Employee
(
EmployeeID VARCHAR(10) PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Phone INT(20) NOT NULL,
JobTitle VARCHAR(10) NOT NULL
)
Error at Command Line : 5 Column : 10
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error starting at line : 10 in command -
create TABLE Airplane
(
AirplaneID VARCHAR(10) PRIMARY KEY,
Capacity INT(1000) NOT NULL,
Modle VARCHAR(10) NOT NULL
)
Error at Command Line : 13 Column : 13
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error starting at line : 18 in command -
create TABLE Route
(
FlightID VARCHAR(10) PRIMARY KEY,
Origin VARCHAR(20) NOT NULL,
Destination VARCHAR(20) NOT NULL,
ETD INT(10) NOT NULL,
ETA INT(10) NOT NULL
)
Error at Command Line : 23 Column : 8
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error starting at line : 28 in command -
create TABLE Customer
(
CustomerID VARCHAR(10) PRIMARY KEY,
Name VARCHAR(10) NOT NULL,
PhoneNumber INT(30) NOT NULL
)
Error at Command Line : 32 Column : 16
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error starting at line : 36 in command -
create TABLE Maintenance
(
MaintenanceID VARCHAR(20) PRIMARY KEY,
MaintenanceDate date NOT NULL,
AirplaneID VARCHAR(10) NOT NULL,
EmployeeID VARCHAR(10) NOT NULL,
FOREIGN KEY (AirplaneID) REFERENCES Airplane(AirplaneID),
FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)
)
Error at Command Line : 42 Column : 37
Error report -
SQL Error: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Error starting at line : 46 in command -
create TABLE Flight
(
FlightID VARCHAR(10),
FlightDate date,
AirplaneID VARCHAR(10),
ATD INT(10) NOT NULL,
ATA INT(10) NOT NULL,
FOREIGN KEY (FlightID) REFERENCES Flight(FlightID),
FOREIGN KEY (AirplaneID) REFERENCES Airplane(AirplaneID),
CONSTRAINT PK_FlightID PRIMARY KEY (FlightID,FlightDate)
)
Error at Command Line : 51 Column : 8
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error starting at line : 60 in command -
create TABLE Reservation
(
ReservationID VARCHAR(20) PRIMARY KEY,
CustomerID VARCHAR(10) NOT NULL,
FlightID VARCHAR(10) NOT NULL,
FlightDate date NOT NULL,
Fare float,
PaymentMethod VARCHAR(20),
CardNumber INT(30) NOT NULL,
ExperationDate date,
check (PaymentMethod = "Cash" OR PaymentMethod ="Credit" OR PaymentMethod
="Cheque",
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID),
FORIGN KEY (FlightID,FlightDate) REFERENCES Flight(FlightID,FlightDate)
)
Error at Command Line : 68 Column : 15
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error starting at line : 76 in command -
create TABLE CrewResveration
(
EmployeeID VARCHAR(10),
FlightID VARCHAR(10),
FlightDate date NOT NULL,
Role VARCHAR(20) NOT NULL,
ShiftHour INT(10) NOT NULL,
FORIGN KEY (FlightID,FlightDate) REFERENCES Flight(FlightID,FlightDate)
FORIGNKEY (EmployeeID) REFERENCES Employee(EmployeeID)
CONSTRAINT PK_CrewAssignment PRIMARY (FlightID,FlightDate,EmployeeID)
)
Error at Command Line : 82 Column : 14
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:

The error description is misleading, but the pointer (line 5, column 10) points to exactly after reading the word "INT" ... which, is unknown by him. He should state he doesn't understand that, but he mentions something with parenthesis, which ironically are actually correct. Unfortunately, it happens on occasions that error messages are misleading, and you have to think one step further.

The problem is with your data types. INT is not a valid Oracle datatype. Try this instead:
create TABLE Employee ( EmployeeID VARCHAR(10) PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Phone NUMBER(10) NOT NULL,
JobTitle VARCHAR(10) NOT NULL );

Related

sql tables cant be imported for unknown reason

my tables don't want to be imported from vertabelo and I honestly don't know why. I'm using an oracle connection. if someone could give me a hand with it, it would be kind of you. I'll provide the code below with the errors it's giving. tables equipment, coach and rent are working. however, tables customer and training cant be imported and I honestly don't know what to do.
-- tables
-- Table: coach
CREATE TABLE coach (
id integer NOT NULL,
first_name varchar(255) NOT NULL,
second_name varchar(255) NOT NULL,
CONSTRAINT coach_pk PRIMARY KEY (id)
);
-- Table: customer
CREATE TABLE customer (
id int NOT NULL,
first_name Varchar(55) NOT NULL,
second_name Varchar(55) NOT NULL,
birth_date DateTime NULL,
skill_level int NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY (id)
);
-- Table: equipment
CREATE TABLE equipment (
id int NOT NULL,
price int NOT NULL,
name_of_equipment int NOT NULL,
category_of_equipment int NOT NULL,
CONSTRAINT equipment_pk PRIMARY KEY (id)
);
-- Table: rent
CREATE TABLE rent (
equipment_id int NOT NULL,
customer_id int NOT NULL,
number_of_days date NOT NULL,
CONSTRAINT rent_pk PRIMARY KEY (number_of_days)
);
-- Table: training
CREATE TABLE training (
coach_id int NOT NULL,
customer_id int NOT NULL,
training_time_start time NOT NULL,
training_time_end time NOT NULL,
training_place_id int NOT NULL,
CONSTRAINT training_pk PRIMARY KEY (training_place_id)
);
-- foreign keys
-- Reference: rent_customer (table: rent)
ALTER TABLE rent ADD CONSTRAINT rent_customer FOREIGN KEY rent_customer (customer_id)
REFERENCES customer (id);
-- Reference: rent_equipment (table: rent)
ALTER TABLE rent ADD CONSTRAINT rent_equipment FOREIGN KEY rent_equipment (equipment_id)
REFERENCES equipment (id);
-- Reference: training_coach (table: training)
ALTER TABLE training ADD CONSTRAINT training_coach FOREIGN KEY training_coach (coach_id)
REFERENCES coach (id);
-- Reference: training_customer (table: training)
ALTER TABLE training ADD CONSTRAINT training_customer FOREIGN KEY training_customer (customer_id)
REFERENCES customer (id);
Error starting at line : 3 in command -
CREATE TABLE coach (
id integer NOT NULL,
first_name varchar(255) NOT NULL,
second_name varchar(255) NOT NULL,
CONSTRAINT coach_pk PRIMARY KEY (id)
)
Error report -
ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object"
*Cause:
*Action:
Error starting at line : 11 in command -
CREATE TABLE customer (
id int NOT NULL,
first_name Varchar(55) NOT NULL,
second_name Varchar(55) NOT NULL,
birth_date DateTime NULL,
skill_level int NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY (id)
)
Error report -
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Error starting at line : 21 in command -
CREATE TABLE equipment (
id int NOT NULL,
price int NOT NULL,
name_of_equipment int NOT NULL,
category_of_equipment int NOT NULL,
CONSTRAINT equipment_pk PRIMARY KEY (id)
)
Error report -
ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object"
*Cause:
*Action:
Error starting at line : 30 in command -
CREATE TABLE rent (
equipment_id int NOT NULL,
customer_id int NOT NULL,
number_of_days date NOT NULL,
CONSTRAINT rent_pk PRIMARY KEY (number_of_days)
)
Error report -
ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object"
*Cause:
*Action:
Error starting at line : 38 in command -
CREATE TABLE training (
coach_id int NOT NULL,
customer_id int NOT NULL,
training_time_start time NOT NULL,
training_time_end time NOT NULL,
training_place_id int NOT NULL,
CONSTRAINT training_pk PRIMARY KEY (training_place_id)
)
Error report -
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Error starting at line : 49 in command -
ALTER TABLE rent ADD CONSTRAINT rent_customer FOREIGN KEY rent_customer (customer_id)
REFERENCES customer (id)
Error report -
ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
Error starting at line : 53 in command -
ALTER TABLE rent ADD CONSTRAINT rent_equipment FOREIGN KEY rent_equipment (equipment_id)
REFERENCES equipment (id)
Error report -
ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
Error starting at line : 57 in command -
ALTER TABLE training ADD CONSTRAINT training_coach FOREIGN KEY training_coach (coach_id)
REFERENCES coach (id)
Error report -
ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
Error starting at line : 61 in command -
ALTER TABLE training ADD CONSTRAINT training_customer FOREIGN KEY training_customer (customer_id)
REFERENCES customer (id)
Error report -
ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:

Creating a Table called PRESENTER -- Encountering ERROR ORA-00907

CREATE TABLE PRESENTER(
PresenterNo VARCHAR2(9) NOT NULL,
PresenterName VARCHAR2(45) NOT NULL,
Biography VARCHAR2(500) NOT NULL,
InstitutionName VARCHAR2(45) NOT NULL,
CONTRAINT labPK PRIMARY KEY (PresenterNo));
Having following error:
Error starting at line : 4 in command -
CREATE TABLE PRESENTER(
PresenterNo VARCHAR2(9) NOT NULL,
PresenterName VARCHAR2(45) NOT NULL,
Biography VARCHAR(500) NOT NULL,
InstitutionName VARCHAR2(45) NOT NULL,
CONTRAINT labPK PRIMARY KEY (PresenterNo))
Error report -
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
You misspelled the CONSTRAINT keyword. Correct way is down below.
CREATE TABLE PRESENTER
(
PresenterNo VARCHAR2 (9) NOT NULL,
PresenterName VARCHAR2 (45) NOT NULL,
Biography VARCHAR (500) NOT NULL,
InstitutionName VARCHAR2 (45) NOT NULL,
CONSTRAINT labPK PRIMARY KEY (PresenterNo)
)

Error report - ORA-00904: : invalid identifier 00904. 00000 - "%s: invalid identifier"

This is my SQL Oracle script and I cannot figure out why I keep getting the following error when I try and run this code:
I keep getting this exact code on each table I try to run. Can anyone point me in the right direction as to why my code will not run? I have tried researching this myself ALL DAY LONG and cant get it.**
Error starting at line : 12 in command -
CREATE TABLE COURSE (
CRS_CODE VARCHAR (8) NOT NULL,
CRS_DESCRIPTION VARCHAR (35) NOT NULL,
CONSTRAINT CRS_CREDIT NOT NULL,
CHECK (CRS_CREDIT IN (1,2,3,4)),
PRIMARY KEY (CRS_CODE))
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
SQL>
**CODE BEFORE ERROR:**
/* This script file creates the following tables:*/
/*COURSE, CLASS, ENROLL, STUDENT */
set echo on;
set serveroutput on ;
/*DROP TABLE COURSE;
DROP TABLE CLASS;
DROP TABLE STUDENT;
DROP TABLE ENROLL;*/
CREATE TABLE COURSE (
CRS_CODE VARCHAR (8) NOT NULL,
CRS_DESCRIPTION VARCHAR (35) NOT NULL,
CONSTRAINT CRS_CREDIT NOT NULL,
CHECK (CRS_CREDIT IN (1,2,3,4)),
PRIMARY KEY (CRS_CODE));
CREATE TABLE CLASS (
CLASS_CODE NUMBER (5) NOT NULL,
CONSTRAINT CLASS_SECTION_Ck NOT NULL,
CLASS_TIME VARCHAR (25) NOT NULL,
CLASS_ROOM CHAR (6),
CHECK (CLASS_SECTION IN (1,2,3,4,5,6,7,8)),
PRIMARY KEY (CLASS_CODE),
FOREIGN KEY (CRS_CODE) REFERENCES COURSE (CRS_CODE));
CREATE TABLE STUDENT (
STU_NUM INTEGER NOT NULL,
STU_LNAME VARCHAR (25) NOT NULL,
STU_FNAME VARCHAR (20) NOT NULL,
STU_INIT CHAR (1),
STU_DOB DATE NOT NULL,
CONSTRAINTS STU_HRS_Ck DEFAULT 0 NOT NULL,
STU_CLASS CHARACTER(2) REFERENCES "Fr", "So", "Jr", "Gr" NOT NULL,
CONSTRAINT STU_GPA_Ck DEFAULT 0.00 NOT NULL,
STU_PHONE VARCHAR (4) NOT NULL,
PRIMARY KEY (STU_NUM),
CHECK (STU_HRS <= 0),
CHECK (STU_HRS >= 1000),
CHECK (STU_GPA BETWEEN 0.00 AND 4.00));
CREATE TABLE ENROLL (
ENROLL_GRADE_Ck VARCHAR (1) REFERENCES "A","B","C","D","F","I","W", "Z", DEFAULT Z NOT NULL,
FOREIGN KEY (STU_NUM) REFERENCES STUDENT (STU_NUM),
FOREIGN KEY (CLASS_CODE) REFERENCES CLASS (CLASS_CODE),
PRIMARY KEY (CLASS_CODE));
commit;
It is not clear from your question whether you are trying to create a COLUMN level CONSTRAINT or not. If you are trying to create a COLUMN level CONSTRAINT, then there seems to be a syntax error in your CONSTRAINT definition (a comma before the word CONSTRAINT).
See the following link from Oracle:
https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13590.html
It has examples on how to define constraints including COLUMN level constraints. You may want to compare your CONSTRAINT clause with the examples to see if there are errors in your CONSTRAINTS.

SQL- Invalid identifier when creating foreign key- SQL Error: ORA-00904

Whenever I run the script I keep getting this error:
Error report - SQL Error: ORA-00904: "HORSE_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
I am trying to make a foreign key linking the HORSES and MEDICAL table. I think everything looks correct. Can someone let me know what I have done incorrectly? I am using Oracle SQLDeveloper.
CREATE TABLE HORSES (
Horse_ID varchar(10) NOT NULL Primary Key,
Name varchar(50) NOT NULL,
Height varchar(50) NOT NULL,
Weight varchar(50) NOT NULL,
Breed varchar(50) NOT NULL,
Surrender varchar(50) NOT NULL,
Seize varchar(50) NOT NULL,
Score varchar(15) NOT NULL,
Aq_Date varchar(10) NOT NULL,
Ridable varchar(50) NOT NULL,
Trim varchar(50) NOT NULL,
Age varchar(50) NOT NULL
);
/* MEDICAL */
/* ------------------------------------------------------------ */
CREATE TABLE MEDICAL (
Med_ID varchar(50) NOT NULL primary key,
Feed_Ins varchar(4000) NOT NULL,
Special_Vet varchar(2000) NOT NULL,
Coggins varchar(50) NOT NULL,
Vaccs varchar(50) NOT NULL,
Deworm varchar(50) NOT NULL,
CONSTRAINT Horse_ID_HORSES_FK FOREIGN KEY (Horse_ID) REFERENCES HORSES(Horse_ID)
);
You don't have a horse_id column in medical, how do you want it to be fk for that table?

oracle sql dev: SQL Error: ORA-00905: missing keyword 00905. 00000 - "missing keyword"

I am using oracle sql developer.
On executing the following code, I get a vague sounding error.
CREATE TABLE students
(
student_id INT NOT NULL,
username VARCHAR2(30),
email VARCHAR2(80),
password VARCHAR2(30),
f_name VARCHAR2(30),
l_name VARCHAR2(30),
bio VARCHAR2(350),
dp VARCHAR2(15),
is_suspended CHAR(1) DEFAULT '0' NOT NULL,
suspension_reason VARCHAR2(50),
role_id INT NOT NULL,
created_on TIMESTAMP DEFAULT SYSDATE NOT NULL,
updated_on TIMESTAMP,
is_active CHAR(1) DEFAULT '1' NOT NULL,
city VARCHAR2(15) NOT NULL,
state VARCHAR2(15) NOT NULL,
zip VARCHAR(6) NOT NULL,
b_day DATE,
CONSTRAINT students_id_pk PRIMARY KEY(student_id),
CONSTRAINT students_role_id_fk FOREIGN KEY(role_id),
CONSTRAINT students_username_uq UNIQUE(username),
CONSTRAINT students_email_uq UNIQUE(email)
);
The error is:
Error report -
SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
please advice.
The correct syntax for a foreign key constraint is:
CONSTRAINT students_role_id_fk FOREIGN KEY(role_id) REFERENCES roles(role_id)
----------------------------------------------------^
You are missing the REFERENCES keyword and the appropriate table/column afterwards (which may not be correct in the above answer).
This is at least one error.