Getting "missing right parenthesis' error SQL--Cannot find the issue - sql

I am trying to create four tables below. My first two tables run correctly, but my last two error out with the same error message:
ORA-00907: missing right parenthesis
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200200", line 626
ORA-06512: at "SYS.DBMS_SYS_SQL", line 1658
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200200", line 612
ORA-06512: at "APEX_200200.WWV_FLOW_DYNAMIC_EXEC", line 1749
I cannot figure out what is wrong with this code. I recognize that it's probably a syntax error somewhere, but I cannot find it. What should I do to fix this error?
create table item
(
itemID number(5) constraint CAMP_itemID_pk primary key,
itemDescription varchar2(100),
cleaned varchar2(1),
stock number(5)
);
create table people
(
peopleID number(10) constraint CAMP_peopleID_pk primary key,
firstName varchar2(20),
lastName varchar2(20),
phone number(10),
discount varchar2(20)
);
create table checkout
(
checkoutID constraint CAMP_checkoutID_pk primary key number(10),
peopleID number(10),
checkoutDate date(),
returnDate date(),
saleTotal number(10),
returned varchar(1),
constraint CAMP_peopleID_fk foreign key (peopleID) references people(peopleID)
);
create table checkoutDetail
(
orderDetailID constraint CAMP_orderDetailID_pk primary key number(10),
checkoutID number(10),
itemID,
quantity number(5),
constraint CAMP_checkoutID_fk foreign key (checkoutID) references checkout(checkoutID),
constraint CAMP_itemID_fk foreign key (itemID) references item(itemID)
);

The syntax is to define a column with inline constraints is:
COLUMN_NAME DATATYPE CONTRAINTS
but you have the data type after the constraints.
Date has no precision (and is not a function) so should not have braces after it.
itemID needs a data type.
Like this:
create table checkout (
checkoutID number(10) constraint CAMP_checkoutID_pk primary key,
peopleID number(10),
checkoutDate date,
returnDate date,
saleTotal number(10),
returned varchar(1),
constraint CAMP_peopleID_fk foreign key (peopleID) references people(peopleID)
);
create table checkoutDetail (
orderDetailID number(10) constraint CAMP_orderDetailID_pk primary key,
checkoutID number(10),
itemID number(5),
quantity number(5),
constraint CAMP_checkoutID_fk foreign key (checkoutID) references checkout(checkoutID),
constraint CAMP_itemID_fk foreign key (itemID) references item(itemID)
);
db<>fiddle here

Related

'Missing keyword' this error is coming while i create a table

'Missing keyword' this error is coming while I create a table using the below query:
CREATE TABLE ACCTS
(
ACCT_NO NUMBER (12),
ACCTH_NAME VARCHAR2(50),
ACCTH_ADD VARCHAR2(100),
ACCTH_STATE VARCHAR2(50),
ACCTH_DOB DATE ,
ACCT_DT_CREATED DATE,
BRANCH_CODE NUMBER(5),
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_NO_PK PRIMARY KEY (ACCT_NO),
CONSTRAINT ACCTH_STATE_FK1 FOREIGN KEY (ACCTH_STATE) REFERENCES STATES (STATE_ID),
CONSTRAINT ACCT_TYPE_CODE_FK2 FOREIGN KEY (ACCT_TYPE_CODE) REFERENCES ACCT_TYPES(ACCT_TYPE_CODE)
)
I added your query to SQL Fiddle (adding the missing tables so the foreign key constraints will work) and the query works:
CREATE TABLE STATES
(
STATE_ID VARCHAR2(50),
CONSTRAINT ACCT_STATE_ID PRIMARY KEY (STATE_ID)
);
CREATE TABLE ACCT_TYPES
(
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_TYPES_ID PRIMARY KEY (ACCT_TYPE_CODE)
);
CREATE TABLE ACCTS
(
ACCT_NO NUMBER (12),
ACCTH_NAME VARCHAR2(50),
ACCTH_ADD VARCHAR2(100),
ACCTH_STATE VARCHAR2(50),
ACCTH_DOB DATE ,
ACCT_DT_CREATED DATE,
BRANCH_CODE NUMBER(5),
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_NO_PK PRIMARY KEY (ACCT_NO),
CONSTRAINT ACCTH_STATE_FK1 FOREIGN KEY (ACCTH_STATE) REFERENCES STATES (STATE_ID),
CONSTRAINT ACCT_TYPE_CODE_FK2 FOREIGN KEY (ACCT_TYPE_CODE) REFERENCES ACCT_TYPES(ACCT_TYPE_CODE)
)
http://sqlfiddle.com/#!4/b726da

How do I fix a ORA-00902: invalid datatype creating a table

I am creating a table in a command SQL sections into a script already populated I have created several tables already but in this one I get a message saying
ORA-00902: invalid datatype
CREATE TABLE Weapons
(
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2 (10),
CONSTRAINT pk_Weapons PRIMARY_KEY(id),
CONSTRAINT fk_Weapons_company
FOREIGN_KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo
FOREIGN_KEY(ammo_id) REFERENCES Ammo(id)
);
In the CONSTRAINT, it should be FOREIGN KEY and not FOREIGN_KEY. Also it should be PRIMARY KEY, not PRIMARY_KEY.
There is no underscore required as per syntax. So the query will be:
CREATE TABLE Weapons (
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2(10),
CONSTRAINT pk_Weapons PRIMARY KEY(id),
CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES Ammo(id)
);
About Foreign Keys: https://www.techonthenet.com/oracle/foreign_keys/foreign_keys.php
About Primary Keys: https://www.techonthenet.com/oracle/primary_keys.php
Here's a working example. I've created the AMMO table (whose description you didn't post, so I used only the ID column so that the foreign key constraint wouldn't fail). Pay attention to comments I wrote within the code.
SQL> create table ammo
2 ( id VARCHAR2(10),
3 CONSTRAINT pk_ammo PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Company
2 ( id VARCHAR(3),
3 name VARCHAR(30), --> switch from CHAR to VARCHAR2
4 CONSTRAINT pk_Company PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Weapons
2 ( id NUMBER(4),
3 name VARCHAR2(30),
4 damage NUMBER(4),
5 company_id VARCHAR2(3), --> should match COMPANY.ID datatype
6 ammo_id VARCHAR2(10), --> should match AMMO.ID datatype
7 CONSTRAINT pk_Weapons PRIMARY KEY(id),
8 CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
9 CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES ammo(id) );
Table created.
SQL>
In referential integrity constraint, you should match datatypes of the foreign and primary key columns. There's no sense in having a VARCHAR2(10) in the detail table which points to a VARCHAR2(3) column in the master table; you won't be able to put anything longer than 3 characters into the detail table's column anyway (foreign key constraint won't let you).

I'm trying to create a table with a foreign key but i keep getting the ORA-00904 error. What am I doing wrong?

create table reservation (
reserve_id number PRIMARY KEY,
date_in TIMESTAMP,
date_out TIMESTAMP,
made_by number(4),
constraint LocationID_fk foreign key (locId) references location(locId),
constraint fk_guest_id foreign key (guest_id) references guest(guest_id)
);
--these are the parent tables
create table guest(
guest_id NUMBER(3) PRIMARY KEY,
fname varchar(10),
lname varchar(5),
email varchar(10)
);
Create table location (
locId NUMBER(4) PRIMARY KEY,
loc_name varchar(10),
manager_name varchar(15)
);
-------error that keeps coming up
Error report -
SQL Error: ORA-00904: "LOCID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
I think you misunderstand what the constraint definitions do:
constraint LocationID_fk foreign key (locId) references location(locId),
constraint fk_guest_id foreign key (guest_id) references guest(guest_id)
Maybe you are under the impression that by defining the foreign key constraints on locId and guest_id, that it also automatically defines the 2 columns on the reservation table? That is not the case. You have to explicitly define the 2 columns besides the foreign key constraint definition. Something like:
create table reservation (
reserve_id number PRIMARY KEY,
date_in TIMESTAMP,
date_out TIMESTAMP,
made_by number(4),
locId number(4), -- explicitly defined
guest_id number(3) -- explicitly defined
constraint LocationID_fk foreign key (locId) references location(locId),
constraint fk_guest_id foreign key (guest_id) references guest(guest_id)
);

query not working for foreign key as a composite primary key of another table oracle SQL

I am trying to add two foreign keys for an associative entity in SQL Oracle. The primary key that I am referring from another table is a composite primary key. When I try to enter the SQL it says
no matching unique or primary key for this column-list
The code that I used to create the tbl_customer
CREATE TABLE tbl_Customer(
customer_id NUMBER(4)
CONSTRAINT pk_customer PRIMARY KEY,
CustomerName VARCHAR2(50) NOT NULL,
Telephone VARCHAR2(10),
CusEmail VARCHAR2(20),
CONSTRAINT cus_email UNIQUE(CusEmail),
location_id NUMBER(4)
CONSTRAINT fk_location_id references tbl_Location(location_id));
SQL to create tbl_Vehicle
CREATE TABLE tbl_Vehicle(
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT pk_v PRIMARY KEY(vehicle_id,PlateNo),
Brand VARCHAR2(20),
Model VARCHAR2(10),
TotalNoSeats NUMBER(2),
Class VARCHAR2(4) NOT NULL,
CONSTRAINT no_seats CHECK (TotalNoSeats<100),
driver_id NUMBER(4)
CONSTRAINT fk_driveridV references tbl_Driver(driver_id),
location_id NUMBER(4)
CONSTRAINT fk_locationV references tbl_Location(location_id));
The associative table is
CREATE TABLE tbl_Customer_Vehicle(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4)
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
);
where the error is in this line
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
*
Is this error because vehicle_id is a composite primary key?
Please help!!
You need to add PlateNo column in tbl_Customer_Vehicle table and define foreign key on it.
CREATE TABLE tbl_Customer_Vehicle
(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT fk_vehicle_id_PlateNo FOREIGN KEY(vehicle_id,PlateNo)
references tbl_Vehicle(vehicle_id,PlateNo)
);

ORA-02270 - Can't find fault

Having trouble with a ORA-02270 error. After going through the primary keys and foreign keys over and over, I can't find what the problem is.
CREATE TABLE CUSTACC(
REFNO CHAR(4),
ACCNO NUMBER(7),
CONSTRAINT CUSTACC_PK PRIMARY KEY (REFNO, ACCNO));
CREATE TABLE CUST(
CUSTNO CHAR(4),
NAME VARCHAR2(30),
ADDRESS VARCHAR2(50),
AREA VARCHAR2(10),
CONSTRAINT CUSTNO_PK PRIMARY KEY(CUSTNO),
CONSTRAINT CUSTNO_FK FOREIGN KEY (CUSTNO)
REFERENCES CUSTACC(REFNO));
CREATE TABLE ACC(
ACCNO NUMBER(7),
BALANCE NUMBER(8,2),
BRANCH VARCHAR2(10),
OPENED DATE,
BONUS NUMBER(8,2),
CONSTRAINT ACCNO_PK PRIMARY KEY (ACCNO),
CONSTRAINT ACCNO_FK FOREIGN KEY (ACCNO)
REFERENCES CUSTACC(ACCNO));
I must be blind but I've checked everything people have suggested before.
The primary key on CUSTACC consists of two columns: REFNO and ACCNO.
The foreign key reference should include both of them. So, your tables need both columns.
I suspect, in fact, that you want the foreign key reference in the first table, something like this:
CREATE TABLE CUST (
CUSTNO CHAR(4),
NAME VARCHAR2(30),
ADDRESS VARCHAR2(50),
AREA VARCHAR2(10),
CONSTRAINT CUSTNO_PK PRIMARY KEY(CUSTNO)
);
CREATE TABLE ACC (
ACCNO NUMBER(7),
BALANCE NUMBER(8,2),
BRANCH VARCHAR2(10),
OPENED DATE,
BONUS NUMBER(8,2),
CONSTRAINT ACCNO_PK PRIMARY KEY (ACCNO)
);
CREATE TABLE CUSTACC (
REFNO CHAR(4),
ACCNO NUMBER(7),
CONSTRAINT CUSTACC_PK PRIMARY KEY (REFNO, ACCNO),
CONSTRAINT CUSTNO_FK FOREIGN KEY (REFNO)
REFERENCES CUST(CUSTNO),
CONSTRAINT ACCNO_FK FOREIGN KEY (ACCNO)
REFERENCES ACC(ACCNO)
);
Here is a SQL Fiddle.