Mysql Basics1
Mysql Basics1
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
What is MySQL?
What is MySQL?
Most popular open source database
> High performance > High reliability > Ease of use
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 Workbench
> GUI tool
5
MySQL Server
mysqld is a runnable program which represents MySQL database server
Installation Options
Windows
> MySQL database server can be installed either
What is SQL?
SQL is language for retrieving and manipulating data in a relational database
> Data definition > Data manipulation > Data control
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
tables and other objects in the database > Examples: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE VIEW, ...
Creating a database
mysql> CREATE DATABASE mydb; Query OK, 1 row affected (0.01 sec)
16
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
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
22
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
25
Field Definitions
Each field has
> Field name > Data type > Field modifier or constraint
27
SMALLINT
> 2 bytes, -32768 to 32767 (signed), 0 to 65535
(unsigned)
MEDIUMINT
> 3 bytes
INT
> 4 bytes
BIGINT
> 8 bytes
28
DOUBLE
> double precision floating-point value
DECIMAL
> decimal values
BIT
> bit value > b'0101'
29
VARCHAR
> Variable length strings up to 255 characters
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
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
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
38
39
40
41
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
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
47
Thank you!