zoukankan      html  css  js  c++  java
  • 索引的创建

    自动创建

      当在CREATE TABLE或者ALTER TABLE语句中定义了一个主键或者唯一键约束时,MySQL自动为它们创建一个唯一索引。主键索引的名字叫primary。唯一键索引的名字一般就是唯一键的第一个列的名字(有单列索引和复合索引)。

    手工创建使用CREATE INDEX或者ALTER TABLE语句

      alter table语句创建和删除索引的语法为:

    ALTER TABLE tbl_name  ADD {INDEX|KEY} [index_name]

            [index_type] (index_col_name,…) [index_option] …

         其中 index_type USING {BTREE | HASH}

       ALTER TABLE tbl_name

          | DROP PRIMARY KEY

          | DROP {INDEX|KEY} index_name

    create/drop index的语法为:

      CREATE [UNIQUE] INDEX index_name [index_type]

        ON tbl_name (index_col_name,…)

        DROP INDEX index_name ON tbl_name

    例:在players表的postcode列上建一个索引

      create index player_pc on players(postcode); 索引可以同时建在多个列上

    例:在matches表的wonlost列上建一个复合索引。

      create index mat_wl on matches(won, lost);

     例:在players表的nameinitials列上建一个唯一索引。

      create unique index nameinit ON players(NAME, initials);

      ## 该语句执行后,就不能向表中插入列值组合完全相同的两行

      使用alter table语句添加索引;在teams表的division列上创建一个非唯一索引。

        alter table teams add index teams_division_idx USING BTREE(division);

    CREATE TABLE语句中创建索引

    例:创建matches表,在wonlost列上键一个复合索引

      create table matches_copy(

         matchno INTEGER PRIMARY KEY, teamno INTEGER NOT NULL,

         playerno INTEGER NOT NULL, won SMALLINT NOT NULL ,

         lost SMALLINT NOT NULL, INDEX mat_wl_idx (won,lost) );

     删除索引:

      DROP INDEX mat_wl_idx ON matches_copy;

      ALTER TABLE matches_copy DROP INDEX mat_wl_idx;

  • 相关阅读:
    Javascript FP-ramdajs
    微信小程序开发
    SPA for HTML5
    One Liners to Impress Your Friends
    Sass (Syntactically Awesome StyleSheets)
    iOS App Icon Template 5.0
    React Native Life Cycle and Communication
    Meteor framework
    RESTful Mongodb
    Server-sent Events
  • 原文地址:https://www.cnblogs.com/5945yang/p/11259140.html
Copyright © 2011-2022 走看看