CREATE Table
The CREATE TABLE statement is used to define a new table in a database - specifying its name, columns, and the data type and constraints for each column.
Basic Syntax
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
city VARCHAR(50),
signup_date DATE
);
Common Data Types
- INT - whole numbers
- VARCHAR(n) - variable-length text up to n characters
- DATE / DATETIME - date and time values
- DECIMAL(p, s) - precise decimal numbers, useful for currency
- BOOLEAN - true/false values
Common Constraints
- PRIMARY KEY - uniquely identifies each row
- NOT NULL - column cannot be left empty
- UNIQUE - all values in the column must be different
- FOREIGN KEY - links a column to another table's primary key
Choosing the right data type and constraints upfront in CREATE TABLE prevents a lot of data-quality problems later - it's much harder to fix bad data than to prevent it.