In today's high technology environment, organizations are becoming more and more dependent on their information systems. The public is increasingly concerned about the proper use of data and information. Most Organizations like banks, airlines, markets, manufactures and universities widely used computer systems to manage, manipulate and process their information. Many of today's most widely used computer systems are database applications, for example, Amazon, which was built on top of MySQL. Database application is involved like everywhere in our world, it touches all aspects of our lives.
A database application is a computer program whose primary purpose is entering and retrieving information from a computerized database. Early examples of database applications were accounting systems and airline reservations systems.
The aim of this course is to explore fundamentals of database application related to MySQL, phpMyAdmin, MySQL command lines, apache server and PHP Maker. It details the relational database principles. It shows how to build and develop database application with web interface.
Upon completion of this course, computer students will have gained knowledge of database application concepts and the ability to: Must know the basic concepts related relational database application. Must know how to manage relational database via using MySQL command line and phpMyAdmin. Must know how to build database application with web interface by using MySQL and PHPMaker. A combination of lectures and practical sessions will be used in this course in order to achieve the aim of the course.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
DatabaseDatabase applicationapplicationChapter 1: Introduction to MySQL DatabaseChapter 1: Introduction to MySQL Database
Faculty of Physical and Faculty of Physical and BasicBasic EducationEducation
Computer ScienceComputer Science By: Msc. Karwan M. By: Msc. Karwan M.
Introducing Relational Database Introducing Relational Database A relational database manages data in tables. Databases are managed by a relational database management system
(RDBMS).
An RDBMS supports a database language to create and delete databases and to manage and search data.
The database language used in almost all DBMSs is SQL.
After creating a database, the most common SQL statements used are INSERT to add data UPDATE to change data
DELETE to remove data
SELECT to search data
A database table may have multiple columns, or attributes, each of which has a name.
Introducing Relational Database Introducing Relational Database Tables usually have a primary key, which is one or more values that
uniquely identify each row in a table (Figure 3.1.) Typical DBMS Functionality are
Define a particular database in terms of its data types, structures, and constraints
Construct or Load the initial database contents on a secondary storage medium
Manipulate the database:o Retrieval: Querying, generating reportso Modification: Insertions, deletions and updates to its contento Accessing the database through Web applications
Share a database allows multiple users and programs to access the database simultaneously
Known facts that can be recorded and have an implicit meaning.
Table
The part of a database that stores the data. A table has columns or attributes, and the data stored in rows.
Attributes
The columns in a table. All rows in table entities have the same attributes. For example, a customer table might have the attributes name, address, and city. Each attribute has a data type such as string, integer, or date.
The data entries in a table. Rows contain values for each attribute. For example, a row in a customer table might contain the values "Matthew Richardson," "Punt Road," and "Richmond." Rows are also known as records.
Relational model
A model that uses tables to store data and manage the relationship between tables.
Relational database management system A software system that manages data in a database and is based on
the relational model.
Entity-relationship modeling
A technique used to describe the real-world data in terms of entities, attributes, and relationships.
Restrictions or limitations on tables and attributes. For example, a wine can be produced only by one winery, an order for wine can't exist if it isn't associated with a customer, having a name attribute could be mandatory for a customer.
Primary key
One or more attributes that contain values that uniquely identify each row. For example, a customer table might have the primary key of cust ID. The cust ID attribute is then assigned a unique value for each customer. A primary key is a constraint of most tables.
Index
A data structure used for fast access to rows in a table. An index is usually built for the primary key of each table and can then be used to quickly find a particular row. Indexes are also defined and built for other attributes when those attributes are frequently used in queries.
A correctly designed database that is created from an ER model. There are different types or levels of normalization, and a third-normal form database is generally regarded as being an acceptably designed relational database.
Foreign key
A foreign key is the linking pin between two tables. SQL
A query language that interacts with a DBMS. SQL is a set of statements to manage databases, tables, and data.
Database system: Database Management System + Database
13
MySQL Database MySQL Database
MySQL DatabaseMySQL is an open source relational database management system (RDBMS)
based on Structured Query Language (SQL).
MySQL is originally developed, distributed, and supported by the Swedish company ( MySQL AB) , was acquired by Oracle in 2008. Developers can still use MySQL under the GNU General Public License (GPL).
Applications which use MySQL databases include: TYPO3, MODx, Joomla, WordPress, phpBB, MyBB, Drupal.
MySQL is also used in many high-profile, large-scale websites, including Google, Facebook, Twitter, Flickr, and YouTube.
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 on many operating systems ( Windows, UNIX and Linux)
and with many languages including PHP, PERL, C, C++, JAVA etc.
MySQL works very quickly and works well even with large data sets.
MySQL is customizable. The open source GPL license allows programmers to modify the MySQL software to fit their own specific environments.
MySQL is very friendly to PHP and JSP which are most appreciated languages for web development.
MySQL supports large databases, up to 50 million rows or more in a table. The default file size limit for a table is 4GB, but you can increase this to a theoretical limit of 8 million terabytes (TB).
Free graphical administration applications (or "front ends") are available that integrate with MySQL and enable users to work with database structure and data visually.LAMP
It’s a Web development platform that uses Linux as the operating system, Apache as the Web server, MySQL as the relational database management system and PHP as the object-oriented scripting language. (Sometimes Perl or Python is used instead of PHP.)
MySQL Command Line Client MySQL Command Line Client is a single exe that allows connecting
and running a sample query. it is a simple SQL shell for creating tables, inserting data, ..etc (with GNU readline capabilities).
It's a PHP-driven tool and handles every aspect of creating and managing a MySQL database application. phpMyAdmin also allows for the execution of MySQL commands from within the interface.
It is a unified visual tool for database architects, developers. MySQL Workbench provides data modeling, SQL development, and administration tools for server configuration, user administration, backup etc.. MySQL Workbench is available on Windows, Linux and Mac OS X.
HeidiSQL – a full featured free front end that runs on Windows, and can connect to local or remote MySQL servers to manage databases, tables, column structure, and individual data records. Also supports specialized GUI features for date/time fields and enumerated multiple-value fields
PHPMaker is a powerful automation tool that can generate a full set of PHP quickly from MySQL, PostgreSQL, Microsoft Access, Microsoft SQL Server and Oracle databases.
Using PHPMaker, you can instantly create web sites that allow users to view, edit, search, add and delete records on the web.
PHPMaker is designed for high flexibility, numerous options enable you to generate PHP database applications that best suits your needs.
The generated codes are clean, straightforward and easy-to-customize.
PHP Report Maker is a powerful reporting tool that can generate dynamic PHP Web reports from MySQL, PostgreSQL, Microsoft Access, Microsoft SQL Server and Oracle database.
You can instantly create live detail and summary reports or crosstabs reports for your Database Application.
PHP Report Maker is designed for high flexibility, numerous options enable you to generate the reports that best suits your needs.
PHP Report Maker can save you tons of time and is suitable for both beginners and experienced developers alike.
BIGINT - A large integer, the allowable range is from -9223372036854775808 to 9223372036854775807. You can specify a width of up to 11 digits.
FLOAT(M,D) - A floating-point number. You can define the display length (M) and the number of decimals (D). Decimal precision can go to 24 places for a FLOAT.
DOUBLE(M,D) - A double precision floating-point number. You can define the display length (M) and the number of decimals (D). Decimal precision can go to 53 places for a DOUBLE.
DECIMAL(M,D) - An unpacked floating-point number. In unpacked decimals, each decimal corresponds to one byte. Defining the display length (M) and the number of decimals (D) is required.
Note: MySQL uses all the standard ANSI SQL numeric data types ..
26
MySQL Database MySQL Database
Date and Time TypesThe MySQL date and time data types are:
DATE - A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For example, December 30th, 1973 would be stored as 1973-12-30.
DATETIME - A date and time combination in YYYY-MM-DD HH:MM:SS format, between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the afternoon on December 30th, 1973 would be stored as 1973-12-30 15:30:00.
TIMESTAMP- This looks like the previous DATETIME format, only without the hyphens between numbers; 3:30 in the afternoon on December 30th, 1973 would be stored as 19731230153000 ( YYYYMMDDHHMMSS ).
YEAR(M) - Stores a year in 2-digit or 4-digit format. If the length is specified as 2 (for example YEAR(2)), YEAR can be 1970 to 2069 (70 to 69). If the length is specified as 4, YEAR can be 1901 to 2155. The default length is 4.
CHAR(M) - A fixed-length string between 1 and 255 characters in length (for example CHAR(5)). Defining a length is not required, but the default is 1.
VARCHAR(M) - A variable-length string between 1 and 255 characters in length; for example VARCHAR(25). You must define a length when creating a VARCHAR field.
BLOB or TEXT - A field with a maximum length of 65535 characters. BLOBs are "Binary Large Objects" and are used to store large amounts of binary data, such as images or other types of files. Defining a length is not required.
TINYBLOB or TINYTEXT - A BLOB or TEXT column with a maximum length of 255 characters.
MEDIUMBLOB or MEDIUMTEXT - A BLOB or TEXT column with a maximum length of 16777215 characters.
LONGBLOB or LONGTEXT - A BLOB or TEXT column with a maximum length of 4294967295 characters.
ENUM - An enumeration, which is a fancy term for list. When defining an ENUM, you are creating a list of items from which the value must be selected (or it can be NULL). For example, if you wanted your field to contain "A" or "B" or "C", you would define your ENUM as ENUM ('A', 'B', 'C').
MySQL Command Line Client MySQL Command Line Client
Sample Session
Enter password: *****Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 241 to server version: 5.0.51b-community-nt-log MySQL
Community Edition < GPL>
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
To exit the MySQL Command , just type QUIT or EXIT:
mysql> QUITmysql> exit
MySQL Command Line Client …
Note: MySQL Command Line Client is a single exe that allows connecting and running a sample query. it is a simple SQL shell (with GNU readline capabilities).
Managing Databases Managing Databases mysql> CREATE TABLE pet (
-> name VARCHAR(20), -> owner VARCHAR(20), -> species VARCHAR(20), -> sex CHAR(1), -> birth DATE, death DATE); Query OK, 0 rows affected (0.04 sec)
To verify that the table has been created: mysql> show tables; +------------------------- + | Tables_in_webdb | +------------------------- + | pet | +------------------------- + 1 row in set (0.01 sec)
Manipulating Data Manipulating Data »Second approach:
INSERT INTO pet SET Name = 'Fluffy', Owner = 'Harold', Species = 'cat', Sex = 'f', Birth = '1999-02-04', Date = NULL ;
Loading Data to pet table …
The first approach can actually be varied to function in a similar way to the second by including parenthesized attribute names before the VALUES keyword.
INSERT INTO pet (Name, Owner ) VALUES ('Fluffy', 'Harold');
Manipulating Data Manipulating Data Pattern Matching
SQL Pattern matching
» To perform pattern matching, use the LIKE or NOT LIKE comparison operators.
» By default, patterns are case insensitive.
Special Characters
» _ Used to match any single character.
» % Used to match an arbitrary number of characters.
Note: MySQL providesstandard SQL pattern matching.regular expression pattern matching, similar to those used by Unix utilities such as vi, grep and sed.
» The GROUP BY clause is different from ORDER BY because it doesn't sort the data for output. Instead, it sorts the data early in the query process, for the purpose of grouping or aggregation.
Manipulating Data Manipulating Data Join Queries / Cartesian Product
» A join query is a querying technique that matches rows from two or more tables based on a join condition in a WHERE clause and outputs only those rows that meet the condition.
SELECT winery_name, region_name FROM winery, region
ORDER BY winery_name, region_name;
» The query produces all possible combinations of the four region names and 300 wineries in the sample database! In fact, the size of the output can be accurately calculated as the total number of rows in the first table multiplied by the total rows in the second table. In this case, the output is 4 x 300 = 1,200 rows.