有没有办法从mysql数据库获取主键字段的名称?例如:
我有一个这样的表:
+----+------+
| id | name |
+----+------+
| 1 | Foo1 |
| 2 | Foo2 |
| 3 | Foo3 |
+----+------+
其中字段ID是主键(它具有自动递增功能,但我不能使用它)。如何在php中检索字段名称“ id”?
Answers:
一种更好的方法是使用,SHOW KEYS
因为您并不总是可以访问information_schema。以下作品:
SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'
Column_name将包含主键的名称。
column_name
密钥?
cut
,如果你想只列名:`| cut -f5`
这是主键列名
SELECT k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k
USING(constraint_name,table_schema,table_name)
WHERE t.constraint_type='PRIMARY KEY'
AND t.table_schema='YourDatabase'
AND t.table_name='YourTable';
PRIMARY
SELECT kcu.column_name, kcu.ordinal_position
FROM information_schema.table_constraints tc
INNER JOIN information_schema.key_column_usage kcu
ON tc.CONSTRAINT_CATALOG = kcu.CONSTRAINT_CATALOG
AND tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
AND tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE tc.table_schema = schema() -- only look in the current schema
AND tc.constraint_type = 'PRIMARY KEY'
AND tc.table_name = '<your-table-name>' -- specify your table.
ORDER BY kcu.ordinal_position
constraint_name
看起来像UK_entity_name
,PK_Users
,FK_Users_Contacts
等等,所以它似乎并不值得信任有一个众所周知的平台无关的值。😞
这个怎么样:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'Your Database'
AND TABLE_NAME = 'Your Table name'
AND COLUMN_KEY = 'PRI';
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'Your Database'
AND TABLE_NAME = 'Your Table name'
AND COLUMN_KEY = 'UNI';
db
通过TABLE_SCHEMA重命名...?我不明白您为什么为此获得+1 ...
对于PHP方法,可以使用mysql_field_flags
$q = mysql_query('select * from table limit 1');
for($i = 0; $i < mysql_num_fields(); $i++)
if(strpos(mysql_field_tags($q, $i), 'primary_key') !== false)
echo mysql_field_name($q, $i)." is a primary key\n";
最短的代码似乎是这样的
// $dblink contain database login details
// $tblName the current table name
$r = mysqli_fetch_assoc(mysqli_query($dblink, "SHOW KEYS FROM $tblName WHERE Key_name = 'PRIMARY'"));
$iColName = $r['Column_name'];
终于知道了!
<?php
function mysql_get_prim_key($table){
$sql = "SHOW INDEX FROM $table WHERE Key_name = 'PRIMARY'";
$gp = mysql_query($sql);
$cgp = mysql_num_rows($gp);
if($cgp > 0){
// Note I'm not using a while loop because I never use more than one prim key column
$agp = mysql_fetch_array($gp);
extract($agp);
return($Column_name);
}else{
return(false);
}
}
?>
我用SHOW INDEX FROM table; 它给我很多信息; 如果键是唯一的,则它在索引中的顺序,排序规则,子部分(如果为null),类型和注释(如果存在),请参阅此处的屏幕快照
MySQL有一个SQL查询“ SHOW INDEX FROM”,它从表中返回索引。例如。-以下查询将显示产品表的所有索引:-
SHOW INDEXES FROM products \G
它返回类型,column_name,Key_name等的表,并将所有索引和主键的输出显示为-
*************************** 1. row ***************************
Table: products
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: product_id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
要仅显示表中的主键,请使用:-
SHOW INDEXES FROM table_name WHERE Key_name = 'PRIMARY'
您应该从以下位置使用PRIMARY key_column_usage.constraint_name = "PRIMARY"
示例查询
SELECT k.column_name as PK, concat(tbl.TABLE_SCHEMA, '.`', tbl.TABLE_NAME, '`') as TABLE_NAME
FROM information_schema.TABLES tbl
JOIN information_schema.key_column_usage k on k.table_name = tbl.table_name
WHERE k.constraint_name='PRIMARY'
AND tbl.table_schema='MYDB'
AND tbl.table_type="BASE TABLE";
SELECT k.column_name
FROM information_schema.key_column_usage k
WHERE k.table_name = 'YOUR TABLE NAME' AND k.constraint_name LIKE 'pk%'
我建议您注意所有领域