日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

msyql索引用法

發布時間:2024/2/28 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 msyql索引用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 索引
  • 什么是索引(index)
    • 索引有什么作用
    • 索引的分類
    • 創建索引有什么原則
    • 什么是外鍵
  • 創建索引的語法
    • 如何查看索引
    • 刪除索引的方法 drop
    • 創建普通索引
    • 創建主鍵索引
    • 創建唯一索引
    • 創建全文索引

索引

什么是索引(index)

數據庫中的索引與書籍中的目錄類似

在數據庫中,索引使數據庫程序無須對整個表進行掃描,就可以在其中找到所需數據

需要額外的磁盤空間

索引有什么作用

  • 設置了合適的索引之后,數據庫利用各種快速的定位技術,能夠大大加快查詢速率
  • 特別是當表很大時,或者查詢涉及到多個表時,使用索引可使查詢加快成干倍
  • 可以降低數據庫的IO成本,并且索引還可以降低數據庫的排序成本
  • 通過創建唯一性索引保證數據表數據的唯一性
  • 可以加快表與表之間的連接
  • 在使用分組和排序時,可大大減少分組和排序時間

索引的分類

  • 普通索引
    • 這是最基本的索引類型,而且它沒有唯一性之類的限制
  • 唯一性索引
    • 這種索引和前面的“普通索引”基本相同,但有一個區別:索引列的所有值都只能出現一次,即必須唯一
  • 主鍵
    • 主鍵是一種唯一性索引,但它必須指定為“ PRIMARY KEY
  • 全文索引
    • MySQL從32323版開始支持全文索引和全文檢索。在 MySQL中全文索引的索引類型為 FULLTEXT,全文索引可以在 ARCHAR或者TEXT類型的列上創建
  • 單列索引與多列索引
    • 索引可以是單列上創建的索引,也可以是在多列上創建的索引

創建索引有什么原則

  • 表的主鍵、外鍵必須有索引
  • 數據量超過300行的表應該有索引
  • 經常與其他表進行連接的表,在連接字段上應該建立索引
  • 唯一性太差的字段不適合建立索引
  • 更新太頻繁地字段不適合創建索引
  • 經常出現在 Where子句中的字段,特別是大表的字段,應該建立索引
  • 索引應該建在選擇性高的字段上
  • 索引應該建在小字段上,對于大的文本字段甚至超長字段,不要建索引

什么是外鍵

  • 主表中的外鍵是令一張表的主鍵

創建索引的語法

  • 創建普通索引
    最基本的索引類型,沒有唯一性之類的限制
    創建普通索引的方式
語法:方法一;create index <索引的名字> on 表名(列表名);方法二:mysql> alter table (表名) add 索引名(列表名));方法三:在建表時創建索引mysql> create table test(...index abc(id));例如:
  • 創建唯一性索引
    與“普通索引”基本相同

與普通索引的區別是索引列的所有值只能出現一次,即必須唯一

創建唯一索引的方式

方法一: create unique index 索引名 on 表名 (列表名);方法二:alter table 表名 add unique 索引名(列表名);方法三:在建表時就創建索引mysql> create table test(...unqiue index abc(id));
  • 創建主鍵索引
    是一種特殊的唯一索引,指定為“PRIMARY KEY”
    一個表只能有一個主鍵,不允許有空值
    創建主鍵索引的方式

  • 創建全文索引

1 直接創建 mysql> create unique index index_name on table_name(column(length));2 修改表的方式創建 mysql> alter table table_name add unique index index_name(column(length));3 創建表的時候創建 mysql> create table table_name (id int(3) not null auto_increment,name varchar(10) not null,score decimal(5,2) not null,address varchar(50) default '未知',primary key(id),unique index index_name(id));

組合索引 單列索引與多列索引

create table index isis on benat (id,name);

如何查看索引

語法: show index from tablename; show keys from tablename; mysql> show index from num\G; 縱向查看索引 mysql> show index from num; 橫著查看索引

刪除索引的方法 drop

drop index 索引名 on 表名; alter table 表名 drop index 索引名; alter table 表名 drop primary key; ## 刪除主鍵索引

創建普通索引

最基本的索引類型,沒有唯一性之類的限制
創建普通索引方式(3種)

1 直接創建 mysql> create index index_name on table_name(column(length)); 'lengtn是可選項'2 修改表的方式創建(修改表結構) mysql> alter table table_name add index index_name (column(length));3 創建表的時候創建 mysql> create table table_name (id int(3) auto_increment,name varchar(10) not null,score decimal(5,2) not null,address varchar(50) default '未知',primary key(id),index index_name(id(3)));

舉例子

mysql> create table infos (id int(4),name varchar(10)); 創建一個表 mysql> desc infos; 查看表 +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ mysql> create index id_index on infos(id); 創建索引 mysql> desc infos; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | MUL(索引) | NULL | | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> show index from infos\G; 查看索引 *************************** 1. row ***************************Table: infosNon_unique: 1Key_name: id_indexSeq_in_index: 1Column_name: idCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment: Index_comment:

方法2

mysql> alter table info add index name_index (name) mysql> create table table_name (id int(3) not null auto_increment,name varchar(10) not null,primary key(id)); mysql> desc info; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | MUL | NULL | | | name | varchar(10) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)mysql> show index from info\G *************************** 2. row ***************************Table: infoNon_unique: 1Key_name: name_indexSeq_in_index: 1Column_name: nameCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment: Index_comment: 2 rows in set (0.00 sec)

方法3

mysql> create table num (id int,index id_index(id)); mysql> desc num; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | MUL | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec)mysql> show index from num\G *************************** 1. row ***************************Table: numNon_unique: 1Key_name: id_indexSeq_in_index: 1Column_name: idCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment: Index_comment: 1 row in set (0.00 sec)

創建主鍵索引

  • 是一種特殊的唯一索引,指定為 “primary key”
  • 一個表只能有一個主鍵,不允許有空值(唯一且非空)
  • 創建主鍵索引的方式

創建唯一索引

創建唯一索引的方式(3種)

1 直接創建 mysql> create unique index index_name on table_name(column(length));2 修改表的方式創建 mysql> alter table table_name add unique index index_name(column(length));3 創建表的時候創建 mysql> create table table_name (id int(3) not null auto_increment,name varchar(10) not null,score decimal(5,2) not null,address varchar(50) default '未知',primary key(id),unique index index_name(id));

舉例子

mysql> create table benat (id int(4),name vaechar(10)); mysql> desc benat; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+mysql> create unique index unique_name on infos(name);mysql> show index from benat\G *************************** 1. row ***************************Table: benatNon_unique: 0Key_name: unique_nameSeq_in_index: 1Column_name: idCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment: Index_comment: 1 row in set (0.00 sec)

方法2

mysql> alter table benat add unique index_id(name) mysql> desc benat; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | UNI | NULL | | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ mysql> show index from benat\G *************************** 2. row ***************************Table: benatNon_unique: 0Key_name: index_idSeq_in_index: 1Column_name: idCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment: Index_comment:

創建全文索引

alter table test01 add fulltext index abc(address); mysql> create table index isis on benat (id,name); info;mysql> show index from benat; +-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | benat | 1 | isis | 1 | id | A | 4 | NULL | NULL | | BTREE | | | | benat | 1 | isis | 2 | name | A | | +-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 8 rows in set (0.00 sec)

創建索引的原則依據

  • 表的主鍵、外鍵必須有索引
  • 記錄數超過300行的表應該有索引
  • 經常與其他表進行連接的表,在連接字段上應該建立索引
  • 唯一性太差的字段不適應建立索引
  • 更新太頻繁地字段不適合創建索引
  • 經常出現呢在where子句中的字段,特別是大表的字段,應該建立索引
  • 索引應該建在選擇性高的字段上
  • 索引應該建在小字段上,對于大的文本字段甚至超長字段,不要建索引

總結

以上是生活随笔為你收集整理的msyql索引用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。