SQL: How to Create and Delete A Table with SQL Query

Home /

Table of Contents

CREATE TABLE is a command used in SQL to create a new table in a database. When you create a table, you must specify the table name and the names and data types of the columns that will be contained within the table. You can also specify constraints on the data in the table, such as primary keys, foreign keys, and check constraints. By creating a table, you can organize your data in a logical and efficient way, making it easier to store, retrieve, and analyze data.

CREATE TABLE Statement

In SQL, the CREATE TABLE statement is used to create a new table in a database. The basic syntax for creating a new table is:

SQL
CREATE TABLE [table_name] (
column1_name data_type constraint,
column2_name data_type constraint,

column_n_name data_type constraint,
constraint_name constraint_type (column_name)
);

The table_name is the name you choose for the table, and the column1_name, column2_name, … column_n_name are the names of the columns in the table. The data_type is the data type for each column (e.g. INT, VARCHAR, DATE, etc.) and the constraint is any additional constraint you want to apply to the column (e.g. NOT NULL, UNIQUE, etc.).

Here’s an example of how you might use the CREATE TABLE statement to create a new table called “employees” with columns for “employee_id”, “first_name”, “last_name”, and “hire_date”:

SQL
CREATE TABLE employees (
employee_id INT NOT NULL PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
hire_date DATE NOT NULL
);

In this example, the employee_id column is of type INT and has a NOT NULL and PRIMARY KEY constraint applied to it. The first_name and last_name columns are of type VARCHAR with a maximum length of 255 characters and a NOT NULL constraint applied to them. The hire_date column is of type DATE and has a NOT NULL constraint applied to it.

You can also add more columns, indexes, constraints, and other table options like foreign keys, default values, partitioning, etc. depending on the SQL implementation you are using.

You can also set indexes on columns to improve the performance of the queries, for example:

SQL
CREATE INDEX idx_employee_name ON employees (first_name, last_name);

It’s essential to consult the documentation of the SQL implementation you are using for specific information on the syntax and options that are available for creating a new table. And also you need to have appropriate privileges to create a new table.

DROP or DELETE Table

The DROP TABLE statement is used to delete an existing table from a database. The basic syntax for dropping a table is:

DROP TABLE [table_name];

For example, to delete a table called “employees“:

SQL
DROP TABLE employees;

It’s important to note that when you drop a table, all the data and objects within that table will be permanently deleted and cannot be recovered. So, be very careful when using this statement. And also you need to have appropriate privileges to drop a table.

In some SQL implementations, you may also use the DELETE or REMOVE statement to delete a table. Here’s an example of how the syntax may vary based on the SQL implementation you are using:

For MySQL:

SQL
-- MySQL Syntax
DROP TABLE employees;

For PostgreSQL:

SQL
-- PostgreSQL Syntax
DROP TABLE employees;

For SQL Server:

SQL
-- SQL Server Syntax
DROP TABLE employees;

It’s always recommended to consult the documentation of the SQL implementation that you are using for specific information on the syntax and options that are available for deleting a table. Again, It’s essential to be very careful when using this statement as it will delete all the data and objects within that table permanently and cannot be recovered.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article