Lab Manual For Introduction To Database Systems: Lab-05 Data Definition Language (DDL)
Lab Manual For Introduction To Database Systems: Lab-05 Data Definition Language (DDL)
Lab-05
Data Definition Language (DDL)
Lab 05: Data Definition Language
Contents
1. Introduction 4
4. Concept Map 4
4.1 Relational Data Model: Error! Bookmark not defined.
4.2 Database Management System (DBMS): Error! Bookmark not defined.
4.3 Relational Database Management System (RDBMS): Error! Bookmark not defined.
4.4 SQL (Structured Query Language): Error! Bookmark not defined.
4.4.1 Data Definition Language Error! Bookmark not defined.
4.4.2 Data Manipulation Language Error! Bookmark not defined.
4.4.3 Data Control Language Error! Bookmark not defined.
4.5 MySQL: Error! Bookmark not defined.
4.6 PHP: Error! Bookmark not defined.
4.7 PHPMyAdmin: Error! Bookmark not defined.
4.8 XAMPP: Error! Bookmark not defined.
7. Practice Tasks 13
7.1 Practice Task 1 [Expected time = 65mins] 13
7.1.1 Customers 13
7.1.2 Payments 142
7.1.3 Practice queries 142
4. Concept Map
In this section, a brief overview of the concepts is presented, those will be used in this lab
afterwards.
DDL or Data Definition Language actually consists of the SQL commands that can be used to
define the database schema. DDL allows to add / modify / delete the logical structures which
contain the data or which allow users to access / maintain the data (databases, tables, keys,
views...). DDL is about "metadata"
In a walkthrough section we have discussed all the operation related to DDL.
5.1 Task 1
Study Relational Data Model.
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.
6.1.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.1.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 (
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;
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 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:
\\fs\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 Description Marks
No
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 \\fs\lectures$\
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.
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 2
Figure 3
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 4
Figure 5
Figure 6
Figure 7
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
Example:
Consider the following generic SQL statement for creating a table
INSERT INTO <table_name> (column1Name, ..., columnNName) VALUES
(column1Value, ..., columnNValue)