Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

What is an index in SQL?

Indexes are special lookup tables. SQL indexes are used in relational databases to retrieve
data. An index acts as a pointer to data in a specific table. It works in the same way as the
indexes you see in any book you read. You can perform a lot of functions by using them.
Learn more about SQL Server and its framework from the SQL Training Course.

How to create an index?

In order to create an index, follow the given syntax.

1 CREATE INDEX index_name ON table_name;

Now there are several indexes that can be created. Have a look.

Index Definition Syntax

CREATE INDEX index_name


It is created on only one table
Single-column index ON table_name
column.
(column_name);

These indexes are used on CREATE INDEX index_name;


Composite indexes two or more columns of a on table_name
table. (column_name);

These are used for Data CREATE UNIQUE INDEX


integrity. No duplicate values index_name
Unique indexes
are allowed to be inserted On table_name
into the table. (column_name);

The next segment is on how to drop an index in SQL!

How to Drop an index?

SQL DROP command is used in order to drop and index. The syntax is as follows:

1 DROP INDEX index_name;

Now let us see how to alter an index using SQL command!

How to alter an index?


Use the following syntax to alter an index.

1ALTER INDEX index_name on object_name;

There are three terms that can be used while altering and index.

Types of index in SQL

There are two types of indexes in SQL.

 Clustered index

 Non-clustered index

Clustered index

1. Clustered index helps in arranging the rows physically in the memory.

2. The search for the range of values is fast.

3. This is maintained by using a b tree Data structure leaf node, the nodes of the indexes
point to the table directly.

Non clustered index

1. Non clustered index will not arrange rows physical in the memory in sorted order.

2. The maximum number of indexes that can be created is 999.

3. This index is also maintained by a b-tree data structure but the leaf nodes of the index
do not point to the table data directly.
DML(Data Manipulation Language)

The SQL commands that deal with the manipulation of data present in the database belong to
DML or Data Manipulation Language and this includes most of the SQL statements. It is the
component of the SQL statement that controls access to data and to the database. Basically,
DCL statements are grouped with DML statements.

List of DML commands:

 INSERT: It is used to insert data into a table.

 UPDATE: It is used to update existing data within a table.

 DELETE: It is used to delete records from a database table.

 LOCK: Table control concurrency.


It is possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will be intact:
The following SQL statement deletes all rows in the "Customers" table, without deleting the
table. This means that the table structure, attributes, and indexes will be intact:

TRUNCATE TABLE

The SQL TRUNCATE TABLE command is used to delete all the records from an existing
table by reinitializing the table's structure. This command instructs the database to deallocate
the space for all records in a table and change this table's structure by resetting the table size.
This is the reason why it is deemed to be a Data Definition Language (DDL) operation rather
than Data Manipulation Language (DML), even though all the table data is removed.
Logically, the TRUNCATE TABLE statement performs similarly to the DELETE TABLE
statement but without the WHERE clause. However, TRUNCATE is much faster than
DELETE and does not allow roll back once committed.

You can also use DROP TABLE command to delete a table but it will remove the complete
table structure from the database and you would need to re-create this table once again if you
wish you store some data again.

Syntax
The basic syntax of a TRUNCATE TABLE command is as follows.
TRUNCATE TABLE table_name;

TRUNCATE vs DELETE
Even though the TRUNCATE and DELETE commands work similar logically, there are
some major differences that exist between them. They are detailed in the table below.

DELETE TRUNCATE

The DELETE command in SQL removes one or SQL's TRUNCATE command is used to
more rows from a table based on the conditions remove all of the rows from a table, regardle
specified in a WHERE Clause.
ss of whether or not any conditions are met.

It is a DML(Data Manipulation Language) It is a DDL(Data Definition Language)


command. command.

There is a need to make a manual COMMIT after When you use the TRUNCATE command,
making changes to the DELETE command, for the the modifications made to the table are
modifications to be committed. committed automatically.

It deletes rows one at a time and applies some criteria It removes all of the information in one go.
to each deletion.

The WHERE clause serves as the condition in this There is no necessity of using a WHERE
case. Clause.

All rows are locked after deletion. TRUNCATE utilizes a table lock, which
locks the pages so they cannot be deleted.
It makes a record of each and every transaction in the The only activity recorded is the
log file. deallocation of the pages on which the data
is stored.

It consumes a greater amount of transaction space It takes comparatively less amount of


compared to TRUNCATE command. transaction space.

If there is an identity column, the table identity is not It returns the table identity to a value it was
reset to the value it had when the table was created. given as a seed.

It requires authorization to delete. It requires table alter permission.

When it comes to large databases, it is much slower. It is faster.

TRUNCATE vs DROP
Unlike TRUNCATE that resets the table structure, DROP command completely frees the
tablespace from the memory. They are both Data Definition Language operations as they
interact with the definitions of database objects; which allows the database to automatically
commit once these commands are executed with no chance to roll back.
However, there are still some differences existing between these commands as well, which
are summarized in the following table −

DROP TRUNCATE

The DROP command in SQL removes an entire The TRUNCATE command is used to remove
table from a database including its definition, all of the rows from a table, regardless of
indexes, constraints, data etc. whether or not any conditions are met and
resets the table definition.

It is a DDL(Data Definition Language) It is also a DDL(Data Definition Language)


command. command.

The table space is completely freed from the The table still exists in the memory.
memory.

All the integrity constraints are removed. The integrity constraints still exist in the table.

Requires ALTER and CONTROL permissions Only requires the ALTER permissions to
on the table schema and table respectively, to be truncate the table.
able to perform this command.
DROP command is much slower than It is faster than both DROP and DELETE
TRUNCATE but faster than DELETE. commands.

You might also like