MySQL Activity 1
MySQL Activity 1
MySQL Basics
Objectives
At the end of the activity, student is expected to perform the following:
Page 1 of 7 http://ThrivingAndLiving.blogspot.com
Figure 2 shows the mysql command prompt line . You are now on MySQL Server
environment.
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.
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.
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.
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:
Our initial table attributes for products are productID, description and unit.
We issue this command to create our first table:
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:
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:
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:
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.
Describing the table again after the ALTER command shows on Figure 7.
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:
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> ;
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.
Page 6 of 7 http://ThrivingAndLiving.blogspot.com
Figure 9. Using UPDATE statement
We issue again a SELECT statement to see the deletion was done. Figure 10 shows
it.
Page 7 of 7 http://ThrivingAndLiving.blogspot.com