Skip to main content

How do you create a table in SQL?

To create a table in SQL, use the CREATE TABLE command. It defines the table's name, its fields, their data types, and constraints (such as a primary key).

Syntax

sql
CREATE TABLE table_name ( field1 data_type constraints, field2 data_type constraints, ... PRIMARY KEY (field1) );

Example: creating an Employees table

sql
CREATE TABLE Employees ( id INT PRIMARY KEY, -- unique identifier name VARCHAR(50) NOT NULL, -- employee name, required age INT, -- age department_id INT -- reference to a department );

Explanation

  • id INT PRIMARY KEY is the primary key, unique for every record.
  • name VARCHAR(50) NOT NULL is a text field up to 50 characters, required.
  • The remaining fields can be filled in or left empty.

Once this query runs, the table is created in the database and ready to have data added with INSERT.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.