You are on page 1of 4

INFO20003 S2 2017

Week 2 Lab1: MySQL, MySQL Workbench and


SQL
Objectives:
In this laboratory session you will familiarise yourself with the software package we
use in this subject: MySQL Workbench.
In this laboratory you will also:
Log into your University MySQL server account and change your password
Explore some simple SQL statements
1. MySQL Workbench

1.1 In the Resources link on LMS open and read the MySQL Workbench file.
Follow the instructions to set up your MySQL database on the University
server.

2. Introduction to SQL
Depending on when you are doing this workshop you may have already briefly
encountered some SQL commands, such as CREATE, INSERT and SELECT.
The CREATE command is a Data Definition Language (DDL) command that will
create objects in the database. The lecture slides contain a CREATE TABLE
command that is used to create tables in the database. The syntax for the create table
command is shown below:
CREATE TABLE tablename
(field1 datatype [modifier],
field2 datatype [modifier],
,
PRIMARY KEY (identifier)
) ENGINE=InnoDB;
Note:
tablename is the name of the table.
field1, field2 etc are the names of the fields in the table.
datatype is the type of the field.
modifier ([ ] means optional) changes how the field operates in the database.
identifier is a meaningful name to identify in this case the PRIMARY KEY
2.1 Try to create a table called Customer containing the following:

FieldName DataType Modifier


CustomerID SMALLINT AUTO_INCREMENT
CustomerFirstName VARCHAR(50)

INFO20003-2017-S2-L1 1
CustomerLastName VARCHAR(50) NOT NULL
CustomerPhone VARCHAR(16) NOT NULL
CustomerBirthDate DATE NOT NULL

2.3 Write the SQL code in the Query window (dont forget the trailing semi-
colon at the end of the statement). And run (or execute the statement).

HINT: To run the statement press the first lightening symbol in the Toolbar
2.4 Did you see anything change?

HINT: You may have seen a "Query Completed" in the bottom left of the window.
MySQL Workbench only displays output on SELECT statements (as we are
SELECTing data FROM the database), or when there is an error in the syntax or
execution.
To know whether your statement is successful you can:
Click on the output tab and select Action Output in the drop down dialog.
Inspect the list of tables in the navigation bar on the right hand side of the
screen.
Hint: You may have to right click and refresh to see the created table in the
database

Once the table is created in the database we need to put some data into it. To insert
data into a database we use the INSERT statement.
An example of the INSERT statement is:
INSERT INTO tablename VALUES
(val1, val2, val3, val4, ),
(val1, val2, val3, val4, );

This INSERT syntax allows the insert of many rows at once. Keep repeating the
() for each row you want to add and separate them with a comma and put a ;
after the final one.
2.5 Use the example of the INSERT statement to enter the following data into
the customer table

CustomerID CustomerFirstName CustomerLastName CustomerPhone CustomerBirthDate


DEFAULT Jon Smith 22522255 1985-12-5
DEFAULT An Hone 22454852 1956-1-22
DEFAULT Jun De 26854582 1984-6-6
DEFAULT Kent King 12556842 1971-5-9
DEFAULT Barb Smith 33665252 1969-6-12
DEFAULT NULL Senti 75584252 2000-2-25

INFO20003-2017-S2-L1 2
You will need to think carefully when you are crafting the SQL statement. The order
of the data in this version of the INSERT statement must be the same as the table,
and no missing fields are allowed. Think about the data types you are entering via
this insert into the table. The data type chosen will affect how you craft your
statement.
2.6 Once you think you have written the SQL INSERT statement, run the SQL
again using the lightning bolt.

NOTE: if you have the create statement in the query window still, you will get an
error as clicking the lightning bolt runs all code in the window. To only run the
current statement, make sure the cursor is on the statement and click on the 2nd
lightning bolt.
2.7 Did your INSERT statement succeed or did you receive an error message?

HINT: Once again look at what happensYou will need to check to see if it worked
go to the OUTPUT page. You should see a message stating that there were 6
row(s) affected.
3. Introduction to the Lab Data Model
In the next few labs we will be using the Lab Data Model and gradually developing
your SQL skills.
In each lab a number of the SQL queries will be set for you to copy and execute. In
amongst these SQL queries will be questions that need you to write the SQL. You will
have the question and the correct data set answer. You will have to write the SQL.
Once you have tried these queries by example, an additional a set of questions will be
asked that you will need to solve on your own.
In each case, you will need to think carefully about what the question is asking prior
to attempting to solve it with SQL.
An approach to do this is:
Look at the data model
Look at the tables of data
This will help you understand what data set results you are looking for and how to
solve question with the SQL query.
Some of these SQL statements are easy, and some will challenge everyone (including
your lab instructor and lecturers). Solutions will be posted the week after the
previous tutorial and lab.

Creating and Populating the Case Study Tables

Open a web browser and login to LMS. In the INFO20003 subject area there is a
folder called Resources. You will see a file called Lab1 Database Creation and
Insertion Script
3.1. Download and save the SQL script that will create the tables and insert data into
them for the Case.
3.2. Uncompress the file by double clicking the filename.
3.3 Open the file in MySQL Workbench
Hint: To open the file Select File from the menu and then Open SQL Script

INFO20003-2017-S2-L1 3
Use the lightning bolt to run the complete file. Check to see if it worked. If you have
any issues please ask your tutor for help.
END OF LAB 1

INFO20003-2017-S2-L1 4

You might also like