Introduction to Databases and Big Data (55-702187-AF-20212)
Week 4 - MySQL
Creating a table
Last class we learned how to access the module’s Virtual Machine in Azure Labs. We will continue our
Learning activities using Workbench to learn more about MySQL.
Remember that W3schools offers a series of very useful tutorials, including one for SQL at:
http://www.w3schools.com/sql/default.asp.
Tasks
1. Log in the module’s Azure Lab following the instructions given last week (see learning activities Week
3).
Please double-click on MySQL Workbench icon. Then choose the Localhost and login as root, the
password for this is password.
2. Please take a few minutes to familiarise yourself with the Workbench environment.
To create a new Database in MySQL, you use the following statements:
CREATE DATABASE newdatabase;
USE newdatabase;
3. Select the Query tab and write the following SQL statement to create a new database called
DataBank:
1
Introduction to Databases and Big Data (55-702187-AF-20212)
In Workbench your statement will be executed when you click the execute button in the Query
toolbar.
The list of Schemas in the side bar will now show the new database
created: DataBank. (Remember that in Workbench Schema and
Database are used as synonyms).
4. Select the Query tab and write the following
SQL statements to create a new table:
Please note that the MySQL client will not run a query until it encounters a semicolon; hence you can enter
statements over multiple lines as shown above. This often makes a query easy to read and debug.
You should now have a new table in your database:
ALTER Statement
Sometimes you will need to modify the structure of an existing table; for example, adding, deleting, or
changing its columns. The ALTER statement can be used for this purpose.
2
Introduction to Databases and Big Data (55-702187-AF-20212)
Tasks
5. Select the Query tab and write the following
SQL statements to rename the marks table:
6. Add a new column: markDate of
type Date.
The table is now called grades and has an extra column called markDate:
Note that there are many possible Clauses for the ALTER TABLE statement; the most common ones are
listed in Appendix A.
7. Add a new column: firstName VARCHAR(40) NOT NULL
Inserting Records
After a table has been created you can start populating them using the INSERT statement. There are two
ways that an INSERT query can be written. With the first method you name the columns to be populated,
for example:
INSERT INTO tablename (column1, column2…) VALUES (value1, Value2…)
or:
INSERT INTO tablename (column4, column8…) VALUES (valueX, ValueY…)
Using this syntax you can add new records to your tables, populating only the columns that matter; any
columns not given a value will be treated as NULL, or given the default value if one was defined. Note that if
a column has been defined as NOT NULL and does not have any default value, not specifying a value will
cause an error.
The second format for inserting records is not to specify any columns at all but to include values for every
one
INSERT INTO tablename VALUES (value1, Value2, Null, Value4…)
3
Introduction to Databases and Big Data (55-702187-AF-20212)
If you use this second method, you must specify a value (even if it is Null) for every column -i.e. if there are
eight columns, you must list eight values; otherwise, you will cause an error. For this reason, the first
method for inserting records is preferred.
Tasks
8. Let's INSERT a new record INTO grades:
You will get a confirmation message, and will be able to see the data stored in the new record of
the table.
Note for every SQL statement:
You do not have to indicate a value for a field that is AUTO_INCREMENT field (e.g. Id in the example
above).
Numeric values should not be quoted.
String values (for CHAR, VARCHAR, and TEXT column types) must always be quoted.
Date and time values must always be quoted.
The word NULL must not be quoted.
It does not matter if you use single or double quotation marks, as long as you pair them
consistently.
If you need to use a quotation mark in a value, you can escape the mark by preceding it with a
backslash:
INSERT INTO tablename (last_name) VALUES ('O\'Toole');
9. INSERT some new rows INTO grades (at least 10).
a. Make sure that you have a good spread of marks -from 0 to a 100!
b. Make sure that at least three of your new records have the same surname: "Smith"
Selecting Data
When your database has some records in it, you can retrieve the stored information using the SELECT
command. The syntax for the SELECT statement is:
SELECT column_1, column_2,… column_n FROM tablename
4
Introduction to Databases and Big Data (55-702187-AF-20212)
Tasks
10. Retrieve all the data from the grades table:
The above command should return every column for every record stored in the table.
11. Retrieve just the first and last names from grades:
Using conditionals
The SELECT queries done above will retrieve every record form a table, but sometimes you will want to limit
what rows are returned, based upon certain criteria. This can be achieved by adding some conditionals to
your queries.
Conditionals use the term WHERE:
SELECT column_1, column_2,… column_n FROM tablename WHERE
condition(s)
Example:
SELECT firstName FROM grades WHERE mark >= 60;
Tasks
12. Retrieve all the rows from the grades table WHERE the mark is equal or greater than 60.
13. Retrieve all the rows from the grades table WHERE the surname "Smith".
14. Retrieve all the rows from the grades table WHERE the id number is equal or greater than 4 and less
than 8
5
Introduction to Databases and Big Data (55-702187-AF-20212)
Appendix A - Common SQL statements and their clauses
ALTER TABLE Clauses
The basic syntax of Alter is: ALTER TABLE tablename CLAUSE
Clause Usage Meaning
Adds a new column to the
ADD COLUMN ALTER TABLE tablename ADD COLUMN columnname TYPE
table
Changes the data type and
CHANGE COLUMN ALTER TABLE tablename CHANGE COLUMN columnname TYPE
properties of a column
Removes a column from a
DROP COLUMN ALTER TABLE tablename DROP COLUMN columnname
table, including all of its data
Adds a new index on
ADD INDEX ALTER TABLE tablename ADD INDEX index(columnname)
columnname
Removes and existing index
DROP INDEX ALTER TABLE tablename DROP INDEX index
Changes the name of a table
RENAME TO ALTER TABLE tablename RENAME TO new_tablename