Error when setting Foreign Key in SQL Oracle - sql

I've looked this up and I see that I'm doing it right but when I go to execute it, I get this error:
Error report -
SQL Error: ORA-00904: "HOSPITALCODE": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Here is my code:
CREATE TABLE "Hospital" (
HospitalCode number,
HospitalName varchar(75),
HospitalStreet varchar(100),
HospitalCity varchar(75),
HospitalState varchar(12),
HospitalZip number,
HospitalPhone varchar(15),
CONSTRAINT HospitalCode_pk
PRIMARY KEY(HospitalCode));
CREATE TABLE "Doctor" (
DoctorID number,
DoctorFirstName varchar(75),
DoctorLastName varchar(75),
DoctorOfficeNumber number,
DoctorPhone varchar(10),
CONSTRAINT DoctorID_pk
PRIMARY KEY(DoctorID),
CONSTRAINT HospitalCode_fk
FOREIGN KEY (HospitalCode)
REFERENCES "Hospital" (HospitalCode));

You are creating the foreign key HospitalCode_fk in the table doctor without creating the column HospitalCode in this table:
CREATE TABLE "Doctor" (
DoctorID number,
DoctorFirstName varchar(75),
DoctorLastName varchar(75),
DoctorOfficeNumber number,
DoctorPhone varchar(10),
HospitalCode number, /* added */
CONSTRAINT DoctorID_pk
PRIMARY KEY(DoctorID),
CONSTRAINT HospitalCode_fk
FOREIGN KEY (HospitalCode)
REFERENCES "Hospital" (HospitalCode)
);

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:

Oracle SQL "invalid identifier error" when trying to create a table

I have successfully built these tables:
CREATE TABLE Tables
(
tnum NUMBER(4) PRIMARY KEY,
floor NUMBER(1), CHECK (floor >= 0 AND floor <= 4),
capacity NUMBER(2), CHECK(capacity >= 0 AND capacity <= 25),
location VARCHAR(50)
);
CREATE TABLE Customer
(
name VARCHAR(25),
phone VARCHAR(16),
city VARCHAR(25),
PRIMARY KEY(name, phone)
);
CREATE TABLE Menu
(
pid NUMBER(4) PRIMARY KEY,
pname VARCHAR(25),
price NUMBER(4, 1)
);
BUT when I try build this one, I got an error:
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
CREATE TABLE Orders
(
orderid NUMBER(4) PRIMARY KEY,
custname VARCHAR(25),
CONSTRAINT fk_customer
FOREIGN KEY (custname)
REFERENCES Customer(customer.name),
phone VARCHAR(16),
CONSTRAINT fk_customer
FOREIGN KEY(phone)
REFERENCES Customer(customer.phone),
date DATE,
tnum NUMBER(4),
CONSTRAINT fk_tables
FOREIGN KEY(tnum)
REFERENCES Tables(tables.tnum),
numofdiners NUMBER(2)
);
I tried to understand what can cause the problem, even though I checked this link and some more other links, I didn't manage to solve the problem.
You can specify a column level constraint directly after the column, but then you have to get rid of the comma and the foreign key part as it's clear which column is meant.
But you need to include all columns of the primary key in the foreign key definition. And because the primary key of the customer table consist of two columns, you can't use an inline foreign key in the table orders.
The foreign key "references" part needs to specify the column name in the target table without a prefix for the table (the table is already clear because of the references table_xxx part, so there is no need to prefix the target column with the table name).
Additionally DATE is a reserved keyword.
CREATE TABLE Orders
(
orderid NUMBER(4) PRIMARY KEY,
custname VARCHAR(25) not null
phone VARCHAR(16) not null,
reservation_date DATE,
tnum NUMBER(4) --<< no comma here!
-- no FOREIGN KEY keyword here
CONSTRAINT fk_tables
REFERENCES Tables(tnum), --<< only the column name between the (...)
numofdiners NUMBER(2), --<< you need a comma before the table level constraints
CONSTRAINT fk_customer
FOREIGN KEY(name, phone) --<< list both columns here
REFERENCES Customer(name, phone) --<< and here
);
Note that the columns in a multi-column foreign key are matched by position, not by name.
Therefore, the following would be wrong
CONSTRAINT fk_customer
FOREIGN KEY(phone, name)
REFERENCES Customer(name, phone)

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)
);

(Oracle) Adding a constraint after a table has been made

I'm relatively new to Databases and Oracle and I'm atempting to make a database for a project involving 2 tables; Klant(customer) and Account_. These 2 both have each others PK as a foreign key. Since this is the case I had to make the foreign keys after the creation of the tables, resulting in this
DROP TABLE KLANT CASCADE CONSTRAINTS;
DROP TABLE ACCOUNT_ CASCADE CONSTRAINTS;
CREATE TABLE KLANT
(
"KlandId" INT PRIMARY KEY,
"AccountId" INT,
"Voornaam" VARCHAR2(64)NOT NULL,
"Achternaam" VARCHAR2(64) NOT NULL,
"GENDER" CHAR(1) DEFAULT 'M' CHECK (UPPER(GENDER) in ('M','F')),
"Tussenvoegsels" VARCHAR2(16),
"EmailAdres" VARCHAR2(64),
"Land" VARCHAR2(64) DEFAULT 'Nederland',
"Stad" VARCHAR2(64),
"Adres" VARCHAR2(64),
"Toevoeging" CHAR(1));
CREATE TABLE Account_
(
"AccountId" INT PRIMARY KEY,
"KlantId_" INT,
"GebruikersNaam" VARCHAR2(64)UNIQUE NOT NULL,
"Wachtwoord" VARCHAR2(64)
);
ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY (AccountId) REFERENCES "Account_"(AccountId);
ALTER TABLE Account_
ADD CONSTRAINT fk_klantId FOREIGN KEY (KlantId_) REFERENCES "Klant"(KlantId);
This yielded me the following error:
Error starting at line : 39 in command -
ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY (AccountId) REFERENCES "Account_"(AccountId)
Error report -
SQL Error: ORA-00904: "ACCOUNTID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Sorry for all the dutch words :(
My question being: Where did I mess up, because it probably is something riduculously stupid.
ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY ("AccountId") REFERENCES Account_("AccountId");
Name of the Account_ Table should not be in double quotes. Same for the other table .
Quote AccountId. In Oracle objects are generally uppercase. When you use AccountId without quoting it, Oracle will look for ACCOUNTID.

SQL Error: ORA-00904: : invalid identifier 3

I am trying to create a table (customer) to database by using this query,
CREATE TABLE customers(
"customer_id" VARCHAR2(20),
f_name VARCHAR2(30),
CONSTRAINT f_name_not_null NOT NULL,
l_name VARCHAR2(30),
CONSTRAINT l_name_not_null NOT NULL,
mobile_no VARCHAR2(30),
CONSTRAINT mobile_no_not_null NOT NULL,
address VARCHAR2(30),
CONSTRAINT address_not_null NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(customer_id),
CONSTRAINT mobile_no_address_unique UNIQUE(mobile_no,address));
i get the following back:
Error report - SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
There are two problems in this statement
first is the inconsistent usage of quoted column (as noted in comment by #a_horse_with_no_name )
second problem is that the column constraint should not be separated with a comma from the column definition
The correct statement with changes in comments is
CREATE TABLE customers(
/*"*/customer_id/*"*/ VARCHAR2(20),
f_name VARCHAR2(30) /*,*/
CONSTRAINT f_name_not_null NOT NULL,
l_name VARCHAR2(30)/*,*/
CONSTRAINT l_name_not_null NOT NULL,
mobile_no VARCHAR2(30)/*,*/
CONSTRAINT mobile_no_not_null NOT NULL,
address VARCHAR2(30) /*,*/
CONSTRAINT address_not_null NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(customer_id),
CONSTRAINT mobile_no_address_unique UNIQUE(mobile_no,address));
Interesting is that the error you see concerns the second issue, after resolving it you will get the error for the first issue which is more specific:
ORA-00904: "CUSTOMER_ID": invalid identifier