开发者

Get MySQL Unique Key Combos

开发者 https://www.devze.com 2023-03-23 12:31 出处:网络
If I have a table created similarly to the following: CREATE TABLE MyTable( id1Part1 INT NOT NULL, id1Part2 INT NOT NULL,

If I have a table created similarly to the following:

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

    id2Part1 INT NOT NULL,
    id2Part2 INT NOT N开发者_开发知识库ULL,

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

how can I now ask the database to give me the two "unique key" tuples?

(SHOW INDEX doesn't seem to do this.)


I'm not sure if you're looking for something like this

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


mysql can't give you two primary keys. Check this out:

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    |       |
+----------+---------+------+-----+---------+-------+

It shows there is only one primary key.

0

精彩评论

暂无评论...
验证码 换一张
取 消