Skip to main content

SQL CREATE Statement

SQL CREATE Statement

The SQL CREATE statement is used to create database objects such as tables, indexes, views, and stored procedures.


1. Creating Tables

To create a new table in a database, you can use the CREATE TABLE statement followed by the table name and a list of columns with their data types and constraints.

Example:

// Example of creating a new table named "employees"
CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    department VARCHAR(50)
);

In this example, a table named "employees" is created with columns for ID, name, age, and department.


2. Creating Indexes

An index is used to improve the performance of queries by allowing faster data retrieval. You can create an index on one or more columns of a table using the CREATE INDEX statement.

Example:

// Example of creating an index on the "name" column of the "employees" table
CREATE INDEX idx_name ON employees (name);

This example creates an index named "idx_name" on the "name" column of the "employees" table.


3. Creating Views

A view is a virtual table based on the result of a SQL query. It simplifies complex queries and provides a layer of abstraction over the underlying tables. You can create a view using the CREATE VIEW statement.

Example:

// Example of creating a view named "employee_view"
CREATE VIEW employee_view AS
SELECT id, name, age FROM employees WHERE department = 'IT';

This example creates a view named "employee_view" that selects specific columns from the "employees" table where the department is 'IT'.


4. Conclusion

The CREATE statement in SQL is a powerful tool for creating database objects such as tables, indexes, and views. By understanding how to use the CREATE statement effectively, you can design and build well-structured databases to meet your application's requirements.

Comments