Create Index
Create Index
Create Index
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.
Now there are several indexes that can be created. Have a look.
SQL DROP command is used in order to drop and index. The syntax is as follows:
There are three terms that can be used while altering and index.
Clustered index
Non-clustered index
Clustered index
3. This is maintained by using a b tree Data structure leaf node, the nodes of the indexes
point to the table directly.
1. Non clustered index will not arrange rows physical in the memory in sorted order.
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.
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.
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.
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.
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.
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.