SlideShare a Scribd company logo
1 of 48
Download to read offline
MySQL Basics I


         Sang Shin
http://www.javapassion.com
     “Learning is fun!”

                             1
Topics
•   What is MySQL?
•   Installation of MySQL server
•   “mysql” command-line client
•   SQL basics
    >   Concept & terminology
    >   Databases, tables, fields
    >   Insert/Update/Delete
    >   Retrieving records through SELECT
    >   Arithmetic operations
• Read and execute SQL script file
                                            2
What is MySQL?
What is MySQL?
• Most popular open source database
 > High performance
 > High reliability
 > Ease of use
• Runs many of the world's most demanding
  websites
 > Yahoo, Google, YouTube, ...
• “M” of LAMP (Linux, Apache, MySQL, PHP)
  stack
• Runs on all possible OS platforms
                                            4
MySQL Products
• MySQL community server
 > Free
• MySQL Enterprise
 > Commercial
 > Enterprise features - monitoring
• MySQL Cluster
 > Provides fault tolerance
• MySQL embedded database
 > Embedded in small devices
• MySQL Workbench
 > GUI tool
                                      5
Installation of
MySQL Server
MySQL Server
• “mysqld” is a runnable program which
  represents MySQL database server




                                         7
Installation Options
• Windows
 > MySQL database server can be installed either
   runnable program or Windows service
• Other platforms (Linux, MacOS, OpenSolaris)
 > As part of LAMP stack or
 > Independently as runnable program




                                                   8
“mysql” Command-line
        Client
What is “mysql” command-line client?
• Comes with MySQL package
• Connects to the running MySQL database
  server when run
• Can be run either in interactive mode or
  non-interactive mode
• When run in interactive mode, it provides a
  shell in which SQL commands can be
  executed
• Can be run with many options
  > mysql --help
                                                10
SQL Basics:
Concept & Terminology
What is SQL?
• SQL is language for retrieving and
  manipulating data in a relational database
  > Data definition
  > Data manipulation
  > Data control
• Open standard - ANSI
  > Vendor implementations add vendor-specific
   features, however




                                                 12
SQL Terminology
• Table
 > A set of rows
 > Analogous to a “file”
• Row
 > Analogous to a record of a “file”
• Column
 > A column is analogous to a field of a record
 > Each column in a given row has a single value
• Primary Key
 > One of more columns whose contents are unique
   within a table and thus can be used to identify a
   row of that table                                 13
Types of SQL Statements
• DDL (Data Definition Language)
 > Used to build and modify the structure of your
   tables and other objects in the database
 > Examples: CREATE TABLE, ALTER TABLE, DROP
   TABLE, CREATE VIEW, ...
• DML (Data Manipulation Language)
 > Used to work with the data in tables
 > INSERT INTO, UPDATE, DELETE
• DCL (Data Control Language)
 > Used to control access rights
 > GRANT, REVOKE

                                                    14
SQL Basics:
 Databases
Creating a database
mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.01 sec)




                                      16
Setting a default database
mysql> USE mydb;
Database changed




                             17
Dropping Databases
mysql> DROP DATABASE temp_db;
Query OK, 0 rows affected (0.01 sec)

mysql> DROP DATABASE IF EXISTS temp_db;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database             |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| test             |
+--------------------+
4 rows in set (0.00 sec)

                                                  18
SQL Basics:
  Tables
Creating a Table
mysql> CREATE TABLE person (
  -> person_id SMALLINT UNSIGNED NOT NULL,
  -> first_name VARCHAR(45) NOT NULL,
  -> last_name VARCHAR(45) NOT NULL,
  -> PRIMARY KEY (person_id)
  -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.14 sec)

mysql> SHOW TABLES;
+----------------+
| Tables_in_mydb |
+----------------+
| person          |
+----------------+
1 row in set (0.00 sec)


                                             20
Altering table name (Two options)
mysql> ALTER TABLE person rename to person1;
Query OK, 0 rows affected (0.06 sec)

mysql> SHOW TABLES;
+----------------------+
| Tables_in_mydb |
+----------------------+
| person1              |
+----------------------+
1 row in set (0.00 sec)

mysql> RENAME TABLE person1 TO whatever;
Query OK, 0 rows affected (0.05 sec)

mysql> SHOW TABLES;
+----------------------+
| Tables_in_mydb |
+----------------------+
| whatever             |
+----------------------+                       21
Altering field name and type
mysql> ALTER TABLE person CHANGE last_name surname varchar(30);
Query OK, 0 rows affected (0.62 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESCRIBE person;
+------------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default | Extra           |
+------------+----------------------+------+-----+---------+----------------+
| person_id | smallint(5) unsigned | NO | PRI | NULL |
| first_name | varchar(45)               | NO | | NULL |                       |
| surname | varchar(30)                 | YES | | NULL |                      |
+------------+----------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)




                                                                                   22
Adding or removing fields
mysql> ALTER TABLE person ADD age smallint(3) unsigned not null;
Query OK, 0 rows affected (0.42 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESCRIBE person;
+------------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default | Extra           |
+------------+----------------------+------+-----+---------+----------------+
| person_id | smallint(5) unsigned | NO | PRI | NULL |
| first_name | varchar(45)               | NO | | NULL |                       |
| surname | varchar(30)                 | YES | | NULL |                      |
| age        | smallint(3) unsigned | NO | | NULL |                             |
+------------+----------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> ALTER TABLE person DROP first_name;
Query OK, 0 rows affected (0.25 sec)
Records: 0 Duplicates: 0 Warnings: 0                                                23
Dropping Tables
mysql> SHOW TABLES;
+-------------------+
| Tables_in_temp_db |
+-------------------+
| temp_table          |
+-------------------+
1 row in set (0.00 sec)

mysql> DROP TABLE temp_table;
Query OK, 0 rows affected (0.06 sec)

mysql> DROP TABLE IF EXISTS temp_table;
Query OK, 0 rows affected, 1 warning (0.12 sec)

mysql> SHOW TABLES;
Empty set (0.00 sec)

                                                  24
Working with tables from Multiple Databases
mysql> SELECT * FROM temp_db.temp_table;
+---------+---------------------+
| temp_id | temp_whatever         |
+---------+---------------------+
|     1 | life is good         |
|     2 | life is even better |
+---------+---------------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM mydb.student;
+------------+------------+-----------+-----+-------+
| student_id | first_name | last_name | age | grade |
+------------+------------+-----------+-----+-------+
|        1 | yuna        | kim       | 19 | 4 |
|        2 | kelly      | jones | 22 | 5 |
+------------+------------+-----------+-----+-------+
2 rows in set (0.00 sec)
                                                        25
SQL Basics:
  Fields
Field Definitions
• Each field has
  > Field name
  > Data type
  > Field modifier or constraint




                                   27
Field Data types - Integers
• TINYINT
  > 1 byte, -128 to 127 (signed), 0 to 255 (unsigned)
• SMALLINT
  > 2 bytes, -32768 to 32767 (signed), 0 to 65535
    (unsigned)
• MEDIUMINT
  > 3 bytes
• INT
  > 4 bytes
• BIGINT
  > 8 bytes
                                                        28
Field Data types
• FLOAT
  > single precision floating-point value
• DOUBLE
  > double precision floating-point value
• DECIMAL
  > decimal values
• BIT
  > bit value
  > b'0101'


                                            29
Field Data Types
• CHAR
 > Fixed length strings up to 255 characters
• VARCHAR
 > Variable length strings up to 255 characters
• DATE, TIME, YEAR
• DATETIME, TIMESTAMP
• ENUM, SET
 > Predefined set of values



                                                  30
Field Modifiers
• NULL or NOT NULL
 > Indicates if the field can be null or not
• DEFAULT
 > Assigns default value if no value is specified
   when a new record is inserted
• AUTO_INCREMENT
 > MySQL automatically generates a number (by
   incrementing the previous value by 1)
 > Used for creating primary key
• CHARACTER SET
 > Specifies the character set for string values
                                                    31
SQL Basics:
INSERT/UPDATE/DELETE
INSERT'ing a single record
mysql> INSERT INTO person (person_id, first_name, last_name, age)
    -> VALUES (1, 'sang', 'shin', 88);
Query OK, 1 row affected (0.10 sec)

mysql> SELECT * FROM person;
+-----------+------------+-----------+-----+
| person_id | first_name | last_name | age |
+-----------+------------+-----------+-----+
|       1 | sang        | shin     | 88 |
+-----------+------------+-----------+-----+
1 row in set (0.00 sec)




                                                                    33
INSERT'ing multiple records
mysql> INSERT INTO person (person_id, first_name, last_name, age)
  -> VALUES
  -> (2, 'kelly', 'jones', 22),
  -> (3, 'jack', 'kennedy', 56),
  -> (4, 'paul', 'kennedy', 34),
  -> (5, 'daniel', 'song', 24),
  -> (6, 'nichole', 'scott', 9);
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0




                                                                    34
Deleting recored(s)
mysql> DELETE FROM person WHERE age < 10;
Query OK, 1 row affected (0.07 sec)




                                            35
Updating record(s)
mysql> UPDATE person SET age = 88
  -> WHERE age = 99 OR first_name = 'paul';
Query OK, 1 row affected, 2 warnings (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 2

mysql> SELECT * FROM person;
+-----------+------------+-----------+-----+
| person_id | first_name | last_name | age |
+-----------+------------+-----------+-----+
|       1 | sang        | shin     | 88 |
|       2 | kelly      | jones | 22 |
|       3 | jack       | kennedy | 56 |
|       4 | paul       | kennedy | 88 |
+-----------+------------+-----------+-----+
4 rows in set (0.00 sec)


                                                  36
SQL Basics:
 SELECT
Retrieving some fields selectively
mysql> SELECT last_name, age FROM person;
+-----------+-----+
| last_name | age |
+-----------+-----+
| shin      | 88 |
| jones | 22 |
| kennedy | 56 |
| kennedy | 34 |
| song       | 24 |
+-----------+-----+
5 rows in set (0.00 sec)




                                            38
Retrieving with WHERE clause
mysql> SELECT first_name, age FROM person
       -> WHERE age > 50;
+------------+-----+
| first_name | age |
+------------+-----+
| sang       | 88 |
| jack      | 56 |
+------------+-----+
2 rows in set (0.00 sec)

mysql> SELECT first_name, last_name, age FROM person
       -> WHERE age < 50 AND first_name LIKE '%niel';
+------------+-----------+-----+
| first_name | last_name | age |
+------------+-----------+-----+
| daniel | song          | 24 |
+------------+-----------+-----+
1 row in set (0.00 sec)                                 39
Retrieving records in order
mysql> SELECT last_name, age FROM person
    -> ORDER BY age ASC;
+-----------+-----+
| last_name | age |
+-----------+-----+
| jones | 22 |
| song       | 24 |
| kennedy | 34 |
| kennedy | 56 |
| shin      | 88 |
+-----------+-----+
5 rows in set (0.00 sec)

mysql> SELECT * FROM person
      -> ORDER BY age DESC;
+-----------+------------+-----------+-----+
| person_id | first_name | last_name | age |
+-----------+------------+-----------+-----+
|       1 | sang        | shin     | 88 |
|       3 | jack      | kennedy | 56 |
|       4 | paul       | kennedy | 34 |
|       5 | daniel | song           | 24 |     40
Retrieving limited number of records
mysql> SELECT * from person
   -> ORDER BY age DESC
   -> LIMIT 3;
+-----------+------------+-----------+-----+
| person_id | first_name | last_name | age |
+-----------+------------+-----------+-----+
|       1 | sang        | shin     | 88 |
|       3 | jack       | kennedy | 56 |
|       4 | paul       | kennedy | 34 |
+-----------+------------+-----------+-----+
3 rows in set (0.00 sec)




                                               41
Basic SQL Commands:
 Arithmetic Functions
Arithmetic operations
mysql> SELECT 3 + 6;
+-------+
|3+6|
+-------+
| 9|
+-------+
1 row in set (0.00 sec)

mysql> SELECT 45 * (1+2);
+------------+
| 45 * (1+2) |
+------------+
|      135 |
+------------+
1 row in set (0.00 sec)


                            43
COUNT, AVG, SUM
mysql> SELECT COUNT(age) FROM person;
+------------+
| COUNT(age) |
+------------+
|       5|
+------------+
1 row in set (0.04 sec)

mysql> SELECT AVG(age) from person;
+----------+
| AVG(age) |
+----------+
| 44.8000 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT SUM(age) FROM person;
+----------+
| SUM(age) |
+----------+
|    224 |
+----------+
1 row in set (0.00 sec)                 44
MIN, MAX
mysql> SELECT MIN(age) FROM person;
+----------+
| MIN(age) |
+----------+
|     22 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT MAX(age) FROM person;
+----------+
| MAX(age) |
+----------+
|     88 |
+----------+
1 row in set (0.00 sec)


                                      45
Reading and Executing
   SQL Script File
Read and execute a SQL script file
mysql> SOURCE c:/tmp/student.sql
Query OK, 0 rows affected (0.10 sec)




                                       47
Thank you!



         Sang Shin
http://www.javapassion.com
     “Learning is fun!”

                             48
                             49

More Related Content

What's hot

MySQL's JSON Data Type and Document Store
MySQL's JSON Data Type and Document StoreMySQL's JSON Data Type and Document Store
MySQL's JSON Data Type and Document StoreDave Stokes
 
The Ring programming language version 1.10 book - Part 37 of 212
The Ring programming language version 1.10 book - Part 37 of 212The Ring programming language version 1.10 book - Part 37 of 212
The Ring programming language version 1.10 book - Part 37 of 212Mahmoud Samir Fayed
 
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitMySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitDave Stokes
 
Cassandra 3.0 Awesomeness
Cassandra 3.0 AwesomenessCassandra 3.0 Awesomeness
Cassandra 3.0 AwesomenessJon Haddad
 
MariaDB and Cassandra Interoperability
MariaDB and Cassandra InteroperabilityMariaDB and Cassandra Interoperability
MariaDB and Cassandra InteroperabilityColin Charles
 
The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.1 book - Part 27 of 180The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.1 book - Part 27 of 180Mahmoud Samir Fayed
 
Cassandra 3.0 advanced preview
Cassandra 3.0 advanced previewCassandra 3.0 advanced preview
Cassandra 3.0 advanced previewPatrick McFadin
 
Database administration commands
Database administration commands Database administration commands
Database administration commands Varsha Ajith
 
Cassandra Summit 2013 Keynote
Cassandra Summit 2013 KeynoteCassandra Summit 2013 Keynote
Cassandra Summit 2013 Keynotejbellis
 
Json improvements in my sql 8.0
Json improvements in my sql 8.0  Json improvements in my sql 8.0
Json improvements in my sql 8.0 Mysql User Camp
 
The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31Mahmoud Samir Fayed
 
Cassandra 3.0 Data Modeling
Cassandra 3.0 Data ModelingCassandra 3.0 Data Modeling
Cassandra 3.0 Data ModelingDataStax Academy
 
MariaDB Cassandra Interoperability
MariaDB Cassandra InteroperabilityMariaDB Cassandra Interoperability
MariaDB Cassandra InteroperabilityColin Charles
 
The Ring programming language version 1.2 book - Part 17 of 84
The Ring programming language version 1.2 book - Part 17 of 84The Ring programming language version 1.2 book - Part 17 of 84
The Ring programming language version 1.2 book - Part 17 of 84Mahmoud Samir Fayed
 
Presentation indexing new features oracle 11g release 1 and release 2
Presentation    indexing new features oracle 11g release 1 and release 2Presentation    indexing new features oracle 11g release 1 and release 2
Presentation indexing new features oracle 11g release 1 and release 2xKinAnx
 

What's hot (20)

MySQL's JSON Data Type and Document Store
MySQL's JSON Data Type and Document StoreMySQL's JSON Data Type and Document Store
MySQL's JSON Data Type and Document Store
 
The Ring programming language version 1.10 book - Part 37 of 212
The Ring programming language version 1.10 book - Part 37 of 212The Ring programming language version 1.10 book - Part 37 of 212
The Ring programming language version 1.10 book - Part 37 of 212
 
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitMySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
 
Cassandra 3.0 Awesomeness
Cassandra 3.0 AwesomenessCassandra 3.0 Awesomeness
Cassandra 3.0 Awesomeness
 
MariaDB and Cassandra Interoperability
MariaDB and Cassandra InteroperabilityMariaDB and Cassandra Interoperability
MariaDB and Cassandra Interoperability
 
The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.1 book - Part 27 of 180The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.1 book - Part 27 of 180
 
Cassandra 3.0 advanced preview
Cassandra 3.0 advanced previewCassandra 3.0 advanced preview
Cassandra 3.0 advanced preview
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 
Database administration commands
Database administration commands Database administration commands
Database administration commands
 
Cassandra 2.2 & 3.0
Cassandra 2.2 & 3.0Cassandra 2.2 & 3.0
Cassandra 2.2 & 3.0
 
Cassandra Summit 2013 Keynote
Cassandra Summit 2013 KeynoteCassandra Summit 2013 Keynote
Cassandra Summit 2013 Keynote
 
Oracle ORA Errors
Oracle ORA ErrorsOracle ORA Errors
Oracle ORA Errors
 
Json improvements in my sql 8.0
Json improvements in my sql 8.0  Json improvements in my sql 8.0
Json improvements in my sql 8.0
 
The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31
 
Mysql all
Mysql allMysql all
Mysql all
 
Cassandra 3.0 Data Modeling
Cassandra 3.0 Data ModelingCassandra 3.0 Data Modeling
Cassandra 3.0 Data Modeling
 
MariaDB Cassandra Interoperability
MariaDB Cassandra InteroperabilityMariaDB Cassandra Interoperability
MariaDB Cassandra Interoperability
 
The Ring programming language version 1.2 book - Part 17 of 84
The Ring programming language version 1.2 book - Part 17 of 84The Ring programming language version 1.2 book - Part 17 of 84
The Ring programming language version 1.2 book - Part 17 of 84
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Presentation indexing new features oracle 11g release 1 and release 2
Presentation    indexing new features oracle 11g release 1 and release 2Presentation    indexing new features oracle 11g release 1 and release 2
Presentation indexing new features oracle 11g release 1 and release 2
 

Viewers also liked

My sql fabric webinar tw2
My sql fabric webinar tw2My sql fabric webinar tw2
My sql fabric webinar tw2Ivan Tu
 
3 周彦偉-隨需而變 我所經歷的my sql架構變遷﹣周彥偉﹣acmug@2015.12台北
3 周彦偉-隨需而變 我所經歷的my sql架構變遷﹣周彥偉﹣acmug@2015.12台北3 周彦偉-隨需而變 我所經歷的my sql架構變遷﹣周彥偉﹣acmug@2015.12台北
3 周彦偉-隨需而變 我所經歷的my sql架構變遷﹣周彥偉﹣acmug@2015.12台北Ivan Tu
 
My sql resources_april2012_zht
My sql resources_april2012_zhtMy sql resources_april2012_zht
My sql resources_april2012_zhtIvan Tu
 
mysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handlermysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handlerUlf Wendel
 
10 things you might not know about MySQL
10 things you might not know about MySQL10 things you might not know about MySQL
10 things you might not know about MySQLJorge Bernal
 
Linux17 MySQL_installation
Linux17 MySQL_installationLinux17 MySQL_installation
Linux17 MySQL_installationJainul Musani
 
Easy MySQL Replication Setup and Troubleshooting
Easy MySQL Replication Setup and TroubleshootingEasy MySQL Replication Setup and Troubleshooting
Easy MySQL Replication Setup and TroubleshootingBob Burgess
 
MySQL Tuning using digested slow-logs
MySQL Tuning using digested slow-logsMySQL Tuning using digested slow-logs
MySQL Tuning using digested slow-logsBob Burgess
 
Performance Schema in MySQL (Danil Zburivsky)
Performance Schema in MySQL (Danil Zburivsky)Performance Schema in MySQL (Danil Zburivsky)
Performance Schema in MySQL (Danil Zburivsky)Ontico
 
The care and feeding of a MySQL database
The care and feeding of a MySQL databaseThe care and feeding of a MySQL database
The care and feeding of a MySQL databaseDave Stokes
 
PoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HAPoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HAUlf Wendel
 
Mysql query optimization
Mysql query optimizationMysql query optimization
Mysql query optimizationBaohua Cai
 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group ReplicationUlf Wendel
 
Modern MySQL Monitoring and Dashboards.
Modern MySQL Monitoring and Dashboards.Modern MySQL Monitoring and Dashboards.
Modern MySQL Monitoring and Dashboards.Mydbops
 
Best Practices in Migrating to MySQL - Part 1
Best Practices in Migrating to MySQL - Part 1Best Practices in Migrating to MySQL - Part 1
Best Practices in Migrating to MySQL - Part 1Ronald Bradford
 

Viewers also liked (20)

My sql fabric webinar tw2
My sql fabric webinar tw2My sql fabric webinar tw2
My sql fabric webinar tw2
 
3 周彦偉-隨需而變 我所經歷的my sql架構變遷﹣周彥偉﹣acmug@2015.12台北
3 周彦偉-隨需而變 我所經歷的my sql架構變遷﹣周彥偉﹣acmug@2015.12台北3 周彦偉-隨需而變 我所經歷的my sql架構變遷﹣周彥偉﹣acmug@2015.12台北
3 周彦偉-隨需而變 我所經歷的my sql架構變遷﹣周彥偉﹣acmug@2015.12台北
 
My sql resources_april2012_zht
My sql resources_april2012_zhtMy sql resources_april2012_zht
My sql resources_april2012_zht
 
ppt
pptppt
ppt
 
My sql102
My sql102My sql102
My sql102
 
mysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handlermysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handler
 
10 things you might not know about MySQL
10 things you might not know about MySQL10 things you might not know about MySQL
10 things you might not know about MySQL
 
Linux17 MySQL_installation
Linux17 MySQL_installationLinux17 MySQL_installation
Linux17 MySQL_installation
 
Cb08 sanchez citlali.ppsx
Cb08 sanchez citlali.ppsxCb08 sanchez citlali.ppsx
Cb08 sanchez citlali.ppsx
 
Easy MySQL Replication Setup and Troubleshooting
Easy MySQL Replication Setup and TroubleshootingEasy MySQL Replication Setup and Troubleshooting
Easy MySQL Replication Setup and Troubleshooting
 
MySQL Tuning using digested slow-logs
MySQL Tuning using digested slow-logsMySQL Tuning using digested slow-logs
MySQL Tuning using digested slow-logs
 
Performance Schema in MySQL (Danil Zburivsky)
Performance Schema in MySQL (Danil Zburivsky)Performance Schema in MySQL (Danil Zburivsky)
Performance Schema in MySQL (Danil Zburivsky)
 
MySQL database
MySQL databaseMySQL database
MySQL database
 
The care and feeding of a MySQL database
The care and feeding of a MySQL databaseThe care and feeding of a MySQL database
The care and feeding of a MySQL database
 
PoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HAPoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HA
 
Mysql query optimization
Mysql query optimizationMysql query optimization
Mysql query optimization
 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group Replication
 
MySQL DBA
MySQL DBAMySQL DBA
MySQL DBA
 
Modern MySQL Monitoring and Dashboards.
Modern MySQL Monitoring and Dashboards.Modern MySQL Monitoring and Dashboards.
Modern MySQL Monitoring and Dashboards.
 
Best Practices in Migrating to MySQL - Part 1
Best Practices in Migrating to MySQL - Part 1Best Practices in Migrating to MySQL - Part 1
Best Practices in Migrating to MySQL - Part 1
 

Similar to Mysql basics1

Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationRichard Crowley
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesDamien Seguy
 
Introduction databases and MYSQL
Introduction databases and MYSQLIntroduction databases and MYSQL
Introduction databases and MYSQLNaeem Junejo
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction databaseMudasir Syed
 
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 Design and Develop SQL DDL statements which demonstrate the use of SQL objec... Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...bhavesh lande
 
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)Tesora
 
MySQL Idiosyncrasies That Bite SF
MySQL Idiosyncrasies That Bite SFMySQL Idiosyncrasies That Bite SF
MySQL Idiosyncrasies That Bite SFRonald Bradford
 
MySQL Idiosyncrasies That Bite 2010.07
MySQL Idiosyncrasies That Bite 2010.07MySQL Idiosyncrasies That Bite 2010.07
MySQL Idiosyncrasies That Bite 2010.07Ronald Bradford
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsSveta Smirnova
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query TuningSveta Smirnova
 
Lecture3 mysql gui by okello erick
Lecture3 mysql gui by okello erickLecture3 mysql gui by okello erick
Lecture3 mysql gui by okello erickokelloerick
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingSveta Smirnova
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.pptKISHOYIANKISH
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfzJoshua Thijssen
 
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018teachersduniya.com
 
How to Avoid Pitfalls in Schema Upgrade with Galera
How to Avoid Pitfalls in Schema Upgrade with GaleraHow to Avoid Pitfalls in Schema Upgrade with Galera
How to Avoid Pitfalls in Schema Upgrade with GaleraSveta Smirnova
 

Similar to Mysql basics1 (20)

Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System Presentation
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queries
 
Hanya contoh saja dari xampp
Hanya contoh saja dari xamppHanya contoh saja dari xampp
Hanya contoh saja dari xampp
 
My sql1
My sql1My sql1
My sql1
 
My SQL
My SQLMy SQL
My SQL
 
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
 
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 Design and Develop SQL DDL statements which demonstrate the use of SQL objec... Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
Percona Live 4/15/15: Transparent sharding database virtualization engine (DVE)
 
MySQL Idiosyncrasies That Bite SF
MySQL Idiosyncrasies That Bite SFMySQL Idiosyncrasies That Bite SF
MySQL Idiosyncrasies That Bite SF
 
MySQL Idiosyncrasies That Bite 2010.07
MySQL Idiosyncrasies That Bite 2010.07MySQL Idiosyncrasies That Bite 2010.07
MySQL Idiosyncrasies That Bite 2010.07
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database Administrators
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
 
Instalar MySQL CentOS
Instalar MySQL CentOSInstalar MySQL CentOS
Instalar MySQL CentOS
 
Lecture3 mysql gui by okello erick
Lecture3 mysql gui by okello erickLecture3 mysql gui by okello erick
Lecture3 mysql gui by okello erick
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfz
 
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
 
How to Avoid Pitfalls in Schema Upgrade with Galera
How to Avoid Pitfalls in Schema Upgrade with GaleraHow to Avoid Pitfalls in Schema Upgrade with Galera
How to Avoid Pitfalls in Schema Upgrade with Galera
 

Recently uploaded

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 

Recently uploaded (20)

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 

Mysql basics1

  • 1. MySQL Basics I Sang Shin http://www.javapassion.com “Learning is fun!” 1
  • 2. Topics • What is MySQL? • Installation of MySQL server • “mysql” command-line client • SQL basics > Concept & terminology > Databases, tables, fields > Insert/Update/Delete > Retrieving records through SELECT > Arithmetic operations • Read and execute SQL script file 2
  • 4. What is MySQL? • Most popular open source database > High performance > High reliability > Ease of use • Runs many of the world's most demanding websites > Yahoo, Google, YouTube, ... • “M” of LAMP (Linux, Apache, MySQL, PHP) stack • Runs on all possible OS platforms 4
  • 5. MySQL Products • MySQL community server > Free • MySQL Enterprise > Commercial > Enterprise features - monitoring • MySQL Cluster > Provides fault tolerance • MySQL embedded database > Embedded in small devices • MySQL Workbench > GUI tool 5
  • 7. MySQL Server • “mysqld” is a runnable program which represents MySQL database server 7
  • 8. Installation Options • Windows > MySQL database server can be installed either runnable program or Windows service • Other platforms (Linux, MacOS, OpenSolaris) > As part of LAMP stack or > Independently as runnable program 8
  • 10. What is “mysql” command-line client? • Comes with MySQL package • Connects to the running MySQL database server when run • Can be run either in interactive mode or non-interactive mode • When run in interactive mode, it provides a shell in which SQL commands can be executed • Can be run with many options > mysql --help 10
  • 11. SQL Basics: Concept & Terminology
  • 12. What is SQL? • SQL is language for retrieving and manipulating data in a relational database > Data definition > Data manipulation > Data control • Open standard - ANSI > Vendor implementations add vendor-specific features, however 12
  • 13. SQL Terminology • Table > A set of rows > Analogous to a “file” • Row > Analogous to a record of a “file” • Column > A column is analogous to a field of a record > Each column in a given row has a single value • Primary Key > One of more columns whose contents are unique within a table and thus can be used to identify a row of that table 13
  • 14. Types of SQL Statements • DDL (Data Definition Language) > Used to build and modify the structure of your tables and other objects in the database > Examples: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE VIEW, ... • DML (Data Manipulation Language) > Used to work with the data in tables > INSERT INTO, UPDATE, DELETE • DCL (Data Control Language) > Used to control access rights > GRANT, REVOKE 14
  • 16. Creating a database mysql> CREATE DATABASE mydb; Query OK, 1 row affected (0.01 sec) 16
  • 17. Setting a default database mysql> USE mydb; Database changed 17
  • 18. Dropping Databases mysql> DROP DATABASE temp_db; Query OK, 0 rows affected (0.01 sec) mysql> DROP DATABASE IF EXISTS temp_db; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) 18
  • 19. SQL Basics: Tables
  • 20. Creating a Table mysql> CREATE TABLE person ( -> person_id SMALLINT UNSIGNED NOT NULL, -> first_name VARCHAR(45) NOT NULL, -> last_name VARCHAR(45) NOT NULL, -> PRIMARY KEY (person_id) -> ) ENGINE=InnoDB; Query OK, 0 rows affected (0.14 sec) mysql> SHOW TABLES; +----------------+ | Tables_in_mydb | +----------------+ | person | +----------------+ 1 row in set (0.00 sec) 20
  • 21. Altering table name (Two options) mysql> ALTER TABLE person rename to person1; Query OK, 0 rows affected (0.06 sec) mysql> SHOW TABLES; +----------------------+ | Tables_in_mydb | +----------------------+ | person1 | +----------------------+ 1 row in set (0.00 sec) mysql> RENAME TABLE person1 TO whatever; Query OK, 0 rows affected (0.05 sec) mysql> SHOW TABLES; +----------------------+ | Tables_in_mydb | +----------------------+ | whatever | +----------------------+ 21
  • 22. Altering field name and type mysql> ALTER TABLE person CHANGE last_name surname varchar(30); Query OK, 0 rows affected (0.62 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESCRIBE person; +------------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+----------------------+------+-----+---------+----------------+ | person_id | smallint(5) unsigned | NO | PRI | NULL | | first_name | varchar(45) | NO | | NULL | | | surname | varchar(30) | YES | | NULL | | +------------+----------------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) 22
  • 23. Adding or removing fields mysql> ALTER TABLE person ADD age smallint(3) unsigned not null; Query OK, 0 rows affected (0.42 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESCRIBE person; +------------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+----------------------+------+-----+---------+----------------+ | person_id | smallint(5) unsigned | NO | PRI | NULL | | first_name | varchar(45) | NO | | NULL | | | surname | varchar(30) | YES | | NULL | | | age | smallint(3) unsigned | NO | | NULL | | +------------+----------------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec) mysql> ALTER TABLE person DROP first_name; Query OK, 0 rows affected (0.25 sec) Records: 0 Duplicates: 0 Warnings: 0 23
  • 24. Dropping Tables mysql> SHOW TABLES; +-------------------+ | Tables_in_temp_db | +-------------------+ | temp_table | +-------------------+ 1 row in set (0.00 sec) mysql> DROP TABLE temp_table; Query OK, 0 rows affected (0.06 sec) mysql> DROP TABLE IF EXISTS temp_table; Query OK, 0 rows affected, 1 warning (0.12 sec) mysql> SHOW TABLES; Empty set (0.00 sec) 24
  • 25. Working with tables from Multiple Databases mysql> SELECT * FROM temp_db.temp_table; +---------+---------------------+ | temp_id | temp_whatever | +---------+---------------------+ | 1 | life is good | | 2 | life is even better | +---------+---------------------+ 2 rows in set (0.00 sec) mysql> SELECT * FROM mydb.student; +------------+------------+-----------+-----+-------+ | student_id | first_name | last_name | age | grade | +------------+------------+-----------+-----+-------+ | 1 | yuna | kim | 19 | 4 | | 2 | kelly | jones | 22 | 5 | +------------+------------+-----------+-----+-------+ 2 rows in set (0.00 sec) 25
  • 26. SQL Basics: Fields
  • 27. Field Definitions • Each field has > Field name > Data type > Field modifier or constraint 27
  • 28. Field Data types - Integers • TINYINT > 1 byte, -128 to 127 (signed), 0 to 255 (unsigned) • SMALLINT > 2 bytes, -32768 to 32767 (signed), 0 to 65535 (unsigned) • MEDIUMINT > 3 bytes • INT > 4 bytes • BIGINT > 8 bytes 28
  • 29. Field Data types • FLOAT > single precision floating-point value • DOUBLE > double precision floating-point value • DECIMAL > decimal values • BIT > bit value > b'0101' 29
  • 30. Field Data Types • CHAR > Fixed length strings up to 255 characters • VARCHAR > Variable length strings up to 255 characters • DATE, TIME, YEAR • DATETIME, TIMESTAMP • ENUM, SET > Predefined set of values 30
  • 31. Field Modifiers • NULL or NOT NULL > Indicates if the field can be null or not • DEFAULT > Assigns default value if no value is specified when a new record is inserted • AUTO_INCREMENT > MySQL automatically generates a number (by incrementing the previous value by 1) > Used for creating primary key • CHARACTER SET > Specifies the character set for string values 31
  • 33. INSERT'ing a single record mysql> INSERT INTO person (person_id, first_name, last_name, age) -> VALUES (1, 'sang', 'shin', 88); Query OK, 1 row affected (0.10 sec) mysql> SELECT * FROM person; +-----------+------------+-----------+-----+ | person_id | first_name | last_name | age | +-----------+------------+-----------+-----+ | 1 | sang | shin | 88 | +-----------+------------+-----------+-----+ 1 row in set (0.00 sec) 33
  • 34. INSERT'ing multiple records mysql> INSERT INTO person (person_id, first_name, last_name, age) -> VALUES -> (2, 'kelly', 'jones', 22), -> (3, 'jack', 'kennedy', 56), -> (4, 'paul', 'kennedy', 34), -> (5, 'daniel', 'song', 24), -> (6, 'nichole', 'scott', 9); Query OK, 3 rows affected (0.05 sec) Records: 3 Duplicates: 0 Warnings: 0 34
  • 35. Deleting recored(s) mysql> DELETE FROM person WHERE age < 10; Query OK, 1 row affected (0.07 sec) 35
  • 36. Updating record(s) mysql> UPDATE person SET age = 88 -> WHERE age = 99 OR first_name = 'paul'; Query OK, 1 row affected, 2 warnings (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 2 mysql> SELECT * FROM person; +-----------+------------+-----------+-----+ | person_id | first_name | last_name | age | +-----------+------------+-----------+-----+ | 1 | sang | shin | 88 | | 2 | kelly | jones | 22 | | 3 | jack | kennedy | 56 | | 4 | paul | kennedy | 88 | +-----------+------------+-----------+-----+ 4 rows in set (0.00 sec) 36
  • 38. Retrieving some fields selectively mysql> SELECT last_name, age FROM person; +-----------+-----+ | last_name | age | +-----------+-----+ | shin | 88 | | jones | 22 | | kennedy | 56 | | kennedy | 34 | | song | 24 | +-----------+-----+ 5 rows in set (0.00 sec) 38
  • 39. Retrieving with WHERE clause mysql> SELECT first_name, age FROM person -> WHERE age > 50; +------------+-----+ | first_name | age | +------------+-----+ | sang | 88 | | jack | 56 | +------------+-----+ 2 rows in set (0.00 sec) mysql> SELECT first_name, last_name, age FROM person -> WHERE age < 50 AND first_name LIKE '%niel'; +------------+-----------+-----+ | first_name | last_name | age | +------------+-----------+-----+ | daniel | song | 24 | +------------+-----------+-----+ 1 row in set (0.00 sec) 39
  • 40. Retrieving records in order mysql> SELECT last_name, age FROM person -> ORDER BY age ASC; +-----------+-----+ | last_name | age | +-----------+-----+ | jones | 22 | | song | 24 | | kennedy | 34 | | kennedy | 56 | | shin | 88 | +-----------+-----+ 5 rows in set (0.00 sec) mysql> SELECT * FROM person -> ORDER BY age DESC; +-----------+------------+-----------+-----+ | person_id | first_name | last_name | age | +-----------+------------+-----------+-----+ | 1 | sang | shin | 88 | | 3 | jack | kennedy | 56 | | 4 | paul | kennedy | 34 | | 5 | daniel | song | 24 | 40
  • 41. Retrieving limited number of records mysql> SELECT * from person -> ORDER BY age DESC -> LIMIT 3; +-----------+------------+-----------+-----+ | person_id | first_name | last_name | age | +-----------+------------+-----------+-----+ | 1 | sang | shin | 88 | | 3 | jack | kennedy | 56 | | 4 | paul | kennedy | 34 | +-----------+------------+-----------+-----+ 3 rows in set (0.00 sec) 41
  • 42. Basic SQL Commands: Arithmetic Functions
  • 43. Arithmetic operations mysql> SELECT 3 + 6; +-------+ |3+6| +-------+ | 9| +-------+ 1 row in set (0.00 sec) mysql> SELECT 45 * (1+2); +------------+ | 45 * (1+2) | +------------+ | 135 | +------------+ 1 row in set (0.00 sec) 43
  • 44. COUNT, AVG, SUM mysql> SELECT COUNT(age) FROM person; +------------+ | COUNT(age) | +------------+ | 5| +------------+ 1 row in set (0.04 sec) mysql> SELECT AVG(age) from person; +----------+ | AVG(age) | +----------+ | 44.8000 | +----------+ 1 row in set (0.00 sec) mysql> SELECT SUM(age) FROM person; +----------+ | SUM(age) | +----------+ | 224 | +----------+ 1 row in set (0.00 sec) 44
  • 45. MIN, MAX mysql> SELECT MIN(age) FROM person; +----------+ | MIN(age) | +----------+ | 22 | +----------+ 1 row in set (0.00 sec) mysql> SELECT MAX(age) FROM person; +----------+ | MAX(age) | +----------+ | 88 | +----------+ 1 row in set (0.00 sec) 45
  • 46. Reading and Executing SQL Script File
  • 47. Read and execute a SQL script file mysql> SOURCE c:/tmp/student.sql Query OK, 0 rows affected (0.10 sec) 47
  • 48. Thank you! Sang Shin http://www.javapassion.com “Learning is fun!” 48 49