如何识别任何Mysql数据库表中的复合主键?

发布于 2024-09-02 00:19:57 字数 630 浏览 6 评论 0原文

如何识别任何Mysql数据库表中的复合主键? 或者

编辑2 应该使用什么sql查询 显示任何表的索引 包含复合主键?

我在 mysql 数据库中有许多表,它们具有 2 或 3 个主键的复合键,我正在使用 phpmyadmin,我必须编写一个 php 脚本来识别哪个表具有复合键,现在我可以识别通过使用查询来查找表

SHOW INDEXES FROM `".$row3['TABLE_NAME']."` WHERE Key_name = 'PRIMARY'

,该查询给了我想要的东西,但是现在我如何找到具有复合键的索引?

编辑1

在丹尼尔图像评论的背景下 查找复合主键 phpmyadmin

复合主键在 phpmyadmin 中如下所示: 替代文本

How to identify composite primary key in any Mysql Database table?
or

EDIT 2
what sql query should be used to
display the indees of any table who
contains the composite primary keys?

I have many tables in mysql database which are having composite keys of 2 or 3 primary keys, I am using phpmyadmin, and I have to code a php script to identify which table has the composite keys, right now i can identify the primary key of the tables by using a query

SHOW INDEXES FROM `".$row3['TABLE_NAME']."` WHERE Key_name = 'PRIMARY'

which is giving me what i want, but now how can i find out the indexes where i have composite keys?

EDIT 1

In the context of Daniel Image comment
for look of composite primary keys in
phpmyadmin

composite primary keys look like this in phpmyadmin:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2024-09-09 00:19:57

更新:

对于更新的问题,您可能需要在 PHP 脚本中使用以下内容:

SELECT COUNT(*) num_keys 
FROM   information_schema.KEY_COLUMN_USAGE     
WHERE  table_name ='tb' AND constraint_name = 'PRIMARY';

此查询将返回 num_keys > 1 如果表 tb 具有复合主键。


我不确定我是否理解您想要实现的目标,但您可能需要考虑使用 SHOW INDEX ,如下所示:

CREATE TABLE tb (a int, b int, c int);
Query OK, 0 rows affected (0.21 sec)

ALTER TABLE tb ADD CONSTRAINT pk_tb PRIMARY KEY (a, b);
Query OK, 0 rows affected (0.06 sec)

SHOW INDEX FROM tb WHERE key_name='PRIMARY';
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tb    |          0 | PRIMARY  |            1 | a           | A         |        NULL |     NULL | NULL   |      | BTREE      |         |
| tb    |          0 | PRIMARY  |            2 | b           | A         |           0 |     NULL | NULL   |      | BTREE      |         |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.02 sec)

如果它不是复合键,则您只会在SHOW INDEX 查询:

CREATE TABLE tb2 (a int, b int, c int);
Query OK, 0 rows affected (0.05 sec)

ALTER TABLE tb2 ADD CONSTRAINT pk_tb PRIMARY KEY (a);
Query OK, 0 rows affected (0.05 sec)

SHOW INDEX FROM tb2 WHERE key_name='PRIMARY';
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tb2   |          0 | PRIMARY  |            1 | a           | A         |           0 |     NULL | NULL   |      | BTREE      |         |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.02 sec)

UPDATE:

Further to the updated question, you may want to use the following in your PHP script:

SELECT COUNT(*) num_keys 
FROM   information_schema.KEY_COLUMN_USAGE     
WHERE  table_name ='tb' AND constraint_name = 'PRIMARY';

This query will return num_keys > 1 if table tb has a composite primary key.


I'm not sure if I understood what you are trying to achieve, but you may want to consider using SHOW INDEX as follows:

CREATE TABLE tb (a int, b int, c int);
Query OK, 0 rows affected (0.21 sec)

ALTER TABLE tb ADD CONSTRAINT pk_tb PRIMARY KEY (a, b);
Query OK, 0 rows affected (0.06 sec)

SHOW INDEX FROM tb WHERE key_name='PRIMARY';
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tb    |          0 | PRIMARY  |            1 | a           | A         |        NULL |     NULL | NULL   |      | BTREE      |         |
| tb    |          0 | PRIMARY  |            2 | b           | A         |           0 |     NULL | NULL   |      | BTREE      |         |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.02 sec)

If it were not a composite key, you would only get one row in the SHOW INDEX query:

CREATE TABLE tb2 (a int, b int, c int);
Query OK, 0 rows affected (0.05 sec)

ALTER TABLE tb2 ADD CONSTRAINT pk_tb PRIMARY KEY (a);
Query OK, 0 rows affected (0.05 sec)

SHOW INDEX FROM tb2 WHERE key_name='PRIMARY';
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tb2   |          0 | PRIMARY  |            1 | a           | A         |           0 |     NULL | NULL   |      | BTREE      |         |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.02 sec)
薄荷港 2024-09-09 00:19:57
SELECT COUNT( *  ) num_keys
FROM information_schema.KEY_COLUMN_USAGE
WHERE table_name = 'jos_modules_menu'
AND constraint_name = 'PRIMARY'
AND table_schema = 'pranav_test'

谢谢丹尼尔和普拉纳夫:)

SELECT COUNT( *  ) num_keys
FROM information_schema.KEY_COLUMN_USAGE
WHERE table_name = 'jos_modules_menu'
AND constraint_name = 'PRIMARY'
AND table_schema = 'pranav_test'

Thanks Daniel and Pranav :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文