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

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)

Related

I'm trying to create a primary key from 2 columns, but it doesn't work well

I'm learning Oracle by myself.
Here's my code:
create table Schedule
(
Schedule_SN number(10) primary key,
ScreeningDate date not null,
Price number(6) not null
);
create table Seat
(
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(4) not null
);
create table Reservation
(
Reservation_SN number(15) primary key,
DCtype number(2) not null,
DCamount number(7),
PaymentMethod number(1) not null,
TotalPrice number(7) not null,
ReservationDate date not null
);
create table Reservation_details ** I need help here **
(
Reservation_SN number(15) REFERENCES Reservation(Reservation_SN),
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(10) REFERENCES Seat(Seat_SN),
CONSTRAINT Reservation_detailesPK primary key (Reservation_SN, Schedule_SN)
);
Error messages:
Errors - ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement gives a column-list for which there is no matching unique or primary key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS catalog view
How can I make my 2 columns (Reservation_SN, Schedule_SN) into a primary key?
The problem is with seat_sn. You want child column in reservation_details to reference parent column in seat, but the parent column is not a primary or unique key. Actually, seat has no primary key; just make seat_sn the primay key of this table (if this fits your use case), and the rest should run fine:
create table seat (
schedule_sn nmber(10) references schedule(schedule_sn),
seat_sn varchar3(4) primary key
)
Demo on DB Fiddle

Why is parent key not found?

When I insert data into the ASSIGNMENTS table it all works fine except for the HARDWARE and SOFTWARE values, which are coded exactly in the same way as the others. I just don't get it.
This works:
INSERT INTO ASSIGNMENTS (ASSIGNMENT_ID, PROJECT_ID, STAFF_ID, JOB_ID)
VALUES ('A0005','B0002','ST002','J0002');
But when I try to include values for HARDWARE or SOFTWARE, like this:
INSERT INTO ASSIGNMENTS (ASSIGNMENT_ID, PROJECT_ID, STAFF_ID, JOB_ID, HARDWARE_ID)
VALUES ('A0005','B0002','ST002','J0002','H0002');
I just get the following error:
SQL Error: ORA-02291: integrity constraint (JAS1224.SYS_C0028418) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
Here is all the code, and all of the tables have been populated correctly (the hardware and software tables are coded in exactly the same format as the staff table):
CREATE TABLE PROJECT
(PROJECT_ID CHAR(5) NOT NULL,
PROJECT_NAME CHAR(20),
PROJECT_TYPE CHAR(20),
START_DATE DATE,
END_DATE DATE,
PRIMARY KEY (PROJECT_ID));
CREATE TABLE HARDWARE
(HARDWARE_ID CHAR(5) NOT NULL,
HARDWARE_NAME CHAR(20),
PRIMARY KEY (HARDWARE_ID));
CREATE TABLE SOFTWARE
(SOFTWARE_ID CHAR(5) NOT NULL,
SOFTWARE_NAME CHAR(20),
PRIMARY KEY (SOFTWARE_ID));
CREATE TABLE STAFF
(STAFF_ID CHAR(5) NOT NULL,
STAFF_NAME CHAR(20),
PRIMARY KEY (STAFF_ID));
CREATE TABLE JOB
(JOB_ID CHAR(5) NOT NULL,
JOB_TYPE CHAR(20),
JOB_GRADE CHAR(20),
PRIMARY KEY (JOB_ID));
CREATE TABLE ASSIGNMENTS
(ASSIGNMENT_ID CHAR(5) NOT NULL,
PROJECT_ID CHAR(5),
STAFF_ID CHAR(5),
JOB_ID CHAR(5),
HARDWARE_ID CHAR(5),
SOFTWARE_ID CHAR(5),
PRIMARY KEY (ASSIGNMENT_ID),
FOREIGN KEY (PROJECT_ID) REFERENCES PROJECT(PROJECT_ID),
FOREIGN KEY (STAFF_ID) REFERENCES STAFF(STAFF_ID),
FOREIGN KEY (JOB_ID) REFERENCES JOB(JOB_ID),
FOREIGN KEY (HARDWARE_ID) REFERENCES HARDWARE(HARDWARE_ID),
FOREIGN KEY (SOFTWARE_ID) REFERENCES SOFTWARE(SOFTWARE_ID));
The error message may be a bit cryptic at first, but it is pretty clear.
The only difference between the two INSERTs is for HARDWARE_ID. Hence 'H0002' is not a valid HARDWARE_ID. It is not in the HARDWARE table.

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

"no matching unique or primary key for this column-list". The primary key does exist though

So i'm practicing some sql coding for a test and I can't get a foreign key to reference a primary key.
Here's the table that doesn't work:
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
Here's the table it is referencing:
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
Whenever I try to run the script it always comes back with:
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I've looked around but can't seem to find the problem. Any help would be appreciated.
Here's the full code (haven't tested the last table yet):
CREATE TABLE EXPERTISE(
EXPERT_ID NUMBER(2) NOT NULL,
DESCRIPTION VARCHAR(50),
HOURLY_RATE NUMBER(3,2),
CHARGE_RATE NUMBER(3,2),
PRIMARY KEY(EXPERT_ID)
);
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
CREATE TABLE ALLOCATION(
EMP_ID NUMBER(3) NOT NULL,
ASSIGN_ID NUMBER(3) NOT NULL,
DAYS_WORKED_ON DATE,
HOURS_WORKED_ON DATE,
PRIMARY KEY(EMP_ID,ASSIGN_ID),
FOREIGN KEY(EMP_ID) REFERENCES EMPLOYEE(EMP_ID),
FOREIGN KEY(ASSIGN_ID) REFERENCES ASSIGNMENT(ASSIGN_ID)
);
I'm using Oracle SQL Developer to make it
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement gives a column-list for which there is no matching unique or primary key
constraint in the referenced table.
The problem is that EMP_ID (by itself) isn't a primary or unique key of table Employees, instead, you have a compound primary key (EMP_ID, EXPERT_ID).
To fix the issue either make EMP_ID the primary key of the Employees table (which seems intuitive as each employee ought to have a unique id) or add a separate unique constraint on EMP_ID.
As pointed out in the comments, if you make EMP_ID the primary key, then (EMP_ID, EXPERT_ID) will also be unique by extension.
As the error suggest, the column you've referenced a foreign key doesn't match a unique constraint/pk on the parent table. Specifically for the primary key EMP_ID, EXPERT_ID you reference only EMP_ID.

Oracle SQL Database error: "no matching unique or primary key for this column-list"

I'm trying to set up a database in Oracle sql developer, I've got these 3 tables.
I need the table "GuyAddress" to have 3 primary keys, which are all foreign keys as well. This is where I'm running into an error which I can't get my head around.
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
"number" NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number")
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
This is the error, hopefully someone can spot the mistake I made because I can't...
Error starting at line : 18 in command -
CREATE TABLE "GuyAddress"
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
)
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
Thanks!
You don't need separate foreign keys for each column in the referenced table's primary key - you can have multiple columns in a foreign key, e.g.:
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
address_number NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, address_number)
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address FOREIGN KEY(Address_zipcode, Address_number) REFERENCES Address(zipcode,address_number),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
Note that I've updated your address.number column to be address.address_number, as it's not recommended that you use a column based on a keyword. Using doublequotes to get around this (and also enforce case sensitivity) is not recommended either; you'll have to remember to use them every time you reference that column!
As an aside, I assume that your address table has other columns? Because as things stand, the address table is pointless and could be skipped!