Foreign Keys in MySQL Workbench not working? - mysql

I am reverse engineering a database in MySQL Workbench but it doesn't seem to be importing or recognising the foreign keys. Which means I cant get it to draw the relationships.
While I am trying to get this to work I am using a snippet of a couple of tables, so its nothing complex.
Here are the 2 demo tables that I am trying to get to work:
CREATE TABLE users (
UserID varchar(32) NOT NULL,
OrgID varchar(6) NOT NULL,
RegionID int NULL,
LocationID int NULL,
Name nvarchar(60) NULL,
Crypt varchar(32) NULL,
Security int NULL,
Status int NULL,
PRIMARY KEY ( UserID ),
CONSTRAINT fk_OrgID_Users FOREIGN KEY (OrgID) REFERENCES Organisations(OrgID)
);
CREATE TABLE Organisations(
OrgID varchar(6) NOT NULL,
Name nvarchar(70) NOT NULL,
Address1 varchar(50) NULL,
Address2 varchar(50) NULL,
Address3 varchar(50) NULL,
Town varchar(20) NULL,
County varchar(20) NULL,
PostCode varchar(10) NULL,
NotificationEmail varchar(500) NOT NULL,
PRIMARY KEY ( OrgID )
)
But in MySQL Workbench, there are no relationships or foreign keys being created. If I look at the table data, and the foreign keys tab, theres nothing there?
I think I'm creating the foreign keys correctly, so is there a reason its not importing them?

On further research there were several things that I needed to do for this to work.
I had to make sure that the tables reverse engineered from MySQL were defined as InnoDB.
And I also had to remove the foreign key constraint from the create table command and create the foreign keys after the tables were created, like so:
CREATE TABLE Users (
UserID varchar(32) NOT NULL,
OrgID varchar(6) NOT NULL,
RegionID int NULL,
LocationID int NULL,
Name nvarchar(60) NULL,
Crypt varchar(32) NULL,
Security int NULL,
Status int NULL,
PRIMARY KEY ( UserID )
) ENGINE=InnoDB;
CREATE TABLE Organisations(
OrgID varchar(6) NOT NULL,
Name nvarchar(70) NOT NULL,
Address1 varchar(50) NULL,
Address2 varchar(50) NULL,
Address3 varchar(50) NULL,
Town varchar(20) NULL,
County varchar(20) NULL,
PostCode varchar(10) NULL,
NotificationEmail varchar(500) NOT NULL,
PRIMARY KEY ( OrgID )
) ENGINE=InnoDB;
ALTER TABLE Users ADD CONSTRAINT fk_OrgID_Users FOREIGN KEY (OrgID) REFERENCES Organisations(OrgID);
This then worked fine and the relationships were visible and connected in MySQL Workbench

Related

Trying to learn Mysql and run into a foreign key constraint is incorrectly formed error

For some reason my code won't let me create the projects table due to the foreign key error. I have tried a few different things and just cant seem to get it to work, ive tried looking on here for solutions but cant seem to do it. Any help would be much appreciated.
CREATE TABLE PEOPLE (
NAME VARCHAR(32) NOT NULL,
GENDER ENUM('Male', 'Female', 'Other'),
DOB DATE NOT NULL,
SALARY VARCHAR(16) NOT NULL,
PROJECT VARCHAR(32) NOT NULL,
BUSINESS_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY(NAME, PROJECT)
);
CREATE TABLE PEOPLE_EMAILS (
NAME_ID VARCHAR(32) NOT NULL,
EMAIL VARCHAR(64) NOT NULL,
PRIMARY KEY(EMAIL),
FOREIGN KEY(NAME_ID) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PEOPLE_PHONE (
NAME_ID2 VARCHAR(32) NOT NULL,
PHONE_NUMBER VARCHAR(32) NOT NULL,
PRIMARY KEY (PHONE_NUMBER),
FOREIGN KEY(NAME_ID2) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PROJECTS (
PROJECT_NAME VARCHAR(32) NOT NULL,
PROJECT_LOCATION VARCHAR(32) NOT NULL,
BUDGET VARCHAR(16) NOT NULL,
FOREIGN KEY(PROJECT_NAME) REFERENCES PEOPLE(PROJECT)
);
Presumably, you want the foreign key the other way around. You would expect people to reference projects instead of projects to reference people.
This means that you need to create the projects table first, and then the people table. Also, you need a proper primary key on projects so you can reference it in people (I assumed project_name).
CREATE TABLE PROJECTS (
PROJECT_NAME VARCHAR(32) NOT NULL,
PROJECT_LOCATION VARCHAR(32) NOT NULL,
BUDGET VARCHAR(16) NOT NULL,
PRIMARY KEY (PROJECT_NAME)
);
CREATE TABLE PEOPLE (
NAME VARCHAR(32) NOT NULL,
GENDER ENUM('Male', 'Female', 'Other'),
DOB DATE NOT NULL,
SALARY VARCHAR(16) NOT NULL,
PROJECT VARCHAR(32) NOT NULL,
BUSINESS_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY(NAME, PROJECT),
FOREIGN KEY(PROJECT) REFERENCES PROJECTS(PROJECT_NAME)
);
CREATE TABLE PEOPLE_EMAILS (
NAME_ID VARCHAR(32) NOT NULL,
EMAIL VARCHAR(64) NOT NULL,
PRIMARY KEY(EMAIL),
FOREIGN KEY(NAME_ID) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PEOPLE_PHONE (
NAME_ID2 VARCHAR(32) NOT NULL,
PHONE_NUMBER VARCHAR(32) NOT NULL,
PRIMARY KEY (PHONE_NUMBER),
FOREIGN KEY(NAME_ID2) REFERENCES PEOPLE(NAME)
);
Demo on DB Fiddle

MySQL ERROR 1215 (HY000): Cannot add foreign key constraint when I'm trying to create a table

I tried looking up the reasons why it's failing but I can't find the different data types issues mentioned in the solutions in my tables.
I'm getting the error when I'm trying to run the following:
comments (id int NOT NULL AUTO_INCREMENT,
user_id varchar(10) NOT NULL,
blog_id int NOT NULL,
comment varchar(150) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES user_details(id),
FOREIGN KEY(blog_id) REFERENCES blog(id));
existing data bases for primary keys:
user_details (id varchar(10) NOT NULL ,
name varchar(70) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(40) NOT NULL,
masked_password varchar(40) NOT NULL,
is_active varchar(10) DEFAULT 'False');
blog (id int NOT NULL AUTO_INCREMENT,
category_id int NOT NULL,
title varchar(240) NOT NULL,
body longtext NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(category_id) REFERENCES blog_catagories(id));
Let me know where I'm going wrong

how to retrieve foreign key linked to a certain primary key

I have a simple MySQL DB with a table which has PK composed by 2 varchar columns.
CREATE TABLE prodotti(
type_prod varchar(10) not null,
model_prod varchar(10) not null,
brand_prod varchar(20) not null,
name_prod varchar(30) not null,
year_prod int not null,
description_prod varchar(500) not null,
price_prod float not null,
qnt_prod int not null,
PRIMARY KEY(type_prod,model_prod) );
There are also other table with FKs linked to this PK (in this case,I show you just 2 of these tables)
CREATE TABLE cpu_component(
id_cpu varchar(10) not null,
series_cpu varchar(20) not null,
num_cores int not null,
frequency_ghz int not null,
socket_cpu int not null,
label_cpu varchar(10),
model_cpu varchar(10),
PRIMARY KEY (id_cpu),
FOREIGN KEY(label_cpu,model_cpu) REFERENCES prodotti(type_prod,model_prod)
ON UPDATE CASCADE
ON DELETE CASCADE );
CREATE TABLE motherboard_component(
id_motherboard varchar(10) not null,
series_motherboard varchar(20) not null,
chipset_motherboard varchar(15) not null,
socket_motherboard int not null,
type_ram_motherboard varchar(15) not null,
ram_frequency_index int not null,
slot_ram int not null,
hdmi_ports_gpu int not null,
usb_ports_motherboard int not null,
bios_motherboard varchar(10) not null,
label_motherboard varchar(10),
model_motherboard varchar(10),
PRIMARY KEY (id_motherboard),
FOREIGN KEY(label_motherboard,model_motherboard) REFERENCES prodotti(type_prod,model_prod)
ON UPDATE CASCADE
ON DELETE CASCADE );
I want to retrieve all FKs linked to a specific PK obtained by user input
P.S this is my first question here, please be clement lol
You can use SQL's INNER JOIN statements to achieve this in which joining the tables prodotti and cpu_component will give you all the rows from cpu_component that are also in prodotti.
SELECT p.type_prod, p.model_prod, cpu.num_cores
FROM prodotti p
INNER JOIN cpu_component cpu ON(cpu.label_cpu = p.type_prod AND cpu.model_cpu = model_prod)
As for joining the table prodotti with the table motherboard_component:
SELECT p.type_prod, p.model_prod, mc.chipset_motherboard
FROM prodotti p
INNER JOIN motherboard_component mc ON(mc.label_motherboard = p.type_prod AND mc.model_motherboard = p.model_prod)

How to make reference between tables

I am a beginner in MySQL.. Trying to do my first reference and cannot run it neither in MySQL workbench (Error 3730) nore on Live SQL from Orakle (missing right parenthesis). Here is my code:
CREATE TABLE Customer(
CustNo CHAR(4) NOT NULL,
CustName varchar(30) NOT NULL,
Adress varchar(20) NOT NULL,
Internal boolean NOT NULL,
Contact varchar(30) NOT NULL,
Phone INT(7) NOT NULL,
City varchar(10) NOT NULL,
State CHAR(2) NOT NULL,
Zip INT(6));
CREATE TABLE Facility(
FacNo CHAR(4) NOT NULL,
FacName varchar(30) NOT NULL,
CONSTRAINT FacilityPK PRIMARY KEY (FacNo) );
CREATE TABLE Location(
LocNo CHAR(4) NOT NULL,
FacNo CHAR(4) NOT NULL,
LocName VARCHAR(20) NOT NULL,
CONSTRAINT LocationFK FOREIGN KEY (FacNo) REFERENCES Facility (FacNo));

VIEW created in MySQL giving wrong output with SELECT

I'm creating a database for a courtier company and I have 5 relations
CREATE TABLE Customer
(
cid int(7) NOT NULL,
cfname char(25) NOT NULL,
clname char(25) NOT NULL,
aptnum int(100) NOT NULL,
street char(50) NOT NULL,
pobox int(10) NOT NULL,
area char(50) NOT NULL,
country char(50) NOT NULL,
phone int(12) NOT NULL,
PRIMARY KEY (cid)
)ENGINE=INNODB;
CREATE TABLE Orderr
(
orderid int(8) NOT NULL,
origin char(100) NOT NULL,
destination char(100) NOT NULL,
eta date NOT NULL,
weight int(100) NOT NULL,
priority enum('F','R') NOT NULL,
task enum('P','D') NOT NULL,
odate date NOT NULL,
cnum int(12) NOT NULL,
cpin int(8) NOT NULL,
custid int(7) NOT NULL,
PRIMARY KEY (orderid),
FOREIGN KEY (custid) REFERENCES Customer(cid)
)ENGINE=INNODB;
CREATE TABLE History
(
histid int(6) NOT NULL,
orderid int(8) NOT NULL,
status enum('D','O','R') NOT NULL,
current_loc char(50) NOT NULL,
PRIMARY KEY(histid),
FOREIGN KEY (orderid) REFERENCES Orderr(orderid)
)ENGINE=INNODB;
CREATE TABLE Driver
(
driverid int(6) NOT NULL,
dfname varchar(25) NOT NULL,
dlname varchar(25) NOT NULL,
dob date NOT NULL,
phone int(10) NOT NULL,
vehicle int(6) NOT NULL,
PRIMARY KEY (driverid)
)ENGINE=INNODB;
CREATE TABLE Vehicle
(
vid int(6) NOT NULL,
num_plate varchar(6) NOT NULL,
vtype enum('T','B','P') NOT NULL,
driverr int(6) NOT NULL,
orders int(8) NOT NULL,
PRIMARY KEY (vid),
FOREIGN KEY (driverr) REFERENCES Driver(driverid),
FOREIGN KEY (orders) REFERENCES Orderr(orderid)
)ENGINE=INNODB;
I am trying to create a VIEW for the Customer giving access to specific columns, here is the query
CREATE VIEW CustView AS
SELECT cfname, clname, aptnum, street, pobox, area, country, origin, destination, weight, priority, task, eta, odate, dfname, dlname, driver.phone
FROM Customer, Orderr, Vehicle, Driver
WHERE Customer.cid=Orderr.custid AND Vehicle.driverr=Driver.driverid AND Orderr.orderid=Vehicle.orders;
When I run SELECT * FROM CustView I do not get the desired output. What changes if any should I make to my query or perhaps to my relations?
Thanks.
As the previous posters remarked, I can only guess. But from your table and view definition, I get the suspicion that you didn't model the relation between Vehicle and Order properly. A Vehicle can be used for many Orders, right? In this case, the Vehicle id must be a foreign key in the Order table. Your design is just the other way round.
Not related to your question, I am suspicious of your History table. The history of orders fulfilled comprises just those orders which are in the past, right? So the history would be just a view that selects orders by past values of some of the dates contained therein.