mysql 1st. act.


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

mysql 1st. act.

  1. 1. Laboratory Activity 1 MySQL BasicsObjectivesAt 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 tablesRun MySQL Command Line PromptWe 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 thepassword that you have assigned during running of MySQL Server Instance Config Wizardor simply hit enter from keyboard. Figure 1. Launching of Command Prompt WindowPage 1 of 7
  2. 2. Figure 2 shows the mysql command prompt line . You are now on MySQL Serverenvironment. Figure 2. MySQL command promptDisplay Available DatabasesMySQL comes with default databases upon installation. These are information_schema,mysql and test. Mysql and information_schema databases are special databases whereMySql 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 signalMySQL that a command is completed and ready for execution. Figure 3 should show theoutput of the command. Figure 3. Databases available currently at serverPage 2 of 7
  3. 3. Create DatabaseWe will now create our own (new) database. Let us say that the name of our database ismy_store. Command syntax for creating database is CREATE DATABASE <databasename>. Figure 4 displays the creation of my_store database after issuing the SHOWDATABASES command. Figure 4. my_store database is createdCreate Table within the DatabaseDatabase consists of table/s where we store rows and columns. Creating a database does notmean 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 aproduct. 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
  4. 4. To check the structure of products table, we use DESCRIBE <tablename> to showits composition. Figure 5 has the output: Figure 5. Creating a TableAlter Table and Set up Primary KeyAll relational database tables should have a primary key/s that will identify uniqueness ofrow/record. On our products table, we will set productID as the primary key. We issuethe following command: ALTER TABLE products ADD PRIMARY KEY (productID); What you have noticed here is the use of ADD PRIMARY KEY command in settingup 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 keyPage 4 of 7
  5. 5. We may even add additional columns/attributes that will further describe data forproducts. 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 columnThe INSERT CommandThe 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 <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 statementto display all rows. * represents all rows.Page 5 of 7
  6. 6. SELECT * FROM products; Figure 8 shows the output of the two rows insertion upon displaying it with SELECTstatement: Figure 8. Display of records after a SELECT statementUsing UPDATE statementThis 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’, 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.Page 6 of 7
  7. 7. Figure 9. Using UPDATE statementDeleting Records from TableThe 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 whatdata 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 showsit. Figure 10. Issuing a DELETE statement.Page 7 of 7