Lab 1 ADMS
Lab 1 ADMS
Class: BSCS-13
Lab 1: MySQL and Workbench Environment and Basic SQL Revision
SAQIB MAHMOOD
460430
Lab 1: MySQL and Workbench Environment
Introduction
Data is a collection of raw facts and figures. It is being processed to obtain useful information to
assist organization in taking better decisions. Database is an organized collection of related data.
In order to manage the databases, Database Management Systems (DBMS) or Database
Systems offer sets of program and tools to efficiently access and manage the databases.
Relational data model is used to model the databases in Database Systems in the form of relation
(table) comprising of tuples (rows) and attributes (columns). Primary key is the unique identifier
for the relation and foreign key is used for referential purposes by the relation to include or refer
to other relations data.
Structured Query Language (SQL) is used to access and modify the relational database. It is the
most widely used query language supported by modern database management systems. SQL is a
nonprocedural language.
Objectives
Tools/Software Requirement
Tools Purpose
Description
Understanding the Work Bench Environment
MySQL Workbench is a unified visual tool for database architects, developers, and DBAs. You can find
it installed on your workstation. Select Start>Program>MySQL>Workbench to start up. The home
screen shown in Figure 1:
2. When MySQL Workbench first starts, it presents the Home window, which has three main
sections:
List of MySQL Connections (help to manage and connect with MySQL server that
actually runs the database)
Models (facilitate database design)
Shortcuts (quick access)
3. MySQL Workbench provides extensive facilities for working directly with SQL code. Before
working directly with a live server, a connection must be created using MySQL Connections
section on Home Window. After a connection is established, it is possible to execute SQL code
directly on the server and manipulate the server using SQL code.
1. To administer your MySQL Server, you must first create a MySQL connection. Creating a
MySQL connection is often the first action performed after installing MySQL Workbench.
2. To add a connection, click the [+] icon to the right of the MySQL Connections label on
Home Window. This opens the Setup New Connection form. Fill out the connection detail as
shown in Figure 2:
3. Define the Connection Name value as MyFirstConnection. Click OK. The connection will
appear in MySQL Connections list on Home Window.
4. Select the MyFirstConnection from Home Window, this opens the SQL Editor screen as
shown in Figure 3:
Figure 3: SQL Editor
5. For querying use SQL Query menu. It will open the SQL Query Panel as shown in Figure 4:
Figure 4: SQL Query Panel
1. Download and unzip Sakila database. The archive contains three files: sakila-data.sql, sakila-
schema.sql, and sakila.mwb.
2. Now right click and select drop on an existing database schema named Sakila. You will load it
a fresh.
3. From File menu select Open SQL Script. Select sakila-schema.sql from the file selection
dialog. It would open the file in the query pan. Now select Execute All from Query menu. You
can alternatively press the Execute All icon from the toolbar. Don’t forget to check the log pan
below the query tab. It mentions all errors, warnings, and messages.
4. Press the refresh icon in the schema section . You must see Sakila database in the list of the
databases.
5. Repeat the process for sakila-data.sql file. If all goes well, you have successfully loaded the
sakila database and now you can close the files (but not the Workbench).
6. Expand the Sakila database. Among the listed categories, expand Tables. You will see list of
tables (relations) in Sakila database.
8. Afterwards right click on the same table and explore the first option: Select Rows. It will open
another tab for navigating through the contents of the relation.
9. The data view supports many different options such as sorting on a column by selecting its
header, searching contents, deleting a row, adding another, or exporting the contents to an
external file. You can play with these options.
Examples:
SQL is a structure query language which is not a full featured programming language; it is only a
data sublanguage, which has commands for data definition and processing. Data definition
commands are Data Definition Language while data processing commands are Data
Modification Language. In first lab we will focus on DML commands for which we will use
Sakila Schema which we have already downloaded and is a sample data base. With the passage
of time we will be able to design our own database.
When we are working on any schema, we need to set that schema as default.
Right click on sakila and select first option,“set as default schema” from menu.as shown below.
Figure 5: Setting Default Schema
You will find various tables including actor, class, catogory etc. All these tables have data stored.
You can retrieve or update data using SQL commands.
Explore various tables of the sakila database and columns that are present in a particular table.
Observe how data is stored in a table.
When you want to retrieve some data from a table, Select statement will be most commonly used
statement.
Syntax of Select statement is
You can select multiple columns separated with commas or single column.
It will select actor Id column from actor table which is present in sakila database and show you
the results.
Always specify the name of the database in which a table/relation is present through which data is to be retrieved.
The select clause can contain arithmetic expressions involving the operation, +, –, *, and /, and operating on
constants or attributes of records (tuples).
The FROM clause
The FROM clause can allow to select attributes from single table or multiple tables. When multiple tables are
applied, it combines the records from the two or more tables listed and presents every possible combination of the
listed attributes in SELECT clause.
This is not very useful until some filtering condition is applied. This is achieved through using the WHERE clause.
The WHERE clause
We can use the WHERE clause to filter the result set based on certain conditions. The syntax for using WHERE in
the SELECT statement is as follows:
SELECT "column_name(s)"
FROM "table_name(s)"
WHERE "condition";
"Condition" can include a single comparison clause (called simple condition) or multiple comparison clauses
combined together using AND or OR operators (compound condition). Conditions can include other operators like
IN, BETWEEN, DISTINCT etc shown in Table 1:
Operator Description Example
= Equal to Author = 'Alcott'
<> Not equal to (most DBMS Dept <> 'Sales'
also accept != instead of <>)
> Greater than Hire_Date > '2012-01-31'
< Less than Bonus < 50000.00
>= Greater than or equal Dependants >= 2
<= Less than or equal Rate <= 0.05
BETWEEN Between an inclusive range Cost BETWEEN 100.00 AND
500.00
LIKE Match a character pattern First_Name LIKE 'Will%'
Table 1: List of Basic Operators available in SQL
Examples:
2. Ordering data
The order of rows returned in a query result is undefined. The ORDER BY clause can be used to sort the rows. This
clause comes last in the SELECT statement. ASC at the end of the ORDER BY clause specifies ascending order
where as DESC specifies descending order. ASC is the default order. The syntax for an ORDER BY statement is as
follows:
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC];
select title from film
where rental_duration < '6'
order by title DESC
Example:
4. Write a query to retrieve names of movies starting with P display movies names in desc
order.
Deliverables
Complete your labs tasks and write queries and the steps/screenshots of all the questions
attempted in a document and upload on LMS. The marking will be based on viva/lab task
submitted or the quiz that takes place at the end of the lab.