MySQL Activity 1

Embed Size (px)

Citation preview

  • 8/14/2019 MySQL Activity 1

    1/7

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

    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 | MySQLCommand 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 Wizardor simply hit enter from keyboard.

    Figure 1. Launching of Command Prompt Window

  • 8/14/2019 MySQL Activity 1

    2/7

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

    Figure 2 shows the mysql command prompt line . You are now on MySQL Serverenvironment.

    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 testingfeatures 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 theoutput of the command.

    Figure 3. Databases available currently at server

  • 8/14/2019 MySQL Activity 1

    3/7

    Page 3 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 . Figure 4 displays the creation of my_store database after issuing the SHOWDATABASES 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 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 ( DATA TYPE, DATA TYPE

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

  • 8/14/2019 MySQL Activity 1

    4/7

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

    To check the structure ofproducts table, we use DESCRIBE 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, Figure6 shows the output:

    Figure 6. Setting up of productID as primary key

  • 8/14/2019 MySQL Activity 1

    5/7

    Page 5 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 theVALUES keyword, a comma-separated list of column names follows:

    INSERT INTO (, .)

    VALUES (

  • 8/14/2019 MySQL Activity 1

    6/7

    Page 6 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 SET = new_value

    WHERE < where condition> ;

    If we want to correct PC002s Description to a value Dell laptop computer, ourstatement 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.

  • 8/14/2019 MySQL Activity 1

    7/7

    Page 7 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 rowsdeleted. Below is the syntax that uses also a WHERE clause to identify the criteria of what

    data should be removed:

    DELETE FROM WHERE < where condition>;

    Let us delete productID which has a value of PC001.

    DELETE FROM products

    WHEREproductID = PC001

    ;

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

    it.

    Figure 10. Issuing a DELETE statement.