ADVERTISEMENT

SQL CREATE TABLE

Creating tables is fundamental — tables store your data in rows and columns.

🔹 Basic Syntax

CREATE TABLE table_name (
  column1 datatype [constraints],
  column2 datatype [constraints],
  ...
);
  • table_name: Name of your table.
  • column1, column2: Columns with their data types.
  • Constraints like PRIMARY KEY, NOT NULL can be added.

🔹 Example

CREATE TABLE employees (
  employee_id INT PRIMARY KEY,
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  hire_date DATE,
  salary DECIMAL(10, 2)
);

Creates an employees table with 5 columns and constraints.

🔹 Notes by DBMS

  • Data types and syntax can slightly vary across DBMS.
  • In Oracle, use NUMBER instead of INT.
  • Add AUTO_INCREMENT (MySQL) or IDENTITY (SQL Server) for auto-generated IDs.

🧠 Quick Recap

Key PointExplanation
CommandCREATE TABLE with columns
Columns & Data TypesDefine structure of data
ConstraintsControl data integrity

💡 Always plan table structure carefully before creating it.

ADVERTISEMENT