Skip to content

MySQL Database Course – Part II (Video) – Entity-Relationship

In this second part of the database course, you'll learn how to create tables with MySQL Workbench and how to relate them (1:1, 1:N, N:M).

Ricardo Moya

Part II: Creating Tables and Relationships Between Tables (Entity-Relationship Model)

In this second part of the database course, we’re going to learn how to create tables with MySQL Workbench and how to relate those tables. The most important thing here is to look at the three types of relationships that can exist between tables and how these relationships "affect" the tables or the Entity-Relationship model you’ve created. In short, in this second part of the course we’ll cover the knowledge you need to model the database you are building.

Below is the video explaining how to create tables and how to build the Entity-Relationship model.

In the video tutorial, the following two tables were created with the following attributes:

Diagram of the Persona (id, name, surnames, DNI) and Coche (plate, make, model, horsepower) tables used in the example

"N:M" relationship: In an N:M relationship, an intermediate table is created with the keys of the tables and of the relationship. If the relationship has attributes, those attributes are added to the table:

N:M relationship in the entity-relationship model: Coche_VS_Persona join table holding the keys of Persona and Coche

 

"1:N or N:1" relationship: In this relationship, the key of the "1" table is added to the "N" table. (A person can have from 1 to N cars, and a car belongs to only one person).

1:N relationship: the Persona id key is added to the Coche table as Persona_id, the N side

"1:1" relationship: Add the key of either table to the other table.

1:1 relationship: the Persona id key is added to the Coche table as Persona_id

or

Alternative 1:1 relationship: the Coche plate key is added to the Persona table as Coche_Matricula

To create the "Persona" table, the following had to be run:


CREATE  TABLE `MiPrimeraBaseDeDatos`.`Persona` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `Nombre` VARCHAR(80) NOT NULL ,
  `Apellido1` VARCHAR(80) NOT NULL ,
  `Apellido2` VARCHAR(80) NOT NULL ,
  `DNI` VARCHAR(9) NOT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX `id_UNIQUE` (`id` ASC) );

Tables can also be created with the following structure:


CREATE  TABLE `nombreBaseDatos`.`nombreTabla` (
  `nombreAtributo1` TIPO CARACTERISTICAS ,
  `nombreAtributo2` TIPO CARACTERISTICAS ,
.......................
  PRIMARY KEY (`nombreAtributoPrimaryKey`);

“MySQL Database Course – Part II (Video) – Entity-Relationship” by Ricardo Moya at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

Ricardo Moya

I work in Machine Learning and Big Data. I like learning new technologies and explaining them as clearly as I can on this site.

You may also like

ETL: Extract, Transform, and Load

ETL is used for massive, real-time data processing (Big Data): Extract data from different sources, Transform it into a useful structure, and Load it where you can use it.

Jan 9, 2022

Comments

Leave a comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.