加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 钦州站长网 (https://www.0777zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL 索引的创建、删除

发布时间:2023-02-15 13:19:54 所属栏目:MySql教程 来源:
导读:  MySQL中索引的创建有三种方法,索引的删除有两种方法。

  一、创建索引 (1)使用create index

  # 1.创建普通索引
  create index 索引名 on 表名 (列名[(限制索引长度)]);
  # 2.创建唯一性
  MySQL中索引的创建有三种方法,索引的删除有两种方法。
 
  一、创建索引 (1)使用create index
 
  # 1.创建普通索引
  create index 索引名 on 表名 (列名[(限制索引长度)]);
  # 2.创建唯一性索引
  create unique index 索引名 on 表名 (列名);
  # 3.创建全文索引
  create fulltext index 索引名 on 表名 (列名);
  注意:
 
  (2)修改表的方式创建索引
 
  #在修改表的同时为该表添加索引
  # 1.普通索引
  alter table examination_info add index 索引名(列名);
  # 2.唯一性索引
  alter table examination_info add unique index 索引名(列名);
  # 3.全文索引
  alter table examination_info add fulltext index 索引名(列名);
  #在修改表的同时为该表添加主键
  alter table examination_info ADD PRIMARY KEY [索引名] (列名,...);
  #在修改表的同时为该表添加外键
  alter table examination_info ADD FOREIGN KEY [索引名] (列名,...);
  注意:
 
  (3)建表的时候创建索引
 
  #在创建新表的同时创建该表的索引
  create table tableName(  
    id int not null,   
    列名  列的类型,
    unique | index [索引名] [索引类型] (列名,...);
  );
  #在创建新表的同时创建该表的外键
  create table tableName(  
    id int not null,   
    列名  列的类型,
    foreign key [索引名] 列名;
  );
  二、删除索引 (1)使用drop index
 
  drop index 索引名 on 表名;
  (2)使用alter table
 
  alter table 表名 drop index 索引名;
  参考文章
 
  MySQL创建索引(CREATE INDEX)
 

(编辑:PHP编程网 - 钦州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!