FOUNDATION BEGINNER lesson-02

Setting Up MySQL & First Queries

🕑 30 min
📃 5 sections
🎓 Foundation
Before writing SQL, you need a database running. This lesson covers two paths: install MySQL locally for full power, or use db-fiddle.com in your browser for instant practice with zero setup. By the end you'll have a live database and your first table created and populated.

Setup Options

Option A — Local Install: MySQL Community Server (free download from mysql.com) + MySQL Workbench GUI. Best for real projects.

Option B — Browser: db-fiddle.com — free browser SQL sandbox, no install, choose MySQL 8.0. Perfect for this course.

Option C — Instant SQLite: SQLiteOnline.com — instant SQLite in browser, zero setup required.

💡
Recommended for this course
Use db-fiddle.com to follow along with zero setup. Switch to a local MySQL install when you start building real projects.

Database Commands

SQL
-- See all databases on the server
SHOW DATABASES;

-- Create a new database
CREATE DATABASE school_db;

-- Select database to use
USE school_db;

-- See current database
SELECT DATABASE();

-- Delete a database (CAREFUL — no undo!)
-- DROP DATABASE school_db;

CREATE TABLE

SQL
USE school_db;

CREATE TABLE students (
  id         INT           PRIMARY KEY AUTO_INCREMENT,
  name       VARCHAR(100)  NOT NULL,
  age        INT,
  email      VARCHAR(150)  UNIQUE,
  grade      VARCHAR(5),
  created_at TIMESTAMP    DEFAULT CURRENT_TIMESTAMP
);

-- Confirm it was created
SHOW TABLES;
DESCRIBE students;
⚠️
Danger: DROP TABLE
DROP TABLE permanently deletes the table and all its data. There is no recycle bin.

INSERT Rows

SQL
-- Insert one student
INSERT INTO students (name, age, email, grade)
VALUES ('Sara Ahmed', 17, 'sara@example.com', 'A');

-- Insert multiple rows (more efficient)
INSERT INTO students (name, age, email, grade) VALUES
  ('Ali Khan',      16, 'ali@example.com',    'B'),
  ('Fatima Zahra',  18, 'fatima@example.com', 'A+'),
  ('Omar Farooq',   17, 'omar@example.com',   'B+'),
  ('Nadia Hussain', 16, 'nadia@example.com',  'A');

-- View all students
SELECT * FROM students;

SHOW and DESCRIBE

SQL
-- List all tables in current database
SHOW TABLES;

-- See column structure of a table
DESCRIBE students;
-- or: SHOW COLUMNS FROM students;

-- See the CREATE TABLE statement
SHOW CREATE TABLE students;
🎤 Knowledge Check
1. Which command creates a new database?
CREATE DATABASE is the standard SQL command for creating a new database.
2. What does AUTO_INCREMENT do?
AUTO_INCREMENT auto-generates sequential unique IDs (1, 2, 3, ...) for new rows so you don't have to provide an ID manually.
3. Which command shows the structure of a table?
DESCRIBE tablename shows all columns, their data types, and constraints. You can also use SHOW COLUMNS FROM tablename.
4. What does NOT NULL constraint do?
NOT NULL requires that a value always be provided for that column — INSERT or UPDATE will fail if you try to leave it empty.
5. How do you insert 3 rows efficiently?
One INSERT with multiple value sets is more efficient — it uses a single transaction instead of 3 separate round trips to the database.