Lab 1 Introduction To SQL Tools (Xampp, MySQL, PHPMyAdmin) and Data Defination Language (CREATE)
Lab 1 Introduction To SQL Tools (Xampp, MySQL, PHPMyAdmin) and Data Defination Language (CREATE)
Lab-01
Introduction to SQL Tools and Data Definition Language (DDL)
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)
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
1. Read pages:
2. Read URL:
i. http://www.tomjewett.com/dbdesign/dbdesign.php?
page=ddldml.php
3. Revise the DDL
4. 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.
5.1 Task 1
Study Relational Data Model.
6.1.2 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.
6.1.2.1 The XAMPP installation starts with a splash screen, see Figure 2. Click “Next”.
6.1.2.3 Choose which directory to install XAMPP. In Figure 4, It is being installed in a directory
xampp under drive D.
6.1.2.4 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.
6.1.2.5 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.
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.
6.1.3.1 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.
Copy Path to MySQL > BIN folder from inside your XAMPP installation folder, See Figure 10.
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,
m_year year
);
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.
6.2.4.1 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.
6.2.4.2 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 (
Department of Computer Science,
C.U.S.T. Page 15
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)
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,
);
For Example,
2. As a separate constraint
For Example,
CREATE TABLE movies (
m_id int(10),
m_name varchar(255),
PRIMARY KEY (m_id)
);
A primary key of integer datatype can be set to auto_increment if no value is given while
inserting value. We can set auto_increment constraint as:
Department of Computer Science,
C.U.S.T. Page 16
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)
For Example,
CREATE TABLE movies (
m_id int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
m_name varchar(255)
);
DESCRIBE <tablename>;
For Example, to view the details of “movies” table, the following statement would be written,
see Figure 18.
DESCRIBE movies;
For Example, to rename movies table to products, the following statement can be written, see
Figure 18.
ALTER TABLE movies RENAME TO products;
Figure 18: Changing the name and datatype of a column “movie_name” of table “products”.
For Example, to add a new column “product_price” of type double, the following statement can
be written:
ALTER TABLE products ADD product_price double;
In Figure 19, A new column product_price can be seen in the description of table products after
adding the column.
For Example, to drop a column “product_price”, the following statement can be written:
In Figure 20, the column product_price is dropped, see description before and after dropping the
column.
For Example, to add primary key constraint to a column “id”, the following statement can be
written:
Figure 21: Adding PRIMARY KEY constraint to “id” column of products table.
In Figure 21, the column “id” now has a constraint PRIMARY KEY, see key column in
description before and after adding the constraint.
For Example, to drop table “products” from shop_db, the following statement can be written:
In Figure 22, the table “products” is dropped from the database shop_db.
For Example, to drop the database shop_db, the following statement can be written:
(a) (b)
Figure 22: Dropping “shop_db” database.
In Figure 22(a), the database shop_db is present and after executing the DROP DATABASE
statement it gets dropped, see Figure 22 (b).
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\Introduction to Database Management Systems\Lab1
7.1.1 Customers
Create table customers with the columns given in Table 4.
7.1.2 Payments
Create table payments with the columns given in Table 5.
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
Table 3: Evaluation of the Lab
Sr. No. Task No Description Marks
1 6 Procedures and Tools 05
2 7 Practice tasks and Testing 15
3 8 Evaluation Tasks (Unseen) 80
10.1 Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\
11. REFERENCES:
Appendix I
MySQL data types:
MySQL uses many different data types broken into three categories:
1) numeric,
2) date and time, and
3) string types.
19731230153000 ( YYYYMMDDHHMMSS ).
4) TIME - Stores the time in HH:MM:SS format.
5) 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.
String Types:
Most data you'll store will be in string format. This list describes the common string datatypes in
MySQL.
1) CHAR(M) - A fixed-length string between 1 and 255 characters in length (for example
CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length is
not required, but the default is 1.
2) 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.
3) LOB 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. Fields defined as TEXT also hold large amounts of data; the difference
between the two is that sorts and comparisons on stored data are case sensitive on BLOBs and
are not case sensitive in TEXT fields. You do not specify a length with BLOB or TEXT.
5) TINYBLOB or TINYTEXT - A BLOB or TEXT column with a maximum length of
255 characters. You do not specify a length with TINYBLOB or TINYTEXT.
6) MEDIUMBLOB or MEDIUMTEXT - A BLOB or TEXT column with a maximum
length of 16777215 characters. You do not specify a length with MEDIUMBLOB or
MEDIUMTEXT.
7) LONGBLOB or LONGTEXT - A BLOB or TEXT column with a maximum length of
4294967295 characters. You do not specify a length with LONGBLOB or
LONGTEXT.
8) 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') and only those values (or NULL) could ever populate that field.
Appendix II
While starting the Apache server, if port default port (80) is already being used by some other
application, it would display an error in XAMPP Control Panel As shown in Figure 23. We can
change the port by performing the following steps,
In 80 is in use then you will click on “Config” button infront of “Apache”, select “httpd.conf”
from the menu. Httpd.conf file will be opened in NotePad, Find “Listen 80” and change the port
80 to someother value of your choice. i.e. 8082.
Figure 4
Figure 5
In 443 is in use then you will click on “Config” button infront of “Apache”, select “httpd-
ssl.conf” from the menu.
Httpd-ssl.conf file will be opened in NotePad, Find “Listen 443” and change the port 443 to
someother value of your choice. i.e. 444,
Figure 6
Figure 7
Figure 8
Figure 9
Appendix III
1.3. { } Braces:
Braces surround mandatory syntax groupings. You must include the entire grouping when
forming an SQL statement. The braces are not part of the syntax; do not include them in your
SQL statement.
Example:
Consider the following generic SQL statement for selecting records from a table
SELECT * FROM <table_name> [ {WHERE <criteria>} ]
Now we will use this statement by replacing the angle brackets with our table name(identifier)
and “WHERE <criteria>” as either
SELECT * FROM my_table OR SELECT * FROM my_table WHERE somefield=10
1.7.Blank spaces:
(whether single or multiple spaces and/or line breaks) separate syntactic elements.
Example:
Consider the following generic SQL statement for creating a table
INSERT INTO <table_name> (column1Name, ..., columnNName) VALUES
(column1Value, ..., columnNValue)