0
0
SQLquery~5 mins

CREATE TABLE syntax in SQL

Choose your learning style9 modes available
Introduction
We use CREATE TABLE to make a new table in a database where we can store information in rows and columns.
When starting a new project and you need to organize data like users or products.
When you want to add a new category of data to your existing database.
When you need to create a temporary table to hold data for a specific task.
When designing a database to keep track of things like books in a library.
When setting up a database for a small business to store customer orders.
Syntax
SQL
CREATE TABLE table_name (
  column1 datatype [constraints],
  column2 datatype [constraints],
  ...
);
Each column needs a name and a datatype like INTEGER or TEXT.
Constraints like PRIMARY KEY or NOT NULL help keep data correct.
Examples
Creates a table named Students with three columns: ID, Name, and Age.
SQL
CREATE TABLE Students (
  ID INTEGER PRIMARY KEY,
  Name TEXT NOT NULL,
  Age INTEGER
);
Creates a Products table with an ID, name, and price.
SQL
CREATE TABLE Products (
  ProductID INTEGER PRIMARY KEY,
  ProductName TEXT,
  Price REAL
);
Creates an Orders table with order ID and date.
SQL
CREATE TABLE Orders (
  OrderID INTEGER PRIMARY KEY,
  OrderDate TEXT NOT NULL
);
Sample Program
This query creates a table called Employees with four columns to store employee details.
SQL
CREATE TABLE Employees (
  EmployeeID INTEGER PRIMARY KEY,
  FirstName TEXT NOT NULL,
  LastName TEXT NOT NULL,
  Email TEXT
);
OutputSuccess
Important Notes
Table names should be unique in the database.
Datatypes define what kind of data each column can hold.
Use PRIMARY KEY to uniquely identify each row.
Summary
CREATE TABLE makes a new table to store data.
You define columns with names and datatypes inside parentheses.
Constraints help keep your data organized and accurate.