DB2 Tutorial

  • Home
  • Start Here
  • Basics
  • Views
  • Triggers
  • Indexes
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
Home » Db2 Index » Db2 DROP INDEX

Db2 DROP INDEX

Summary: in this tutorial, you will learn how to use the Db2 DROP INDEX statement to delete an index.

Introduction to Db2 DROP INDEX statement

The DROP INDEX deletes an index from a table. Here is the basic syntax of the DROP INDEX statement:

DROP INDEX index_name;

In this syntax, you specify the name of the index that you want to delete after the DROP INDEX keyword.

If you delete a non-existing index, Db2 will issue an error. Unfortunately, Db2 doesn’t support IF EXISTS option that conditionally deletes an index only if the index exists.

Notice that you cannot use the DROP INDEX statement to drop a primary key or unique key index.

If the primary index or unique key index was created automatically by the primary key constraint or unique constraint, to drop this kind of index, you use the ALTER TABLE statement to remove the primary key or unique constraint. Db2 will automatically remove the associated index.

If the primary index or the unique constraint was user-defined, then you must drop the primary key or unique key first by using the ALTER TABLE statement. After that, you can use the DROP INDEX statement to explicitly drop the index associated with the primary key or unique constraint.

Db2 DROP INDEX examples

Let’s take some examples of deleting an index

1) Using DROP INDEX statement to delete an index

This statement creates a new index on the title column of the books table:

CREATE INDEX ix_title ON books(title);

To drop the ix_title index, you use the following statement:

DROP INDEX ix_title;

2) Using DROP INDEX statement to delete an index associated with the unique constraint

This example illustrates how to drop a unique index associated with a unique constraint.

First, create a new table named users that has a unique constraint on the email column:

CREATE TABLE users( user_id INT NOT NULL PRIMARY KEY, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL );

Second, create a unique index on the email column:

CREATE UNIQUE INDEX ix_email ON users(email);

Third, add the unique constraint to the email column:

ALTER TABLE users ADD CONSTRAINT unq_email UNIQUE(email);
db2 drop index example

Fourth, attempt to drop the ix_email index:

DROP INDEX ix_email;

Db2 issued the following error:

SQL Error [42917]: A system required index cannot be dropped explicitly.. SQLCODE=-669, SQLSTATE=42917, DRIVER=4.21.29

To delete the index, first, drop the unq_email constraint:

ALTER TABLE users DROP CONSTRAINT unq_email;

And then drop the ix_email index:

DROP INDEX ix_email;

In this tutorial, you have learned how to use the Db2 DROP INDEX statement to delete an index.

  • Was this tutorial helpful ?
  • YesNo
Previous Db2 UNIQUE Index
Next Db2 Expression-based Index

Getting Started

  • What is Db2
  • Installing Db2 Database Server
  • Db2 Sample Database
  • Creating a Db2 Sample Database
  • Connecting to a Db2 Database
  • Interacting with Db2 using SQL Developer

Data Manipulation

  • SELECT
  • ORDER BY
  • WHERE
  • SELECT DISTINCT
  • AND
  • OR
  • BETWEEN
  • LIKE
  • IN
  • LIMIT
  • FETCH
  • Join
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • Self-Join
  • CROSS JOIN
  • GROUP BY
  • Subquery
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • Common Table Expression or CTE
  • INSERT
  • INSERT Multiple Rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE

Managing Database Objects

  • CREATE TABLE
  • Identity Columns
  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME TABLE

Db2 Constraints

  • Primary Key
  • Foreign Key
  • DEFAULT
  • NOT NULL
  • CHECK
  • UNIQUE

Db2 Data Types

  • Integer
  • Decimal
  • VARCHAR
  • CHAR
  • DATE
  • TIME
  • TIMESTAMP

Useful Functions & Expressions

  • CAST
  • CASE Expression
  • COALESCE

About db2tutorial.com

The db2tutorial.com website provides you with a comprehensive IBM DB2 tutorial with many practical examples and hands-on sessions.

Recent Tutorials

  • Db2 Functions
  • Db2 RANK
  • Db2 ROW_NUMBER
  • Db2 Window Functions
  • What is Db2

Site Links

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
Copyright © © 2021 by www.db2tutorial.com. All Rights Reserved.