【问题标题】:How to find table name using primary key name in sql?如何在sql中使用主键名查找表名?
【发布时间】:2014-02-02 15:15:02
【问题描述】:

我在一个变量中有主键的名称,我需要找到它们所属的表。数据库有很多表,所以线性搜索不是一个选项。

【问题讨论】:

  • 您使用的是哪个数据库(Oracle、Mysql、Postgres ...)?

标签: sql sql-server tsql key


【解决方案1】:

您可以使用information_schema 表。如果主键名是表中的第一列,你可以这样做:

select table_name
from information_schema.columns
where column_name in (<your list here>) and
      ordinal_position = 1;

否则,您必须通过约束才能获得想要的东西。比如:

select kcu.table_name, kcu.column_name
from information_schema.table_constraints tc join
     information_schema.key_column_usage kcu
     on tc.contraint_name = kcu.contraint_name and
        tc.table_name = kcu.table_name
where tc.contraint_type = 'PRIMARY KEY' and
      column_name in (<your list here>);

您也可以使用系统表和视图来执行此操作。

【讨论】:

  • 第一个查询好像有bug。
【解决方案2】:

你可以试试这个::

SELECT table_name 
FROM information_Schema.columns 
WHERE column_name='dept_id' 
  and ordinal_position = 1;

【讨论】:

    【解决方案3】:

    这应该可以,请尝试一下:

    SELECT table_name
    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
    WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
    AND column_name in (select column_name from <your list here>)
    

    如果在“CREATE TABLE”查询中将主键约束设置为除第一列之外的任何列,则以下解决方案将不起作用。

    select table_name
    from information_schema.columns
    where column_name in (select column_name from <your list here>) and
    ordinal_position = 1
    

    例如,如果我们这样创建表,它就行不通了:

    create table sample1
    (
     field1 int,
     field2 int primary key
    )
    

    如果我错了,请纠正我。

    【讨论】:

      【解决方案4】:

      以下查询根据约束名称给出表名

      select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
      where CONSTRAINT_NAME ='<ConstraintName>'
      

      【讨论】:

        【解决方案5】:

        要使用主键名查找表名,请遵循以下查询

        select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where
        CONSTRAINT_NAME =<ConstraintName>
        
        ---Find the below views to get constraint details in a db select * from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS select * from
        INFORMATION_SCHEMA.TABLE_CONSTRAINTS select * from
        INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
        

        【讨论】:

          【解决方案6】:

          从上面的答案改进

           select kcu.table_name, kcu.column_name, *
              from information_schema.table_constraints tc join
              information_schema.key_column_usage kcu
              on tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME and
              tc.table_name = kcu.table_name
              where tc.CONSTRAINT_NAME = 'PK__DC_INPUT__7EA5540496A9FD5D' 
          

          【讨论】:

            【解决方案7】:
            SELECT [TABLE_NAME]
            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
            WHERE CONSTRAINT_NAME = 'YourPrimaryKey';
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-08-11
              • 1970-01-01
              • 2017-07-22
              • 1970-01-01
              • 2012-09-04
              • 1970-01-01
              相关资源
              最近更新 更多