-
DB + MySQL Critical Concepts
Client Server Database
======= ======== ========
(1)
mysite.com/allproducts
(2)
------->
GET `/api/products`
(3)
------->
USE COMPANY_DB
SELECT * FROM users
(4)
<-------
|| .users. ||
:::::::::::::::::
| id | name |
::::::::::::::::
| 1 | "Ben" |
| 2 : "Ed" |
| 3 : "Jill" |
<-------
[
{id: 1, name: 'Ben'}
{id: 2, name: 'Ed'}
{id: 3, name: 'Jill'}
]
Relational Model : approach to managing data. Data is organized into tables and relations.
============
TABLES (representation of data)
============
COUNTRY
---------
name
population
capital
STATE
---------
name
population
square meters
PRESIDENT
---------
name
date_of_birh
mayor
LANGUAGE
---------
name
latin_origin
============
RELATIONS (connections between tables)
============
+ COUNTRY has_a PRESIDENT
|- PRESIDENT belongs_to_one COUNTRY
+ COUNTRY has_many STATES
|- STATE belongs_to_one COUNTRY
+ COUNTRY has_many LANGUAGES
|- LANGUAGES belong_to_many COUNTRIES
SQL stands for Structured Query Language. It’s a standard language for implementing a Relational Model accessing and manipulating Relational databases.
MySQL is a RDMS (Relational Database Management System) that implements SQL .
- like SQL Server, Oracle, Postgres, etc.
CREATE DATABASE HOSPITAL
CREATE TABLE EMPLOYEE (
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
username VARCHAR(50) NOT NULL,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
email VARCHAR(25) NOT NULL,
salary INT,
medical_staff BOOL
created_at TIMESTAMP DEFAULT "00:00:00 00:00:00"
);
LEAGUE
--------------
id : INTEGER
name : TEXT
TEAM
--------------
id : INTEGER
name : TEXT
year_established : INTEGER
league_id
PLAYER
--------------
id : INTEGER
first_name : TEXT
last_name : TEXT
height : INTEGER
team_id
Saved in Tables as:
- Rows (each record)
- Columns (fields for a record)
All Records must have: a primary key
# Install mysql :
sudo apt-get install mysql-server
# Start the Service:
sudo service mysql start
# Enter MySQL as 'root'
mysql -u root
# Enter MySQL as 'root'
SHOW DATABASES;
# show current database + user
STATUS;
# clear the terminal
system clear
# quit the mysql shell
\q - quit mysql shell
CREATE TABLE teams (
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(25) NOT NULL,
year_established INT,
CreatedAt TIMESTAMP DEFAULT "00:00:00 00:00:00"
);
CREATE TABLE players (
Id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
id_teams INT NOT NULL,
FOREIGN KEY (id_teams) REFERENCES Users(Id)
);
CREATE TABLE teams (
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(25) NOT NULL,
year_established INT,
created_at TIMESTAMP DEFAULT "00:00:00 00:00:00"
);
CREATE TABLE players (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
height INT,
created_at TIMESTAMP DEFAULT "00:00:00 00:00:00"
id_teams INT NOT NULL,
FOREIGN KEY (id_teams) REFERENCES teams(Id)
);
show Tables