Skip to content

Instantly share code, notes, and snippets.

@tphdev
Created July 10, 2018 01:02
Show Gist options
  • Save tphdev/9d98e335317c60eff25e5aa675483320 to your computer and use it in GitHub Desktop.
Save tphdev/9d98e335317c60eff25e5aa675483320 to your computer and use it in GitHub Desktop.
Intro to MySQL

Overview

Architecture Review

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'}
       ]                      
           
                   

Critical Concepts

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"
);

EXAMPLE - Tuples + Tables + Relations

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

RECORDS

Saved in Tables as:

  • Rows (each record)
  • Columns (fields for a record)

All Records must have: a primary key

INSTALLATION

# Install mysql :
sudo apt-get install mysql-server

# Start the Service:
sudo service mysql start

USING MySQL SHELL

Enter MySQL
# Enter MySQL as 'root'
mysql -u root
Show EXISTING Databases
# Enter MySQL as 'root'
SHOW DATABASES;

OTHER USEFUL COMMANDS

# show current database + user
STATUS;

# clear the terminal
system clear

# quit the mysql shell
\q - quit mysql shell

DB OPERATIONS

Create Tables
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 Records
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment