Guide to SQL CREATE TABLE Statement and Table Operations

Abhishek Kumar Last Updated : 24 Jun, 2024
5 min read

Introduction

Imagine a filing cabinet for data, with drawers for different categories of information. The “CREATE TABLE” statement in SQL is like building a new drawer in that cabinet. You give it a name and define how you’ll organize the information inside, like separate sections for names, dates, and amounts. SQL also lets you copy existing drawers to make new ones, which is helpful if you need a similar filing system but with some changes.

Overview

  • The SQL CREATE TABLE statement allows you to set up new tables in a database, akin to creating a new drawer in a filing cabinet to store organized information.
  • With CREATE TABLE, you specify the table name, columns, data types, and optional constraints, ensuring each section of your drawer (table) holds the right data.
  • A practical example is creating an ‘Employees’ table with various columns, such as EmployeeID, FirstName, LastName, BirthDate, Salary, and DepartmentID, each with specific data types and constraints.
  • SQL provides a suite of operations for managing tables, including querying data (SELECT), inserting records (INSERT INTO), updating records (UPDATE), deleting records (DELETE FROM), and altering table structures (ALTER TABLE).
  • You can create new tables from existing ones using methods like CREATE TABLE … AS SELECT to copy structure and data or CREATE TABLE … LIKE to replicate only the structure.

Understanding the SQL CREATE TABLE Statement

The CREATE TABLE statement is pivotal in SQL, enabling users to establish the structure of a new table within a database. Let’s explore its syntax and components:

CREATE TABLE table_name (

    column1 datatype constraints,

    column2 datatype constraints,

    ...

    columnN datatype constraints

);
  • CREATE TABLE: This is like instructing to build a new data organizer, similar to a filing cabinet drawer.
  • table_name: This is the name you give to your organizer so you can easily refer to it later.
  • () : Imagine this like the inside of your drawer, where you specify how things will be organized.
  • Column 1, column 2, …: These are separate sections you create inside the drawer to hold specific kinds of information. 
  • Datatype: This is like defining what information can go in each section of your organizer. Think of it like using folders for letters in one section and envelopes for bigger documents in another. Here are some common data types:
  • INT: For whole numbers (like ages or counts).
  • VARCHAR: For text with a variable length (like names or addresses).
  • DATE: For storing dates.
  • FLOAT: For numbers with decimal places (like prices or measurements).
  • Constraints (optional): You can set rules for storing information in your organizer. They help keep things neat and organized:
  • NOT NULL: This is like saying a section must always contain some information, like a name can’t be blank.
  • UNIQUE: This ensures no two-column entries have the same value, like no duplicate social security numbers.
  • PRIMARY KEY: This serves as a unique identifier for each row in the table, similar to an ID number assigned to each customer.

Create a Sample Table

Let’s create a sample table named Employees:

CREATE TABLE Employees (

    EmployeeID INT PRIMARY KEY,

    FirstName VARCHAR(50) NOT NULL,

    LastName VARCHAR(50) NOT NULL,

    BirthDate DATE,

    Salary DECIMAL(10, 2),

    DepartmentID INT

);
insert into employees values(1,'Abhi','Shek','2024-01-01',10.0,1)
insert into employees values(2,'Ron','Swanson','2023-01-01',5.0,2);

In this example, we have:

  • EmployeeID is an integer column serving as the primary key.
  • FirstName and LastName are VARCHAR columns for storing employee names with NOT NULL constraints.
  • BirthDate is a DATE column for storing employee birth dates.
  • Salary is a DECIMAL column to store salaries with a precision of 10 digits and 2 decimal places.
  • DepartmentID is an integer column for recording department identifiers.

Performing Operations on Tables

Once a table is created, SQL offers various operations to manage data:

  • Querying data: Retrieve information using SELECT statements.
  • Inserting records: Populate tables using INSERT INTO statements.
  • Updating records: Modify existing data with UPDATE statements.
  • Deleting records: Remove unwanted data using DELETE FROM statements.
  • Altering table structure: Modify the table schema using ALTER TABLE statements.

Creating Tables from Existing Tables

In SQL, you can create new tables based on existing tables to replicate or modify schema structures. There are two primary methods:

Using CREATE TABLE … AS SELECT

This method allows you to create a new table based on the result set of a SELECT query from an existing table:

CREATE TABLE new_table_name

AS

SELECT column1, column2, ...

FROM existing_table_name;

Example:

To create a table EmployeesBackup with the same structure and data as Employees:

CREATE TABLE EmployeesBackup

AS

SELECT *

FROM Employees;

Using CREATE TABLE … LIKE

This method creates a new table with the same structure as an existing table, but without copying data:

CREATE TABLE new_table_name
LIKE existing_table_name;

Example:

To create an empty table, EmployeesCopy with the same structure as Employees:

CREATE TABLE EmployeesCopy
LIKE Employees;

Conclusion

Mastering the CREATE TABLE statement and its variations in SQL is crucial for effective database management and application development. These statements allow for precise control over data structures and enable seamless replication and modification of table schemas. By understanding these fundamentals, SQL users gain essential skills for efficiently organizing and manipulating data within relational databases. Always ensure compatibility with your specific SQL dialect and database system when applying these operations.

Frequently Asked Questions

Q1. What is the SQL CREATE TABLE statement used for?

A. The CREATE TABLE statement in SQL creates a new table within a database. It specifies the table’s name, columns with their data types, and optional constraints that enforce rules on the data.

Q2. How do I create a table based on an existing table in SQL?

A. You can create a table based on an existing table using two primary methods:

1. CREATE TABLE … AS SELECT: Copies the structure and data from an existing table.

CREATE TABLE NewTableASSELECT * FROM ExistingTable;

2. CREATE TABLE … LIKE: This method copies the structure and the data from an existing table.

Q3. What are the constraints in SQL CREATE TABLE statements?

A. Constraints are rules applied to columns to enforce data integrity. Common constraints include:
1. NOT NULL: Ensures a column cannot contain NULL values.
2. PRIMARY KEY: Uniquely identifies each row in the table.
3. UNIQUE: Ensures all values in a column are distinct.
4. FOREIGN KEY: Establishes relationships between data in different tables.
5. CHECK: Validates values in a column based on a specified condition.
6. DEFAULT: Provides a default value for a column when no value is specified during insertion.

Q4. What operations can I perform on tables after creation?

A. After creating a table, common operations include:
1. Querying data: Retrieving information using SELECT statements.
2. Inserting records: Adding new data using INSERT INTO statements.
3. Updating records: Modifying existing data with UPDATE statements.
4. Deleting records: Removing unwanted data using DELETE FROM statements.
5. Altering table structure: Modifying the table’s schema using ALTER TABLE statements to add, modify, or delete columns.

Q5. Are there any differences in the syntax or behavior of CREATE TABLE among different SQL databases?

A. Imagine different brands of phones, like Apple and Samsung. They both let you call and text, but how you use the buttons and menus might be slightly different. SQL is similar to different database systems like MySQL and SQL Server. They let you get information from and update data, but the exact wording and features might vary slightly. To be sure you’re using things correctly, it’s best to check the manual for your specific database system, like how you’d consult the phone’s instructions.

Hello, I'm Abhishek, a Data Engineer Trainee at Analytics Vidhya. I'm passionate about data engineering and video games I have experience in Apache Hadoop, AWS, and SQL,and I keep on exploring their intricacies and optimizing data workflows 

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details