Lab 01
Lab 01
Lab 01
Class: BSCS-13CD
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.
Lab Tasks
1. Write a query to Select columns “city_id” and “country_id” from the table “city” which already
exists in the “Sakila” database. (Observe the output when you execute the queries.)
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.