DataBase Administration
DataBase Administration
MySQL
server
MySQL commands
Help: \h
Quit/exit: \q
Cancel the command: \c …etc
Info about databases and tables
Listing the databases on the MySQL server host
show databases;
Access/change database
Use database_name;
Showing the current selected database
select database();
Showing tables in the current database
show tables;
Showing the structure of a table
describe table_name;
Mysql architecture
The Mysql database system operates using the client/server architecture. The
server is the central program that manages database content, and client
programs connect to the server to retrieve or modify data.
Mysql Architecture overview
Application Layer:
Users and clients interact with the Mysql RDBMS
Three components: Administrators, clients and query users
Query users interact with Mysql RDBMS using “mysql”
Administrators use various administrative interface and utilities like
mysqladmin
Clients communicate with the Mysql RDBMS through various interfaces
and utilities like the Mysql APIs
“mysql” is actually a query interface. It is a monitor that allows users to
issue SQL statements and view the results.
Logical Layer:
The logical layer of Mysql architecture is divided into various
subsystems: Query processor, Transaction Management,
Recovery Management and Storage Management.
Phisical Layer:
Mainly deals with storage of a variety of information, which is kept in
secondary storage and accessed via the storage manager. The types of data
kept in the system are:
Data file, which stores the user data in the database
Data dictionary, which stores metadata about the structure of the database
Indices, which provides fast access to data items that hold particular values
Statistical data, which store statistical information about the data in the
database, it is used by the query processor to select efficient ways to
execute a query
Log information, used to keep track of executed queries such that the
recovery manager can use the information to successfully recover the
database in the case of a system crash.
Communication Protocols
A Mysql client program can connect to a server running on the same
machine or another machine
Database
User Management System Database
phpMyAdmin
MySQL can be controlled through a simple command-line interface;
however, we can use phpMyAdmin as an interface to MySQL.
phpMyAdmin is a very powerful tool; it provides a large number of
facilities for customising a database management system.
Data Administration/Administrator (DA)
DA (sometimes called data architect or even business analyst) is a type
of professional that resides in the IS function or in a unit interfacing with
the IS function.
Focus on informing in function of business, users (reports, output forms,
queries) rather than IT
Data definition and integration (e.g., Customer entity in CRM
systems cutting across Sales, Marketing, R+D…).
Decision support.
Ideas for system design, involvement in system development.
Data governance and security.
Database Administrator (DBA)
DBA is focused on technology.
1. DBA actively participates in DB system life cycle (plan, develop,
install, manage, upgrade…).
2. DBA manages DB system:
2.1 Users: Creating user accounts, assigning use privileges
2.2 System performance: Monitoring and tuning
REVOKING PRIVILEGES
REVOKE Syntax
The REVOKE statement enables system administrators to revoke privileges
from MySQL accounts. MySQL does not automatically revoke any privileges
when you drop a database or table.
mysql> REVOKE INSERT ON *.* FROM 'user_name'@'localhost';
To use the first REVOKE syntax, you must have the GRANT OPTION privilege,
and you must have the privileges that you are revoking. To revoke all
privileges, use the second syntax, which drops all global, database, table,
column, and routine privileges for the named user or users:
mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...
InnoDB is the mostly widely used storage engine for Web/Web 2.0,
eCommerce, Financial Systems, Telecommunications, Health Care and Retail
applications built on MySQL. InnoDB provides highly efficient ACID-compliant
transactional capabilities and includes unique architectural elements that
deliver high performance and scalability. InnoDB is structurally designed to
handle transactional applications that require crash recovery, referential
integrity, high levels of user concurrency and fast response times.
While MyISAM and the other storage engines continue to be readily
available, users can now create applications built on InnoDB without altering
[a] Compressed MyISAM tables are supported only when using the compressed row format.
[b] Compressed InnoDB tables require the InnoDB Barracuda file format.
[c] Implemented in the server (via encryption functions), rather than in the storage engine.
[d] Implemented in the server, rather than in the storage engine.
InnoDB delivers the best blend of performance, reliability and data integrity
for transactional workloads, with a host of parameters that are configurable,
enabling users to optimize performance and behavior for their specific
workload.
MYSQL TRANSACTIONS
Introducing transactions
In everyday life, people conduct different kind of business transactions
buying products, ordering travels, changing or cancelling orders, buying
tickets to concerts, paying rents, electricity bills, insurance invoices, etc.
Transactions do not relate only to computers, of course. Any type of human
activity comprising a logical unit of work meant to either be executed as a
whole or to be cancelled in its entirety comprises a transaction. Almost all
information systems utilize the services of some database management
system (DBMS) for storing and retrieving data. Today's DBMS products are
technically sophisticated securing data integrity in their databases and
providing fast access to data even to multiple concurrent users. They
provide reliable services to applications for managing persistency of data, but
only if applications use these reliable services properly. This is done by
building the data access parts of the application logic using database
transactions. Improper transaction management and control by the
application software may, for example, result in
COURSE : DATABASE ADMINISTRATION FOR SWE/IUC-DOUALA
COURSE FACILITATOR : TATSOPTEU E. ENDELLY/[email protected]
28
customer orders, payments, and product shipment orders being lost in
the case of a web store
failures in the registration of seat reservations or double-bookings
to be made for train/airplane passengers
lost emergency call registrations at emergency response centers etc.
Problematic situations like the above occur frequently in real life, but the
people in charge often prefer not to reveal the stories to the public.
Transactions are recoverable units of data access tasks in terms of
database content manipulation. They also comprise units of recovery for
the entire database in case of system crashes. They also provide basis
for concurrency management in multi-user environment.
Position of SQL transactions in application layers
Client1
UPDATE employee SET salary = (salary +5000) WHERE emp_id=101;
SELECT * from employee;
COMMIT;
SELECT * from employee;
START TRANSACTION;
select * from employee
limit 1;
update employee set salary=175000 where emp_id=100;
SAVEPOINT SP1;
update employee set salary=180000 where emp_id=100;
select * from employee where emp_id=100;
rollback to SP1;
transaction#1>
update employee set salary=250000 where emp_id=100;
transaction#2>
select * from employee limit 1;
transaction#1>
rollback;
transaction#2>
select * from employee limit 1;
transaction#1>
START TRANSACTION;
select * from employee limit 1;
transaction#2>
show variables like 'tx_isolation';
SET tx_isolation = "REPEATABLE-READ";
show variables like 'tx_isolation';
START TRANSACTION;
select * from employee limit 1;
PARTITIONING
This section discusses MySQL's implementation of user-defined partitioning.
You can determine whether your MySQL Server supports partitioning by
means of a SHOW VARIABLES statement such as this one:
mysql> SHOW VARIABLES LIKE '%partition%';
You can also check the output of the SHOW PLUGINS statement, as shown
here:
mysql> SHOW PLUGINS;
MAXVALUE represents an integer value that is always greater than the largest
possible integer value (in mathematical language, it serves as a least upper
bound).
LIST Partitioning
List partitioning is similar to range partitioning in many ways. As in partitioning
by RANGE, each partition must be explicitly defined. The chief difference
between the two types of partitioning is that, in list partitioning, each partition
is defined and selected based on the membership of a column value in one of
a set of value lists, rather than in one of a set of contiguous ranges of values.
This is done by using PARTITIONBY LIST(expr)where expr is a column value
or an expression based on a column value and returning an integer value, and
then defining each partition by means of a VALUES IN (value_list), where
value_list is a comma-separated list of integers. For the examples that follow,
HASH Partitioning
Partitioning by HASH is used primarily to ensure an even distribution of data
among a predetermined number of partitions. With range or list partitioning,
you must specify explicitly into which partition a given column value or set of
column values is to be stored; with hash partitioning, MySQL takes care of this
for you, and you need only specify a column value or expression based on a
CASE STUDY
consider the following Company data requirements.
The company is organized into branches. Each branch has a unique number, a
name, and a particular employee who manages it.
The company makes its money by selling to clients. Each client has a name
and a unique number to identify it.
The foundation of the company is its employee. Each employee has a name,
birthday, sex, salary and a unique number.
An employee can work for one branch at a time, and each branch will be
managed by one of the employees that work there. We will also want to keep
track of when the current manager started as manager.
Many branches will need to work with suppliers to buy inventory. For each
supplier, we will keep track of their name and the type of product they are
selling the branch. A single supplier may supply products to multiple branches.
INSERT INTO employee VALUES(100, ‘DAVID’, ‘WALANCE’, ‘1967-11-17’, ‘M’, 250000, NULL, NULL);
INSERT INTO branch VALUES(1, ‘Corporate’, 100, ‘2006-02-09’);
UPDATE employee
SET branch_id =1
WHERE emp_id = 100;
INSERT INTO employee VALUES(101, ‘jan’, ‘Levinson’, ‘1961-05-11’, ‘F’, 110000, 100, 1);
INSERT INTO employee VALUES(102, ‘Michael’, ‘Scott’, ‘1964-03-15’, ‘M’, 75000, 100, NULL);
INSERT INTO branch VALUES(2, ‘Scraton’, 102, ‘1992-04-06’);
UPDATE employee
SET branch_id =2
WHERE emp_id = 102;
INSERT INTO employee VALUES(103, ‘Angela’, ‘Martin’, ‘1971-06-25’, ‘F’, 63000, 101, 2);
INSERT INTO employee VALUES(104, ‘Kelly’, ‘kapoor’, ‘1980-02-05’, ‘F’, 55000, 101, 2);
INSERT INTO employee VALUES(105, ‘Stanley’, ‘Hudson’, ‘1958-02-19’, ‘M’, 69000, 101, 2);
INSERT INTO employee VALUES(106, ‘Josh’, ‘Porter’, ‘1969-09-05’, ‘M’, 78000, 100, NULL);
INSERT INTO branch VALUES(3, ‘Stamford’, 106, ‘1998-02-13’);
UPDATE employee
SET branch_id =3
WHERE emp_id = 106;
INSERT INTO employee VALUES(107, ‘Andy’, ‘Bernard’, ‘1973-07-22’, ‘F’, 65000, 102, 3);
INSERT INTO employee VALUES(108, ‘Jim’, ‘Halpert’, ‘1978-01-01’, ‘F’, 71000, 102, 3);
SELECT COUNT(super_id)
FROM employee;
-- Find the number of femal employees born after 1st of Jan 1970
SELECT COUNT(emp_id)
FROM employee
WHERE sex= ‘F’ AND birth_date > ‘1970-01-01’;
-- Find the names of all employees who have sold over 30, 000
-- Find all clients who are handled by the branch that Michael Scott manages, assuming you know his ID
SELECT client.client_name
FROM client
WHERE client.branch_id = (
SELECT branch.branch_id
FROM branch
WHERE branch.mgr_id = 102
LIMIT 1
);