Lab 2 Create, Insert, Select
Lab 2 Create, Insert, Select
Lab-01
Introduction to SQL Tools and Data Definition Language (DDL)
Contents
1. Introduction 4
4. Concept Map 4
4.1 Relational Data Model: 4
4.2 Database Management System (DBMS): 5
4.3 Relational Database Management System (RDBMS): 5
4.4 SQL (Structured Query Language): 5
4.4.1 Data Definition Language 5
4.4.2 Data Manipulation Language 5
4.4.3 Data Control Language 5
4.5 MySQL: 5
4.6 PHP: 6
4.7 PHPMyAdmin: 6
4.8 XAMPP: 6
7. Practice Tasks 23
7.1 Practice Task 1 [Expected time = 65mins] 23
7.1.1 Customers 23
7.1.2 Payments 23
7.1.3 Practice queries 23
9. Evaluation criteria 2
11. REFERENCES: 2
11.1 SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer. 2
1. HOW TO READ SQL SYNTAX 9
1.1. < > Angle brackets: 9
1.2. [ ] Square brackets: 9
1.3. { } Braces: 9
1.4. () Small Brackets: 9
1.5. | The vertical: 10
1.6. ... Elipsis: 10
1.7. Blank spaces: 10
Lab 01: Introduction to SQL and Tools
Introduction
You are learning databases, database models, and database management systems, particularly
relational data model and relational database management system (RDBMS). In this lab, you will
get familiar with the tools required for database management. You will use XAMPP application
stack that includes MySQL Database Server, Apache Web Server, PHP interpreter, MySQL CLI
(Statement Line Interface) and PHPMyAdmin as a management system for MySQL Server.
1. Read pages:
2. Read URL:
i. http://www.tomjewett.com/dbdesign/dbdesign.php?
page=ddldml.php
3. Revise the DDL
Concept Map
In this section, a brief overview of the concepts is presented, those will be used in this lab
afterwards.
SQL is based on the relational model. The basic data structure in RDBMS is a table. SQL
provides you the features to define tables, define constraints on tables, query for data in the table,
and change the data in the table by adding, modifying, and removing data. SQL also supports
grouping of data in multiple rows, combining tables and other features.
SQL is high-level declarative language, quite easy to learn, allowing complex tasks on a database
to be specified simply. SQL has a defined syntax that specifies how standard keywords can be
combined to form valid SQL statements. SQL statements can be divided into three categories,
according to their function:
4.5. MySQL:
MySQL is a free, open-source, and the most popular relational database management system
(RDBMS).
4.6. PHP:
PHP is a server-side scripting language and a powerful tool for making dynamic and interactive
Web applications. PHP is a widely-used, free, and open source.
4.7. PHPMyAdmin:
PHPMyAdmin is a free and open source tool written in PHP intended to handle the
administration of MySQL with the use of a web browser.
4.8. XAMPP:
XAMPP is a free and open source cross-platform web server solution stack package developed
by Apache Friends. It includes MySQL, PHP, and PHPMyAdmin.
4.9. Task 1
Study Relational Data Model.
Start the XAMPP installation. Note that Windows 7,8, and 10 will warn that installing to the
Program Files directory will keep XAMPP from being able to write to its directory due to
UAC see Figure 1. Disabling UAC is not recommended. XAMPP can be installed to C:\
XAMPP to avoid this issue or XAMPP can be given permission to run as Administrator.
The XAMPP installation starts with a splash screen, see Figure 2. Click “Next”.
Figure 2: XAMPP Setup Wizard
Click Next to complete the installation. It may take a while. After the completion, it prompts
with a dialogue box, see Figure 5. Click “Yes” Button.
The XAMPP installation completes. The XAMPP control panel can be started. Start Apache
and MySql by clicking the start button next to them, see Figure 6.
Figure 6: XAMP Control Panel
If Apache gives the error “Port 80 is in use by “Some Other Process” with PID X!”, we would
have to change the port see Appendix II.
Go to Control Panel > System and Security > System and Click Advanced system settings, See
Figure 7.
Variable name PATH is available double click it, otherwise click NEW, See Figure 9.
Paste the copied path in Variable Value field for the Variable Name PATH. See Figure 11.
Open Statement prompt and Write statement MySQL then press Enter key, See Figure 12.
We will use MySQL RDBMS that provides Console and GUI based tools to run or test our SQL
Queries. We will use MySQL CLI and an open source web based GUI tool called
“PHPMyAdmin” to execute our SQL queries. We will start with MySQL CLI, a statement line
interface, see Figure 12. Login to MySQL CLI with “root” user and empty string as password.
Type following statement in statement line and hit Enter, it will ask for password, as password is
empty string, simply Hit Enter again. See Figure 13.
MySQL -u root -p
At MySQL CLI, as shown in Figure 13, you can enter database statements followed by Enter.
Note that:
1. Most MySQL statements end with a semicolon (;)
2. MySQL returns the total number of rows found, and the total time to execute the query.
3. Keywords may be entered in any letter case i.e. uppercase or lowercase.
Now we will start using the MySQL CLI to create and manage databases. We would learn SQL
statements in the following sections. For each statement, first its generic definition is provided
and then explained with an example, to learn the generic definition of MySQL statement read
Appendix III.
For Example,
By running the above example, a table would be created with the name “movies” which has 3
columns: m_id, m_title, and m_year. In MySQL, we must specify a data type for each field. The
following section tells about the data types in detail.
DataTypes
We can specify with each column, the data type for that column, See Appendix I for a complete
list of MySQL data types, the most commonly used data types in MySQL are given in the Table
3.
Constraints
In addition to the data type of the columns, we can also specify field modifiers/constraints and
keys when creating a table:
Is the column’s value allowed to be empty? We can specify this using the constraints
NULL and NOT NULL.
For Example,
CREATE TABLE movies (
m_id int(10) NOT NULL,
m_name varchar(255) NOT NULL
);
Using the DEFAULT modifier we can specify a default value for the column.
For Example,
CREATE TABLE movies (
m_id int(10) NOT NULL,
m_name varchar(255) NOT NULL DEFAULT ‘No Name’,
);
If we want the values for a column to be unique, we can use the UNIQUE modifier.
For Example,
CREATE TABLE movies (
m_id int(10),
m_name varchar(255) UNIQUE,
);
DESCRIBE <tablename>;
For Example, to view the details of “movies” table, the following statement would be written,
see Figure 18.
DESCRIBE movies;
You need to list the values in the same order in which the columns are defined in the CREATE
TABLE, separated by commas. For columns of string data type (CHAR, VARCHAR), enclosed
the value with a pair of single quotes (or double quotes). For columns of numeric data type (INT,
DECIMAL, FLOAT, DOUBLE), simply place the number.
For Examples,
To insert a record of an office which have values available for all the columns i.e. officeCode is
“Of123”, city is “Islamabad”, phone is “+1234567”, addressLine1 is “office 1, Street 1”,
addressLine2 is “block 1”, state is “Punjab”, country is “Pakistan”, postalCode is “47080”, and
territory is “Islamabad”, the following statement can be written:
INSERT INTO offices VALUES (‘Of123’, ‘Islamabad, ‘+1234567’, ‘office 1, street 1’, ‘block
1’, ‘Punjab’, ‘Pakistan’, 123456, ‘Islmabad’)
To insert a record of an office which have values available for some columns i.e. officeCode is
“Of123”, city is “Islamabad”, phone is “+1234567” and addressLine1 is “office 1, Street 1”,
values of other columns are not available, the following statement can be written:
e.g. INSERT INTO offices
(officeCode, city, phone, addressLine1)
VALUES
(‘Of123’, ‘Islamabad, ‘+1234567’, ‘office 1, street 1’)
Note:
Inserting NULL to the auto_increment column results in max_previous_value + 1
A column defined with NOT NULL constraint and no DEFAULT constraint, must have a
value i.e. it cannot be NULL.
For example,
To select all rows from the table products for the columns name, and price
mysql> SELECT name, price FROM products;
+-----------+-------+
| name | price |
+-----------+-------+
| Pen Red | 1.23 |
| Pen Blue | 1.25 |
| Pen Black | 1.25 |
| Pencil 2B | 0.48 |
| Pencil 2H | 0.49 |
+-----------+-------+
5 rows in set (0.00 sec)