Databases, Tables & Data Types
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 Class | Datatype | Description |
|---|---|---|
| Numeric | INT, DECIMAL(10,2) | Integers & exact fixed point fractions |
| String | VARCHAR(len), TEXT | Variable strings & large blocks of text |
| Date/Time | DATE, TIMESTAMP | Calendar 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).