Multiple foreign keys oracle - sql

In this database I have created 7 tables but in my last 2 tables, the billing and the product_billing table, I need to add foreign keys, I have used the method below but getting two different errors on each table when executing.
This is my code:
CREATE TABLE CUSTOMER(
CustomerID INT NOT NULL,
FirstName VARCHAR(50) NOT NULL,
Surname VARCHAR(50) NOT NULL,
Address VARCHAR(50)NOT NULL,
ContactNumber NUMBER NOT NULL,
Email VARCHAR(50) NOT NULL,
PRIMARY KEY(CustomerID));
CREATE TABLE EMPLOYEE(
EmployeeID INT NOT NULL,
FirstName VARCHAR(50) NOT NULL,
Surname VARCHAR(50) NOT NULL,
ContactNumber NUMBER NOT NULL,
Position VARCHAR(50)NOT NULL,
Address VARCHAR(50)NOT NULL,
Email VARCHAR(50) NOT NULL,
PRIMARY KEY(EmployeeID));
CREATE TABLE DELIVERY(
DeliveryID INT NOT NULL,
Description VARCHAR(50) NOT NULL,
DespatchDate DATE,
DeliveryDate DATE,
PRIMARY KEY(DeliveryID));
CREATE TABLE RETURNS(
ReturnID INT NOT NULL,
ReturnDate DATE NOT NULL,
Reason VARCHAR(50) NOT NULL,
PRIMARY KEY(ReturnID));
CREATE TABLE PRODUCT(
ProductID INT NOT NULL,
Product VARCHAR(50) NOT NULL,
Price VARCHAR(50) NOT NULL,
QTY INT NOT NULL,
PRIMARY KEY(ProductID));
CREATE TABLE BILLING(
BillID INT NOT NULL,
CONSTRAINT CustomerID FOREIGN KEY (CustomerID) REFERENCES CUSTOMER(CustomerID),
BillDate DATE,
CONSTRAINT EmployeeID FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEE(EmployeeID),
PRIMARY KEY(BillID));
CREATE TABLE PRODUCT_BILLING(
CONSTRAINT DeliveryID FOREIGN KEY (DeliveryID) REFERENCES DELIVERY(DeliveryID),
CONSTRAINT ReturnID FOREIGN KEY (ReturnID) REFERENCES RETURNS(ReturnID),
CONSTRAINT ProductID FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID),
CONSTRAINT BillID FOREIGN KEY (BillID) REFERENCES BILLING(BillID));
Those are the errors:
Error starting at line : 40 in command -
CREATE TABLE BILLING(
BillID INT NOT NULL,
CONSTRAINT CustomerID FOREIGN KEY (CustomerID) REFERENCES CUSTOMER(CustomerID),
BillDate DATE,
CONSTRAINT EmployeeID FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEE(EmployeeID),
PRIMARY KEY(BillID))
Error report -
ORA-00904: "CUSTOMERID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error starting at line : 47 in command -
CREATE TABLE PRODUCT_BILLING(
CONSTRAINT DeliveryID FOREIGN KEY (DeliveryID) REFERENCES DELIVERY(DeliveryID),
CONSTRAINT ReturnID FOREIGN KEY (ReturnID) REFERENCES RETURNS(ReturnID),
CONSTRAINT ProductID FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID),
CONSTRAINT BillID FOREIGN KEY (BillID) REFERENCES BILLING(BillID))
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:

Please use below syntax to create foreign key references,
CREATE TABLE BILLING(
BillID INT NOT NULL,
CustomerID INT REFERENCES CUSTOMER(CustomerID),
BillDate DATE,
EmployeeID INT REFERENCES EMPLOYEE(EmployeeID),
PRIMARY KEY(BillID));
CREATE TABLE PRODUCT_BILLING(
DeliveryID INT REFERENCES DELIVERY(DeliveryID),
ReturnID INT REFERENCES RETURNS(ReturnID),
ProductID INT REFERENCES PRODUCT(ProductID),
BillID INT REFERENCES BILLING(BillID));

Related

doctor schedulering design SQL server

Im trying to run this line of code to create a database to schedule doctor.
EmployeeID int primary key not null,
DPhone int not null,
DFirstName nvarchar (20) not null,
DLastName nvarchar (25) not null,
Specialization nvarchar (100) not null,
Cost int not null);
create table schedule(
DEmployeeID int not null, foreign key (DEmployeeID) references doctor (EmployeeID),
SDay nvarchar (9) not null,
SDate date not null,
STime time not null,
BookingStatus char not null,
primary key (DEmployeeID, SDate, STime),
PMedicalID int, foreign key (PMedicalID) references patient (MedicalID));
create table medRecords (
prescription nvarchar (100),
diagnosis nvarchar (100) not null,
Pdescription text not null,
medRecID int not null primary key,
MedicalID int, foreign key (MedicalID) references patient (MedicalID),
SDate date not null, foreign key (SDate) references schedule (SDate),
EmployeeID int not null, foreign key (EmployeeID) references doctor (EmployeeID))
This is the error
There are no primary or candidate keys in the referenced table 'Schedule' that match the referencing column list in the foreign key 'FK__MedRecord__SDate__3D5E1FD2'.
The Sdate is already a primary key so i don't understand why i get this error.
edit
I already have a patiens table created

Error ORA-00904 in SQL developer. Seemly no fix online that works that I can find while creating a table

I have seen many posts regarding the seemingly infamous ORA-00904 yet nothing seems to be helping. I am creating 2 tables (for now, more to come later) which are as follows;
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
);
and
CREATE TABLE Customer(
CustomerID INT NOT NULL PRIMARY KEY,
ProjectID INT NOT NULL,
FOREIGN KEY(ProjectID) REFERENCES Project(ProjectID), /*This is yet another table with a foreign key which needs to be sorted*/
CustomerName Char(255) NOT NULL,
PhoneNumber INT NOT NULL,
Region CHAR(255) NOT NULL
);
but every time I run it, I get error;
Error starting at line : 4 in command -
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY(customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Error report -
ORA-00904: "CUSTOMERID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
ANY HELP WOULD BE GREATLY APPRECIATED!
Oracle by default converts all names to UpperCase if you don't enquote them. (Edit: At the time of this answer the Foreign Key Constraint in the starting post did contain a quoted column name).
So, your constraint tries to find a column named "customerID" whereas the real name of your column is "CUSTOMERID".
Either enquote your column:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
"customerID" INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY ("customerID") REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Don't use quotes on your FOREIGN KEY constraint:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Or use quotes and write it uppercase in your foreign key constraint:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY ("CUSTOMERID") REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
As already mentioned, keeping the style consistent is recommended to prevent future problems, so Option 2 would be the easiest solution. Otherwise you need to enquote all columns to be consistent.
/Edit:
I just tried it - this definitely works with Oracle 12C:
CREATE TABLE Customer (
CustomerID INT,
PRIMARY KEY (CustomerID)
)
Followed by:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY(customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
The referenced table needs to created before the foreign key reference. One method is to create the tables first and then add the foreign key references:
CREATE TABLE Employee (
EmployeeID int NOT NULL,
PRIMARY KEY (EmployeeID),
customerID INT NOT NULL,
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
);
CREATE TABLE Customer (
CustomerID INT NOT NULL PRIMARY KEY,
ProjectID INT NOT NULL,
-- FOREIGN KEY(ProjectID) REFERENCES Project(ProjectID), /*This is yet another table with a foreign key which needs to be sorted*/
CustomerName Char(255) NOT NULL,
PhoneNumber INT NOT NULL,
Region CHAR(255) NOT NULL
);
alter table Employee add CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES Customer (CustomerID)
Here is a db<>fiddle.
Note that I commented out the foreign key constraint to Project.
Also, in most data models you can just order the table creates -- there are no cycles in the foreign key "linkages". However, because you have not defined Project, I am suggesting this more general approach.

SQL Error: ORA-02267 column type incompatible with referenced column type

Source code for oracle 12c
CREATE TABLE CUSTOMER (
CustomerID Char(20) NOT NULL,
CustomerFirstName Char(20) NOT NULL,
CustomerLastName Char(25) NOT NULL,
CustomerAddress Char(45) NOT NULL,
CustomerEmail Char(100) NOT NULL,
CONSTRAINT Customer_PK Primary Key(CustomerID)
CREATE TABLE EMPLOYEE (
EmployeeID VarChar(10) NOT NULL,
EmployeeFName VarChar(20) NOT NULL,
EmployeeRole VarChar(30) NOT NULL, CONSTRAINT
Employee_PK Primary Key(EmployeeID)
CREATE TABLE PRODUCT (
ProductID VarChar(10) NOT NULL,
ProductName VarChar(20) NOT NULL,
ProductType VarChar(20) NOT NULL,
ProductPrice Number(8,2) NOT NULL,
CONSTRAINT Product_PK Primary Key(ProductID)
CREATE TABLE CUSTORDER (
CustOrderID VarChar(10) NOT NULL,
CustOrderDate Date NOT NULL,
CustShippingStatus VarChar(20) NOT NULL,
SupplierID VarChar(20) NOT NULL,
CONSTRAINT CustOrder_PK Primary Key (CustOrderID),
CONSTRAINT CustOrder_FK FOREIGN KEY(SupplierID)
REFERENCES SUPPLIER(SupplierID)
CREATE TABLE SUPPLIER ( SupplierID Char(10) NOT NULL,
SupplierName Char(20) NOT NULL,
SupplierAddress Char(45) NOT NULL,
CONSTRAINT Supplier_PK Primary Key (SupplierID)
CREATE TABLE INVOICE (
InvoiceID Char(20) NOT NULL,
TotalItems Int NOT NULL,
TotalCost Number(8,2) NOT NULL,
SalesDate Date NOT NULL,
PaymentType VarChar(10) NOT NULL,
ProductID VarChar(10) NOT NULL,
EmployeeID VarChar(10) NOT NULL,
CustomerID Char(20) NOT NULL,
SupplierID Char(10) NOT NULL,
CONSTRAINT Invoice_PK Primary Key(InvoiceID),
CONSTRAINT Invoice_Product_FK Foreign Key(ProductID)
REFERENCES PRODUCT(ProductID),
CONSTRAINT Invoice_Employee_FK Foreign Key(EmployeeID)
REFERENCES EMPLOYEE(EmployeeID),
CONSTRAINT Invoice_Customer_FK Foreign Key(CustomerID)
REFERENCES CUSTOMER(CustomerID),
CONSTRAINT Invoice_Supplier_FK Foreign Key(SupplierID)
REFERENCES SUPPLIER(SupplierID)
You need to align the datatype and length of the referencing column of each foreign key with the column it references in the source table.
In your code, SUPPLIER(SupplierID) is declared as Char(20). On the other hand, referencing column INVOICE(SupplierID) is Char(10) and CUSTORDER(SupplierID) is VarChar(20). If you make these two columns Char(20), you code just works.
Aside: the referenced table must exist at the time when the referencing table is created - your code createst CUSTORDER before SUPPLIER. However I assume that's a typo when writing the question, otherwise you would be getting a different error: ORA-00942: table or view does not exist.
You also have a missing right parentheses at the end of each and every create table statement: I would consider these typos too.
Demo on DB Fiddle

SQL Server : code created and not sure why it is not processing - foreign key problem?

CREATE TABLE THREE_GIRLS_COFFEE_HUT
(
ShopName Char NOT NULL,
PhoneNumber Char(12) NOT NULL,
Address Char(20) NOT NULL,
City Char(20) NOT NULL,
State Char(2) NOT NULL,
ZipCode Char(5) NOT NULL,
CONSTRAINT ShopPK PRIMARY KEY (ShopName)
);
CREATE TABLE EMPLOYEE
(
EmployeeID Int NOT NULL IDENTITY(1,1),
EmployeeName Char(30) NOT NULL,
PhoneNumber Char(10) NOT NULL,
Address Char(20) NOT NULL,
City Char(20) NOT NULL,
State Char(2) NOT NULL,
ZipCode Char(5) NOT NULL,
EmployeeType Char(10) NOT NULL,
ShopName Char (25) FOREIGN KEY REFERENCES THREE_GIRLS_COFFEE_HUT (ShopName)
CONSTRAINT EmployeePK PRIMARY KEY(EmployeeID),
CONSTRAINT EmployeeAK1 UNIQUE(EmployeeName)
);
CREATE TABLE CUSTOMER
(
CustomerID Int NOT NULL IDENTITY(1000,1),
CustomerName Char(30) NULL,
PhoneNumber Char(10) NULL,
EmailAddress Char(30) NOT NULL,
CONSTRAINT CustomerPK PRIMARY KEY(CustomerID),
CONSTRAINT CustomerAK1 UNIQUE(EmailAddress)
);
CREATE TABLE [ORDER]
(
SalesOrderNumber INT NOT NULL IDENTITY (1500,1),
Date Numeric(6) NOT NULL,
Subtotal Numeric(6, 2) NOT NULL,
Tax Numeric(6, 2) NOT NULL,
Total Numeric(6, 2) NOT NULL,
CONSTRAINT OrderPK PRIMARY KEY (SalesOrderNumber),
CONSTRAINT EmpOrdFK FOREIGN KEY(EmployeeID)
REFERENCES EMPLOYEE(EmployeeID)
ON UPDATE CASCADE
ON DELETE NO ACTION,
CONSTRAINT CustOrdFK FOREIGN KEY (CustomerID)
REFERENCES CUSTOMER(CustomerID)
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
CREATE TABLE PRODUCT
(
ProductNumber Int NOT NULL IDENTITY(2000,1),
ProductDescription Char(20) NOT NULL,
QuantityOnOrder Numeric(4) NOT NULL,
QuantityOnHand Numeric(4) NOT NULL,
OrderDate Date NOT NULL,
ExpirationDate Date NOT NULL,
CONSTRAINT ProductPK PRIMARY KEY(ProductNumber),
CONSTRAINT ValidExpDate CHECK (ExpirationDate > OrderDate)
);
CREATE TABLE MENU_ITEM (
ItemNumber Int NOT NULL IDENTITY(3000,1),
ItemDescription Char(30) NOT NULL,
ItemCost Numeric(6,2) NOT NULL,
ProductNumber Int FOREIGN KEY REFERENCES PRODUCT(ProductNumber)
CONSTRAINT MenuPK PRIMARY KEY(ItemNumber),
CONSTRAINT MenuAK1 UNIQUE(ItemDescription),
);
CREATE TABLE ORDER_LINE_ITEM (
SalesOrderNumber INT FOREIGN KEY REFERENCES [ORDER](SalesOrderNumber),
ItemNumber INT FOREIGN KEY REFERENCES MENU_ITEM(ItemNumber),
Quantity Numeric NOT NULL,
UnitPrice Numeric(6,2) NOT NULL,
ExtendedPrice Numeric (6,2) NOT NULL,
);
I got these errors and it will not process- I think I have problem with my foreign key but I am not sure.
Msg 1769, Level 16, State 1, Line 40
Foreign key 'EmpOrdFK' references invalid column 'EmployeeID' in referencing table 'ORDER'.
Msg 1750, Level 16, State 0, Line 40
Could not create constraint or index. See previous errors.
There's no column EmployeeID in the ORDER table, so:
EmpOrdFK FOREIGN KEY(EmployeeID) REFERENCES EMPLOYEE(EmployeeID)
which says, create a foreign key in which Order.EmployeeId references Employee.EmployeeId, can't work.
Did you want to add an EmployeeId column to Order?
(There's also no CustomerId in Order, so the next line
CONSTRAINT CustOrdFK FOREIGN KEY (CustomerID)
would have failed too, except it never got to run because of the first error.)
You probably want to add EmployeeId and CustomerId to the Order table.
When you are mapping foreign key from Order table to Employee table, you have to map on common column in both tables. If you look at order table, which is missing employeeId in
that table.
And also don't call it as ORDER table. ORDER is reserved keyword in SQL server.
References: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-ver15

I've created 'Staff' table. In that table it has a self referencing key

create table STAFF
(StaffID TINYINT IDENTITY NOT NULL,
StaffName varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk foreign key (Mentor) references staff(StaffID)
);
when it is executed it gives the following error.
Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted
with the FOREIGN KEY SAME TABLE constraint "mentor_fk". The conflict
occurred in database "F_T", table "dbo.STAFF", column 'StaffID'. The
statement has been terminated.
Create the staff table first. Then add the foreign key:
create table STAFF (
StaffID TINYINT IDENTITY NOT NULL,
StaffName varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID)
);
alter table staff
add constraint mentor_fk foreign key (Mentor) references staff(StaffID);