教程 > sql 教程 > 阅读:38

sql index(索引) 约束——迹忆客-ag捕鱼王app官网

index 用于非常快速地创建和检索数据库中的数据。可以通过使用表中的单个或一组列来创建索引。创建索引时,会在对数据进行排序之前为每一行分配一个 rowid。

适当的索引有利于大型数据库的性能,但在创建索引时需要小心。字段的选择取决于您在 sql 语句中使用的内容。

示例

例如,以下 sql 语句创建一个名为 customers 的新表并在其中添加五列。

create table customers(
   id   int              not null,
   name varchar (20)     not null,
   age  int              not null,
   address  char (25) ,
   salary   decimal (18, 2),       
   primary key (id)
);

现在,可以使用下面给出的语句在单个或多个列上创建索引。

create index index_name
   on table_name ( column1, column2.....);

要在 age 列上创建 index,优化特定年龄的客户搜索,可以使用下面给出的以下 sql 语句

create index idx_age
   on customers ( age );

删除索引约束

要删除 index 约束,请使用以下 sql 语句。

alter table customers
   drop index idx_age;

查看笔记

扫码一下
查看教程更方便
网站地图