Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
595 views

Lab 1 Introduction To SQL Tools (Xampp, MySQL, PHPMyAdmin) and Data Defination Language (CREATE)

Uploaded by

Misbah Ullah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
595 views

Lab 1 Introduction To SQL Tools (Xampp, MySQL, PHPMyAdmin) and Data Defination Language (CREATE)

Uploaded by

Misbah Ullah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lab Manual for Introduction to Database Systems

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

2. Activity Time boxing 4

3. Objective of the experiment 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

5. Homework before Lab 6


5.1 Task 1 6

6. Procedure & Tools 6


6.1 Installing and Configuring XAMPP (MySQL, Apache) 6
6.1.1 Download XAMPP from http://www.apachefriends.org/en/xampp.html.
XAMPP for Windows exists in three different flavors. The “Installer” version is
“probably the most comfortable way to install XAMPP” according to the XAMPP
website. 6
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
6.1.3 Configuring the Environment Variable 9
6.2 Walkthrough Tasks 12
6.2.1 Creating A Database Using CREATE DATABASE Statement 13
6.2.2 View Available Databases Using SHOW DATABASES Statement 13
6.2.3 Selecting A Database Using USE Statement 14
6.2.4 Creating A Table In Selected Database Using CREATE TABLE Statement 14
6.2.5 View Available Tables Using SHOW TABLES Statement 17
6.2.6 View the Details Of A Table Using DESCRIBE Statement 17
Department of Computer Science,
C.U.S.T. Page 2
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

6.2.7 ALTER TABLE 18


6.2.8 Dropping A Table: 21
6.2.9 Dropping A Database: 22

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

8. Evaluation Task (Unseen) [Expected time = 30mins for tasks] 2

9. Evaluation criteria 2

10. Further Reading 2


10.1 Slides 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

Department of Computer Science,


C.U.S.T. Page 3
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Lab 01: Introduction to SQL and Tools


1. 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.

Relevant Lecture Material

a) Revise Lecture No. 3 and 4


b) Text Book: Database Systems, A practical approach to design, implementation,
and management by Thomas Connolly, Carolyn Begg, Addison Wesley, Fifth
Edition,

1. Read pages:
2. Read URL:
i. http://www.tomjewett.com/dbdesign/dbdesign.php?
page=ddldml.php
3. Revise the DDL

2. Activity Time boxing

Task No. Activity Name Activity time Total Time


6.2 Installing and Setting Up 15mins 15mins
XAMPP (MySQL, Apache)
6.3 Walkthrough Tasks 5mins 60mins
7 Practice tasks 10mins for each task 45mins
Table 1: Activity Time Boxing

3. Objective of the experiment


 To learn the installation, setup, and usage of tools required for relational database
management.
 To learn to implement a database schema in a database management system.

4. Concept Map
In this section, a brief overview of the concepts is presented, those will be used in this lab
afterwards.

4.1 Relational Data Model:


The relational model is centered on this idea of data organization into collections of two-
dimensional tables called “relations.” Each row of the table is called a Tuple or Record. And
each column is given a name and is called Attribute.

Department of Computer Science,


C.U.S.T. Page 4
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

4.2 Database Management System (DBMS):


A database management system (DBMS) is a computer software application that interacts with
the user, other applications, and the database itself to manage data in databases. A general-
purpose DBMS is designed to allow the definition, creation, querying, update, and administration
of databases.

4.3 Relational Database Management System (RDBMS):


A relational database management system (RDBMS) is a database management system (DBMS)
that is based on the relational model.

4.4 SQL (Structured Query Language):


SQL (Structured Query Language) is the standard language used for the retrieval and
management of data in relational databases (RDBMS). It also supports database schema
definition and modification. It is supported by all modern database management systems (e.g.
Oracle, IBM DB2, Microsoft Access, Microsoft SQL Server, PostgreSQL, MySQL, etc.).

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.4.1 Data Definition Language


Statements that create, modify or destroy database objects (such as tables). These statements
begin with the keywords: CREATE, ALTER or DROP.

4.4.2 Data Manipulation Language


Statements that retrieve, insert, update, edit or delete data stored in database tables. These
statements begin with the keywords: SELECT, INSERT, UPDATE or DELETE.

4.4.3 Data Control Language


Statements that authorize certain users to view, change or delete data. These statements begin
with the keywords: GRANT or REVOKE. We do not consider data control in the lab exercises.

4.5 MySQL:
MySQL is a free, open-source, and the most popular relational database management system
(RDBMS).

Department of Computer Science,


C.U.S.T. Page 5
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

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. Homework before Lab


You must solve the following problems at home before the lab.

5.1 Task 1
Study Relational Data Model.

6. Procedure & Tools


In this section, procedure of the tasks and setup of required tools is defined.

6.1 Installing and Configuring XAMPP (MySQL, Apache)

6.1.1 Download XAMPP from http://www.apachefriends.org/en/xampp.html. XAMPP for


Windows exists in three different flavors. The “Installer” version is “probably the most
comfortable way to install XAMPP” according to the XAMPP website.

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.

Figure 1: UAC Warning

6.1.2.1 The XAMPP installation starts with a splash screen, see Figure 2. Click “Next”.

Department of Computer Science,


C.U.S.T. Page 6
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 2: XAMPP Setup Wizard

6.1.2.2 Select the tools you want to install, see Figure 3.

Figure 3: XAMPP Options

Department of Computer Science,


C.U.S.T. Page 7
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

6.1.2.3 Choose which directory to install XAMPP. In Figure 4, It is being installed in a directory
xampp under drive D.

Figure 4: Xampp Startup

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.

Figure 5: Setup Complete Popup

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.

Department of Computer Science,


C.U.S.T. Page 8
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

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.

6.1.3 Configuring the Environment Variable


To run MySQL CLI, the path to MySQL bin folder is required to be configured in an
environment variable named PATH. To make windows’ console aware of the MySQL tools.
Following are the steps to configure PATH variable for MySQL tools.

6.1.3.1 Go to Control Panel > System and Security > System and Click Advanced system
settings, See Figure 7.

Department of Computer Science,


C.U.S.T. Page 9
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 7: Advance System Settings

Click Environment Variables, See Figure 8.

Figure 8: System Properties

Variable name PATH is available double click it, otherwise click NEW, See Figure 9.

Department of Computer Science,


C.U.S.T. Page 10
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 9: Environment Variables

Copy Path to MySQL > BIN folder from inside your XAMPP installation folder, See Figure 10.

Figure 10: Path of bin folder

Paste the copied path in Variable Value field for the Variable Name PATH. See Figure 11.

Department of Computer Science,


C.U.S.T. Page 11
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 11: Edit User Variable

Open Statement prompt and Write statement MySQL then press Enter key, See Figure 12.

Figure 12: Statement Prompt for starting MySQL

MySQL CLI is now running we can now start using it.

6.2 Walkthrough Tasks


These tasks are designed to guide you to use the tools, implement the basic concepts of DDL.
There are three basic statements within the DDL i.e. CREATE, DROP and ALTER. We will be
focusing on CREATE, USE, ALTER, and DROP statements in this lab.

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

Department of Computer Science,


C.U.S.T. Page 12
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 13: Logging in to MySQL CLI

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.

6.2.1 Creating A Database Using CREATE DATABASE Statement


You first need to create a database in which you will store your tables. This can be considered
analogous to opening a blank database. To create a database, we use the CREATE DATABASE
statement.

Figure 14: Creating “Shop_db” database

In Figure 14, A database named “shop_db” is created.

6.2.2 View Available Databases Using SHOW DATABASES Statement


To show all the databases available on MySQL server, we use the SHOW DATABASES
statement. See Figure 15.

Department of Computer Science,


C.U.S.T. Page 13
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 15: Viewing all available databases

6.2.3 Selecting A Database Using USE Statement


Before starting to work (e.g. create tables, write queries etc.) with your database, you must select
the database you are going to use. To do this, we use the USE statement.

Figure 16: Select or Change the current database.

In Figure 16, we have selected a database “shop_db” to perform further operations.

6.2.4 Creating A Table In Selected Database Using CREATE TABLE Statement


To create tables, we use the CREATE TABLE statement, the generic syntax of CREATE
TABLE statement is:

CREATE TABLE [IF NOT EXISTS] <table_name> (


<column_name> <columnDataType> <columnConstraints>, ... )

For Example,

CREATE TABLE movies (


m_id int(10),
m_title varchar(255)
Department of Computer Science,
C.U.S.T. Page 14
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

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.

Data Type Description


TINYINT, SMALLINT, MEDIUMINT, INT, Integer values
BIGINT
FLOAT, DOUBLE Floating-point values
DECIMAL Decimal values
CHAR Fixed length strings up to 255 characters
VARCHAR Variable length strings up to 255 characters
TINYBLOB, BLOB, MEDIUMBLOB, Longer blocks of text data
LONGBLOB
DATE, TIME, YEAR Date; time or duration; year values
DATETIME Combined data and time values
TIMESTAMP Timestamps
ENUM One of a set of predefined values
SET Zero or one
Table 3: The most commonly used data types in MySQL.

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)

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,
);

6.2.4.3 Primary Key


In the relational model, a table shall not contain duplicate rows, because that would create
ambiguity in retrieval. To ensure uniqueness, each table should have a column (or a set of
columns), called primary key, that uniquely identifies every record of the table. For example, a
unique number of the customer can be used as the primary key for the customer's table; product
code for products table; ISBN for books table. A primary key is called a simple key if it is a
single column; it is called a composite key if it is made up of several columns. Most RDBMSs
build an index on the primary key to facilitate the fast search. The primary key is often used to
relate to other tables, we will see this in the upcoming lab on Foreign Keys.

We can define the PRIMARY KEY constraint in following two ways:

1. Inline with the column along with other constraints.

For Example,

CREATE TABLE movies (


m_id int(10) PRIMARY KEY,
m_name varchar(255)
);

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)
);

6.2.5 View Available Tables Using SHOW TABLES Statement


To view all the tables available in the current database, we use SHOW TABLES statement. In
Figure 17, All tables of shop_db are being viewed.

Figure 17: Viewing tables in the shop_db database.

6.2.6 View the Details Of A Table Using DESCRIBE Statement


To view the columns, their data types and constraints applied on the columns we use DESCRIBE
statement. The generic syntax of DESCRIBE statement is:

DESCRIBE <tablename>;

For Example, to view the details of “movies” table, the following statement would be written,
see Figure 18.
DESCRIBE movies;

Figure 18: Viewing details of movies table.

Department of Computer Science,


C.U.S.T. Page 17
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

6.2.7 ALTER TABLE


We use the statement ALTER TABLE statement to modify the design of the tables after we
create them. There are various types of altering operations in MySQL i.e. renaming a table,
adding a new column, dropping a column, and modifying a column’s definition. Now we will see
each operation one by one.

6.2.7.1 Renaming A Table


To rename a table, RENAME TO clause is used with ALTER TABLE statement, the generic
syntax is

ALTER TABLE <tableName> RENAME TO <newTableName>

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: Viewing tables after renaming.

Table movies is “renamed to “products”, in Figure 18.

6.2.7.2 Modifying A Column


The definition of a column can be modified by using the CHANGE clause with the ALTER
statement, The generic syntax to modify the column is:

ALTER TABLE <tableName> CHANGE <columnName> <newColumnName> <newColumnDataType>


<newColumnConstraints>

Department of Computer Science,


C.U.S.T. Page 18
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 18: Changing the name and datatype of a column “movie_name” of table “products”.

In the Figure 18, definition of a column named movie_name is modified as


 Its name is changed to product_name
 Datatype to varchar(255)

6.2.7.3 Adding A New Column


The generic syntax for adding a new column is:

ALTER TABLE <tableName> ADD <columnName> <columnDataType> <columnConstraints>, ...

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;

Figure 19: Adding product_price column in products table.

Department of Computer Science,


C.U.S.T. Page 19
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

In Figure 19, A new column product_price can be seen in the description of table products after
adding the column.

6.2.7.4 Dropping a column:


The generic syntax for dropping a column is:

ALTER TABLE <tableName> DROP <columnName>;

For Example, to drop a column “product_price”, the following statement can be written:

ALTER TABLE products DROP product_price;

Figure 20: Dropping product_price column from products table.

In Figure 20, the column product_price is dropped, see description before and after dropping the
column.

6.2.7.5 Adding Primary Key Constraint


The generic syntax for adding PRIMARY KEY constraint to existing table is:

ALTER TABLE <tableName> ADD [CONSTRAINT <constraint_name>] PRIMARY KEY (<columnName>[,


…]);

For Example, to add primary key constraint to a column “id”, the following statement can be
written:

ALTER TABLE products ADD PRIMARY KEY (id)

Department of Computer Science,


C.U.S.T. Page 20
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

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.

6.2.8 Dropping A Table:


The generic syntax of DROP TABLE statement for dropping a table from a database is:

DROP TABLE <tablename>

For Example, to drop table “products” from shop_db, the following statement can be written:

DROP TABLE products

Figure 22: Dropping “products” table from shop_db.

In Figure 22, the table “products” is dropped from the database shop_db.

6.2.9 Dropping A Database:


The generic syntax of DROP DATABASE statement for dropping a database is:
Department of Computer Science,
C.U.S.T. Page 21
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

DROP DATABASE <database_name>

For Example, to drop the database shop_db, the following statement can be written:

DROP DATABASE shop_db;

(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 Practice Task 1 [Expected time = 65mins]


Your task is to create the database “practice_db” and following tables with the required
constraints.

7.1.1 Customers
Create table customers with the columns given in Table 4.

Column Name Data Type Constraints


customer_number INT NOT NULL, AUTO_INCREMENT, PRIMARY KEY
customer_name VARCHAR(50) NOT NULL
Phone VARCHAR(50) NOT NULL
Department of Computer Science,
C.U.S.T. Page 22
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

address_line1 VARCHAR(50) NOT NULL


address_line2 VARCHAR(50) NULL
joining_date TIMESTAMP NOT NULL, DEFAULT CURRENT_TIMESTMP
credit_limit DOUBLE NOT NULL, DEFAULT 0
Table 4: Column’s details for table customers

7.1.2 Payments
Create table payments with the columns given in Table 5.

Column Name Data Type Constraints


customer_number INT NOT NULL, PRIMARY KEY
check_number VARCHAR(50) NOT NULL, PRIMARY KEY
payement_date VARCHAR(50) NOT NULL
amount DOUBLE NOT NULL
Table 5: Column’s details for table payments

7.1.3 Practice queries


Use the database created above and practice following queries:

1. Remove column address_line2 from the table customers.


2. Change datatype of column phone to varchar(20) in the table customers.
3. Add column status with datatype varchar(50) and default value “active”.
4. Add column email with datatype varchar(100) and constraint UNIQUE.
5. Drop table payments.
6. Drop database “practice_db”.

8. Evaluation Task (Unseen) [Expected time = 30mins for tasks]

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

Department of Computer Science,


C.U.S.T. Page 23
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

10. Further Reading

This section provides the references to further polish your skills.

10.1 Slides

The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\

11. REFERENCES:

11.1 SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer.

Department of Computer Science,


C.U.S.T. Page 24
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

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.

Numeric data types:


1) INT - A normal-sized integer that can be signed or unsigned. If signed, the allowable
range is from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to
4294967295. You can specify a width of up to 11 digits.
2) TINYINT - A very small integer that can be signed or unsigned. If signed, the allowable
range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. You can specify a
width of up to 4 digits.
3) SMALLINT - A small integer that can be signed or unsigned. If signed, the allowable
range is from -32768 to 32767. If unsigned, the allowable range is from 0 to 65535. You can
specify a width of up to 5 digits.
4) MEDIUMINT - A medium-sized integer that can be signed or unsigned. If signed, the
allowable range is from - 8388608 to 8388607. If unsigned, the allowable range is from 0 to
16777215. You can specify a width of up to 9 digits.
5) BIGINT - A large integer that can be signed or unsigned. If signed, the allowable range
is from - 9223372036854775808 to 9223372036854775807. If unsigned, the allowable range is
from 0 to 18446744073709551615. You can specify a width of up to 20 digits.
6) FLOAT(M,D) - A floating-point number that cannot be unsigned. You can define the
display length (M) and the number of decimals (D). This is not required and will default to
10,2, where 2 is the number of decimals and 10 is the total number of digits (including
decimals). Decimal precision can go to 24 places for a FLOAT.
7) DOUBLE(M,D) - A double precision floating-point number that cannot be unsigned.
You can define the display length (M) and the number of decimals (D). This is not required and
will default to 16,4, where 4 is the number of decimals. Decimal precision can go to 53 places
for a DOUBLE. REAL is a synonym for 8) DOUBLE.
8) DECIMAL(M,D) - An unpacked floating-point number that cannot be unsigned. In
unpacked decimals, each decimal corresponds to one byte. Defining the display length (M) and
the number of decimals (D) is required. NUMERIC is a synonym for DECIMAL.

Date and Time Types:


The MySQL date and time datatypes are:
1) 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.
2) 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.
3) TIMESTAMP - A timestamp between midnight, January 1, 1970 and sometime in 2037.
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
Department of Computer Science,
C.U.S.T. Page 25
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

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,

1. Open The XAMPP control panel from C:\xampp\xampp-control.exe.

Department of Computer Science,


C.U.S.T. Page 26
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 3: Xampp Startup

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

Department of Computer Science,


C.U.S.T. Page 27
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

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

Department of Computer Science,


C.U.S.T. Page 28
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Figure 7

Figure 8

Department of Computer Science,


C.U.S.T. Page 29
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

11.1.1.1 Start Apache and MySql.

Figure 9

Department of Computer Science,


C.U.S.T. Page 30
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

Appendix III

1. HOW TO READ SQL SYNTAX


Following notations are used when defining generic SQL statement for MySQL. We will be
seeing these in our upcoming labs so we must know “what they are” and “how to read them”.

1.1. < >  Angle brackets:


Angle brackets surround the names of syntactic elements. The brackets are not part of the syntax;
do not include them in your SQL statement. You will replace them with identifiers/names.
Example:
Consider the following generic SQL statement for creating a database
CREATE DATABASE <database_name> >
Now we will use this statement by replacing the angle brackets with our database
name(identifier) as
CREATE DATABASE my_database .

1.2. [ ]  Square brackets:


Square brackets surround optional syntax. You may choose whether or not to omit such syntax
when forming an SQL statement. The brackets are not part of the syntax; do not include them in
your SQL statement.
Example:
Consider the following generic SQL statement for creating a database
CREATE DATABASE [IF NOT EXISTS] <database_name>
Now we will use this statement by replacing the angle brackets with our database
name(identifier) as either
CREATE DATABASE my_database OR CREATE DATABASE IF NOT EXISTS
my_database

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.4. ()  Small Brackets:


Small brackets are part of syntax and you will write them in your SQL statement as they are.

Department of Computer Science,


C.U.S.T. Page 31
Lab 01: Introduction to SQL Tools and Data Definition Language (CREATE)

1.5. |  The vertical:


Bar separates groups of syntactic elements. You must choose one of the elements when forming
an SQL statement. The vertical bar is not part of the syntax; do not include it in your SQL
statement.
Example:
Consider the following generic SQL statement for creating a table
CREATE TABLE <Table name> (<Column name> {INTEGER | CHARACTER(5)})

Now we will use this statement as

CREATE TABLE my_table (id INTEGER)


OR
CREATE TABLE my_table (id CHARACTER(5))

1.6. ...  Elipsis:


An ellipsis following a syntactic element indicates that the syntax is repeatable. You may include
the element as often as you wish when forming an SQL statement. The ellipsis is not part of the
syntax; do not include it in your SQL statement.

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)

Now we will use this statement as

INSERT INTO my_table (id, age, password) VALUES (2,20,abdcdef)

Department of Computer Science,


C.U.S.T. Page 32

You might also like