25
Session 5: Working with MySQL iNET Academy Open Source Web Development

Session 5: Working with MySQL iNET Academy Open Source Web Development

Embed Size (px)

Citation preview

Page 1: Session 5: Working with MySQL iNET Academy Open Source Web Development

Session 5: Working with MySQL

iNET Academy

Open Source Web Development

Page 2: Session 5: Working with MySQL iNET Academy Open Source Web Development

Objectives

Introduction of MySQL Managing the Database Using phpMyAdmin & SQLyog Structure Query Language

Page 3: Session 5: Working with MySQL iNET Academy Open Source Web Development

MySQL Database

MySQL client interface Login: require an username and a password Different user has different access level to databases and tables To connect to a MySQL Database we need

The IP address of the database server The name of the database The username The password

Several ways to connect to MySQL database The command line phpMyAdmin SQLyog

Page 4: Session 5: Working with MySQL iNET Academy Open Source Web Development

Accessing DB from the Command Line Using MySQL command-line client. First, open the OS command line

Type mysql -h hostname –u user -p

Page 5: Session 5: Working with MySQL iNET Academy Open Source Web Development

Prompts & Commands

At the MySQL prompt, user can enter database command followed by Enter

To see the list of commands that MySQL supports, type help or \h Eg: show databases, use

Page 6: Session 5: Working with MySQL iNET Academy Open Source Web Development

Managing the Database

Creating Users

Creating users need root permission Creating a database Using a database

Page 7: Session 5: Working with MySQL iNET Academy Open Source Web Development

Using phpMyAdmin

Go to the website http://www.phpmyadmin.net/ Download the package phpMyAdmin 2.11.7…..zip Unpack to the C:\www\myadmin Create folder C:\www\myadmin\config In your web browser,

navigate to

http://localhost/myadmin

/scripts/setup.php

Page 8: Session 5: Working with MySQL iNET Academy Open Source Web Development

Using phpMyAdmin (cont.)

In the Servers

section, click the Add

button. The Server

setup page displays

as following

Page 9: Session 5: Working with MySQL iNET Academy Open Source Web Development

Using phpMyAdmin (cont.)

Most of the default values can be left alone. You do need to enter the password for the root MySQL user in the “Password for config auth” field.

Select “cookie” from Authentication type to limit access to your MySQL data to only users with a MySQL account.

Click Add Click “Save” from the Configuration section to save your changes to the

configuration file. Copy the config.inc.php file tp myadmin. Remove the config directory

Page 10: Session 5: Working with MySQL iNET Academy Open Source Web Development

Using phpMyAdmin (cont.)

In your web browser, navigate to http://localhost/myadmin. Your web browser displays a login page as following

Page 11: Session 5: Working with MySQL iNET Academy Open Source Web Development

Using SQLyog

Go to the \\dc computer and download the SQLyog519.exe Install SQLyog using Installation Wizard Run the SQLyog program Type the root’s password Click “Connect”

Page 12: Session 5: Working with MySQL iNET Academy Open Source Web Development

Using SQLyog (cont.)

The main window of SQLyog displays as following

Page 13: Session 5: Working with MySQL iNET Academy Open Source Web Development

Database Concept & SQL

Database are repository of structured information SQL: Structured Query Language is the language used to manage and

manipulate data stored in relational database

Page 14: Session 5: Working with MySQL iNET Academy Open Source Web Development

Creating tables

The title_id column is an integer. The auto_increment keyword make this field’s value automatically is assigned an unique value during row insertion.

The title column holds text up to 150 characters The pages column is an integer The PRIMARY KEY attribute tells MySQL which fields is the key value

Page 15: Session 5: Working with MySQL iNET Academy Open Source Web Development

Creating Tables (cont.)

To verify a table, use DESCRIBE

Page 16: Session 5: Working with MySQL iNET Academy Open Source Web Development

Adding Data to Table

Use INSERT command. Syntax: INSERT INTO table COLUMNS ([columns]) VALUES ([values]);

Rules: Numeric values shouldn’t be quoted String values should always be quoted Date and time value should always be quoted Function shouldn’t be quoted NULL should never be quoted

Page 17: Session 5: Working with MySQL iNET Academy Open Source Web Development

Table Definition Manipulation

Renaming a table: ALTER TABLE table RENAME new_table

Changing a column’s data type: ALTER TABLE table MODIFY column datatype

Changing the order of columns:

Renaming a column: ALTER TABLE table CHANGE column_name new_column_definition

Removing a column: ALTER TABLE table DROP column

Deleting an entire table: DROP TABLE table

Page 18: Session 5: Working with MySQL iNET Academy Open Source Web Development

Querying the Database

SELECT columns FROM tables [WHERE CLAUSE]; [ORDER BY CLAUSE]

Page 19: Session 5: Working with MySQL iNET Academy Open Source Web Development

Querying the Database (cont.)

Limit results with WHERE If there is more than

one condition, logical operators

Like AND or OR are used

Page 20: Session 5: Working with MySQL iNET Academy Open Source Web Development

Querying the Database (cont.)

Specifying the order

Join tables together

Page 21: Session 5: Working with MySQL iNET Academy Open Source Web Development

Querying the Database (cont.)

Natural joins

Join on

Alias

Page 22: Session 5: Working with MySQL iNET Academy Open Source Web Development

Modifying & Deleting Database Data

Use UPDATE command to modify the data

USE DELETE command to delete the data

Page 23: Session 5: Working with MySQL iNET Academy Open Source Web Development

Search Functions

Use the LIKE operator The % sign indicates that anything can match

The _ sign indicates that exact one character can match

Page 24: Session 5: Working with MySQL iNET Academy Open Source Web Development

Logical Operators

Use AND, OR and NOT operators in WHERE clause

Page 25: Session 5: Working with MySQL iNET Academy Open Source Web Development

Practice In this practice, you will

Create a table named “user” which have following fields ID, integer, PrimaryKey, auto_increment Name, varchar (100) Gender, tinyint (1) Email, varchar (100)

Insert 5 users into the table “user” Create a table named “user_log” which have following fields

user_id, integer time_of_login time_of_logout number_of_pages_view

Insert login and logout information of 5 users above Find the user detail based on time_of_login or time_of_logout Find the user have the maximum number_of_pages_view Find the user have the maximum login times Practice using the LIKE operator