Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

PG DIPLOMA IN DATA

SCIENCE AND ANALYTICS


MODULE 2
MYSQL -
STRUCTURED QUERY
LANGUAGE
Agenda
 MySQL
 RDBMS
 RDBMS Terminologies
 Structured Query language
 Storage Engines
 Architecture (Client & Server)
 Installation
 MySQL Installation
 Workbench
 Database operations
MYSQL

 MySQL is an open source relational database.


 MySQL is cross platform which means it runs on a
number of different platforms such as Windows,
Linux, and Mac OS etc.
What is MySQL??

 A Relational Database Management System (RDBMS).


 Supports standard SQL (Structured Query Language).
 Free to download and use.
 Written in C/C++.
 Developed by Michael Widenius and David Axmark in 1994.
 Presently developed, distributed, and supported by Oracle
Corporation.
DBMS – DATABASE
MANAGEMENT SYSTEM
 A DBMS is a separate application that stores a
collection of data.
 A database is an organized collection of structured
information or data. It is controlled by DBMS.
 Data and DBMS = Database Systems
 Each database has one or more distinct APIs for
creating, accessing, managing, searching and replicating
the data it holds.
RDBMS
 Relational Database Management Systems (RDBMS) a
software is used to store, manage, query and retrieve
huge volume of data from a relational database.
 An interface between users, applications and database.
 This is called relational database because all the data is
stored into different tables and relations are established
using primary keys or other keys known as Foreign
Keys.
RDBMS Terminology
• Database − A database is a collection of tables, with
related data.
• Table − A table is a matrix with data. A table in a
database looks like a simple spreadsheet.
• Column − One column (attribute/data element) contains
data of one and the same kind, for example the column
postcode.
• Row − A row (= tuple, entry or record) is a group of
related data, for example the data of one subscription.
• Redundancy − Storing data twice.
RDBMS Terminology
• Primary Key − A primary key is unique. A key value
can not occur twice in one table. With a key, you can
only find one row.
• Foreign Key − A foreign key is the linking pin between
two tables.
• Compound Key − A compound key (composite key) is
a key that consists of multiple columns, because one
column is not sufficiently unique.
• Index − An index in a database resembles an index at
the back of a book. Indexes are used to find rows with
specific column values quickly.
• Referential Integrity − Referential Integrity makes sure
that a foreign key value always points to an existing
row.
Example of Primary key and
Foreign Key
Column/ Attribute
"Persons" table:
PersonID
PersonID LastName FirstName Age
Primary
1 Hansen Ola 20
key
2 Svendson Ola 23

Row/tuple 3 Pettersen Kari 20

"Orders" table:
OrderID OrderNumber PersonID
PersonID
1 77895 3 Foreign
2 44678 3 key
3 22456 2
4 24562 1
ACID properties
 Define relational database transactions -
 Atomicity: All the elements that make up a complete database
transaction.
 Consistency: Defines the rules for maintaining data points in a
correct state after a transaction.
 Isolation: Keeps the effect of a transaction invisible to others until
it is committed, to avoid confusion.
 Durability: Ensures that data changes become permanent once the
transaction is committed.
Structured Query Language
 Structured Query Language is a standard Database
language which is used to create, maintain and
retrieve the relational database.
 A programming language for relational databases.
Structured Query Language
 Structured: The distinction between
 Logical data structure:
 Data tables, views, indexes
 Operation: Application to specify the content it needs.
 Physical storage structures:
 Stored data
 Operation: How the data shall be accessed.
 Database administrators can manage physical data storage without
affecting access to that data as a logical structure.
 Eg: Renaming a database file will not affect the tables stored in it.
contd…

Logical Physical storage


Data Structures Data tables, views and Actual stored data in
indexes database
Data operations An application can How that data should
specify the content it be accessed and then
needs, a query carry out the task
Communication between RDBMS and
MySQL
Why MySQL ?
 MySQL is used for both small and large applications.
 Web applications, ecommerce
 On demand flexibility
 Fast, reliable and easy to use
 High availability
 High volume of data storage
 Security

Facebook, Google, Adobe, Alcatel Lucent, Zappos, LinkedIn, Uber,


Twitter, Youtube, Airbnb
Storage Engines…
STORAGE ENGINE

 A storage engine is a software module that a database


management system uses to create, read and update data
from a database.

 Two types of storage engines in MySQL:


 Transactional – Row-level locking
 Non-transactional – Table-level locking
STORAGE ENGINE

 For MySQL 5.5 and later, the


default storage engine is
InnoDB. The default storage
engine for MySQL prior to
version 5.5 was MyISAM.

 Example command :
mysql> show engines;
Types of Storage Engines
Engines Description
InnoDB This is the default storage engine for MySQL 5.5 and higher. It provides transaction-
safe (ACID compliant) tables, supports FOREIGN KEY referential-integrity
constraints. It supports commit, rollback, and crash-recovery capabilities to protect
data. It also support row-level locking (filtering out the rows). It's "consistent
nonlocking reads" increases performance when used in a multiuser environment. It
stores data in clustered indexes which reduces I/O for queries based on primary
keys.
MyISAM This storage engine, manages non transactional tables (data warehouses) huge
table with minimal write operations, provides high-speed storage and retrieval,
supports full text searching. Table level locking,
MEMORY Provides in-memory tables, formerly known as HEAP. It stores all data in RAM for
faster access than storing data on disks. Useful for quick looks up of reference and
other identical data. Non transactional.
MERGE Groups more than one similar MyISAM tables to be treated as a single table, can
handle non transactional tables, included by default.
EXAMPLE You can create tables with this engine, but can not store or fetch data. Purpose of
this is to teach developers about how to write a new storage engine.
ARCHIVE Used to store a large amount of historical data, does not support indexes,
transactional data, compresses data quickly upon insertion.
CSV Stores data in Comma Separated Value format in a text file.
BLACKHOLE Accepts data to store but always returns empty. Used in performance tests.
FEDERATED Stores data in a remote database providing a distributed data environment.
Specifying and Altering storage
engines
 The storage engine is specified at the time of the table creation.

mysql> CREATE TABLE Cars(


Id INTEGER PRIMARY KEY,
Name VARCHAR(50),
Cost INTEGER) ENGINE='MyISAM';

 The ENGINE keyword specifies the storage engine used for this
particular table.
 If we do not specify the storage engine explicitly, then the
default storage engine is used.
MySQL Front ends
 Tools for managing MySQL
 Building and manipulating data within MySQL
database.
 GUI Tools
 MySQL Workbench, SequelPro, DBVisualizer, and
the Navicat DB Admin Tool.
 MySQL workbench
Versions
 MySQL Community Server (Open Source).
 MySQL Enterprise Server (Proprietary).
 Latest:
 MySQL Community Server 8.0.26
 MySQL workbench version :
- https://downloads.mysql.com/archives/workbench/
Ubuntu 20.04 8.0.20 (mysql-workbench-
community_8.0.20-
1ubuntu20.04_amd64.deb)
Ubuntu 18.04 8.0.19 (mysql-workbench-
community_8.0.19-
1ubuntu18.04_amd64.deb)
Installation of MySQL…
MySQL Client – Server

 Server: The central database management program


 Client: Programs connect to server to retrieve or modify
data.

SQL Request
MySQL Server
MySQL Client
Response
Installing MySQL in Ubuntu

 Update package index


sudo apt update
  Install MySQL
sudo apt-get install mysql-server
 Secure MySQL server installation
sudo mysql_secure_installation
 Manage MySQL Server via Systemd
sudo systemctl status mysql
 Connect to MySQL server
sudo mysql -u root -p
Installing MySQL Workbench in
Ubuntu

sudo apt install mysql-workbench
Using MySQL Workbench
Create user

 CREATE USER user'@'localhost' IDENTIFIED BY


'newpassword';
 GRANT GRAND OPTIONS ON * . * TO
'test'@'localhost';
 GRANT SELECT ON * . * TO 'test'@'localhost';
 FLUSH PRIVILEGES;
Layout of MySQL Workbench
 1 -> is where you enter your SQL queries.
 2 -> is a sidebar where you manage your Databases and
schemas.
 3 -> is where the output of the SQL queries you typed in
the section marked 1, is shown.
Database Creation …
Database Creation

 Create Database :
mysql> create database database_name;
 Example :
mysql> create database student;

 Use Database :
mysql> use database_name;
 Example :
mysql> use student;
Data Types…
Data Types
 MySQL supports these groups of data types:
 Numeric
 Date & time
 String

The integer types differ in their storage. We can choose values that fit our requirements.

mysql> CREATE TABLE Ages(Id SMALLINT, Age TINYINT);


(Temporary Table)

mysql> INSERT INTO Ages VALUES(1, 43);


Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Ages VALUES (2, 128);
ERROR 1264 (22003): Out of range value for
column 'Age' at row 1
Thank You…

You might also like