MySQL資料庫操作之索引的操作例項

2021-12-30 09:10:15 字數 1876 閱讀 1712

資料庫物件索引是一種有效組合資料的方式,通過索引物件,可以快速查詢到資料物件表中的特定記錄,是一種提高效能的常用方式。

索引是建立子啊資料庫表物件上,由表中的乙個欄位或多個字段生成的鍵組成,這些鍵儲存在資料結構(b樹或者雜湊表中),從而快速查詢與鍵值相關的字段。

建立表時建立普通索引create table table_name(

屬性名1 資料型別,

屬性名2 資料型別,

......

index/key 索引名(屬性名1 asc|desc,屬性名2 asc|desc,...)

);index或者key引數是用來指定欄位為索引,asc代表索引為公升序排序,desc代表為降序排序。預設為公升序排序。

舉個栗子:

create table t_dept(

deptno int,

dname varchar(20),

loc varchar(40),

index index_deptno(deptno)

);在已經存在的表上建立普通索引create index 索引名

on 表名(屬性名 asc|desc);on用來指定所要建立索引的表名稱。

舉個栗子:

create index index_deptno

on t_dept(deptno);注:這裡需要先建立t_dept;

通過alter table 建立普通索引alter table table_name

add index|key 索引名(屬性名 asc|desc);舉例:

alter table t_dept

add index index_deptno(deptno);唯一索引,即是在建立索引時,限制索引的值必須是唯一的。

建立表時建立唯一索引create table table_name(

屬性名 資料型別,

屬性名 資料型別,

......

屬性名 資料型別,

unique index|key 索引名(屬性名 asc|desc)

);與建立普通索引相同,只需要加上unique。

在已經存在的表上建立唯一索引create unique index 索引名

on 表名(屬性名 asc|desc);通過sql語句alter table建立唯一索引alter table table_name

add unique index|key 索引名(屬性名 asc|desc);全文索引主要關聯在資料型別為char,varchar和text的字段上。

建立表時建立全文索引create table table_name (

屬性名 資料型別,

......

fulltext index|key 索引名(屬性名 asc|desc)

);在已經存在的表上建立全文索引create fulltext index 索引名

on 表名(屬性名 asc|desc);通過sql語句alter table建立全文索引alter table table_name

add fulltext index|key 索引名(屬性名 asc|desc);建立表時建立多列索引create table table_name(

屬性名1 資料型別,

屬性名2 資料型別,

......

index|key 索引名(屬性名1 asc|desc,屬性名2 asc|desc)

);asc|desc可以省略,預設為增序排序。

在已經存在的表上建立多列索引create index 索引名

on 表名(屬性名 asc|desc,屬性名 asc|desc);通過sql語句alter table建立多列索引alter table table_name

add index|key 索引名(屬性名 asc|desc,屬性名 asc|desc);

mysql資料庫前端操作 MySQL基本操作

mysql 常用操作 1.連線資料庫 mysql h 192.168.0.3 uroot p123456 2.建立資料庫 表 create database test db default charset utf8mb4 檢視建庫語句 show create database test db use...

mysql資料庫索引的優化,實踐,實操

實驗乙個有43萬資料的表,索引情況 對uid設定了unique 約束唯一標識資料庫表中的每條記錄 唯一索引 對mobile設定了normal 普通索引 1 檢視數量 這裡的原因是字段有null的情況,就會全表查詢,然而加上 is not null 就會使索引生效,在字段沒有null的情況索引生效。2...

MySQL資料庫之索引

索引是一種特殊的檔案 innodb資料表上的索引是表空間的乙個組成部分 它們包含著對資料表裡所有記錄的引用指標。更通俗的說,資料庫索引好比是一本書前面的目錄,能加快資料庫的查詢速度。基本的索引型別,值可以為空,沒有唯一性的限制。alter table table name add index 欄位名...