Oracle foreign key error invalid identifier - sql

CREATE TABLE Booking (
Name varchar2(30) NOT NULL,
R_date date NOT NULL,
D_address varchar2(30) NOT NULL,
Email_id varchar2(30) NOT NULL,
No_of_cars int NOT NULL,
Contact_no varchar2(15) NOT NULL,
Price int NOT NULL,
CONSTRAINT FK_CarBooking FOREIGN KEY (C_ID)
REFERENCES Car(C_ID)
);
CONSTRAINT FK_CarBooking FOREIGN KEY (C_ID)
ERROR at line 10:
ORA-00904: "C_ID": invalid identifier
In Car table C_ID is primary key. I don't understand why it's saying invalid identifier.

CONSTRAINT FK_CarBooking FOREIGN KEY (C_ID)
REFERENCES Car(C_ID)
The first C_ID must exist within the table Booking and the second in the Car table.
In order to solve this problem, add a field named C_ID inside the Booking table.

Related

ORA-00906: missing left parenthesis - LIES?

I have stared at this until my eyeballs bleed, where am I missing a parenthesis? It does also say
Error starting at line: 1 in command-".
The cause and action section of the error report is blank.
CREATE TABLE EVENTREQUEST(
EVENTNO VARCHAR2(8) CONSTRAINT EVENTNO_NOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELD_NOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQ_NOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
FACNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
DATEAUTH DATE,
STATUS VARCHAR2(15) CHECK (STATUS IN ('Pending', 'Denied', 'Approved')) CONSTRAINT STATUS_NOTNULL NOT NULL,
ESTCOAST VARCHAR2(30) CONSTRAINT ESTCOAST_NOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(30) CHECK(ESTAUDIENCE > 0) CONSTRAINT ESTAUDIENCE_NOTNULL NOT NULL,
BUDNO VARCHAR2(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY,
CONSTRAINT FK_CUSTONO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER(CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY(FACNO)
);
This just because you missed column name while declaring primary key. There is another problem: constraint name for both fourth and fifth columns are same. I have changed that too.
And there is no need to declare CONSTRAINT EVENTNO_NOTNULL NOT NULL since you are declaring it as primary key.
CREATE TABLE EVENTREQUEST(
EVENTNO VARCHAR2(8) CONSTRAINT EVENTNO_NOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELD_NOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQ_NOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
FACNO VARCHAR2(8) CONSTRAINT FACNO_NOTNULL2 NOT NULL,
DATEAUTH DATE,
STATUS VARCHAR2(15) CHECK (STATUS IN ('Pending', 'Denied', 'Approved')) CONSTRAINT STATUS_NOTNULL NOT NULL,
ESTCOAST VARCHAR2(30) CONSTRAINT ESTCOAST_NOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(30) CHECK(ESTAUDIENCE > 0) CONSTRAINT ESTAUDIENCE_NOTNULL NOT NULL,
BUDNO VARCHAR2(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY (EVENTNO),
CONSTRAINT FK_CUSTONO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER(CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY(FACNO));

Simple database with 3 tables and "no matching unique or primary key for this column"

I have three tables, two are created independently, and the third one is created to include some inputs from the first two. First two tables have no problems, however, when I try to create the third one, I get an error:
Error report -
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
The thing is, I created the third table by copy/pasting the column names/definitions right from the first two tables, yet I still get this ridiculous error message. Now I wonder if the order of the columns and the especially the order of constrains is important.
The tables:
comm_Customers
CREATE TABLE comm_Customers (
custID NUMBER(6) NOT NULL,
FirstName VARCHAR2(10) NOT NULL,
LastName VARCHAR2(15) NOT NULL,
HomeCountry VARCHAR2(2) NOT NULL,
HomeState_Prov VARCHAR2(2) NOT NULL,
HomeCity VARCHAR2(20) NOT NULL,
HomeAddress VARCHAR2(25) NOT NULL,
Phone NUMBER(10) NOT NULL,
Email VARCHAR2(15) NOT NULL,
ShippCountry VARCHAR2(2) NOT NULL,
ShippState_Prov VARCHAR2(2) NOT NULL,
ShippCity VARCHAR2(10) NOT NULL,
ShippAddress VARCHAR2(15) NOT NULL,
CONSTRAINT comm_customers_custid_pk PRIMARY KEY (custID)
);
comm_Items
CREATE TABLE comm_Items (
itemID NUMBER(4) NOT NULL,
ItemCat VARCHAR2(3) NOT NULL,
ItemQty NUMBER(4) NOT NULL,
SalePrice NUMBER(6,2) NOT NULL,
CostPrice NUMBER(6,2) NOT NULL,
ItemDesc VARCHAR2(15),
CONSTRAINT comm_items_itemid_pk PRIMARY KEY (itemID)
);
comm_Orders which gives the error
CREATE TABLE comm_Orders (
orderID NUMBER(10) NOT NULL,
OrderQty NUMBER(4) NOT NULL,
OrderDate DATE NOT NULL,
Shipped VARCHAR2(1),
ShippedDate DATE,
custID NUMBER(6) NOT NULL,
Phone NUMBER(10) NOT NULL,
Email VARCHAR2(15) NOT NULL,
ShippCountry VARCHAR2(2) NOT NULL,
ShippState_Prov VARCHAR2(2) NOT NULL,
ShippCity VARCHAR2(10) NOT NULL,
ShippAddress VARCHAR2(15) NOT NULL,
itemID NUMBER(4) NOT NULL,
SalePrice NUMBER(6,2) NOT NULL,
CONSTRAINT comm_order_orderid_pk PRIMARY KEY (orderID),
CONSTRAINT comm_order_custid_fk FOREIGN KEY (custID)
REFERENCES comm_Customers(custID),
CONSTRAINT comm_order_phone_fk FOREIGN KEY (Phone)
REFERENCES comm_Customers(Phone),
CONSTRAINT comm_order_email_fk FOREIGN KEY (Email)
REFERENCES comm_Customers(Email),
CONSTRAINT comm_order_shippcountry_fk FOREIGN KEY (ShippCountry)
REFERENCES comm_Customers(ShippCountry),
CONSTRAINT comm_order_shippstate_prov_fk FOREIGN KEY (ShippState_Prov)
REFERENCES comm_Customers(ShippState_Prov),
CONSTRAINT comm_order_shippcity_fk FOREIGN KEY (ShippCity)
REFERENCES comm_Customers(ShippCity),
CONSTRAINT comm_order_shippaddress_fk FOREIGN KEY (ShippAddress)
REFERENCES comm_Customers(ShippAddress),
CONSTRAINT comm_order_itemid_fk FOREIGN KEY (itemID)
REFERENCES comm_Items(itemID),
CONSTRAINT comm_order_saleprice_fk FOREIGN KEY (SalePrice)
REFERENCES comm_Items(SalePrice)
ON DELETE CASCADE,
CONSTRAINT comm_order_shipped_chk CHECK (Shipped IN ('Y','N'))
);
The references to non-primary key columns of customers and items do raise the error.
Bottom-line, you should not be duplicating the information from the referential tables. A single foreign key is sufficient.
So:
CREATE TABLE comm_Orders (
orderID NUMBER(10) NOT NULL,
OrderQty NUMBER(4) NOT NULL,
OrderDate DATE NOT NULL,
Shipped VARCHAR2(1),
ShippedDate DATE,
custID NUMBER(6) NOT NULL,
itemID NUMBER(4) NOT NULL,
CONSTRAINT comm_order_orderid_pk PRIMARY KEY (orderID),
CONSTRAINT comm_order_custid_fk FOREIGN KEY (custID) REFERENCES comm_Customers(custID),
CONSTRAINT comm_order_itemid_fk FOREIGN KEY (itemID) REFERENCES comm_Items(itemID),
CONSTRAINT comm_order_shipped_chk CHECK (Shipped IN ('Y','N'))
);
Then, whenever you need to recover an information from a referential table, you join it using the foreign key. Say you want the phone of a customer:
select o.*, c.phone
from comm_orders o
inner join comm_customers c on c.custid = o.custid

I'm getting "Invalid Identifier Error" for one of my Oracle Apex Queries

I'm trying to run a script on Oracle Apex and so far all the tables and queries work except the last one. It returns the error "ORA-00904: : invalid identifier 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." What should I do to fix this error?
CREATE TABLE customer(
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
sale_date DATE
CONSTRAINT sales__sale_date__fk
REFERENCES sales (sale_date) ON DELETE SET NULL,
c_name VARCHAR2(50)
CONSTRAINT sales__c_name__nn NOT NULL,
address VARCHAR2(50)
CONSTRAINT sales__address__nn NOT NULL,
phone VARCHAR2(11)
CONSTRAINT sales__phone__nn NOT NULL,
gender VARCHAR2(6)
CONSTRAINT sales__gender__nn NOT NULL,
a_income VARCHAR2(30)
CONSTRAINT sales__a_income__nn NOT NULL,
);
I don't know if it helps but VIN and sale_date reference these two working queries:
CREATE TABLE vehicles(
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__pk PRIMARY KEY,
brand VARCHAR2(20)
CONSTRAINT vehicles__brand__nn NOT NULL,
model VARCHAR2(20)
CONSTRAINT vehicles__model__nn NOT NULL,
color VARCHAR2(10)
CONSTRAINT vehicles__color__nn NOT NULL
);
CREATE TABLE sales(
sale_date DATE,
price VARCHAR2(30)
CONSTRAINT sales__price__nn NOT NULL,
VIN VARCHAR2(17)
CONSTRAINT vehicles__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
d_id VARCHAR2(10)
CONSTRAINT dealer__d_id__fk
REFERENCES dealer (d_id) ON DELETE SET NULL,
CONSTRAINT sales__sale_date__pk PRIMARY KEY (sale_date)
);
Remove the last comma.
Also, if your constraints have the naming convention <tablename>__<columnname>__<constrainttype> then don't just copy/paste from another table and update the column name; you need to update the table name as well or you will find you have duplicate constraint names which will raise an exception.
CREATE TABLE customer(
VIN VARCHAR2(17)
CONSTRAINT customer__VIN__fk
REFERENCES vehicles (VIN) ON DELETE SET NULL,
sale_date DATE
CONSTRAINT customer__sale_date__fk
REFERENCES sales (sale_date) ON DELETE SET NULL,
c_name VARCHAR2(50)
CONSTRAINT customer__c_name__nn NOT NULL,
address VARCHAR2(500)
CONSTRAINT customer__address__nn NOT NULL,
phone VARCHAR2(11)
CONSTRAINT customer__phone__nn NOT NULL,
gender CHAR(1)
CONSTRAINT customer__gender__nn NOT NULL
CONSTRAINT customer__gender__chk
CHECK ( gender IN ( 'M', 'F' /*, 'A', 'B', 'C'*/ ) ),
a_income NUMBER(12,2)
CONSTRAINT customer__a_income__nn NOT NULL
);
Then comes the other questions:
Why does a customer have a VIN (vehicle identification number)? A customer is not limited to owning a single car.
Why does a customer have a sale_date? If you are referring to a car sale then why is the customer limited to a single sale? You probably want to fix both these two by moving the data to a separate table called customer_cars (or something similar) so that each customer can own multiple cars and each car can be owned by multiple customers (at different times).
Do you expect all customers' addresses to fit in 50 characters?
Why is gender a VARCHAR(6) and not a CHAR(1) with values M/F (extend with additional character codes as appropriate)?
Why is a_income a string and not a NUMBER?

Oracle (ORA-02270) : Have tried following other answers but can not figure it out

Complete beginner here. I have been trying to mess around with this code and I stripped it back to this:
create table Customer
(
Customer_Num varchar2(7) not null,
Surname varchar2(50) not null,
Other_Names varchar2(100) not null,
Email varchar2(320) not null,
Mobile_Phone varchar2(20) not null,
constraint Customer_PK primary key (Customer_Num)
);
create table Store
(
Store_ID varchar2(5) not null,
Region varchar2(50) not null,
constraint Store_PK primary key (Store_ID)
);
create table Sale
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Customer_Num varchar2(7) not null,
Comments varchar2(4000),
constraint Product_PK primary key (Store_ID, Recorded_On),
constraint Sale_Store_FK foreign key (Store_ID) references Store(Store_ID),
constraint Sale_Customer_FK foreign key (Customer_Num) references Customer(Customer_Num)
);
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Value),
constraint Product_FK foreign key(Store_ID) references Store(Store_ID),
constraint Product_FK foreign key(Recorded_On) references Sale(Recorded_On)
);
Error starting at line : 67 in command -
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Value),
constraint Product_FK foreign key(Store_ID) references Store,
constraint Product_FK foreign key(Recorded_On) references Sale(Recorded_On)
)
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 in advance!
UPDATE *
I have changed code as follows
create table Customer
(
Customer_Num varchar2(7) not null,
Surname varchar2(50) not null,
Other_Names varchar2(100) not null,
Email varchar2(320) not null,
Mobile_Phone varchar2(20) not null,
constraint Customer_PK primary key (Customer_Num)
);
create table Store
(
Store_ID varchar2(5) not null,
Region varchar2(50) not null,
constraint Store_PK primary key (Store_ID)
);
create table Sale
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null UNIQUE,
Customer_Num varchar2(7) not null,
Comments varchar2(4000),
constraint Sale_PK primary key (Store_ID, Recorded_On),
constraint Sale_Store_FK foreign key (Store_ID) references Store(Store_ID),
constraint Sale_Customer_FK foreign key (Customer_Num) references Customer
);
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Store_ID, Recorded_On),
constraint Product_Store_FK foreign key(Store_ID) references Store,
constraint Product_recorded_FK foreign key(Recorded_On) references Sale(Recorded_On)
);
Now I run into this error when inserting statement:
INSERT INTO Product (Store_ID, Recorded_On, Product_Name, Value) VALUES ('AB1', to_date('10/05/2016 13:11', 'DD/MM/YYYY HH24:MI'), 'Test', 2.0);
Error starting at line : 80 in command -
INSERT INTO Product (Store_ID, Recorded_On, Product_Name, Value) VALUES ('AB1', to_date('10/05/2016 13:11', 'DD/MM/YYYY HH24:MI'), 'Test', 2.0)
Error report -
SQL Error: ORA-02291: integrity constraint (Hemi89.PRODUCT_RECORDED_FK) 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.
I am little confused here as I believe I have set parent key in constraint Sale_PK primary key (Store_ID, Recorded_On) in the Sales Table.
The foreign key you're trying to create on Recorded_On column in product table must refer to a primary key or an unique key. Recorded_On column in your Sale table must be changed to unique or don't create any constraints on it.
After you resolve this error, the next problem you'll run into is constraints not having unique names.
ORA-02264: name already used by an existing constraint
Constraint Name Product_PK & Product_FK gets repeated twice.

Crreate table with foreign keys

I m trying to create a table in sql, but it gives me an error when I add the foreign key from the table COURSE. With the other foreign keys it; Can anybody tell me why.
Here is my query
CREATE TABLE ENROLL (
Stu_ID NUMBER NOT NULL,
Prog_ID VARCHAR2(20) NOT NULL,
Crs_ID NUMBER NOT NULL,
Crs_Sec_ID VARCHAR2(20) NOT NULL,
Enroll_Outcome CHAR(1) NOT NULL,
PRIMARY KEY(Stu_ID),
FOREIGN KEY(Stu_ID) REFERENCES STUDENT(Stu_ID),
FOREIGN KEY(Prog_ID) REFERENCES PROGRAMS(Prog_ID),
FOREIGN KEY (Crs_Sec_ID) REFERENCES COURSE_SECTION (Crs_Sec_ID),
FOREIGN KEY(Crs_ID) REFERENCES COURSE(Crs_ID));
and here's the output
Error starting at line : 126 in command -
CREATE TABLE ENROLL (
Stu_ID NUMBER NOT NULL,
Prog_ID VARCHAR2(20) NOT NULL,
Crs_ID NUMBER NOT NULL,
Crs_Sec_ID VARCHAR2(20) NOT NULL,
Enroll_Outcome CHAR(1) NOT NULL,
PRIMARY KEY(Stu_ID),
FOREIGN KEY(Stu_ID) REFERENCES STUDENT(Stu_ID),
FOREIGN KEY(Prog_ID) REFERENCES PROGRAMS(Prog_ID),
FOREIGN KEY (Crs_Sec_ID) REFERENCES COURSE_SECTION (Crs_Sec_ID),
FOREIGN KEY(Crs_ID) REFERENCES COURSE(Crs_ID))
Error report -
SQL Error: ORA-02267: column type incompatible with referenced column type
02267. 00000 - "column type incompatible with referenced column type"
*Cause: The datatype of the referencing column is incompatible with the
AGAIN, it runs well with all of the other foreign key without the Crs_ID.
Basically what is says is Couse ID in Enroll table and Couse ID in Couse table are incompatible. Both columns should have the same types and properties (not null) in order to make a foreign key relationship.
Things to check:
Are they both have the same Column type (should be Number)?
Both are not support null?