MySQL Create Table
A table stores records in rows and fields in columns.
What is it?
A table stores records in rows and fields in columns.
Why do we use it?
Tables organize related data such as students or courses.
How does it work?
SQL CREATE TABLE defines columns, types and constraints.
Syntax
CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL );
Example
<?php
$conn = new mysqli("localhost", "root", "", "aifa_php_lab");
$sql = "CREATE TABLE IF NOT EXISTS students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL
)";
if ($conn->query($sql)) {
echo "Students table ready";
}
?>
Example Explained
More Examples
Add course VARCHAR(100), phone VARCHAR(20) and created_at TIMESTAMP columns.
⚠️ Important Rules
Choose appropriate data types and primary keys.
❌ Common Mistakes
Missing primary key or choosing unsuitable column sizes.
🌍 Real-World Use
Every database application organizes information into tables.
📝 Practice Exercise
Create students and courses tables.
💻 Try It Yourself
Write and run this PHP lesson inside the AIFA Coding Lab.