【问题标题】:mySQL CREATE TABLE UNIQUE INDEXmySQL 创建表唯一索引
【发布时间】:2022-01-08 14:25:05
【问题描述】:

早上好。 我正在阅读一些在 mySQL 中创建表的示例,但我不理解以下代码中的最后一条语句:CREATE TABLE 'company_test', 'employee'( ... UNIQUE INDEX 'department_id_UNIQUE' ('department_id' ASC) VISIBLE) 这个 UNIQUE INDEX 'department_id_UNIQUE ('department_id' ASC) VISIBLE 做了什么?我到处寻找可以解释这一点的地方,但我只能在 CREATE TABLE 之外找到使用CREATE INDEX 的示例,索引是做什么的,有人可以详细解释该语句吗?如果你能给我看一些文件,我将不胜感激。

【问题讨论】:

    标签: mysql indexing


    【解决方案1】:

    1.这句话有什么作用? UNIQUE INDEX 'department_id_UNIQUE ('department_id' ASC) 可见

    此语句在您的示例中的员工表的部门 ID 列上创建唯一索引。 索引可以在创建表期间创建,也可以在创建表后创建。 MySQL 创建表语法:create table 创建表后创建索引:create index

    2。索引有什么作用? 索引以写入性能为代价提高了读取性能。在您的示例中,它与索引一起创建了对列的唯一约束,这将防止 department_id 字段的重复值。 This 帖子详细解释了数据库索引的工作原理。

    【讨论】:

      【解决方案2】:

      UNIQUE INDEX 'department_id_UNIQUE ('department_id' ASC) 可见

      UNIQUE -- an index BTree is built and maintained; a uniqueness constraint is established.
      INDEX -- optional (redundant) syntax
      '...' -- optional arbitrary name for this index.  A default will be provided if left out.
      (...) -- The column(s) in the index.  The combination is unique; the BTree is ordered by them.
      ASC -- Ascending (as opposed to DESC: Descending)
      VISIBLE -- below
      

      VISIBLE 是一个新关键字。一些产品采用了这一点;其他一些产品将其视为语法错误。如果这是一个问题,只需删除该词。

      VISIBLEINVISIBLE 的目的是这样的。 CREATEingDROPping 索引可能是一项代价高昂的操作。如果要删除索引(以节省磁盘空间),最好将其设为INVISIBLE,以查看是否有任何查询显着减慢。这可能会告诉您需要索引 。然后您可以再次快速将其设为VISIBLE。否则你可以DROP它。

      VISIBLE 是默认值;把它关掉是可以的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 2019-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多