0% found this document useful (0 votes)
16 views

(Lesson 8) PhpMyAdmin

Uploaded by

aastabdelrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

(Lesson 8) PhpMyAdmin

Uploaded by

aastabdelrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Creating MySQL databases

using phpMyAdmin

Dr. Ali Allam Web Programming (BIS317E)


Prerequisites

 This remaining part of this course assumes that you have prior
knowledge and good awareness of:
 Entity-Relationship Diagrams (ERD)
 MySQL Database Management System
 Structured Query Language (SQL)
 HTML and PHP, of course ☺
What is

 phpMyAdmin is a free software tool written in PHP, intended to


handle the administration of MySQL over the Web.
 Through this web-based software you can create, alter, drop, delete,
import and export MySQL database tables.
 You can run MySQL queries, and execute other database
management commands such as insert, delete, update, etc.
Starting up

 So, we'll create our MySQL database using the phpMyAdmin web-based
software.
 If you installed XAMPP, then you can go and start the MySQL module.
Starting up

 You can open the phpMyAdmin software, using one of the following ways:
1. Type the following address in your browser: http://localhost/phpMyAdmin/
2. Or, click on the Admin button of the MySQL module
Important Note

 You must change the URL address, if you are using a server port number
(i.e. apache) other than the default port number 80.

 For example, if your Apache port number is 8080, you should edit the URL
address as follows:
http://localhost:8080/phpMyAdmin/
started up!

 In some versions of phpMyAdmin, you may get


a login screen.
 Type root as the username, and leave the
password blank.
 Then, click the Go button, to get the screen
shown on the next slide.
Note: If you can't see this page, then either MySQL is not installed, or it is not
configured properly. To fix this, you need to refer to a documentation or to
the technical support team in room (B007).
A Simple Database: Two Tables (1-to-many)

Business Rule: An employee must work at exactly one department.


A department has at least one employee.
Dept
Emp ID Dept No Num Location
Gender

works Dept
Salary EMPLOYEE DEPARTMENT
for Name

Name Username Phone


Password Extension
Create your first MySQL database

 You can create all of your database tables and queries using PHP code,
and that’s what you’ll learn at a later lesson, though. But before doing that,
it's a good idea to get an understanding of just what it is you'll be creating.
 So start your server (i.e. apache) as well as phpMyAdmin, if you haven't
already. There are two ways to create your first database:
1. Click on the left-side of
phpMyAdmin.
2. Or, click on the right-
side of phpMyAdmin.
Create your first MySQL database

 Type a name for your database, where it says


"Create database". Click the "Create" button.
You will be taken to a new area.

 In this new area, you can


create a Table to go in your
database. At the moment, as
it says, there are No tables
found in the database. But
the database itself has been
created.
1. Create Table Employee
Create table “Employee”

 Name: Employee
 Number of columns: 7
1. EmpID
2. Name
3. Username
4. Password
5. Salary
6. Gender
7. DeptNo
 Then press the “Go” button...
Create table “Employee”
 For each of the 7 fields, specify the Name, Type and Length/Values.
Create table “Employee”
 Specify the primary key of this table. Add PRIMARY index to EmpID.
Create table “Employee”
 The shown message box pops up. Click Go.
Create table “Employee”
 When you’re finished. Press on “Save” at the bottom of the page.

 You can also click “Preview SQL” if you wish to display the SQL statement
used to create the Employee table.
2. Create Table Department
(same steps again)
Create table “Department”

 Name: Department
 Number of columns: 4
1. Dept Num
2. Name
3. Location
4. Phone Ext
 Then press the “Go” button...
Create table “Department”
 For each field (attribute), specify its Name, Type and Length/Values.
Create table “Department”
 Specify the primary key of this table. Add PRIMARY index to Dept Num.
Create table “Department”
 The shown message box pops up. Click Go.
Create table “Department”
 When you finish. Press on “Save” at the bottom of the page.

 You can also click “Preview SQL” if you wish to display the SQL statement
used to create the Department table.
3. Create a relationship
Add a Relationship

 Now, we will create a relationship between the two tables, by linking the foreign key
in Employee (Dept No) to the corresponding primary key of Department (Dept Num).
 Click on the “Structure” of table Employee (i.e. the table that contains the foreign key).
Add a Relationship
 Click on the “Relation view” tab.
 Link the foreign key in this table (DeptNo) to the primary key in the
department table (Dept Num), as shown.
Add a Relationship
 When you finish. Press on “Save” at the bottom of the page.

 Again, you can click “Preview SQL” if you wish to display the SQL
statement used to alter this table.
4. Enter some records into the
“Department” table
Adding records to table “Department”
 To insert a new record into the table you created in the previous section,
click on the Insert link.
Adding records to table “Department”

 Type the value of each


field (attribute) in the
corresponding textbox.
Then, click Go.
 By default, you can enter
two records at a time. You
can also change this. At
the end of the page,
specify the number of
rows to be inserted at a
time.
Adding records to table “Department”

 Click on Browse to
view/edit/delete the
records inserted into this
table.
Enter some records into the “Employee” table
(same steps again)
Adding records to table “Employee”
 After inserting records, click on Browse to view/edit/delete these records.
Let’s add more records using the
INSERT SQL statement
SQL Statement: INSERT
 Syntax:
INSERT INTO `TableName` (`field1`, `field2`, `field3`) VALUES (value1,value2, value3)
 Examples:
INSERT INTO `employee` (`EmpID`, `Name`, `Username`, `Password`, `Salary`, `Gender`,
`DeptNo`) VALUES ('2252', 'Omar Khaled', 'omar.khaled', 'roma_98', '8086', 'Male', '210')
INSERT INTO `department` (`Dept Num`, `Name`, `Location`, `Phone Ext`) VALUES ('510',
'Graphical Design', 'Building A, First floor, Room 101', '118')
 Hey ! Note the difference between this single quote: ` and this one: ' .
 The first one is used to include the `table name` as well as the `fields names`.
 The second single quote is used to include the 'fields values'.
 Note the difference between this single quote: ` and this one: ' .
 Here’s where you can usually find these two buttons on your keyboard (some
keyboards may differ):
INSERT Statement

 Click on the “SQL” tab at the


top of the page.
 Type your SQL statement(s).
 Click on “Go” button at the
bottom of the page, to run
your SQL statement.
SQL Statement: INSERT
 These two records were successfully inserted into the two tables, as shown.
Important note to remember about INSERT
 It is very important to set a value for the foreign key that matches one of
the primary keys at the parent table. For example, the following SQL
statement can’t be executed:
This error occurred
because the foreign
key (DeptNo) is set
to 1, although this
department number
does not exist in the
referenced table
(department).
Now, let’s retrieve records using the
SELECT SQL Statement
SQL Statement: SELECT

 Syntax:
SELECT `field1`, `field2`, `field3` FROM `TableName` WHERE condition(s)
 Examples:
1- Retrieve all data about all employees
SELECT * FROM `employee`
2- Retrieve name and location of all departments
SELECT `Name`, `Location` FROM `department`
3- Retrieve names and salaries of employees whose salary is above 7000
SELECT `Name`, `Salary` FROM `employee` WHERE `Salary` >= 7000
4- Retrieve all data about male employees who work at department number 110
SELECT * FROM `employee` WHERE `gender` = 'Male' AND `deptno` = 110
Nested SELECT Statements

 Example:
Retrieve all data about employees working at the finance department
SELECT * FROM `employee` WHERE `deptno` = (SELECT `dept num` FROM
`department` WHERE `Name` = 'Finance')

 Reminder ! Note the difference between this single quote: ` and this one: ' .
 The first one is used to include the `table name` as well as the `fields names`.
 The second single quote is used to include the 'fields values'.
How to Export the database

 It is usually very useful to export and import the database that you have
built. This database which contains the tables, relationships, records, etc.
can be exported in an SQL format.
1. Select the desired database, and then click on the Export tab.
2. Press on the GO button, and then save your exported SQL file to your PC.
How to Export the database
How to Import the database

 The exported SQL file is considered a database backup which can be


eventually imported when needed.
 The SQL file that you created can be imported in the other way around.
1. First, create a new database where you want to have the SQL file imported
to.
2. Click on the Import tab, and then browse and select the SQL file that you will
import, and finally press on the GO button.
How to Import the database

You might also like