Getting errors with my SQL database - mysql

I'm having trouble getting my SQL table to work. It keeps giving me an error that it cannot drop tables due to not permission or not exisiting. And it's telling me that there's an invalid table as well.
I currently have:
drop table Orders
drop table Item
drop table Supplier
drop table Staff
drop table Customers
drop table Ingredient
CREATE TABLE Supplier(
SupplierNo CHAR(7) PRIMARY KEY,
SupplierName CHAR(25),
SupplierAddress VARCHAR(25),
SupplierPhone CHAR(10),
SupplierContactPerson CHAR (35)
)
go
CREATE TABLE Item (
ItemNo CHAR(7) PRIMARY KEY,
ItemName CHAR(10),
ItemType CHAR(10),
Price DEC(3,2),
IngredientCode CHAR (5),
FOREIGN KEY(IngredientCode) REFERENCES Ingredient(IngredientCode)
)
go
CREATE TABLE Staff (
EmployeeID CHAR(3) PRIMARY KEY,
FirstName CHAR(10),
LastName CHAR(10),
PostalAddress VARCHAR(MAX),
ContactNumber CHAR (10),
RateOfPay MONEY,
EmployementStatus CHAR(25)
)
CREATE TABLE Customers (
CustomerID CHAR(4) PRIMARY KEY,
CustomerFN CHAR(25),
CustomerLN CHAR(25),
CustomerPhN CHAR(11),
CAddress VARCHAR (25)
)
go
CREATE TABLE Ingredient(
IngredientCode CHAR (5) PRIMARY KEY,
IngredientName CHAR(25),
IngredientDesc VARCHAR(max),
CurrentStockLevel CHAR(3),
PreviousStockLevel CHAR(3),
SuggestedStockLevel CHAR(3),
SupplierNo CHAR(7),
Supplied DATE(),
FOREIGN KEY(SupplierNo) REFERENCES Supplier(SupplierNo)
)
CREATE TABLE Orders(
OrderNo CHAR(7),
OrderDate DATE(),
CustomerNo CHAR(4) FOREIGN KEY,
EmployeeID CHAR(3) FOREIGN KEY,
ItemNo CHAR(7) FOREIGN KEY,
TypeOfOrder CHAR(10),
TotalAmount MONEY(),
PaymentMethod CHAR(10),
OrderStatus CHAR(10)
FOREIGN KEY CustomerNo, EmployeeID, ItemNo
REFERENCES Customer(CustomerNo), Staff(EmployeeID), Item(ItemNo)
)
It seems like a very simple problem to have, but I'm not even sure what it means with me having no permission or the table not existing at all...

You create constraint, before create table Ingredient
FOREIGN KEY(IngredientCode) REFERENCES Ingredient(IngredientCode)

Related

mysql error while creating table defining foreign keys

create database Geomart;
use geomart;
##Creating the table customers
create table customers (
CustomerID int not null,
Customer_Name varchar (20),
address varchar(50),
City varchar(20),
Postal_code int,
Country varchar(20),
primary key(CustomerID)
);
##Creating the Table Categories
create table Categories (
category_ID int not null,
Category_Name Varchar(20),
descriptions varchar(50),
primary key(Category_ID)
);
##Creating the table employees
create table employees (
employee_ID int not null,
Last_name Varchar(20),
First_Name varchar(20),
Bith_Date date,
Notes varchar(50),
primary key(employee_ID)
);
##Creating the table Shippers
Create table Shippers (
Shipper_ID int not null,
Shipper_name varchar(20),
phone int,
primary key(shipper_ID)
);
##Creating the table Orders - Error
create table orders (
order_ID int not null,
customerID int,
employee_ID int,
order_Date date,
Shipper_ID int,
primary key(order_ID),
foreign key(customerID) references customers,
foreign key(employee_ID) references employees,
foreign key(shipper_ID) references shippers
);
I am getting the error - Error Code: "1239. Incorrect foreign key definition for 'foreign key without name': Key reference and table reference don't match"
tried creating a table with multiple foreign keys in a single table. Is it possible

How to set up Foreign keys MySQL

I am very new to SQL, I am using MySQL running a server set up for me to complete an assignment.
I can't understand what I am doing wrong here when assigning the foreign keys, here is my code
CREATE TABLE Customers (
CustomersID int NOT NULL AUTO_INCREMENT,
CustomerName varchar(50),
AddressLine1 varchar(50),
AddressLine2 varchar(50),
City varchar(50),
State varchar(50),
PostalCode varchar(50),
YTDPurchases decimal(19,2),
PRIMARY KEY (CustomersID)
);
CREATE TABLE TermsCode (
TermsCodeID varchar(50) NOT NULL,
Description varchar(50)
);
CREATE TABLE Invoices (
InvoiceID int NOT NULL AUTO_INCREMENT,
CustomerID int,
InvoiceDate datetime,
TermsCodeID varchar(50),
TotalDue decimal(19,2),
PRIMARY KEY (InvoiceID),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomersID),
FOREIGN KEY (TermsCodeID) REFERENCES TermsCode(TermsCodeID)
);
I get this error
MySQL said: Documentation
#1005 - Can't create table table.Invoices` (errno: 150 "Foreign key constraint is incorrectly formed")
You need to add a primary key to the second table, as in:
CREATE TABLE TermsCode (
TermsCodeID varchar(50) NOT NULL,
Description varchar(50),
primary key(TermsCodeID)
);
Once you do that, the tables' creation works well. See example at db<>fiddle.

SQL Fiddle Giving "cannot add foreign key constraint" error

I am trying to figure out what is wrong with my syntax here. I feel like I have matched the examples I have found over the internet... but something must be amiss. Here is the code I am using. I keep getting the error code "cannot add Foreign Key constraint" when trying to run the build the schema.
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
I just need to figure out what part of my foreign key is incorrectly defined. I know it normally results from an improperly defined primary key, but cannot seem to find where I messed up.
Thanks for your help
You are referencing a KEY that is not set up yet. SQL is linear, so the code gets executed line-by-line and cannot reference future lines (in most cases)
Create your tables in this order:
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);

Creating nested triggers in MySQL

I am currently working with MySQL databases and I am trying to create a nested trigger. But I am having trouble and errors. I have tried many things but I cannot seem to figure out. Any help would be greatly appreciated. You would find my script for tables and trigger below.
CREATE TABLE co_op_members (
MemberID int not null AUTO_INCREMENT,
Description varchar(255),
BusinessName varchar(255),
Date date,
ContactName varchar(255),
Email varchar(255),
Phone int,
Address varchar(255),
PRIMARY KEY (MemberID)
);
CREATE TABLE Customers (
CustomerId int not null AUTO_INCREMENT,
Name varchar(255),
Email varchar(255),
Location varchar(255),
StartDate date,
DeliveryAddress varchar(255),
PRIMARY KEY (CustomerId)
);
CREATE TABLE Message (
MessageId int not null AUTO_INCREMENT,
CustomerId int ,
AepcMemberId int,
Message varchar(255),
Date date,
PRIMARY KEY (MessageId),
FOREIGN KEY (CustomerId) REFERENCES Customers(CustomerId),
FOREIGN KEY (AepcMemberId) REFERENCES co_op_members(MemberID)
);
CREATE TABLE Equipment (
ItemId int not null AUTO_INCREMENT,
StyleName varchar(255),
ItemName varchar(255),
PackSize int,
Description varchar(255),
MemberId int,
Price int,
PriceDate date,
UniteShippingDate date,
UnitShippingCost int,
PRIMARY KEY (ItemId),
FOREIGN KEY (MemberId) REFERENCES co_op_members(MemberId)
);
CREATE TABLE Equipmentorder (
OrderId int not null AUTO_INCREMENT,
CustomerId int,
AepcMemberId int,
ShippingDate date,
OrderStatus varchar(255),
OrderDate date,
CourierName varchar(255),
ItemId int,
ShippingCost float,
ShippingReferenceNo int,
PRIMARY KEY (OrderId),
FOREIGN KEY (CustomerId) REFERENCES Customers(CustomerId),
FOREIGN KEY (ItemId) REFERENCES Equipment(ItemId)
);
And here's the trigger script:
CREATE TRIGGER after_customer_order_plced
AFTER INSERT
ON message FOR EACH ROW
BEGIN
CREATE TRIGGER after_members_insert
AFTER INSERT
ON members FOR EACH ROW
BEGIN
IF NEW.customers IS NULL THEN
INSERT INTO message(memberId, message)
VALUES(new.id,CONCAT('Hi ', NEW.name, ', hello new message.'));
END
Thanks in advance for the help

how to reference multiple fks from different tables in MySQL

i have been looking around and i cannot find the correct way to do this in sql the current way i have coded it just causes my third table to fail but the rest of the code worked great up until the third table, i just need the third table to include fks from both the previous tables
first table
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS Customer;
CREATE TABLE Customer
(
CustomerNumber int NOT NULL,
CustomerName varchar(255),
CustomerAddress varchar(255),
CustomerPhoneNumber varchar(255),
JoinDate varchar(255),
PetName varchar(255),
PayScheme varchar(255),
PremiumPayDate varchar(255),
PRIMARY KEY (CustomerNumber, PetName)
);
second table
DROP TABLE IF EXISTS Policies;
CREATE TABLE Policies
(
PolicyID int NOT NULL,
PolicyNumber int NOT NULL,
PetType varchar(255),
CustomerNumber int NOT NULL,
PetName varchar(255),
EffectiveDate varchar(255),
PRIMARY KEY (PolicyID),
CONSTRAINT fk_CustomerNumber_PetName
FOREIGN KEY (CustomerNumber, PetName)
REFERENCES Customer(CustomerNumber, PetName)
);
third table
DROP TABLE IF EXISTS Claims;
CREATE TABLE Claims
(
ClaimsID int NOT NULL,
AmountForReimbursement varchar(255),
PolicyID int NOT NULL,
PetName varchar(255),
PRIMARY KEY (ClaimsID),
CONSTRAINT fk_PolicyID_PetName
FOREIGN KEY (PolicyID, PetName)
REFERENCES Policies(PolicyID), Customer(PetName)
);
You should consider going through MySQL documentation. You have got the table creation wrong again for Claims. It should be like below
CREATE TABLE Claims
(
ClaimsID int NOT NULL,
AmountForReimbursement varchar(255),
PolicyID int NOT NULL,
PetName varchar(255),
CustomerNumber int NOT NULL,
PRIMARY KEY (ClaimsID),
CONSTRAINT fk_PolicyID_PetName
FOREIGN KEY (PolicyID) REFERENCES Policies(PolicyID),
FOREIGN KEY (CustomerNumber, PetName) REFERENCES Customer(CustomerNumber, PetName)
);
It's always recommended and you should actually refer both the PK column since you have a composite PK defined