SlideShare a Scribd company logo
1 of 20
Download to read offline
Saeid Zebardast 
@saeid 
http://about.me/saeid 
saeid.zebardast@gmail.com 
1
Please Please Please 
Ask Questions 
As Much As You Like 
• This is not a lecture! 
- But an opportunity to learn 
from each other. 
- If you haven’t seen some of 
these frameworks, methods, 
etc. It is OK! 
- Let we know if you know 
‣ Better ways 
‣ Best practices 
‣ My mistakes!
Introduction 
• What’s MySQL? 
- Since 1995 
- Written in C/C++ 
- RDMBS (Relational Database Management 
System) 
3
Installation 
• Just enter the following command: 
- $ sudo apt-get install mysql-server mysql-client 
• Check MySQL CLI: 
- $ mysql -u root -p 
- mysql> SHOW DATABASES; 
4
Execute SQL Statements 
• Interactively 
- $ mysql [database] 
‣ mysql> stmt; 
• Command Line 
- $ mysql [database] -e ‘stmt’ 
• A file or a pipe 
- $ mysql [database] < stmt_file 
- $ cat stmt_file | mysql [database] 
5
SQL Language 
• SQL (Structured Query Language) 
- Provided by RDBMS 
- Data Definition (DDL) 
‣ CREATE TABLE, DROP DATABASE 
- Data Manipulation (DDL) 
‣ SELECT, INSERT, UPDATE, DELETE 
- Data Control (DCL) 
‣ GRANT, REVOKE 
6
Some of The Most Important 
SQL Commands 
• SELECT - extracts data from a database 
• UPDATE - updates data in a database 
• DELETE - deletes data from a database 
• INSERT INTO - inserts new data into a database 
• CREATE DATABASE - creates a new database 
• ALTER DATABASE - modifies a database 
• USE DATABASE - choose a database to execute a statement 
• CREATE TABLE - creates a new table 
• ALTER TABLE - modifies a table 
• DROP TABLE - deletes a table 
• CREATE INDEX - creates an index (search key) 
• DROP INDEX - deletes an index 
7
SQL Syntax 
• SQL is NOT case sensitive: select is the same as 
SELECT 
• Semicolon (;) after SQL Statements 
- or G 
8
Data Types 
Integer 
Type Min Max 2^ 
TINYINT -128 127 2^7 
SMALLINT -32,768 32,767 2^15 
MEDIUMINT -8,388,608 8,388,607 2^23 
INT -2,147,483,648 2,147,483,647 2^31 
BIGINT -9,223,372,036,854,775,808 9,223,372,036,854,775,807 2^63 
Note: If unsigned, the allowable range is from 0 to twice the Max.
Data Types 
Floating-point 
Type Description 
FLOAT(M,D) 
A small number with a floating decimal point. 
Size or Display length (M), Decimals (D) 
0, 23 
DOUBLE(M,D) 
A large number with a floating decimal point. 
0, 53 
DECIMAL(M,D) 0, 65
Data Types 
Date and Time 
Type Description 
DATE 
YYYY-MM-DD format. 
between 1000-01-01 and 9999-12-31. 
DATETIME 
YYYY-MM-DD HH:MM:SS format. 
between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. 
TIMESTAMP 
stored as the number of seconds since the Unix epoch 1970-01-01 
between 1970-01-01 00:00:01 and 2038-01-09 03:14:07. 
TIME HH:MM:SS 
YEAR 1901 to 2155
Data Types 
String 
Type Description 
CHAR 
A fixed-length string between 1 and 255 characters. 
right-padded with spaces. 
VARCHAR A variable-length string between 1 and 255 characters. 
0 to 65,535 in 5.0.3 
TINYTEXT 
TINYBLOB up to 255 characters 
TEXT 
BLOB up to 65,535 characters 
MEDIUMTEXT 
MEDIUMBLOB up to 16,777,215 characters 
LONGTEXT 
LONGBLOB up to 4,294,967,295 characters 
ENUM List of items. For example: ENUM ('A', 'B', 'C') 
Note: case sensitive on BLOBs and are not case sensitive in TEXT fields.
Operators 
(Most Used) 
Operator Description 
= (A = B) is not true. 
!= 
(A != B) is true. 
<> 
> (A > B) is not true. 
< (A < B) is true. 
>= (A >= B) is not true. 
<= (A <= B) is true. 
LIKE Simple pattern matching 
BETWEEN ... AND ... Check whether a value is within a range of values
Basic Syntax 
• Create Database 
- CREATE DATABASE db_name 
• Create Table 
- CREATE TABLE table_name (column_name column_type, …); 
• Insert Data 
- INSERT INTO table_name (field1, field2,...fieldN) VALUES (value1, value2, …valueN); 
• Select Data 
- SELECT field1, field2,...fieldN table_name1, table_name2... [WHERE condition1 [AND -OR condition2]….] 
• Update Data 
- UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause] 
• Delete Data 
- DELETE FROM table_name [WHERE Clause] 
• Sorting Result 
- SELECT field1, field2,...fieldN table_name1, table_name2… ORDER BY field1, [field2...] [ASC [DESC]] 
14
15
Exercise 1 
Data Definition, Data Control 
• Create database `workshop` 
- mysql> CREATE DATABASE `workshop`; 
mysql> USE `workshop`; 
• Grant all privileges to the database user `worker` 
- mysql> GRANT ALL PRIVILEGES on `workshop`.* to `worker`@localhost identified by ‘123456'; 
- mysql> exit; 
• Create table `workshops_list` 
- mysql> CREATE TABLE `workshops_list` ( 
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, 
`title` VARCHAR(255) NOT NULL, 
`description` MEDIUMTEXT DEFAULT NULL, 
PRIMARY KEY(`id`) 
); 
- DESC `workshops_list`; 
16
Exercise 2 
Data Manipulation 
• Show data 
- mysql> SELECT * FROM `workshops_list`; 
• Insert data 
- mysql> INSERT INTO `workshops_list`(`title`) VALUES ('MySQL Workshop'), ('Java Workshop'), 
('Ubuntu'), ('Windows 10’); 
- mysql> SELECT * FROM `workshops_list` G 
• Update data 
- mysql> UPDATE `workshops_list` SET `description` = 'Introduce MySQL database' WHERE id = 1; 
- … 
- mysql> SELECT * FROM `workshops_list` ORDER BY `id` DESC; 
• Remove data 
- mysql> DELETE FROM `workshops_list` WHERE title = 'Windows 10’; 
- mysql> SELECT * FROM `workshops_list` ORDER BY `title` ASC; 
17
Exercise 3 
Export and Import 
• Export database dump. you might want to take a look at the 
contents. 
- $ mysqldump -u worker -p workshop > workshop.sql 
- $ less workshop.sql 
• Delete database `workshop` 
- mysql> DROP DATABASE `workshop`; 
• Import database dump file. 
- $ mysql -u worker -p < workshop.sql 
18
Read The F* Manual 
• RTFM 
- https://dev.mysql.com/doc/ 
• Help 
- mysql> HELP; 
- mysql> HELP CONTENTS; 
- mysql> HELP SELECT; 
19
Thank You

More Related Content

What's hot

MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 

What's hot (20)

Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQL
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Mysql
MysqlMysql
Mysql
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
 
Sql commands
Sql commandsSql commands
Sql commands
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Sql and Sql commands
Sql and Sql commandsSql and Sql commands
Sql and Sql commands
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
 
MySQL Basics
MySQL BasicsMySQL Basics
MySQL Basics
 
MYSQL
MYSQLMYSQL
MYSQL
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 

Viewers also liked

Viewers also liked (17)

MySQL for Beginners - part 1
MySQL for Beginners - part 1MySQL for Beginners - part 1
MySQL for Beginners - part 1
 
Introduction to MySQL
Introduction to MySQLIntroduction to MySQL
Introduction to MySQL
 
MySQL Guide for Beginners
MySQL Guide for BeginnersMySQL Guide for Beginners
MySQL Guide for Beginners
 
Installing MySQL for Python
Installing MySQL for PythonInstalling MySQL for Python
Installing MySQL for Python
 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
 
What is good design?
What is good design?What is good design?
What is good design?
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
What is REST?
What is REST?What is REST?
What is REST?
 
How to be different?
How to be different?How to be different?
How to be different?
 
معرفی سیستم‌های توکار در دانشگاه صنعتی شریف
معرفی سیستم‌های توکار در دانشگاه صنعتی شریفمعرفی سیستم‌های توکار در دانشگاه صنعتی شریف
معرفی سیستم‌های توکار در دانشگاه صنعتی شریف
 
مستندات رفتاری در انجمنهای نرم افزار های آزاد
مستندات رفتاری در انجمنهای نرم افزار های آزادمستندات رفتاری در انجمنهای نرم افزار های آزاد
مستندات رفتاری در انجمنهای نرم افزار های آزاد
 
Web Components Revolution
Web Components RevolutionWeb Components Revolution
Web Components Revolution
 
Java for beginners
Java for beginnersJava for beginners
Java for beginners
 
MySQL Day Paris 2016 - MySQL HA: InnoDB Cluster and NDB Cluster
MySQL Day Paris 2016 - MySQL HA: InnoDB Cluster and NDB ClusterMySQL Day Paris 2016 - MySQL HA: InnoDB Cluster and NDB Cluster
MySQL Day Paris 2016 - MySQL HA: InnoDB Cluster and NDB Cluster
 
Mysql introduction
Mysql introduction Mysql introduction
Mysql introduction
 
Php MySql For Beginners
Php MySql For BeginnersPhp MySql For Beginners
Php MySql For Beginners
 
CBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationCBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL Presentation
 

Similar to MySQL for beginners

xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
WrushabhShirsat3
 
Mysql-overview.pptx
Mysql-overview.pptxMysql-overview.pptx
Mysql-overview.pptx
TamilHunt
 
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdfComplete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
ssuserb5bb0e
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 

Similar to MySQL for beginners (20)

unit-ii.pptx
unit-ii.pptxunit-ii.pptx
unit-ii.pptx
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
 
Introduction databases and MYSQL
Introduction databases and MYSQLIntroduction databases and MYSQL
Introduction databases and MYSQL
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
 
Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)
 
working with database using mysql
working with database using mysql working with database using mysql
working with database using mysql
 
Mysql-overview.pptx
Mysql-overview.pptxMysql-overview.pptx
Mysql-overview.pptx
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Using Mysql.pptx
Using Mysql.pptxUsing Mysql.pptx
Using Mysql.pptx
 
Bt0075, rdbms and my sql
Bt0075, rdbms and my sqlBt0075, rdbms and my sql
Bt0075, rdbms and my sql
 
Bt0075, rdbms and my sql
Bt0075, rdbms and my sqlBt0075, rdbms and my sql
Bt0075, rdbms and my sql
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
My sql1
My sql1My sql1
My sql1
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
Module02
Module02Module02
Module02
 
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdfComplete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
 

More from Saeid Zebardast

More from Saeid Zebardast (7)

An Introduction to Apache Cassandra
An Introduction to Apache CassandraAn Introduction to Apache Cassandra
An Introduction to Apache Cassandra
 
An overview of Scalable Web Application Front-end
An overview of Scalable Web Application Front-endAn overview of Scalable Web Application Front-end
An overview of Scalable Web Application Front-end
 
MySQL Cheat Sheet
MySQL Cheat SheetMySQL Cheat Sheet
MySQL Cheat Sheet
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Developing Applications with MySQL and Java for beginners
Developing Applications with MySQL and Java for beginnersDeveloping Applications with MySQL and Java for beginners
Developing Applications with MySQL and Java for beginners
 
هفده اصل افراد موثر در تیم
هفده اصل افراد موثر در تیمهفده اصل افراد موثر در تیم
هفده اصل افراد موثر در تیم
 
معرفی گنو/لینوکس و سیستم عامل های متن باز و آزاد
معرفی گنو/لینوکس و سیستم عامل های متن باز و آزادمعرفی گنو/لینوکس و سیستم عامل های متن باز و آزاد
معرفی گنو/لینوکس و سیستم عامل های متن باز و آزاد
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

MySQL for beginners

  • 1. Saeid Zebardast @saeid http://about.me/saeid saeid.zebardast@gmail.com 1
  • 2. Please Please Please Ask Questions As Much As You Like • This is not a lecture! - But an opportunity to learn from each other. - If you haven’t seen some of these frameworks, methods, etc. It is OK! - Let we know if you know ‣ Better ways ‣ Best practices ‣ My mistakes!
  • 3. Introduction • What’s MySQL? - Since 1995 - Written in C/C++ - RDMBS (Relational Database Management System) 3
  • 4. Installation • Just enter the following command: - $ sudo apt-get install mysql-server mysql-client • Check MySQL CLI: - $ mysql -u root -p - mysql> SHOW DATABASES; 4
  • 5. Execute SQL Statements • Interactively - $ mysql [database] ‣ mysql> stmt; • Command Line - $ mysql [database] -e ‘stmt’ • A file or a pipe - $ mysql [database] < stmt_file - $ cat stmt_file | mysql [database] 5
  • 6. SQL Language • SQL (Structured Query Language) - Provided by RDBMS - Data Definition (DDL) ‣ CREATE TABLE, DROP DATABASE - Data Manipulation (DDL) ‣ SELECT, INSERT, UPDATE, DELETE - Data Control (DCL) ‣ GRANT, REVOKE 6
  • 7. Some of The Most Important SQL Commands • SELECT - extracts data from a database • UPDATE - updates data in a database • DELETE - deletes data from a database • INSERT INTO - inserts new data into a database • CREATE DATABASE - creates a new database • ALTER DATABASE - modifies a database • USE DATABASE - choose a database to execute a statement • CREATE TABLE - creates a new table • ALTER TABLE - modifies a table • DROP TABLE - deletes a table • CREATE INDEX - creates an index (search key) • DROP INDEX - deletes an index 7
  • 8. SQL Syntax • SQL is NOT case sensitive: select is the same as SELECT • Semicolon (;) after SQL Statements - or G 8
  • 9. Data Types Integer Type Min Max 2^ TINYINT -128 127 2^7 SMALLINT -32,768 32,767 2^15 MEDIUMINT -8,388,608 8,388,607 2^23 INT -2,147,483,648 2,147,483,647 2^31 BIGINT -9,223,372,036,854,775,808 9,223,372,036,854,775,807 2^63 Note: If unsigned, the allowable range is from 0 to twice the Max.
  • 10. Data Types Floating-point Type Description FLOAT(M,D) A small number with a floating decimal point. Size or Display length (M), Decimals (D) 0, 23 DOUBLE(M,D) A large number with a floating decimal point. 0, 53 DECIMAL(M,D) 0, 65
  • 11. Data Types Date and Time Type Description DATE YYYY-MM-DD format. between 1000-01-01 and 9999-12-31. DATETIME YYYY-MM-DD HH:MM:SS format. between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. TIMESTAMP stored as the number of seconds since the Unix epoch 1970-01-01 between 1970-01-01 00:00:01 and 2038-01-09 03:14:07. TIME HH:MM:SS YEAR 1901 to 2155
  • 12. Data Types String Type Description CHAR A fixed-length string between 1 and 255 characters. right-padded with spaces. VARCHAR A variable-length string between 1 and 255 characters. 0 to 65,535 in 5.0.3 TINYTEXT TINYBLOB up to 255 characters TEXT BLOB up to 65,535 characters MEDIUMTEXT MEDIUMBLOB up to 16,777,215 characters LONGTEXT LONGBLOB up to 4,294,967,295 characters ENUM List of items. For example: ENUM ('A', 'B', 'C') Note: case sensitive on BLOBs and are not case sensitive in TEXT fields.
  • 13. Operators (Most Used) Operator Description = (A = B) is not true. != (A != B) is true. <> > (A > B) is not true. < (A < B) is true. >= (A >= B) is not true. <= (A <= B) is true. LIKE Simple pattern matching BETWEEN ... AND ... Check whether a value is within a range of values
  • 14. Basic Syntax • Create Database - CREATE DATABASE db_name • Create Table - CREATE TABLE table_name (column_name column_type, …); • Insert Data - INSERT INTO table_name (field1, field2,...fieldN) VALUES (value1, value2, …valueN); • Select Data - SELECT field1, field2,...fieldN table_name1, table_name2... [WHERE condition1 [AND -OR condition2]….] • Update Data - UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause] • Delete Data - DELETE FROM table_name [WHERE Clause] • Sorting Result - SELECT field1, field2,...fieldN table_name1, table_name2… ORDER BY field1, [field2...] [ASC [DESC]] 14
  • 15. 15
  • 16. Exercise 1 Data Definition, Data Control • Create database `workshop` - mysql> CREATE DATABASE `workshop`; mysql> USE `workshop`; • Grant all privileges to the database user `worker` - mysql> GRANT ALL PRIVILEGES on `workshop`.* to `worker`@localhost identified by ‘123456'; - mysql> exit; • Create table `workshops_list` - mysql> CREATE TABLE `workshops_list` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `description` MEDIUMTEXT DEFAULT NULL, PRIMARY KEY(`id`) ); - DESC `workshops_list`; 16
  • 17. Exercise 2 Data Manipulation • Show data - mysql> SELECT * FROM `workshops_list`; • Insert data - mysql> INSERT INTO `workshops_list`(`title`) VALUES ('MySQL Workshop'), ('Java Workshop'), ('Ubuntu'), ('Windows 10’); - mysql> SELECT * FROM `workshops_list` G • Update data - mysql> UPDATE `workshops_list` SET `description` = 'Introduce MySQL database' WHERE id = 1; - … - mysql> SELECT * FROM `workshops_list` ORDER BY `id` DESC; • Remove data - mysql> DELETE FROM `workshops_list` WHERE title = 'Windows 10’; - mysql> SELECT * FROM `workshops_list` ORDER BY `title` ASC; 17
  • 18. Exercise 3 Export and Import • Export database dump. you might want to take a look at the contents. - $ mysqldump -u worker -p workshop > workshop.sql - $ less workshop.sql • Delete database `workshop` - mysql> DROP DATABASE `workshop`; • Import database dump file. - $ mysql -u worker -p < workshop.sql 18
  • 19. Read The F* Manual • RTFM - https://dev.mysql.com/doc/ • Help - mysql> HELP; - mysql> HELP CONTENTS; - mysql> HELP SELECT; 19