You are on page 1of 7

Laboratory Activity 1

MySQL Basics
Objectives
At the end of the activity, student is expected to perform the following:

 Run MySQL command line
 Display available databases
 Create database and tables
 Alter table and set up primary key
 Insert, update and delete tables

Run MySQL Command Line Prompt
We run MySQL prompt, click Start | Programs | MySQL | MySQL Server 5.1 | MySQL
Command Line Client. See Figure 1. If you are prompted with a password, type the
password that you have assigned during running of MySQL Server Instance Config Wizard
or simply hit enter from keyboard.

Figure 1. Launching of Command Prompt Window

Page 1 of 7 http://ThrivingAndLiving.blogspot.com
Figure 2 shows the mysql command prompt line . You are now on MySQL Server
environment.

Figure 2. MySQL command prompt

Display Available Databases

MySQL comes with default databases upon installation. These are information_schema,
mysql and test. Mysql and information_schema databases are special databases where
MySql server stores various access permissions. Test database is used for various testing
features and sample databases.

To display all the available databases on the server we type:

SHOW DATABASES;

Please take note that all commands should be terminated with semicolon to signal
MySQL that a command is completed and ready for execution. Figure 3 should show the
output of the command.

Figure 3. Databases available currently at server

Page 2 of 7 http://ThrivingAndLiving.blogspot.com
Create Database
We will now create our own (new) database. Let us say that the name of our database is
my_store. Command syntax for creating database is CREATE DATABASE <database
name>. Figure 4 displays the creation of my_store database after issuing the SHOW
DATABASES command.

Figure 4. my_store database is created

Create Table within the Database
Database consists of table/s where we store rows and columns. Creating a database does not
mean you open it automatically. USE <database name> is the syntax in opening a database.
In order to open my_store database we issue:

USE my_store;

We will create now a table products to hold all products data within the database.
Our products table consists of attributes/fieldnames vital in identifying description about a
product. Syntax in creating a table has this format:

CREATE TABLE <table name> ( <fieldname1> DATA TYPE,
<fieldname2> DATA TYPE…
<fieldnameN> DATA TYPE);

Our initial table attributes for products are productID, description and unit.
We issue this command to create our first table:

CREATE TABLE products ( productID VARCHAR(5),
description VARCHAR(50), unit VARCHAR(3));

Page 3 of 7 http://ThrivingAndLiving.blogspot.com
To check the structure of products table, we use DESCRIBE <tablename> to show
its composition. Figure 5 has the output:

Figure 5. Creating a Table

Alter Table and Set up Primary Key

All relational database tables should have a primary key/s that will identify uniqueness of
row/record. On our products table, we will set productID as the primary key. We issue
the following command:

ALTER TABLE products
ADD PRIMARY KEY (productID);

What you have noticed here is the use of ADD PRIMARY KEY command in setting
up the productID as our primary key. As we again describe the structure of our table, Figure
6 shows the output:

Figure 6. Setting up of productID as primary key

Page 4 of 7 http://ThrivingAndLiving.blogspot.com
We may even add additional columns/attributes that will further describe data for
products. Let us add column date_created on our table.

ALTER TABLE products
ADD date_created DATE;

Describing the table again after the ALTER command shows on Figure 7.

Figure 7. Adding date_created column

The INSERT Command
The INSERT INTO statement is used to insert new rows/records into a table. After the
VALUES keyword, a comma-separated list of column names follows:

INSERT INTO <table_name> (<column1>, <column2>….)
VALUES (<value1, value2…);

Let us insert and execute the following statement:

INSERT INTO products (productID, description, unit, date_created)
VALUES (‘PC001’, ‘Compaq laptop computer’,’ea’, ‘2009-11-23’);

Issue another insert:

INSERT INTO products (productID, description, unit, date_created)
VALUES (‘PC002’, ‘Dell Desktop computer’,’ea’, ‘2009-11-22’);

To see if the following rows were successfully added, we will use SELECT statement
to display all rows. * represents all rows.

Page 5 of 7 http://ThrivingAndLiving.blogspot.com
SELECT * FROM products;

Figure 8 shows the output of the two rows insertion upon displaying it with SELECT
statement:

Figure 8. Display of records after a SELECT statement

Using UPDATE statement

This command is used to modify the data in the table. It has the following syntax:

UPDATE <table_name>
SET <column_name> = new_value
WHERE < where condition> ;

If we want to correct PC002’s Description to a value ‘Dell laptop computer’, our
statement will be:

UPDATE products
SET description =’Dell laptop computer’
WHERE productID = ‘PC002’;

We have used WHERE clause in the statement to qualify what data is to be modified,
thereby limiting the scope of the update.

Again, SELECT statement is used to display changes. See Figure 9.

Page 6 of 7 http://ThrivingAndLiving.blogspot.com
Figure 9. Using UPDATE statement

Deleting Records from Table
The DELETE statement is used to delete rows from table and returns the number of rows
deleted. Below is the syntax that uses also a WHERE clause to identify the criteria of what
data should be removed:

DELETE FROM <table_name>
WHERE < where condition>;

Let us delete productID which has a value of ‘PC001’.

DELETE FROM products
WHERE productID = ‘PC001’;

We issue again a SELECT statement to see the deletion was done. Figure 10 shows
it.

Figure 10. Issuing a DELETE statement.

Page 7 of 7 http://ThrivingAndLiving.blogspot.com