SQL CREATE TABLE Statement - Syntax, Examples

CREATE TABLE

Every database begins with a table. A table is where your data lives. In SQL, the CREATE TABLE statement helps you define this storage space — specifying what kind of data you’ll collect, and how it should behave. It's like designing a form before collecting responses.

Syntax of CREATE TABLE

CREATE TABLE table_name (
  column1 datatype constraint,
  column2 datatype constraint,
  ...
);

Let’s break it down:

  • table_name: Name of your table (e.g., students)
  • column: Name of each field (e.g., roll_no, name)
  • datatype: Defines the kind of value stored (e.g., INT, VARCHAR)
  • constraint: Optional rules (e.g., PRIMARY KEY, NOT NULL)

1. Creating a Basic Students Table

Let’s design a table to store student details at a school in India.

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  class VARCHAR(10),
  age INT,
  city VARCHAR(30)
);

Explanation

  • roll_no uniquely identifies each student. Marked as PRIMARY KEY.
  • name cannot be empty. Hence, NOT NULL.
  • class, age, and city store remaining student information.

2. Creating a Table with Default Values

We can predefine values using DEFAULT. Suppose most students belong to ‘Hyderabad:

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  class VARCHAR(10),
  age INT,
  city VARCHAR(30) DEFAULT 'Hyderabad'
);

Best Practices for CREATE TABLE

  • Always define a PRIMARY KEY to uniquely identify rows.
  • Use NOT NULL to prevent blank values in critical columns.
  • Use CHECK and DEFAULT constraints to control behavior.

QUIZ

Question 1:Which of the following SQL statements correctly creates a table to store student data?

Question 2:You must specify a data type for each column when using the CREATE TABLE statement.

Question 3:Which of the following are true about the CREATE TABLE statement?

Question 4:What is the result of executing the following statement?
CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50), age INT);

Question 5:A table in SQL can be created without specifying any primary key or constraints.

Question 6:Which of the following examples correctly include constraints in a CREATE TABLE statement?


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...