Apache Derby - Derby索引



表中的索引只不過是指向其資料的指標。這些用於加快從表中檢索資料的速度。

如果我們使用索引,INSERT和UPDATE語句的執行速度會變慢。而SELECT和WHERE語句的執行速度會加快。

建立索引

CREATE INDEX語句用於在Derby資料庫的表中建立新的索引。

語法

以下是CREATE INDEX語句的語法:

CTREATE INDEX index_name on table_name (column_name);

示例

假設我們在Apache Derby中建立了一個名為Employees的表,如下所示。

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的列上建立索引。

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

建立唯一索引

在Apache Derby中,唯一索引用於資料完整性。一旦你在表的列上建立了唯一索引,它就不允許重複值。

語法

以下是建立唯一索引的語法:

CREATE UNIQUE INDEX index_name on table_name (column_name);

示例

以下示例在Employee表的Id列上建立一個唯一索引。

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

一旦你在列上建立了唯一索引,你就不能在另一行中為該列輸入相同的值。簡而言之,具有唯一索引的列不允許重複值。

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

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

由於我們在Phone_No列上建立了一個唯一索引,如果你嘗試輸入與上一條記錄相同的值,則會顯示錯誤。

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'.

建立複合索引

你可以在兩列上建立一個索引,這稱為複合索引。

語法

以下是複合索引的語法:

CREATE INDEX index_name on table_name (column_name1, column_name2);

示例

以下索引在Name和Location列上建立複合索引。

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

顯示索引

SHOW INDEXES查詢顯示錶上索引的列表。

語法

以下是SHOW INDEXES語句的語法:

SHOW INDEXES FROM table_name;

示例

以下示例顯示Employees表上的索引。

ij> SHOW INDEXES FROM Emp;

這將產生以下結果。

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

刪除索引

DROP INDEX語句刪除給定列上的索引。

語法

以下是DROP INDEX語句的語法。

DROP INDEX index_name;

示例

以下示例刪除上面建立的名為composite_index和unique_index的索引。

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

現在,如果你驗證索引列表,你將看到只有一列上的索引,因為我們已經刪除了其餘的。

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

使用JDBC程式處理索引

以下JDBC程式演示瞭如何在表的列上建立和刪除索引。

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");
   }
}

輸出

執行後,將生成以下結果

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 
廣告