0% found this document useful (0 votes)
19 views

Mysql en Vs PDF

Uploaded by

Md Ehsan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Mysql en Vs PDF

Uploaded by

Md Ehsan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

MySQL

MySQL is an open-source relational database management


system (RDBMS)

• SQL - structured query language


• Data integrity
• Fault tolerance
MySQL: GitHub, US Navy, NASA, Tesla, Net ix, WeChat, Facebook, Zendesk,
Twitter, Zappos, YouTube, Spotify.

Oracle: Bauerfeind AG, CAIRN India, Capcom Co., ChevronTexaco, Coca-


Cola FEMSA, COOP Switzerland, ENEL, Heidelberger Druck, MTU Aero
Engines, National Foods Australia, Spire Healthcare, Stadtwerke München,
Swarovski, Tyson Foods, TVS Motor Company, Vilene.

fl
• Data types;
• Architecture;
• Indexes;
• Isolation
Numeric Data Types

• TINYINT: from -127 to 128, takes 1 byte

• BOOL (BOOLEAN): TINYINT(1).

• TINYINT UNSIGNED: from 0 to 255, takes 1 byte

• SMALLINT: from -32768 to 32767, takes 2 bytes

• SMALLINT UNSIGNED: from 0 to 65535, takes 2 bytes

• MEDIUMINT: from -8388608 to 8388607, takes 3 bytes

• MEDIUMINT UNSIGNED: from 0 to 16777215, takes 3 bytes

• INT: from -2147483648 to 2147483647, takes 4 bytes

• INT UNSIGNED: from 0 to 4294967295, takes 4 bytes

• BIGINT: from -9 223 372 036 854 775 808 to 9 223 372 036 854 775 807, takes 8 bytes

• BIGINT UNSIGNED: from 0 to 18 446 744 073 709 551 615, takes 8 bytes
Numeric Data Types

• DECIMAL: Fixed-precision number. DECIMAL(precision, scale). 1 <


precision < 65

• FLOAT: -3.4028 * 1038 to 3.4028 * 1038, takes 4 bytes FLOAT(M,D)


• DOUBLE: from -1.7976 * 10308 to 1.7976 * 10308, takes 8 bytes. DOUBLE(M,D)
Data types for a work with date and
time
• DATE: stores dates from JAN 1 1000 to DEC 31 9999. By default it stores data
using yyyy-mm-dd format. Takes 3 bytes.

• TIME: stores time from -838:59:59 to 838:59:59. By default it stores data using
“hh:mm:ss" format. Takes 3 bytes.

• DATETIME: from "1000-01-01 00:00:00" to "9999-12-31 23:59:59". By default it


stores data using "yyyy-mm-dd hh:mm:ss” format. Takes 8 bytes

• TIMESTAMP: from "1970-01-01 00:00:01" UTC to "2038-01-19 03:14:07" UTC.


Takes 4 bytes

• YEAR: stores a year as 4 digits. Takes 1 byte.


String Types
Can build index Can build index with limited length

• CHAR: fixed length string


• VARCHAR: variable length string • TINYTEXT: text up to 255 bytes.
• TEXT: text up to 65 КB.
• MEDIUMTEXT: text up to 16 МB
• LARGETEXT: text up to GB
Union Types

• ENUM: stores single value from the list of allowed values. Takes 1-2 bytes
CREATE TABLE tickets (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
priority ENUM('Low', 'Medium', 'High') NOT NULL
);

INSERT INTO tickets(title, priority) VALUES('Scan virus for computer A', 'High');

INSERT INTO tickets(title, priority) VALUES('Upgrade Windows OS for all computers', 1);
Union Types
• SET: stores multiple values from the list of allowed values (up to 64 possible
values). Takes 1-8 bytes.

CREATE TABLE tickets2 (


id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
priority SET('2^0', '2^1', '2^2') NOT NULL
);

INSERT INTO tickets2(title, priority) VALUES('Show example', ‘2^0,2^1');


INSERT INTO tickets2(title, priority) VALUES('Show example 2', 5);
Binary data types

• TINYBLOB: binary text up to 255 bytes.


• BLOB: binary text up to 65 КB.
• MEDIUMBLOB: binary text up to 16 МB
• LARGEBLOB: binary text up to 4 GB
JSON data type

Native JSON support which includes


1. Automatic JSON validation
2. Optimized storage
3. Partial update using
1. JSON_SET
2. JSON_INSERT
3. JSON_REPLACE
4. …
ORM
CREATE TABLE Users
(
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
user_name CHAR(64) NOT NULL
);

DROP TABLE Users;

ALTER TABLE Users


MODIFY COLUMN user_name VARCHAR(64) NOT NULL,
ADD COLUMN group_id TINYINT(3) DEFAULT NULL AFTER user_id,
ADD INDEX name (user_name),
ADD CONSTRAINT FK_Group FOREIGN KEY (group_id) REFERENCES Groups (group_id);
SELECT * FROM Users (WHERE user_id = 1);

UPDATE Users SET user_name = 'Иванов И.И.' WHERE user_id = 1;

INSERT INTO Users (user_id, group_id, user_name) VALUES (2, 1, 'Петров П.П.');

INSERT INTO Users VALUES (3, 1, ‘Петрович П.П.'), (4, NULL, 'Сидоров С.С.');

DELETE FROM Users WHERE user_id = 1;


SELECT * FROM Users a
INNER JOIN Groups g
ON a.group_id = g.group_id
WHERE user_id = 1;

SELECT * FROM Users a


LEFT JOIN Groups g
ON a.group_id = g.group_id
WHERE user_id = 1;
SELECT g.group_name, COUNT(*) as сnt
FROM Users u
INNER JOIN Groups g
ON u.group_id = g.group_id
WHERE u.is_test=0
GROUP BY g.group_id
HAVING cnt > 2;
Architecture
Storage engines

• MyISAM
• Memory
• CSV
• Archive
• Blackhole
NDBCLUSTER
InnoDB
• FOREIGN KEY
• Transactions
• ACID
• Atomicity
• Consistency
• Isolation
• Durability
• Bu er pool
• Change bu er
• Double write bu er
ff
ff
ff
• Primary Key
• Unique
• Index
B-tree Index
B-tree Index
B-tree Index
B-tree Index
Complex index

SELECT * FROM users WHERE age = 29 AND gender = 'male'

CREATE INDEX age_gender ON users(age, gender);

SELECTMy
* FROM users WHERE age => 10 AND gender = 'male'

CREATE INDEX age_gender ON users(gender, age);


Selectivity

The selectivity basically is a measure of how much variety there is in the


values of a given table column in relation to the total number of rows in a
given table
Optimisation
EXPLAIN SELECT * FROM categories WHERE id > 10

********************** 1. row **********************


id: 1
select_type: SIMPLE
table: categories
type: eq_ref (Const, Eq_ref, Ref, …, Index_merge, All)
possible_keys: PRIMARY
key: PRIMARY
key_len: 52
ref: classicmodels.p.productLine
rows: 1
Extra: Using where
1 row in set (0.00 sec)

use/force/ignore index straight_join


InnoDB
• FOREIGN KEY
• Transactions
• ACID
• Atomicity
• Consistency
• Isolation
• Durability
• Bu er pool
• Change bu er
• Double write bu er
ff
ff
ff
Select with locks

SELECT… LOCK IN SHARE MODE (shared lock) — prevent a write


lock is being acquired but not other read locks.

SELECT… FOR UPDATE (exclusive lock) — prevent any other lock


of any kind.
Isolation levels
REPEATABLE READ (DEFAULT VALUE)
This isolation level returns the same result set throughout the
transaction execution for the same SELECT run any number of times
during the progression of a transaction.

Snapshot of the SELECT is taken the rst time the SELECT is run
during the transaction and the same snapshot is used throughout the
transaction when the same SELECT is executed
fi
Isolation levels
READ COMMITED

Each SELECT uses its own snapshot of the committed data that was
committed before the execution of the SELECT. Now because each
SELECT has its own snapshot, here is the trade-o now, so the same
SELECT, when running multiple times during the same transaction,
could return di erent result sets
ff
ff
Isolation levels
READ UNCOMMITTED

A transaction can see changes to data made by other transactions


that are not committed yet
Isolation levels
SERIALIZABLE

Completely isolates the e ect of one transaction from others. It is


similar to REPEATABLE READ with the additional restriction that row
selected by one transaction cannot be changed by another until the
rst transaction nishes.
fi
fi
ff
What we can use

• Terminal
• DataGrip
• HeidiSQL
What we cannot use

• PhpMyAdmin
NoSQL

• Key - value (Redis, Riak, Memcache)


• Column based (HBase, ClickHouse, In uxDB)
• Document oriented (MongoDB, Cassanrda)
• Graph (Neo4J) fl
What to read

• 7 databases in 7 weeks
• High Performance MySQL, 4th Edition
• Designing Data-Intensive Applications - Kleppmann
Questions?

You might also like