如何确定两个表之间共享哪些列?

[英]How to determine which columns are shared between two tables?


Very new to SQL Sever here... I understand the concept of joining tables, etc. but what is the easiest way to determine which columns are shared?

这里对SQL Sever很新......我理解连接表等的概念但是确定共享哪些列的最简单方法是什么?

Say for instance we have Table 1 and Table 2, assume table 1 has over 100 columns as does table 2, but they only have 1 column in common.

比如说我们有表1和表2,假设表1和表2一样有100多列,但它们只有1列共同。

Is there a simple way to check to see what column / if any are shared without annoyingly going in and checking?

有没有一种简单的方法来检查哪些列/如果有任何共享而不烦人地进入并检查?

Pretty trivial question but very useful. Thanks

相当琐碎的问题,但非常有用。谢谢

7 个解决方案

#1


9  

You can find data like this in the INFORMATION_SCHEMA tables. Technically those are more standardized than the sys views. (See this question.)

您可以在INFORMATION_SCHEMA表中找到这样的数据。从技术上讲,这些比sys视图更标准化。 (见这个问题。)

Here's a query you could use:

这是您可以使用的查询:

select A.COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS A
join INFORMATION_SCHEMA.COLUMNS B
  on A.COLUMN_NAME = B.COLUMN_NAME
where A.TABLE_NAME = 'table1'
  and B.TABLE_NAME = 'table2'

If you need to specify the schema to avoid name collisions, add A.TABLE_SCHEMA = 'dbo' etc to the where clause.

如果需要指定模式以避免名称冲突,请将A.TABLE_SCHEMA ='dbo'等添加到where子句中。

#2


3  

Use the INFORMATION_SCHEMA.COLUMNS like this:

像这样使用INFORMATION_SCHEMA.COLUMNS:

IF OBJECT_ID('Table1') IS NOT NULL DROP TABLE Table1
IF OBJECT_ID('Table2') IS NOT NULL DROP TABLE Table2
GO
CREATE TABLE Table1 (
    a INT
  , b INT
  , c INT
  , d INT
  , e INT
  , f INT
)

CREATE TABLE Table2 (
    c INT
  , d INT
  , e INT
  , f INT
  , g INT
  , h INT
  , i INT
)

GO

SELECT t1.COLUMN_NAME 
FROM        INFORMATION_SCHEMA.COLUMNS AS t1 
INNER JOIN  INFORMATION_SCHEMA.COLUMNS AS t2 ON t1.COLUMN_NAME = t2.COLUMN_NAME 
WHERE t1.TABLE_NAME = 'Table1' AND t2.TABLE_NAME = 'Table2'

-- OUTPUT

- 输出

COLUMN_NAME
c
d
e
f

#3


2  

select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'Table1'

intersect

select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'Table2'

#4


1  

This could well indicate a fundamental design issue but to find column names shared by both tables a couple of options would be

这很可能表明了一个基本的设计问题,但是要找到两个表共享的列名,可能会有两个选项

SELECT name 
FROM sys.columns 
WHERE object_id IN (object_id('dbo.Table1'),
                    object_id('dbo.Table2'))
GROUP BY name
HAVING COUNT(*) = 2

Or

要么

SELECT name 
FROM sys.columns 
WHERE object_id = object_id('dbo.Table1')
INTERSECT
SELECT name 
FROM sys.columns 
WHERE object_id = object_id('dbo.Table2')

#5


0  

Here is a handy query you can use to list out columns in a table:

这是一个方便的查询,可用于列出表中的列:

SELECT c.name ColumnName
FROM sys.columns c INNER JOIN
     sys.tables t ON c.object_id = t.object_id 
WHERE t.name = 'something'

And here is a JOIN you could use to find common column names:

这是一个可以用来查找常用列名的JOIN:

SELECT * 
FROM  (SELECT c.name ColumnName
        FROM sys.columns c INNER JOIN
             sys.tables t ON c.object_id = t.object_id 
        WHERE t.name = 'table1'
      )t1
JOIN (SELECT c.name ColumnName
        FROM sys.columns c INNER JOIN
             sys.tables t ON c.object_id = t.object_id 
        WHERE t.name = 'table2'
     )t2
ON t1.ColumnName = t2.ColumnName

#6


0  

To know if you have similar columns could potentially be trickier than the other solutions suggest. We might think that two columns are the same because they share the same name but in reality when you work in a large database with more than one person creating, removing, and/or changing the data structure inconsistencies can happen.

要知道你是否有类似的列可能比其他解决方案建议的更棘手。我们可能认为两列是相同的,因为它们共享相同的名称,但实际上当您在一个大型数据库中工作时,多个人创建,删除和/或更改数据结构时可能会出现不一致的情况。

The more parameters we check for likeness the more confident we can be that our columns are similar without manual inspection of raw data.

我们检查相似性的参数越多,我们就越有信心我们的列就相似而无需手动检查原始数据。

1. First, I suggest you run a query to understand the parameters of a given column.

1.首先,我建议您运行查询以了解给定列的参数。

SELECT 
    *
FROM 
    DATABASENAME.INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_NAME = N'TABLE1'

This will return several columns of meta data on the columns in the table. Some of the meta data I found interesting for uniqueness included...

这将在表中的列上返回多列元数据。我发现有些独特的元数据包括......

enter image description here

2. In my case I have identified the column attributes of COLUMN_NAME, IS_NULLABLE, AND DATA_TYPE to determine if my columns truly match.

2.就我而言,我已经确定了COLUMN_NAME,IS_NULLABLE和DATA_TYPE的列属性,以确定我的列是否真正匹配。

SELECT 
    DISTINCT A.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS A
    LEFT join INFORMATION_SCHEMA.COLUMNS B 
        ON A.COLUMN_NAME = B.COLUMN_NAME 
        AND A.DATA_TYPE = B.DATA_TYPE
        AND A.IS_NULLABLE = B.IS_NULLABLE
WHERE 
    A.TABLE_NAME = N'TABLE1'
    AND B.TABLE_NAME = N'TABLE2'

3. Concept Check... Maybe if when we JOIN using only COLUMN_NAME there are 10 matching columns. Maybe when we JOIN using COLUMN_NAME AND DATA_TYPE there are 7 matching columns. Maybe when we use all three conditions as in the example above there are 4 matching columns. Does it mean you can only JOIN on 4 matching columns...absolutely not. What it does mean is you will need to consider how to craft error handling and casting depending on how you intend to JOIN the tables. Point being is be careful performing JOIN on INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME as your results may be far from intended.

3.概念检查......也许如果我们仅使用COLUMN_NAME加入时有10个匹配的列。也许当我们使用COLUMN_NAME AND DATA_TYPE加入时,有7个匹配的列。也许当我们使用上面示例中的所有三个条件时,有4个匹配的列。这是否意味着你只能在4个匹配的列上加入......绝对不能。它的意思是你将需要考虑如何制作错误处理和转换,具体取决于你打算如何加入表格。要点是在INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME上执行JOIN,因为您的结果可能远非预期。

#7


0  

Use following query (name used to show coman columns list)

使用以下查询(用于显示coman列列表的名称)

select name from syscolumns s1 where id = object_id('table1') and            exists(select 1 from syscolumns s2 where s2.name = s1.name and s2.id = object_id('table2'))

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/06/13/fa04068536074ae72f0c809153b8e5f6.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号