Mastering MySQL DBMS
Mastering MySQL DBMS
▪1
What is Database?
Organization of data
Efficient retrieval of data
Reliable storage of data
Maintaining consistent data
Making useful information for decision making
What will be learned?
This topic aims to equip students with practical skill in
understanding,
designing,
developing,
administering and managing a database system in an
organization.
Data:
Known facts that can be recorded and have
an implicit meaning.
Mini-world:
Some part of the real world about which
data is stored in a database.
… … …
MySQL Introduction
MySQL is an open source database management
system
SQL stands for the Structured Query Language. It
defines how to create, insert, retrieve, modify
and delete data
Can be downloaded for FREE from
www.mysql.com
Reference sites
NASA, Yahoo!, Compaq, Motorola
Basic MySQL Operations
Create Database
Create table
Insert records
Load data
Retrieve records
Update records
Delete records
Modify table
Join table
Drop table
Optimize table
Count, Like, Order by, Group by
More advanced ones (sub-queries, stored procedures, triggers, views …)
Example
% mysql -u usrname -p
Enter password: passowrd
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23 to server version: 3.23.41.
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Create User
Syntax
mysql>use mysql;
Use database mysql, used by the system
Example
mysql>insert into user (Host, User, Password)
values (‘localhost’, ‘aauuser’, ‘MyPass@123’);
Create a new database user aauuser
An alternative
GRANT USAGE ON *.* TO ‘aauuser’@’localhost‘ IDENTIFIED BY
‘MyPass@123’;
Create User
mysql>insert into db (Host, Db, User, Select_priv,
Insert_priv, Update_priv, Delete_priv, Create_priv,
Drop_priv) values (‘localhost’, ‘is_workshopbd’, ‘aauuser‘,
‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’);
Create a new database is_workshopbd for user aauuser
mysql>flush privileges
Reloads the privileges from the grant tables in the database
mysql
An alternative
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE,
DROP ON is_workshopbd.* TO ‘aauuser’@’localhost’
IDENTIFIED BY ‘MyPass@123’;
Create Database
What are the current databases at the server?
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| mysql | mysql is a database (stores users’ password ) used by system.
| is_workshopbd |
+----------------------+
Create Table
CREATE TABLE Table_Name (column_specifications)
Example
Insert Record
INSERT INTO table_name SET col_name1=value1,
col_name2=value2, col_name3=value3, …
Example
mysql> INSERT INTO student SET student_ID=S-101, name=‘Belay',
major=‘Biology’, cgpa=‘2.01’;
Query OK, 1 row affected (0.00 sec)
Student_ID Name Major CGPA
Retrieve Record
Syntax
SELECT what_columns
FROM table or tables
WHERE condition
Example
Update Record
UPDATE table_name
SET which columns to change
WHERE condition
Example
mysql> UPDATE student SET cgpa=3.01WHERE name=‘Belay';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql>
SELECT * FROM studentWHERE name=‘Belay’;
+------------+---------------+--------+--------+
| name | student_ID | major | cgpa |
+------------+---------------+--------+--------+
| Belay | S-101 | Biology | 3.01 |
+------------+---------------+--------+--------+
1 row in set (0.00 sec)
Delete Record
DELETE FROM table_nameWHERE condition
Example
mysql> DELETE FROM studentWHERE name=‘Belay';
Query OK, 1 row affected (0.00 sec)
Logout MySQL
mysq> quit;
Data Loading
Load batch data instead of inserting records one by one
Example
mysql> LOAD DATA LOCAL INFILE “proj.txt" INTO TABLE project;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
Writing to a Table
MariaDB [today]> select * from employee into outfile "emp_out.txt";
Query OK, 4 rows affected (0.00 sec)
Joining Tables
Types of Joins
Inner joins
return only matching rows
enable a maximum of 256 tables to be joined at the same
time.
Types of Joins
Outer joins
return all matching rows, plus nonmatching rows
from one or both tables
can be performed on only two tables or views at a
time.
Cartesian Product
How many rows are returned from this query?
select * from three,
four;
and FK
Joining tables requires tables be related with one another using
primary key foreign key relationship.
One has to create the related tables before joining/combining tables.
MariaDB [today]> create table employee
-> (EID char(5) primary key,
-> Name char(25) not null,
-> Age int unsigned,
-> Salary float
-> );
Query OK, 0 rows affected (0.29 sec)
MariaDB [today]> insert into employee values ('E-02', 'Belay', 41, 24000);
Query OK, 1 row affected (0.01 sec)
MariaDB [today]> insert into employee values ('E-03', 'Taye', 52, 4000);
Query OK, 1 row affected (0.02 sec)
MariaDB [today]> insert into employee values ('E-04', 'Hirut', 32, 14000);
Query OK, 1 row affected (0.09 sec)
MariaDB [today]> insert into dept values ('D02', 'Human Resource', '555555', 'E-03');
Query OK, 1 row affected (0.03 sec)
Combining Tables
Combining tables using Cartesian will result in a table where each
record of the first table is combined with each table of the second
table in a combinatory manner. This will note extract related tuple.
MariaDB [today]> select * from employee, dept;
+--------+----------+-------+----------+-------+------------------------+-----------+-------------+
| EID | Name | Age | Salary | DID | DName | Tel | MangID |
+-------+-----------+-------+----------+-------+------------------------+-----------+-------------+
| E-01 | Abebe | 29 | 14650 | D01 | Finance | 343434 | E-02 |
| E-01 | Abebe | 29 | 14650 | D02 | Human Resource| 555555 | E-03 |
| E-02 | Belay | 41 | 24000 | D01 | Finance | 343434 | E-02 |
| E-02 | Belay | 41 | 24000 | D02 | Human Resource | 555555 | E-03 |
| E-03 | Taye | 52 | 4000 | D01 | Finance | 343434 | E-02 |
| E-03 | Taye | 52 | 4000 | D02 | Human Resource | 555555 | E-03 |
| E-04 | Hirut | 32 | 14000 | D01 | Finance | 343434 | E-02 |
| E-04 | Hirut | 32 | 14000 | D02 | Human Resource | 555555 | E-03 |
+-------+----------+------+------------+--- ---+------------------------+------------+-------------+
8 rows in set (0.00 sec)
Backup Database
mysqldump
Writes the contents of database tables into text files
Example
>mysqldump –p bcb –T ./
MySQL Indexing
Index
Index columns that you search for
Example
MariaDB [today]> alter table employee add index(name);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [today]> describe employee;
+--------+--------------------+------+-------+----------+--------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------+------+-------+-----------+-------+
| EID | char(5) | NO | PRI | NULL | |
| Name | char(25) | NO | MUL | NULL | |
| Age | int(10) unsigned | YES | | NULL | |
| Salary | float | YES | | NULL | |
+--------+--------------------+------+--------+----------+-------+
4 rows in set (0.00 sec)
MariaDB [today]>delimiter ;
Example: Triggers MariaDB [today]> select * from employee;
+------+-------+------+--------+
| EID | Name | Age | Salary |
+------+-------+------+--------+
| E-01 | Abebe | 29 | 14650 |
| E-02 | Belay | 41 | 24000 |
| E-03 | Taye | 52 | 4000 |
| E-04 | Hirut | 32 | 14000 |
+------+-------+------+--------+
MariaDB [today]> insert into employee values ('E-05','Hareg',29,NULL);
Query OK, 1 row affected (0.03 sec)
MariaDB [today]>
End of Topic