๐ ๏ธ SQL CREATE TABLE โ Define a New Table Structure
๐งฒ Introduction โ What is SQL CREATE TABLE?
The CREATE TABLE statement in SQL is used to define the structure of a new table in a database. It specifies the table name, column names, data types, and optional constraints like primary keys, uniqueness, or defaults.
๐ฏ In this guide, youโll learn:
- Syntax of CREATE TABLE
- How to define columns and data types
- How to add constraints like PRIMARY KEY,UNIQUE, andDEFAULT
- Common best practices for schema design
โ 1. Basic CREATE TABLE Syntax
CREATE TABLE table_name (
  column1 datatype [constraint],
  column2 datatype [constraint],
  ...
);
โ Defines the table and its columns in one command.
๐ 2. Example โ Create a Customers Table
CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100) UNIQUE,
  created_at DATE DEFAULT CURRENT_DATE
);
โ
 Includes PRIMARY KEY, UNIQUE, NOT NULL, and DEFAULT constraints.
๐ 3. Common Constraints
| Constraint | Description | 
|---|---|
| PRIMARY KEY | Uniquely identifies each row | 
| NOT NULL | Column must have a value | 
| UNIQUE | No duplicate values allowed | 
| DEFAULT | Sets a default value if none is provided | 
| CHECK | Ensures values meet a condition | 
| FOREIGN KEY | Establishes a link to another table’s key | 
๐งฉ 4. Example โ Create Table with Foreign Key
CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT,
  amount DECIMAL(10,2),
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);
โ
 Links orders.customer_id to customers.id.
๐งฑ 5. Data Type Examples
| Type | Description | 
|---|---|
| INT | Integer numbers | 
| VARCHAR(n) | Variable-length string | 
| DATE | Calendar date | 
| DECIMAL(x,y) | Fixed-point number | 
| BOOLEAN | True or False (some DBMS) | 
๐ Best Practices
| โ Recommended | โ Avoid This | 
|---|---|
| Use clear, lowercase table/column names | Naming inconsistently | 
| Include PRIMARY KEYin every table | Forgetting identifiers | 
| Use appropriate data types | Overusing VARCHARorTEXT | 
| Add comments or documentation | Leaving schema unannotated | 
๐ Summary โ Recap & Next Steps
CREATE TABLE is the foundation of SQL database design. It sets up how data is stored and validated, providing structure and consistency.
๐ Key Takeaways:
- Use CREATE TABLEto define new table schemas
- Choose correct data types and apply constraints
- Always define primary keys for relational integrity
โ๏ธ Real-World Relevance:
Used in application setup scripts, ETL pipelines, schema migrations, and data warehousing.
โก๏ธ Next: Learn ALTER TABLE to modify existing table structures.
โ FAQ โ SQL CREATE TABLE
โ Can I create a table without a primary key?
โ Yes, but it’s not recommended. Primary keys ensure row uniqueness.
โ What is the difference between VARCHAR and TEXT?
โ
 VARCHAR(n) limits length; TEXT allows unlimited text (often less index-friendly).
โ Can I set default values?
โ
 Yes. Use DEFAULT in column definitions.
โ Can I define multiple constraints in one table?
โ Yes. Constraints can be defined inline or at the bottom of the column list.
Share Now :
