Databases, Tables & Data Types

📄 MySQLLesson 3Beginner

Tables hold your database records. When designing tables, you must declare appropriate data types for each column to optimize storage and validate values.

1 Creating a Table
SQL — Table Definition
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    email VARCHAR(100) NOT NULL,
    age INT DEFAULT 18,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

# Show fields of a table
DESCRIBE users;
2 Common Data Types
Type ClassDatatypeDescription
NumericINT, DECIMAL(10,2)Integers & exact fixed point fractions
StringVARCHAR(len), TEXTVariable strings & large blocks of text
Date/TimeDATE, TIMESTAMPCalendar date & timezone-aware time
3 Code Challenge
Challenge: Write a SQL command creating a table named products containing fields: id (int), name (varchar), price (decimal for money values), and stock (int).