mysql怎么查主键名称 查询数据库主键的sql语句
怎么看MYSQL表的主键
楼主用的什么样的客户端程序?不同的客户端程序查看的方式可能不一样。我用Navicat。选择表后点右键---选择设计表,在字段后面有个黄色的钥匙,那个字段就是主键。
创新互联是一家专注于网站设计制作、成都做网站与策划设计,个旧网站建设哪家好?创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:个旧等地区。个旧做网站价格咨询:13518219792
尽管客户端不同,但查看应该都是在设计表中。
mysql 主键查询是怎么查的
SELECT
t.TABLE_NAME,
t.CONSTRAINT_TYPE,
c.COLUMN_NAME,
c.ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c
WHERE
t.TABLE_NAME = c.TABLE_NAME
AND t.TABLE_SCHEMA = 'test'
AND t.CONSTRAINT_TYPE = 'PRIMARY KEY';
测试执行结果如下:
mysql SELECT
- t.TABLE_NAME,
- t.CONSTRAINT_TYPE,
- c.COLUMN_NAME,
- c.ORDINAL_POSITION
- FROM
- INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
- INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c
- WHERE
- t.TABLE_NAME = c.TABLE_NAME
- AND t.TABLE_SCHEMA = 'test'
- AND t.CONSTRAINT_TYPE = 'PRIMARY KEY'
- LIMIT 3;
+------------+-----------------+-------------+------------------+
| TABLE_NAME | CONSTRAINT_TYPE | COLUMN_NAME | ORDINAL_POSITION |
+------------+-----------------+-------------+------------------+
| mr_dept | PRIMARY KEY | dept_id | 1 |
| order | PRIMARY KEY | id | 1 |
| tab | PRIMARY KEY | id | 1 |
+------------+-----------------+-------------+------------------+
3 rows in set (0.06 sec)
怎么样去得到主键的列名在MySql中
哈哈,楼主遇到我算是你有福气了,我专门为你编写了一个函数,来获取表的主键名()(可能还有更简单的方法,但我不知道)。
请看如下代码:
?php
function get_primary($host,$user,$password,$tb) //定义函数,参数分别为主机名、用户名、密码、表名(还可以加入库名,我忘了)
{
mysql_connect($host,$user,$password); //连接主机
mysql_select_db("test"); //选择库
$result=mysql_query("DESCRIBE ".$tb); //分析表
while($rows=mysql_fetch_array($result)) //遍历结果集(即所有字段情况)
{
if($rows["Key"]=="PRI") //判断相应字段主键情况,如果是主键则跳出
break;
}
return $rows["Field"]; //返回所在字段名称
}
echo get_primary("localhost","root","","te"); //调用函数
?
MYSQL查询表的主键
SELECT
t.TABLE_NAME,
t.CONSTRAINT_TYPE,
c.COLUMN_NAME,
c.ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c
WHERE
t.TABLE_NAME = c.TABLE_NAME
AND t.TABLE_SCHEMA = 'test'
AND t.CONSTRAINT_TYPE = 'PRIMARY KEY';
测试执行结果如下:
mysql SELECT
- t.TABLE_NAME,
- t.CONSTRAINT_TYPE,
- c.COLUMN_NAME,
- c.ORDINAL_POSITION
- FROM
- INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
- INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c
- WHERE
- t.TABLE_NAME = c.TABLE_NAME
- AND t.TABLE_SCHEMA = 'test'
- AND t.CONSTRAINT_TYPE = 'PRIMARY KEY'
- LIMIT 3;
+------------+-----------------+-------------+------------------+
| TABLE_NAME | CONSTRAINT_TYPE | COLUMN_NAME | ORDINAL_POSITION |
+------------+-----------------+-------------+------------------+
| mr_dept | PRIMARY KEY | dept_id | 1 |
| order | PRIMARY KEY | id | 1 |
| tab | PRIMARY KEY | id | 1 |
+------------+-----------------+-------------+------------------+
3 rows in set (0.06 sec)
当前标题:mysql怎么查主键名称 查询数据库主键的sql语句
标题URL:http://pwwzsj.com/article/dospccg.html