Apache Derby 简明教程

Apache Derby - Derby Indexes

在执行以上程序时,将生成以下输出:

An index in a table is nothing but a pointer to its data. These are used to speed up the data retrieval from a table.

表格中的索引只不过是其数据的指针。这些用于加速从表格中检索数据。

If we use indexes, the INSERT and UPDATE statements get executed in a slower phase. Whereas SELECT and WHERE get executed with in lesser time.

Creating an Index

如果我们使用索引,则 INSERT 和 UPDATE 语句将以较慢的速度执行。而 SELECT 和 WHERE 将在较短的时间内执行。CREATE INDEX 语句用于在 Derby 数据库中表格内创建新索引。

The CREATE INDEX statement is used for creating a new Index in a table in Derby database.

Syntax

以下是 CREATE INDEX 语句的语法。

Following is the syntax of the CREATE INDEX statement −

CTREATE INDEX index_name on table_name (column_name);

Example

假设我们创建了一个名为 Employees 的 Apache Derby 表,如下所示。

Suppose we have created a table named Employees in Apache Derby as shown below.

CREATE TABLE Emp ( Id INT NOT NULL GENERATED ALWAYS AS IDENTITY,
   Name VARCHAR(255),
   Salary INT NOT NULL,
   Location VARCHAR(255),
   Phone_Number BIGINT
);

以下 SQL 语句在表 Employees 中名为 Salary 的列上创建了一个索引。

The following SQL statement creates an index on the column named Salary in the table Employees.

ij> CREATE INDEX example_index on Emp (Salary);
0 rows inserted/updated/deleted

Creating a UNIQUE index

在 Apache Derby 中,UNIQUE 索引用于数据集成。一旦在表中的列上创建了 UNIQUE 索引,不允许出现重复值。

In Apache Derby, UNIQUE indexes are used for data integration. Once you create a UNIQUE index on a column in a table, it does not allow duplicate values.

Syntax

以下是创建唯一索引的语法。

Following is the syntax of creating a unique index.

CREATE UNIQUE INDEX index_name on table_name (column_name);

Example

以下示例在表 Employee 的 Id 列上创建了一个 UNIQUE 索引。

Following example creates a UNIQUE index on the column Id of the table Employee.

ij> CREATE UNIQUE INDEX unique_index on Emp (Phone_Number);
0 rows inserted/updated/deleted

一旦您在某列上创建了唯一索引,您就不能为该列在另一行中输入相同的值。简而言之,具有 UNIQUE 索引的列不允许出现重复值。

Once you have created a unique index on a column, you cannot enter same values for that column in another row. In short, a column which is has a UNIQE index will not allow duplicate values.

如下所示,在 Emp 表中插入一行。

Insert a row in the Emp table as shown below

ij> INSERT INTO Emp(Name, Salary, Location, Phone_Number) VALUES
   ('Amit', 45000, 'Hyderabad', 9848022338);
1 row inserted/updated/deleted

由于我们在列 Phone_No 上创建了唯一索引,如果您尝试输入与前一个记录中的相同值,它将显示错误。

Since we have created a unique index on the column Phone_No, if you ty to enter the same value as in the previous record, it shows an error.

ij> INSERT INTO Emp(Name, Salary, Location, Phone_Number) VALUES
   ('Sumit', 35000, 'Chennai', 9848022338);
ERROR 23505: The statement was aborted because it would have caused a duplicate
key value in a unique or primary key constraint or unique index identified by
'UNIQUE_INDEX' defined on 'EMP'.

Creating a COMPOSITE index

您可以在两行上创建单个索引,它称为复合索引。

You can create a single index on two rows and it is called Composite index.

Syntax

以下是复合索引的语法。

Following is the syntax of the composite index.

CREATE INDEX index_name on table_name (column_name1, column_name2);

Example

以下索引在列 Name 和 Location 上创建了一个复合索引。

Following index creates a composite index on the columns Name and Location.

ij> CREATE INDEX composite_index on Emp (Name, Location);
0 rows inserted/updated/deleted

Displaying the Indexes

SHOW INDEXES 查询显示表上的索引列表。

The SHOW INDEXES query displays the list of indexes on a table.

Syntax

以下是 SHOW INDEXES 语句的语法。

Following is the syntax of the SHOW INDEXES statement −

SHOW INDEXES FROM table_name;

Example

以下示例 i 显示表 Employees 上的索引。

Following example, i displays the indexes on the table Employees.

ij> SHOW INDEXES FROM Emp;

这将产生以下结果。

This produces the following result.

ij> SHOW INDEXES FROM Emp;
TABLE_NAME |COLUMN_NAME |NON_U&|TYPE|ASC&|CARDINA&|PAGES
----------------------------------------------------------------------------
EMP        |PHONE_NUMBER|false |3   |A   |NULL    |NULL
EMP        |NAME        |true  |3   |A   |NULL    |NULL
EMP        |LOCATION    |true  |3   |A   |NULL    |NULL
EMP        |SALARY      |true  |3   |A   |NULL    |NULL
4 rows selected

Dropping Indexes

Drop Index 语句会删除/放弃某个列上的给定索引。

The Drop Index statement deletes/drops the given index on a column.

Syntax

以下是 DROP INDEX 语句的语法。

Following is the syntax of the DROP INDEX statement.

DROP INDEX index_name;

Example

以下示例放弃了上面创建的 composite_index 和 unique_index。

Following example drops an indexes named composite_index and unique_index created above.

ij> DROP INDEX composite_index;
0 rows inserted/updated/deleted
ij>Drop INDEX unique_index;
0 rows inserted/updated/deleted

现在,如果您验证索引列表,您可以看到一列上的索引,因为我们删除了剩余的索引。

Now, if you verify the list of indexes you can see index on one column since we have deleted the remaining.

ij> SHOW INDEXES FROM Emp;
TABLE_NAME |COLUMN_NAME |NON_U&|TYPE|ASC&|CARDINA&|PAGES
----------------------------------------------------------------------------
EMP        |SALARY      |true  |3   |A   |NULL    |NULL
1 row selected

Handling Indexes using JDBC program

以下 JDBC 程序演示如何删除表中一列上的索引。

Following JDBC program demonstrates how to create drop indexes on a column in a table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class IndexesExample {
   public static void main(String args[]) throws Exception {
      //Registering the driver
      Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
      //Getting the Connection object
      String URL = "jdbc:derby:MYDATABASE;create=true";
      Connection conn = DriverManager.getConnection(URL);

      //Creating the Statement object
      Statement stmt = conn.createStatement();

      //Creating the Emp table
      String createQuery = "CREATE TABLE Emp( "
         + "Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
         + "Name VARCHAR(255), "
         + "Salary INT NOT NULL, "
         + "Location VARCHAR(255), "
         + "Phone_Number BIGINT )";
      stmt.execute(createQuery);
      System.out.println("Table created");
      System.out.println(" ");

      //Creating an Index on the column Salary
      stmt.execute("CREATE INDEX example_index on Emp (Salary)");
      System.out.println("Index example_index inserted");
      System.out.println(" ");

      //Creating an Unique index on the column Phone_Number
      stmt.execute("CREATE UNIQUE INDEX unique_index on Emp (Phone_Number)");
      System.out.println("Index unique_index inserted");
      System.out.println(" ");

      //Creating a Composite Index on the columns Name and Location
      stmt.execute("CREATE INDEX composite_index on Emp (Name, Location)");
      System.out.println("Index composite_index inserted");
      System.out.println(" ");

      //listing all the indexes
      System.out.println("Listing all the columns with indexes");

      //Dropping indexes
      System.out.println("Dropping indexes unique_index and, composite_index ");
      stmt.execute("Drop INDEX unique_index");
      stmt.execute("DROP INDEX composite_index");
   }
}

Output

执行后,生成以下结果

On executing, this generates the following result

Table created
Index example_index inserted
Index unique_index inserted
Index composite_index inserted

Listing all the columns with indexes
Dropping indexes unique_index and, composite_index