Database management system Document Notes

advertisement
DATABASES MANAGEMENT SYSTEMS (DBMS) DOCUMENT NOTES
Author: Mary Waswa-Wilson
Date: 5th June 2013
Database Management Systems (DBMS)
Objectives
1) To define Database Management Systems (DBMS)
2) To understand and define various terms and basic functionalities of
phpmyadmin interface
3) Learn to install, configure a MySQL database
4) To create users on MySQL
5) To be able to list databases and creating tables
6) To populate and query databases
By the end of the module, participants should be able to:1. Design a simple database with 3rd level normalization
2. Install a MySQL server using Xampp
3. Login to MySQL Server and implement the database designed above
4. Populate and query the database using different means: Phpmyadmin,
command line
DATABASE MANAGEMENT SYSTEMS (DBMS)
What is a Database?
A database is an integrated collection of logically related records or files
consolidated into a common pool that provides data for one or more multiple
uses. You can think of a database as an electronic filing system.
A Database Management System (DBMS)
Software that organizes the storage of data. A DBMS controls the creation,
maintenance, and use of the database storage structures of organizations and of
their end users. In large organizations this task is being manned by a Database
Administrator
Database management systems are usually categorized according to the
database model that they support, such as the network, relational or object
model. The model tends to determine the query languages that are available to
access the database. One commonly used query language for the relational
database is SQL,
Database management systems range from all systems that run on personal
computers to huge systems that run on mainframes. After storage a DBMS
needs an application that can enable us access, modify the information in a
contextual manner. Examples of database applications may include:
o Computerized library systems - Koha
o Automated teller machines (ATM)
o Flight reservation systems
o Computerized parts inventory systems
Database Management System (DBMS)
There are several Database Management Systems (DBMS), such as:
 Microsoft SQL Server
 Oracle
 Sybase
 DBase
 Microsoft Access
 MySQL from Sun Microsystems (Oracle)
 DB2 from IBM etc.
The goal of a DBMS is to provide an environment that is both convenient and
efficient to use in


Retrieving information from the database.
Storing information into the database.
Databases are usually designed to manage large bodies of information. This
involves




Definition of structures for information storage (data modeling).
Provision of mechanisms for the manipulation of information (file and
systems structure, query processing).
Providing for the safety of information in the database (crash recovery and
security).
Concurrency control if the system is shared by users.
In our study, this document will focus on MySQL server as our case study
DBMS Terminologies:
Case Study of MySQL: Before we proceed to explain MySQL database system,
let's revise few definitions related to database.

Database: A database is a collection of tables, with related data.

Table: A table is a matrix with data. A table in a database looks like a
simple spreadsheet.

Column: One column (data element) contains data of one and the same
kind, for example the column postcode.

Row: A row (= tuple, entry or record) is a group of related data, for
example the data of one subscription.

Redundancy: Storing data twice, redundantly to make the system faster.

Primary Key: A primary key is unique. A key value cannot occur twice in
one table. With a key, you can find at most one row.

Foreign Key: A foreign key is the linking pin between two tables.

Compound Key: A compound key (composite key) is a key that consists
of multiple columns, because one column is not sufficiently unique.

Index: An index in a database resembles an index at the back of a book.

Referential Integrity: Referential Integrity makes sure that a foreign key
value always points to an existing row.
MySQL
MySQL is a fast, easy-to-use RDBMS being used for many small and big
businesses. MySQL is becoming so popular because of many good reasons:

MySQL is released under an open-source license. So you have nothing to
pay to use it.

MySQL is a very powerful program in its own right. It handles a large
subset of the functionality of the most expensive and powerful database
packages.

MySQL uses a standard form of the well-known SQL data language.

MySQL works very quickly and works well even with large data sets.

MySQL is very friendly to PHP, the most appreciated language for web
development.
Exercise One: on Practice
What makes up a database?
Tables
The basic units in a database are tables and the relationship between them.
Strictly, a relational database is a collection of relations (frequently called tables).
Unique key
In relational database design, a unique key or primary key is a candidate key to
uniquely identify each row in a table. A unique key or primary key comprises a
single column or set of columns. No two distinct rows in a table can have the
same value (or combination of values) in those columns.
A primary key
Is a special case of unique keys. The major difference is that for unique keys the
implicit NOT NULL constraint is not automatically enforced, while for primary
keys it is enforced. Thus, the values in unique key columns may or may not be
NULL.
Foreign key
In the context of relational databases, a foreign key is a referential constraint
between two tables. The foreign key identifies a column or a set of columns in
one table that refers to a column or set of columns in another table. The columns
in the referencing table must be the primary key or other candidate key in the
referenced table. The values in one row of the referencing columns must occur in
a single row in the referenced table. Thus, a row in the referencing table cannot
contain values that don't exist in the referenced table.
Views
In database theory, a view consists of a stored query accessible as a virtual table
composed of the result set of a query. Unlike ordinary tables in a relational
database, a view does not form part of the physical schema: it is a dynamic,
virtual table computed or collated from data in the database.
Changing the data in a table alters the data shown in subsequent invocations of
the view
Functions
In SQL databases, a user-defined function provides a mechanism for extending
the functionality of the database server by adding a function that canbe evaluated
in SQL statements.
Triggers
A database trigger is procedural code that is automatically executed in response
to certain events on a particular table or view in a database. The trigger is mostly
used for keeping the integrity of the information on the database. For example,
when a new record (representing a new worker) added to the employees table,
new records should be created also in the tables of the taxes, vacations, and
salaries.
THE PHPMYADMIN COMMAND
Working with databases can be a complex and confusing process. Fortunately
PhpMyAdmin provides an easy to use and easy to understand interface to
administer your database(s). It was intended to handle the administration of
MySQL via a web browser. PhpMyAdmin supports a wide range of operations
with MySQL. The most frequently used operations are supported by the user
interface (managing databases, tables, fields, relations, indexes, users,
permissions, etc), while you still have the ability to directly execute SQL
statements.
Figure 1: phMyadmin interface
Understanding Tabs in the Interface
Structure: Lists the structor of the databases
SQL: Run SQL query/queries on database mysql:
Search: Search in database
Query: Used to write queries and vuews from table
Export: View dump (schema) of database, Export
Import: introduce/ import a file
Operations: Basic Operations within the database
Privileges: Accessing database and user scope to manipulate the database
Drop: Used to delete a database or table
Database tables
Listing and creating database tables

At its simplest, a database will consist of tables like this:
Name
Age
Ken Waweru
13
I haven't decided yet.
Mike Kibet
37
Red... no, wait ...
Arnold Kemboi 82

Favorite Color
None of your business.
Tables contain records (sometimes called rows), and records
contain fields (sometimes called columns) :
Field Names:
Field 1
Field 2
Field 3
Name
Age
Favorite Color
Record 1
Ken Waweru
13
I haven't decided yet.
Record 2
Mike Kibet
37
Red .. no, wait ...
Record 3
Arnold Kemboi 82
None of your business.
The above is the basic premise of a simple database table, even a real one
having millions of records.
POPULATING AND QUERYING DATABASES
After creating your database, inserting tables, you need to populate it. The LOAD
DATA and INSERT statements are useful for this.
Suppose that your table was to hold your pet information. Then the records can
be described as shown here.
name
owner species sex
birth
death
Fluffy
Harold cat
f
1993-02-04
Claws
Gwen cat
m
1994-03-17
Buffy
Harold dog
f
1989-05-13
Fang
Benny dog
m
1990-08-27
Bowser Diane dog
m
1979-08-31 1995-07-29
Chirpy
f
1998-09-11
Gwen bird
Whistler Gwen bird
Slim
Benny snake
1997-12-09
m
1996-04-29
Because you are beginning with an empty table, an easy way to populate it is to
create a text file containing a row for each of your animals.
INSERT Statement
The INSERT INTO SELECT statement selects data from one table and inserts it
into an existing table. Any existing rows in the target table are unaffected.
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;
SELECT Statement
The SELECT statement is used to pull information from a table. The general form
of the statement is:
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy; what_to_select
Indicates what you want to see. This can be a list of columns, or * to indicate “all
columns.” which_table indicates the table from which you want to retrieve data.
The WHERE clause is optional. If it is present, conditions_to_satisfy specifies
one or more conditions that rows must satisfy to qualify for retrieval.
Back- Ups
Introduction
It is very important to do backup of your MySql database, you will probably
realize it when it is too late.
A lot of web applications use MySql for storing the content. This can be blogs,
and a lot of other things. When you have all your content as html files on your
web server it is very easy to keep them safe from crashes, you just have a copy
of them on your own PC and then upload them again after the web server is
restored after the crash. All the content in the MySql database must also be
backed up. A lot of web service providers say they do backup of all the files, but
you should never blindly trust them. If you have spent a lot of time making the
content and it is only stored in the Mysql server, you will feel very bad if it gets
lost for ever. Backing it up once every month or so makes sure you never loose
too much of your work in case of a server crash, and it will make you sleep better
at night. It is easy and fast, so there is no reason for not doing it.
Backup of Mysql database
Open phpMyAdmin.
1. Click Export in the Menu to get to where you can backup you MySql database.
2. Make sure that you have selected to export your entire database, and not just
one table. There should be as many tables in the export list as showing under the
database name.
3. Select"SQL"-> for output format, Check "Structure" and "Add
AUTO_INCREMENT" value. Check "Enclose table and field name with
backquotes". Check "DATA", check use "hexadecimal for binary field". Export
type set to "INSERT".
4. Check "Save as file", do not change the file name, use compression if you
want. Then click "GO" to download the backup file.
Restoring a backup of a MySql database
1. To restore a database, you click the SQL tab.
2. On the "SQL"-page , unclick the show query here again.
3. Browse to your backup of the database.
4. Click Go.
Download