如何判断表的id字段是否为主键 ? sql语句 ?请大家帮帮忙!

解决方案 »

  1.   

    desc table;
    还有
    show create table table名;
    用很多工具也可以呀。
      

  2.   


    mysql> show create table test;
    +-------+----------------------------------------------------------------------
    -------------------------------------------------------------------------------
    | test  | CREATE TABLE `test` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(3) default NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
    +-------+----------------------------------------------------------------------
    -------------------------------------------------------------------------------mysql> select column_key
        -> from information_schema.columns
        -> where table_schema='csdn' and table_name='test' and column_name='id';
    +------------+
    | column_key |
    +------------+
    | PRI        |
    +------------+
      

  3.   


    select id from tb where key="PRI";
      

  4.   

    zhoupuyue说的正确,根据你的数据库名,表名,列名,到information_schema这个库的columns表里查询,看你找的列对应的column_key的值是否为PRI,如果为空,就不是KEY.
      

  5.   

    5以上可以用系统表,
    select column_key
         from information_schema.columns
         where table_schema='csdn' and table_name='test' and column_name='id';
      

  6.   

    #3楼 
    select column_key
    from information_schema.columns
    where table_schema='csdn' and table_name='test' and column_name='id';