How to insert multiple rows into an Order table? - sql

So I set up a simple database and I am at the point where I am trying to test that it is working correctly. I am trying to figure out how I get multiple entries into an Order table. I need to have an order# and be able to order multiple items and use multiple shippers and such. I just can't seem to figure out a way to get the data into my DB properly.
Below is how I have my DB set up. Could someone please explain to me how to get the my test data into the DB.
Here are the reference tables:
CREATE TABLE Product_Table (
ProductID INT NOT NULL,
Product_Name char(50) NOT NULL,
Product_Cost number(9,2) NOT NULL,
Product_In_Stock INT NOT NULL,
CONSTRAINT Products_PK PRIMARY KEY (ProductID)
);
CREATE TABLE Payment_Terms_Table (
PayTermNum INT NOT NULL,
Payment_Time_Frame CHAR(20) NOT NULL,
CONSTRAINT Payment_Terms_PK PRIMARY KEY (PayTermNum)
);
CREATE TABLE Shipper_Table (
ShipperNum INT NOT NULL,
Shipper_Name CHAR(50) NOT NULL,
Shipper_Phone CHAR(22) NULL,
CONSTRAINT ShipperNum_PK PRIMARY KEY (ShipperNum)
);
CREATE TABLE Supplier_Table (
SupplierID INT NOT NULL,
Supplier_Name CHAR(50) NOT NULL,
Sup_Address CHAR(50) NOT NULL,
Sup_City CHAR(20) NOT NULL,
Sup_State CHAR(20) NOT NULL,
Sup_Zip CHAR(9) NOT NULL,
Sup_Phone CHAR(22) NULL,
ShipperNum INT NOT NULL,
PayTermNum INT NOT NULL,
CONSTRAINT Supplier_PK PRIMARY KEY (SupplierID),
CONSTRAINT ShipperNum_Relationship FOREIGN KEY (ShipperNum)
REFERENCES Shipper_Table (ShipperNum),
CONSTRAINT PayTermNum_Relationship FOREIGN KEY (PayTermNum)
REFERENCES Payment_Terms_Table (PayTermNum)
);
Here is my Order table:
CREATE TABLE Order_Table (
OrderID INT NOT NULL,
ProductID INT NOT NULL,
SupplierID INT NOT NULL,
Wholesale_Price NUMBER (9,2) NOT NULL,
Units_Ordered INT NOT NULL,
Order_Date DATE DEFAULT SYSDATE NOT NULL,
Order_Received DATE NULL,
CONSTRAINT Order_PK PRIMARY KEY (OrderID),
CONSTRAINT ProductID_Relationship FOREIGN KEY (ProductID)
REFERENCES Product_Table (ProductID),
CONSTRAINT SupplierID_Relationship FOREIGN KEY (SupplierID)
REFERENCES Supplier_Table (SupplierID)
);

Your problem is that you have a defined one table to hold Orders, so you can have only one Item per Order. Normally we handle this scenario by having two tables: a Header with the information for the whole Order and a Line table for each ordered Item.
CREATE TABLE Order_Header (
OrderID INT NOT NULL,
Order_Date DATE DEFAULT SYSDATE NOT NULL,
Order_Received DATE NULL,
CONSTRAINT Order_PK PRIMARY KEY (OrderID)
)
/
CREATE TABLE Order_Line (
OrderID INT NOT NULL,
LineNo INT NOT NULL,
ProductID INT NOT NULL,
SupplierID INT NOT NULL,
Wholesale_Price NUMBER (9,2) NOT NULL,
Units_Ordered INT NOT NULL,
CONSTRAINT Order_Line_PK PRIMARY KEY (OrderID, LineNo),
CONSTRAINT Order_Line_Header_FK FOREIGN KEY (OrderID)
REFERENCES Order_Header (OrderID)
CONSTRAINT ProductID_Relationship FOREIGN KEY (ProductID)
REFERENCES Product_Table (ProductID),
CONSTRAINT SupplierID_Relationship FOREIGN KEY (SupplierID)
REFERENCES Supplier_Table (SupplierID)
)
/
I have declared a composite primary key because it is easier to understand what's happening.
Now that you have two tables you can easily create orders with more than one line.
On the subject of normal practice, an Order should have a CUSTOMER. That would be an attribute of ORDER_HEADER. Also you have a SHIPPER table but don't use it. Again that would probably be an attribute of ORDER_HEADER.
Also your naming convention is ugly. There's no need to include _TABLE: just name the objects for teh things they represent. Likewise your foreign key needs are opaque; it doesn't matter so much in this toy example but in a real database you will find it helpful to specify the child and parent tables in the FK names.

Related

How can I correct the following SQL script?

I am assigned to create a database in SQL based on an ERD that I studied and recreated a week prior. I am using this app called "Oracle SQL Developer" and trying to learn about creating tables, primary keys, foreign keys, sequences, views, etc. I tested my drafts out on the developer and they keep on coming up with the following errors:
[enter image description here][1]
[1]: https://i.stack.imgur.com/vk0cu.png this is some syntax error due to partially recognized rules.
Other errors involve missing right parentheses, tables having more than one primary key, etc. So far, this is my best effort at starting a database:
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER
(CUST_EMAIL VARCHAR(10) PRIMARY KEY,
CUST_NAME VARCHAR(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR(10) NOT NULL,
CUST_CITY VARCHAR(10) NOT NULL,
CUST_STATE VARCHAR(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS
(ORDER_NO NUMERIC(10) PRIMARY KEY,
CUST_EMAIL VARCHAR(10) FOREIGN KEY,
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE(10) NOT NULL,
SHIP_DATE DATE(10) NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO)
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT
(PAY_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
CARD_TYPE VARCHAR(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE(10) NOT NULL,
PAY_CONF INTEGER(10) NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY
(DEL_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
SHIP_DATE DATE(10) FOREIGN KEY,
DEL_DATE DATE(10) NOT NULL,
STATUS VARCHAR(10) NOT NULL,
DEL_MODE VARCHAR(10) NOT NULL,
INVOICE_NO INTEGER(10) NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (SHIP_DATE) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT
(STREET_ADDRESS VARCHAR(10) PRIMARY KEY,
NAME VARCHAR(10) NOT NULL,
CITY VARCHAR(10) NOT NULL,
STATE VARCHAR(10) NOT NULL,
ZIP INTEGER(10) NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
Where should I place my parentheses if the app is correct in saying that I am missing some of them? Where do I even have more than one primary key and how can I rephrase my lines to reduce them? How can I take my rules from partially recognized to fully recognized?
This is for a college project on relational database systems. I just need to create some tables, primary keys, and foreign keys so I can be allowed to create sequences.
DATE and INTEGER do not have a precision.
Either declare the PRIMARY KEY inline or out-of-line but you cannot do both.
Same for foreign keys (and inline foreign keys need the REFERENCES keyword and not the FOREIGN KEY keywords).
VARCHAR would be better as VARCHAR2
You cannot have a FOREIGN KEY that refers to a non-primary key, non-unique column (i.e. SHIP_DATE). While you could create a UNIQUE composite key on ORDER_NO and SHIP_DATE and reference that (example below); it is probably better to entirely remove SHIP_DATE from the DELIVERY table (and then you don't need a foreign key) and just keep it in a single table so the tables are in 3rd normal form. If you want the information to display it then JOIN the tables using the ORDER_NO foreign key.
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER(
CUST_EMAIL VARCHAR2(10),
CUST_NAME VARCHAR2(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR2(10) NOT NULL,
CUST_CITY VARCHAR2(10) NOT NULL,
CUST_STATE VARCHAR2(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS(
ORDER_NO NUMERIC(10),
CUST_EMAIL VARCHAR2(10),
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE NOT NULL,
SHIP_DATE DATE NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO),
CONSTRAINT U_PRODUCTS UNIQUE (ORDER_NO, SHIP_DATE),
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER
);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT(
PAY_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
CARD_TYPE VARCHAR2(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE NOT NULL,
PAY_CONF INTEGER NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY(
DEL_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
SHIP_DATE DATE, -- Delete this line
DEL_DATE DATE NOT NULL,
STATUS VARCHAR2(10) NOT NULL,
DEL_MODE VARCHAR2(10) NOT NULL,
INVOICE_NO INTEGER NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS,
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (ORDER_NO, SHIP_DATE) REFERENCES PRODUCTS (ORDER_NO, SHIP_DATE) -- Delete this line.
);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT(
STREET_ADDRESS VARCHAR2(10),
NAME VARCHAR2(10) NOT NULL,
CITY VARCHAR2(10) NOT NULL,
STATE VARCHAR2(10) NOT NULL,
ZIP INTEGER NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
db<>fiddle here

No unique index found for the referenced field of the primary table

When I run the booking table, it show the message "No unique index found for the referenced field of the primary table".
Create Table Customer
CREATE TABLE CUSTOMER
(
CUSTID INTEGER NOT NULL,
FNAME CHAR(18) NOT NULL,
LNAME CHAR(18) NOT NULL,
STREET CHAR(6) NOT NULL,
CITY CHAR(18) NOT NULL,
PROVINCE CHAR(8) NOT NULL,
COUNTRY CHAR(8) NOT NULL,
POSTCODE CHAR(6) NOT NULL,
GENDER CHAR(6) NOT NULL,
PRIMARY KEY (CUSTID)
);
Create Table Booking
CREATE TABLE BOOKING
(
BKGNO INTEGER NOT NULL,
CUSTID INTEGER NOT NULL,
FNO INTEGER NOT NULL,
STATUSID CHAR(3) NOT NULL,
CLASSID CHAR(4) NOT NULL,
ORIG CHAR(18) NOT NULL,
DEST CHAR(18),
DEPTTIME DATE NOT NULL,
ARRTIME DATE NOT NULL,
BKGCITY CHAR(18) NOT NULL,
PAIDBY CHAR(18) NOT NULL,
FPRICE CURRENCY NOT NULL,
TOTPRICE CURRENCY NOT NULL,
PAIDAMT CURRENCY NOT NULL,
BAL CURRENCY NOT NULL,
BKGDATE DATE NOT NULL,
PRIMARY KEY (BKGNO),
INDEX (ORIG,DEST,DEPTTIME, ARRTIME),
INDEX (CUSTID),
FOREIGN KEY (CUSTID) REFERENCES CUSTOMER,
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO),
FOREIGN KEY(DEST) REFERENCES FLIGHT_AVAILABILITY(DEST),
FOREIGN KEY(DEPTTIME) REFERENCES FLIGHT_AVAILABILITY(DEPTTIME),
FOREIGN KEY(ARRTIME) REFERENCES FLIGHT_AVAILABILITY(ARRTIME),
FOREIGN KEY(DEST,DEPTTIME,ARRTIME) REFERENCES FLIGHT_AVAILABILITY(DEST, DEPTTIME, ARRTIME),
FOREIGN KEY (ORIG) REFERENCES AIRPORT(AIRPORTCD)
);
Create Table Flight Availability
CREATE TABLE FLIGHT_AVAILABILITY
(
FNO INTEGER NOT NULL,
ORIG CHAR(18) NOT NULL,
DEST CHAR(18),
DEPTTIME DATE NOT NULL,
ARRTIME DATE NOT NULL,
FLENGTH INTEGER NOT NULL,
PRIMARY KEY (FNO,ORIG,DEST,DEPTTIME,ARRTIME)
);
Create TableAirport
CREATE TABLE AIRPORT
(
AIRPORTCD CHAR(18) NOT NULL,
CITYID CHAR(18) NOT NULL,
AIRPORTNM CHAR(18) NOT NULL,
AIRPORTTAX CURRENCY,
PRIMARY KEY (AIRPORTCD)
);
This part causes errors:
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO),
FOREIGN KEY(DEST) REFERENCES FLIGHT_AVAILABILITY(DEST),
FOREIGN KEY(DEPTTIME) REFERENCES FLIGHT_AVAILABILITY(DEPTTIME),
FOREIGN KEY(ARRTIME) REFERENCES FLIGHT_AVAILABILITY(ARRTIME),
FOREIGN KEY(DEST,DEPTTIME,ARRTIME) REFERENCES FLIGHT_AVAILABILITY(DEST, DEPTTIME, ARRTIME)
A foreign key constraint (also called a referential integrity
constraint) designates a column as the foreign key and establishes a
relationship between that foreign key and a specified primary or
unique key, called the referenced key. A composite foreign key
designates a combination of columns as the foreign key.
So a referenced key should be either unique or primary key.
For example:
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO)
Referenced key = FNO which is not unique and is not a primary key. Although FNO is part of the composite PRIMARY KEY (FNO,ORIG,DEST,DEPTTIME,ARRTIME), it's not unique (the combination of all the columns of the primary key is unique).
P.S. you really don't need so many columns in your primary key in FLIGHT_AVAILABILITY table. You may create a composite unique index on those columns (if it's really required) and create a NOT NULL constraint on each column. In this case you'll have the same checks as in your current composit primary key. But I don't think you need this.

There are no primary or candidate keys in the referenced table 'PayRoll' that match the referencing column list in the foreign key 'fk_EmployeeNumber'

I'm having some problem with the table called HoursandEarnings and I kept getting an error saying,
"There are no primary or candidate keys in the referenced table
'PayRoll' that match the referencing column list in the foreign key
'fk_EmployeeNumber'"
create table Address
(
PostalCode nvarchar(6) not null,
Address nvarchar(50) not null,
City nvarchar(30) not null,
Province nvarchar(30) not null,
constraint pk_postalcode primary key(PostalCode)
)
create table Payperiod
(
StartDate DateTime not null,
EndDate DateTime not null,
constraint pk_StartDate primary key(StartDate)
)
create table PayRoll
(
EmployeeNumber nvarchar(30) not null,
StartDate DateTime not null
constraint fk_StartDate references Payperiod(StartDate),
PostalCode nvarchar(6) not null
constraint fk_PostalCode references Address(PostalCode),
Department nvarchar(50) not null,
TotalEarningsCurrent decimal(5,2) not null,
TotalEarningsYearToDate decimal(5,2) not null,
Netpay decimal(5,2) not null,
EmployeeName nvarchar(30) not null,
constraint pk_PayRoll primary key(EmployeeNumber,StartDate)
)
create table HoursandEarnings
(
EmployeeNumber nvarchar(30) not null
constraint fk_EmployeeNumber references PayRoll(EmployeeNumber),
StartDate DateTime not null
constraint fk_StartDate references Payperiod(StartDate),
HoursAndEarningsDescription nvarchar(50) not null,
HoursandEarningsCurrent decimal(5,2) not null,
HoursandEarningsYearToDate decimal(5,2) not null,
constraint pk_HoursandEarnings primary
key clustered(EmployeeNumber,StartDate,HoursAndEarningsDescription)
)
create table EmployerPaidBenefits
(
EmployeeNumber nvarchar(30) not null
constraint fk_EmployerPaidBenefits_EmployeeNumber references PayRoll(EmployeeNumber),
StartDate DateTime not null
constraint fk_EmployerPaidBenefits_StartDate references Payperiod(StartDate),
EmployerpaidBenefitsDescription nvarchar(50) not null,
EmployerPaidBenefitsCurrent decimal(5,2) not null,
EmployerPaidBenefitsYearToDate decimal(5,2) not null
constraint pk_EmployerPaidBenefits
primary key(EmployeeNumber,StartDate,EmployerpaidBenefitsDescription)
)
create table Taxesanddeductions
(
EmployeeNumber nvarchar(30) not null,
constraint fk_Taxesanddeductions_EmployeeNumber references PayRoll(EmployeeNumber),
StartDate DateTime not null
constraint fk_Taxesanddeductions_StartDate references Payperiod(StartDate),
TaxesandDeductionsDescription nvarchar(50) not null,
TaxesandDeductionsCurrent decimal(5,2) not null,
EmployerPaidBenefitsYeartoDate decimal(5,2) not null,
constraint pk_Taxesanddeductions
primary key(EmployeeNumber,StartDate,TaxesandDeductionsDescription)
)
Because you have a composite key (EmployeeNumber,StartDate) on table PayRoll, you will need to reference both keys in the foreign key in the other tables which reference it.
If EmployeeNumber is unique in table Payroll, then you can make just EmployeeNumber the primary key on Payroll, and then your foreign keys on the other 3 tables to Payroll will be valid as-is.(Not the case)
Not related, but I believe you may run into problems making PostalCode a primary key on Address - this will mean that only one address with the same PostalCode may be added to the table. I would suggest a surrogate key instead.
Edit
Example of both columns in the foreign key:
constraint fk_HE_EmployeeNumber
foreign key(EmployeeNumber, StartDate)
references PayRoll(EmployeeNumber, StartDate)
Note also that primary and foreign key constraint names must be unique within the database (not just in the table) - you'll need to rename the duplicate keys like fk_StartDate.
SqlFiddle here
You may also find at some point that the encumberance and additional storage requirements of composite / compound keys may make implementation of simple surrogate keys (like int identity() or Guids) a simpler approach.

Type matches correctly, but why " #1215 - Cannot add foreign key constraint" keeps poping out?

This is my first table "address".
create table address(
street varchar(32) not null,
city varchar(32) not null,
state varchar(32) not null,
zip_code varchar(32) not null,
primary key(zip_code)
)
This is my second table "customer".
create table customer(
customer_id integer not null,
name varchar(32) not null,
primary key(customer_id)
)
This is my relational table about above two tables. But why cannot I make it? The type matches well. So please.
create table cus_live(
customer_id integer not null,
zip_code varchar(32) not null,
primary key(customer_id, zip_code),
foreign key(customer_id) references customer,
foreign key(zip_code) references address
)
This is the correct way to create a foreign key constraint:
create table cus_live(
cus_live_id int not null,
customer_id int not null,
address_id int not null,
primary key(cus_live_id),
foreign key(customer_id) references customer (customer_id),
foreign key(address_id) references address (address_id)
)
Notice the changes I made:
After references [tableName], added the key name in parentheses. This will tell SQL which column is referenced in the foreign key constrant.
Changed the primary key of the table to its own unique id rather than a composite of two columns. Usually this is better practice.
Changed primary key of address table to address_id instead of zipcode. Otherwise you would have conflicts if two customers have the same zipcode but different addresses within that zipcode.
Made your id's integers instead of strings.
Also, note that this is all assuming that cus_live has some other columns that you have excluded (and cannot be included on either the address or customer table) and hence warrants having this third table in the first place. Otherwise you could just put address_id on the customer table like this:
create table address(
address_id int not null,
street varchar(32) not null,
city varchar(32) not null,
state varchar(32) not null,
zip_code varchar(32) not null,
primary key(address_id)
)
create table customer(
customer_id int not null,
name varchar(32) not null,
address_id int not null,
primary key(customer_id)
foreign key(address_id) references address (address_id)
)

Created Primary Key on SQL Server not Identified

CREATE TABLE sampProduct
(
Product_ID VARCHAR(15) NOT NULL,
Supplier_ID INT NOT NULL,
Category_ID INT NOT NULL,
Unit_Price DECIMAL(10, 2)
PRIMARY KEY(Product_ID)
)
CREATE TABLE sampMachine
(
M_Product_ID VARCHAR(15) NOT NULL,
Serial_No VARCHAR(15) NOT NULL,
Machine_Model VARCHAR(20),
PRIMARY KEY(M_Product_ID, Serial_No),
FOREIGN KEY(M_Product_ID) REFERENCES sampProduct(Product_ID)
)
CREATE TABLE sampService
(
Service_ID VARCHAR(15) NOT NULL,
Serial_No VARCHAR(15) NOT NULL,
Complaint VARCHAR(40) NOT NULL,
PRIMARY KEY(Service_ID),
FOREIGN KEY(Serial_No) REFERENCES sampMachine(Serial_No)
)
Machine is a subtype of Product. M_ProductID is referencing the primary key of the parent table Product by this key. And Serial_No is used to uniquely identify a Machine. The problem is, I want to create a foreign key from Serial_No into the Service table, since I want to track the serial numbers of the machine (this is because, for example, we can have many machine for each model or type, but each of them has their unique serial numbers), but there is an error for the creation. Can you help me out?
One of the requirements for creating a foreign key is that the table you reference must have a unique key on the columns you want referenced. So in your example in order for it to work you would need a unique key on sampMachine (Serial_No) so that you could create foreign keys to it using just Serial_No. The way you have it now you would need to use M_Product_Id in the table sampService and add it to the foreign key definition.
create table sampService(
M_Product_ID varchar(15) not null,
Service_ID varchar(15) not null,
Serial_No varchar(15) not null,
Complaint varchar(40) not null,
primary key (Service_ID),
foreign key (Serial_No) references sampMachine(m_Product_Id, Serial_No)
)