如何获取表的主键? [英] How to get primary key of table?

查看:49
本文介绍了如何获取表的主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从mysql-database获取主键字段的名称?例如:

Is there a way to get the name of primary key field from mysql-database? For example:

我有一个这样的表:

+----+------+
| id | name |
+----+------+
| 1  | Foo1 |
| 2  | Foo2 |
| 3  | Foo3 |
+----+------+

其中字段ID是主键(它具有自动递增功能,但我不能使用它).如何在php中检索名称为"id"的字段?

Where the field id is primary key (it has auto increment but I can't use that). How can I retrieve fields name "id" in php?

推荐答案

更好的方法是使用SHOW KEYS,因为您并非总是可以访问information_schema.以下作品:

A better way is to use SHOW KEYS since you don't always have access to information_schema. The following works:

SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'

Column_name将包含主键的名称.

Column_name will contain the name of the primary key.

这篇关于如何获取表的主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆