Lab 01

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Department of Computer Science

CS220: Database Systems

Class: BSCS-13CD
Lab 1: MySQL and Workbench Environment

Date: January 31, 2024


Time: 10:00-12:50/ 02:00-04:50
Instructor: Ms. Naema Asif

Lab Engineer: Ms. Ayesha Asif


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

After performing this lab the students should be


o familiarized with MySQL and Workbench environment
o Load or unload a database in MySQL through MySQL Workbench
o Explore structure of tables in a database
o Create a test database and tables

Tools/Software Requirement

Tools Purpose

MySQL Community Server (5.6+) Freely downloadable version of the


world's most popular open source
database
MySQL Workbench (6.3+) MySQL Workbench provides a
graphical tool for working with
MySQL Servers and databases

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:

Figure 1: Workbench Home Window

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.

Making connection with MySQL Server

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:

Figure 2: MySQL Server Connection Setup Window

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

Using the Sakila database

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.

11111111111111111111111111111111117. Right click on a table and explore the second


option Table Inspector. It opens a new tab showing relation schema, attribute names and other
metadata.

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

Expand “Sakila”, there are further tabs as tables,views etc.Expand “Tables”.

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.

Retrieving Data from Tables:

When you want to retrieve some data from a table, Select statement will be most commonly used
statement.
Syntax of Select statement is

Select Column name From Table name;

You can select multiple columns separated with commas or single column.

Selecting all the columns


Select * from Customers;
Selecting specific columns
Select customer name, customer id from Customers;
Example 1:
Write a query in query 1 tab.

Select actor_id from actor;

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

2. Write a query to retrieve first and last name of actors.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy