๐Ÿ—๏ธ SQL Table & Database Management
Estimated reading: 2 minutes 28 views

๐Ÿ› ๏ธ 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, and DEFAULT
  • 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

ConstraintDescription
PRIMARY KEYUniquely identifies each row
NOT NULLColumn must have a value
UNIQUENo duplicate values allowed
DEFAULTSets a default value if none is provided
CHECKEnsures values meet a condition
FOREIGN KEYEstablishes 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

TypeDescription
INTInteger numbers
VARCHAR(n)Variable-length string
DATECalendar date
DECIMAL(x,y)Fixed-point number
BOOLEANTrue or False (some DBMS)

๐Ÿ“˜ Best Practices

โœ… RecommendedโŒ Avoid This
Use clear, lowercase table/column namesNaming inconsistently
Include PRIMARY KEY in every tableForgetting identifiers
Use appropriate data typesOverusing VARCHAR or TEXT
Add comments or documentationLeaving 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 TABLE to 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ› ๏ธ SQL CREATE TABLE

Or Copy Link

CONTENTS
Scroll to Top