【问题标题】:Get MySQL Unique Key Combos获取 MySQL 唯一键组合
【发布时间】:2011-07-28 22:59:07
【问题描述】:

如果我创建了一个类似于以下内容的表:

CREATE TABLE MyTable(
    id1Part1 INT NOT NULL,
    id1Part2 INT NOT NULL,

    id2Part1 INT NOT NULL,
    id2Part2 INT NOT NULL,

    UNIQUE KEY (id1Part1, id1Part2),
    UNIQUE KEY (id2Part1, id2Part2)
);

我现在如何询问数据库给我两个“唯一键”元组?
SHOW INDEX 似乎没有这样做。)

【问题讨论】:

    标签: mysql database unique-key


    【解决方案1】:

    我不确定你是否正在寻找类似的东西

    select
    constraint_name,
    group_concat(column_name order by ordinal_position) as cols
    from information_schema.key_column_usage
    where table_schema = 'db_name' and table_name = 'table_name'
    group by constraint_name
    

    【讨论】:

    • 嗯...有机会我会尝试检查一下,谢谢。看来这可能是我需要的。
    【解决方案2】:

    mysql 不能给你两个主键。看看这个:

    mysql> CREATE TABLE MyTable(
        ->     id1Part1 INT NOT NULL,
        ->     id1Part2 INT NOT NULL,
        -> 
        ->     id2Part1 INT NOT NULL,
        ->     id2Part2 INT NOT NULL,
        -> 
        ->     UNIQUE KEY (id1Part1, id1Part2),
        ->     UNIQUE KEY (id2Part1, id2Part2)
        -> );
    Query OK, 0 rows affected (0.17 sec)
    
    mysql> desc mytable;
    +----------+---------+------+-----+---------+-------+
    | Field    | Type    | Null | Key | Default | Extra |
    +----------+---------+------+-----+---------+-------+
    | id1Part1 | int(11) | NO   | PRI | NULL    |       |
    | id1Part2 | int(11) | NO   | PRI | NULL    |       |
    | id2Part1 | int(11) | NO   | MUL | NULL    |       |
    | id2Part2 | int(11) | NO   |     | NULL    |       |
    +----------+---------+------+-----+---------+-------+
    

    它表明只有一个主键。

    【讨论】:

    • 我很困惑...我们是在谈论 unique 键还是 primary 键?
    猜你喜欢
    • 2012-08-20
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2023-04-06
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多