(Lesson 8) PhpMyAdmin
(Lesson 8) PhpMyAdmin
using phpMyAdmin
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
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!
works Dept
Salary EMPLOYEE DEPARTMENT
for Name
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
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”
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
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