Introduction:
Learn the fundamentals of managing MySQL databases with phpMyAdmin. This step-by-step guide covers creating databases, designing tables, inserting data, and executing basic SQL queries.
we will learn how to create a database, create a table, insert data, and execute basic SELECT, UPDATE, and DELETE queries using phpMyAdmin.
phpMyAdmin is a web-based tool written in PHP, designed to handle the administration of MySQL databases through a graphical interface.
Open your web browser and navigate to the phpMyAdmin URL.
http://localhost/phpmyadmin
Log in with your MySQL username and password.
You’ll see the data organized in rows and columns.
Click on the “SQL” tab in phpMyAdmin to open the SQL query editor.
To fetch data, you can use the SELECT statement.
sql
SELECT * FROM your_table_name;
Replace your_table_name with the actual table name.
To add new data, use the INSERT INTO statement.
sql
INSERT INTO your_table_name (column1, column2) VALUES ('value1', 'value2');
To update existing data, use the UPDATE statement.
sql
UPDATE your_table_name SET column1 = 'new_value' WHERE some_condition;
To delete data, use the DELETE FROM statement.
sql
DELETE FROM your_table_name WHERE some_condition;
You can execute multiple queries at once by separating them with semicolons.
After typing your query, click on the “Go” button to execute it.
Results (if any) will be displayed below the query editor.
Remember to be cautious when executing queries, especially those that modify data (INSERT, UPDATE, DELETE).
Always have a backup before making significant changes to your database.
let’s walk through the process of creating a database in phpMyAdmin and then performing some basic queries.
For this example, I’ll assume you have already installed and set up phpMyAdmin.
Open your web browser and navigate to your phpMyAdmin URL.
Log in with your MySQL username and password.
On the left side, you’ll see a list of existing databases.
To create a new one:
Inside your newly created database (example_db), click on the “SQL” tab to open the SQL query editor.
sql
CREATE TABLE users ( id INT PRIMARY KEY, username VARCHAR(50), email VARCHAR(100) );
Click the “Go” button to execute the query.
INSERT INTO users (id, username, email) VALUES
(1, ‘Omar_aboBakr’, ‘omar@example.com’),
(2, ‘gog_abobakr’, ‘gogo@example.com’);
Click the “Go” button to execute the query.
Go back to the “SQL” tab to run some SELECT queries.
SELECT * FROM users;
Click the “Go” button.
You should see a result set displaying the data you inserted.
Now, let’s update a user’s email address.
sql
UPDATE users SET email = 'omar_updated@example.com' WHERE id = 1;
Click the “Go” button.
Verify the update by running a SELECT query again.
Finally, let’s delete a user from the table.
DELETE FROM users WHERE id = 2;
Click the “Go” button.
Confirm the deletion by running a SELECT query once more.
Here’s a quiz about the lesson on using phpMyAdmin to create a database and execute basic queries.
Each question is followed by an explanation of the correct answer.
a) A programming language
b) A web-based tool for MySQL database administration
c) A database management system
Explanation: b) phpMyAdmin is a web-based tool for MySQL database administration.
a) Click on “Create” and enter the database name
b) Use the “Insert” tab to create a database
c) Click on “Databases” and enter the database name
Explanation: a) Click on “Databases” and then “Create database” to enter the database name.
a) CREATE TABLE students (id INT, name VARCHAR(50), age INT);
b) CREATE TABLE (id INT, name VARCHAR(50), age INT) students;
c) INSERT INTO students (id, name, age) VALUES ();
Explanation: a) The correct syntax for creating a table is CREATE TABLE table_name (column1 datatype, column2 datatype, …);.
a) Use the “Insert” tab in phpMyAdmin
b) Write an INSERT INTO SQL statement
c) Use the “Update” tab in phpMyAdmin
Explanation: b) Write an INSERT INTO SQL statement to insert data into a table.
a) It ensures data uniqueness in a column
b) It is used for aesthetic purposes
c) It represents the primary data type of a table
Explanation: a) The PRIMARY KEY ensures data uniqueness in a column and is used to uniquely identify each row in a table.
a) SELECT * FROM employees;
b) RETRIEVE * FROM employees;
c) GET ALL FROM employees;
Explanation: a) The correct SQL statement to retrieve all columns is SELECT * FROM table_name;.
a) Use the “Execute All” button
b) Separate queries with commas
c) Separate queries with semicolons
Explanation: c) Separate queries with semicolons to execute multiple SQL queries at once.
a) Adds new rows to a table
b) Deletes rows from a table
c) Modifies existing data in a table
Explanation: c) The UPDATE SQL statement is used to modify existing data in a table.
a) “Browse” tab
b) “SQL” tab
c) “Export” tab
Explanation: a) The “Browse” tab is used to view query results, and the “Export” tab is used to export results in various formats.
a) Use the “Undo” feature in phpMyAdmin
b) Have a backup of the database
c) Execute changes during off-peak hours
Explanation: b) Always have a backup of the database before executing significant changes.
a) REMOVE FROM table_name WHERE condition;
b) DELETE FROM table_name WHERE condition;
c) DROP FROM table_name WHERE condition;
Explanation: b) The correct SQL statement to delete data is DELETE FROM table_name WHERE condition;.
a) To specify the columns to be retrieved
b) To filter the rows that meet a specified condition
c) To order the result set
Explanation: b) The WHERE clause is used to filter rows based on a specified condition.
a) “Databases” tab
b) “SQL” tab
c) “Structure” tab
Explanation: c) The “Structure” tab in phpMyAdmin is used to create and modify database tables.
a) The arrangement of data in the database
b) The sorting and comparison rules for character data
c) The size of the database
Explanation: b) Collation in phpMyAdmin refers to the sorting and comparison rules for character data.
a) ALTER TABLE contacts ADD COLUMN phone VARCHAR(20);
b) ADD COLUMN phone TO contacts;
c) UPDATE TABLE contacts SET phone VARCHAR(20);
Explanation: a) The correct SQL statement to add a new column is ALTER TABLE table_name ADD COLUMN column_name datatype;.