简体   繁体   English

SQL查询以列出所有表字段和键(外部和主键)

[英]SQL Query to list all Table fields and Keys (Foreign & Primary)

o/ Hello All, o /大家好,

I am trying to create a schema for an ERP that, laughable, doesn't have one for the 62 tables in its database. 我正在尝试为ERP创建一个架构,可笑的是,它的数据库中没有62个表。 We, who I work for, don't have a DBA and thus am trying to sort through this on my own. 我为之工作的我们没有DBA,因此我试图自己解决这个问题。 In addition, I do not have DB owner rules and believe this limits what I can view; 另外,我没有数据库所有者规则,并且认为这限制了我可以查看的内容。 however, I have confirmed that is no standard schema/diagram 但是,我已经确认这不是标准的架构/图表

I searched and tried about a dozen queries I found on here but wasn't getting the information I am looking for. 我搜索并尝试了大约12条查询,但没有获得所需的信息。

Is there a sysobjects query that I can use to list all of the table's fields and appropriate keys? 是否可以使用sysobjects查询列出表的所有字段和适当的键? Aaaaaaaand, I'm new to SQL. aa,我是SQL新手。 I can get around with queries, but on the administration side I have no clue. 我可以处理查询,但是在管理方面我不知道。 I am working on MS SQL Server Management Studio 2017. 我正在研究MS SQL Server Management Studio 2017。

Thanks good people. 谢谢好人。

Partial view of tables 表的局部视图

PS I understand this may appear as a duplicate, but I have been told time and time again by our IT department that the ERP's databases is a mess and was why they have been reluctant to auth me access. 附言:我知道这可能是重复的,但我们的IT部门一次又一次告诉我,ERP的数据库很乱,这就是为什么它们不愿对我进行身份验证的原因。 I think our ideas of standards may not be applicable with this particular database, hence the long post/descriptions. 我认为我们关于标准的想法可能不适用于该特定数据库,因此篇幅较长的说明/描述。

I havn't worked with MS SQL for a long time, but i think you can use the following: 我已经很长时间没有使用MS SQL了,但是我认为您可以使用以下代码:

describe table_name OR sp_columns table_name 描述table_name或sp_columns table_name

Hope that helps. 希望能有所帮助。

The following script will provide you with all the table names, column names, the data types for each column, width of the columns, whether a key is Primary or Secondary, and the referenced table and column for Foreign Keys. 以下脚本将为您提供所有表名,列名,每列的数据类型,列的宽度,键是主键还是辅助键,以及外键引用的表和列。

SELECT SA.TABLE_NAME,
       SA.COLUMN_NAME, 
       SA.DATA_TYPE, 
       SA.CHARACTER_MAXIMUM_LENGTH AS Column_Width,
       PK.type AS PRIMARY_KEY,
       FK.type AS Foriegn_KEY, 
       SAU.CONSTRAINT_NAME,
       SO.[Name] + '_' + SAU.COLUMN_NAME Referenced_Table_Column
FROM Stack.INFORMATION_SCHEMA.COLUMNS AS SA
LEFT JOIN STACK.INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS SAU
    ON SAU.COLUMN_NAME = SA.COLUMN_NAME
LEFT JOIN sys.foreign_keys AS FK
    ON FK.[NAME] = SAU.CONSTRAINT_NAME
LEFT JOIN sys.key_constraints AS PK
    ON PK.[NAME] = SAU.CONSTRAINT_NAME
LEFT JOIN sys.objects AS SO 
    ON SO.object_id = FK.referenced_object_id OR
       SO.object_id = PK.parent_object_id

Possible Limitations: 可能的局限性:

  1. You may not have enough permissions to the database to be able to run this query as it use sys tables and the information_schema. 您可能没有足够的权限来运行该查询,因为该数据库使用sys表和information_schema。

Work Arounds: 解决方法:

  1. Have you IT admins run the query for you and export the data to CSV or Excel file. 您的IT管理员是否已为您运行查询并将数据导出到CSV或Excel文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM