CREATING AND
DROPPING TABLES IN
MYSQL USING PHP
Introduction
Objective: Understand how to create and drop tables
in MySQL using PHP.
Key Concepts:
SQL commands in PHP
Connection and error handling
Creating a MySQL Table
Syntax:
sql
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
Example:
Creating a "Users" table with columns for ID,
username, and email.
Creating a Table in PHP
To create a table, use the CREATE TABLE SQL command inside a PHP script.
Example: Creating a users table with columns for id, username, and email.
php
$sql = "CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Table 'users' created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
Dropping a MySQL Table
Syntax:
sql
DROP TABLE table_name;
Purpose: Deletes the table and all its data.
Dropping a table removes it permanently, along with all its data.
Use the DROP TABLE SQL command within PHP to delete a table.
php
$sql = "DROP TABLE users";
if ($conn->query($sql) === TRUE) {
echo "Table 'users' dropped successfully";
} else {
echo "Error dropping table: " . $conn->error;
}
CONCLUSION
Recap:
Creating tables helps store structured data.
Dropping tables removes tables and their data
permanently.
Best Practices:
Test SQL commands carefully.
Avoid dropping tables unless necessary.
THANK YOU
DAVID 22-UCS-014