CREATE TABLE
Build a brand-new, empty table with CREATE TABLE — name it, list its columns and types, and add rules.
Every IPL database starts empty. Before you can record players, matches or scores, you need tables to hold them. CREATE TABLE is the command that builds a new table from nothing.
Definition
CREATE TABLE is a DDL statement that creates a new, empty table in the database by defining its name, its columns, the data type of each column and, optionally, constraints (rules) on those columns.
- New — the table must not already exist; otherwise SQL Server raises "There is already an object named 'players' in the database."
- Empty — a freshly created table has 0 rows.
CREATEbuilds the container only; you fill it later withINSERT. - Columns + data types — every column needs a name and a type.
- Constraints — optional rules like
PRIMARY KEYandNOT NULLthat protect the data.
Syntax
sql
CREATE TABLE table_name (
column1 data_type [constraint],
column2 data_type [constraint],
...
[CONSTRAINT constraint_name PRIMARY KEY (column)]
);| Part | Meaning |
|---|---|
CREATE TABLE | Keywords: "build a new table" |
table_name | Name of the new table, e.g. players (must be unique in the schema) |
( ... ) | The column list goes inside round brackets |
column data_type | One column definition: its name followed by its data type |
, | A comma between column definitions (no comma after the last one) |
[constraint] | Optional rule for that column, e.g. NOT NULL |
CONSTRAINT name PRIMARY KEY (col) | Optional table-level rule with a name you choose |
; | Ends the statement |
Step by step
- Name the table —
players - List the columns + data types —
id INT,name VARCHAR(50), … - Add rules (constraints) —
NOT NULL,PRIMARY KEY
Example — the course's players table
This is the exact statement from the course slides:
sql
CREATE TABLE players (
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
team VARCHAR(10),
country VARCHAR(30),
runs INT,
CONSTRAINT pk_players PRIMARY KEY (id)
);Result — a brand-new, empty table:
| id 🔑 (INT) | name (VARCHAR(50)) | team (VARCHAR(10)) | country (VARCHAR(30)) | runs (INT) |
|---|---|---|---|---|
| (0 rows — empty table) |
How it works:
idandnameareNOT NULL— every player must have them.team,countryandrunshave no rule, so they may be left empty (NULL).CONSTRAINT pk_players PRIMARY KEY (id)makesidthe primary key (the 🔑): every player gets a unique id.pk_playersis just the name we gave to this rule.
Note: In the playground, a
playerstable already exists (it's part of the IPL practice database). So the runnable examples below create new tables likematchesandplayers_new. If you really want to rebuildplayers,DROPit first.
Example — a matches table
sql
CREATE TABLE matches (
match_id INT NOT NULL,
match_date DATE NOT NULL,
home_team VARCHAR(10) NOT NULL,
away_team VARCHAR(10) NOT NULL,
venue VARCHAR(50),
CONSTRAINT pk_matches PRIMARY KEY (match_id)
);
INSERT INTO matches (match_id, match_date, home_team, away_team, venue)
VALUES (1, '2025-03-22', 'KKR', 'RCB', 'Eden Gardens');
SELECT * FROM matches;| match_id | match_date | home_team | away_team | venue |
|---|---|---|---|---|
| 1 | 2025-03-22 | KKR | RCB | Eden Gardens |
How it works: CREATE TABLE built the empty structure, INSERT added one row and SELECT read it back.
Naming good tables and columns
- Use lowercase with underscores (
snake_case):player_profiles,match_date. - Use clear, meaningful names:
runs, notr. - Avoid spaces and reserved words (
order,user,date,table) — they need[square brackets]in SQL Server and cause confusion. - Be consistent: either always singular (
player) or always plural (players).
Common mistakes
- ❌ A comma after the last column:
runs INT,)→ syntax error. ✅ No comma before the closing bracket. - ❌ Forgetting the data type:
name NOT NULL. ✅name VARCHAR(50) NOT NULL. - ❌ Creating a table that already exists → "There is already an object named …". ✅ Pick a new name or
DROP TABLEfirst. - ❌ Expecting rows after
CREATE TABLE. ✅ A new table is always empty — useINSERT. - ❌ Using
"double quotes"around text values later. ✅ Text values use single quotes:'RCB'.
SQL Server vs MySQL / PostgreSQL
"Create only if it doesn't exist" is written differently:
| Database | Syntax |
|---|---|
| MySQL, PostgreSQL, SQLite | CREATE TABLE IF NOT EXISTS matches (...) |
| SQL Server | IF OBJECT_ID('dbo.matches', 'U') IS NULL CREATE TABLE matches (...) |
SQL Server tables also live inside a schema — the default is dbo, so players is really dbo.players.
Interview tip
Q: You run CREATE TABLE and then SELECT * from the new table. What do you see?
A: Only the column headers and zero rows. CREATE TABLE defines the structure; it never inserts data.
Key points to remember
- CREATE TABLE builds a new, EMPTY table (0 rows) — you add data later with INSERT.
- Inside the brackets, list each column as: name + data type + optional constraint, separated by commas.
- Three steps: name the table, list the columns with data types, add rules (constraints).
- CONSTRAINT pk_name PRIMARY KEY (col) defines a named primary key at table level.
- Creating a table that already exists raises an error — choose a new name or DROP it first.
- SQL Server has no CREATE TABLE IF NOT EXISTS; use IF OBJECT_ID(...) IS NULL instead.
- Use clear snake_case names and avoid reserved words like order, user and date.
Try it yourself
Create a matches table
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Rebuild the course's players table (drop the old one first)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
SQL Server: create only if it does not exist
IF OBJECT_ID('dbo.matches', 'U') IS NULL
CREATE TABLE dbo.matches (
match_id INT NOT NULL,
home_team VARCHAR(10) NOT NULL,
away_team VARCHAR(10) NOT NULL,
CONSTRAINT pk_matches PRIMARY KEY (match_id)
);SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Practice: create players_new
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
