Courses/SQL/Database Basics & Tables

    Lesson 2 โ€ข Beginner

    Database Basics & Tables

    Create databases, design tables with the right data types, and insert your first rows of data.

    ๐ŸŽฏ What You'll Learn

    • Create databases and tables with CREATE statements
    • Choose the right data types for each column
    • Use constraints: PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT
    • Insert data and modify table structure with ALTER

    Database Structure

    A relational database organizes data into tables. Each table has columns (the attributes) and rows (the records). Think of it as a well-organized spreadsheet that can handle millions of rows efficiently.

    ๐Ÿข Real-World Analogy

    A database is like an office building. Each floor (table) has rooms (rows), and each room has specific features (columns) โ€” room number, size, occupant name. The building manager (SQL) can find any room instantly.

    Create a Database & Table

    Build your first database and student table from scratch

    Try it Yourself ยป
    SQL
    -- Create a new database
    CREATE DATABASE school;
    
    -- Switch to the database
    USE school;
    
    -- Create your first table
    CREATE TABLE students (
        id INT PRIMARY KEY,
        name VARCHAR(100) NOT NULL,
        age INT,
        grade DECIMAL(3,1),
        enrollment_date DATE
    );

    Common Data Types

    TypeUse ForExample
    INTWhole numbersage, quantity, id
    VARCHAR(n)Variable text up to n charsname, email
    DECIMAL(p,s)Precise decimalsprice, salary
    DATEDate values2024-03-15
    BOOLEANTrue/Falseis_active
    TEXTLong textdescription, bio

    โš ๏ธ Common Mistake

    Using VARCHAR(255) for everything. Choose the right type โ€” use INT for numbers, DATE for dates. Wrong types waste storage and cause bugs.

    Table Constraints

    Constraints are rules that enforce data quality:

    PRIMARY KEY

    Uniquely identifies each row. Every table should have one. No duplicates, no NULLs.

    NOT NULL

    Column must always have a value โ€” can't be left empty.

    UNIQUE

    No two rows can have the same value in this column (e.g., email addresses).

    DEFAULT

    Automatically fills in a value if none is provided.

    ๐Ÿ’ก Pro Tip

    Use AUTO_INCREMENT (MySQL) or SERIAL (PostgreSQL) for primary keys. The database automatically generates unique IDs for you.

    Constraints in Action

    Create a table with PRIMARY KEY, NOT NULL, UNIQUE, and DEFAULT constraints

    Try it Yourself ยป
    SQL
    -- Create a table with constraints
    CREATE TABLE employees (
        employee_id INT PRIMARY KEY AUTO_INCREMENT,
        first_name VARCHAR(50) NOT NULL,
        last_name VARCHAR(50) NOT NULL,
        email VARCHAR(100) UNIQUE,
        department VARCHAR(50) DEFAULT 'General',
        salary DECIMAL(10,2),
        hire_date DATE DEFAULT CURRENT_DATE
    );
    
    -- View the table structure
    DESCRIBE employees;

    Insert & Modify Data

    Add rows to your table and alter its structure

    Try it Yourself ยป
    SQL
    -- Insert data into your table
    INSERT INTO students (id, name, age, grade, enrollment_date)
    VALUES
        (1, 'Alice Johnson', 20, 3.8, '2024-01-15'),
        (2, 'Bob Smith', 22, 3.2, '2023-09-01'),
        (3, 'Carol White', 19, 3.9, '2024-01-15');
    
    -- Verify the data
    SELECT * FROM students;
    
    -- Modify the table: add a new column
    ALTER TABLE students ADD COLUMN email VARCHAR(100);

    ๐Ÿ“˜ Quick Reference

    StatementPurpose
    CREATE DATABASECreates a new database
    CREATE TABLECreates a new table with columns
    INSERT INTOAdds new rows to a table
    ALTER TABLEModifies table structure
    DROP TABLEDeletes a table permanently

    ๐ŸŽ‰ Lesson Complete!

    You can now create databases, design tables with proper data types and constraints, and insert data. Next, you'll learn to retrieve data with the SELECT statement โ€” the most-used SQL command!

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service