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 中,UNIQUE 索引用于数据集成。一旦对表中的列创建 UNIQUE 索引,就不允许出现重复值。
语法
以下是创建唯一索引的语法。
CREATE UNIQUE INDEX index_name on table_name (column_name);
示例
以下示例在 Employee 表的列 Id 上创建一个 UNIQUE 索引。
ij> CREATE UNIQUE INDEX unique_index on Emp (Phone_Number);
0 rows inserted/updated/deleted
一旦在列上创建了唯一索引,就不能在另一行中为该列输入相同的值。简而言之,具有 UNIQE 索引的列将不允许重复值。
如下图在 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);
示例
以下索引在名称和位置列上创建复合索引。
ij> CREATE INDEX composite_index on Emp (Name, Location);
0 rows inserted/updated/deleted
显示索引
SHOW INDEXES 查询显示表上的索引列表。
语法
以下是 SHOW INDEXES 语句的语法-
SHOW INDEXES FROM table_name;
示例
下面的例子,我显示了雇员表上的索引。
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 语句的语法。
示例
以下示例删除了上面创建的名为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