Printing Table's structure/schema

删除回忆录丶 提交于 2019-12-18 11:28:30

问题


I have like 20 or so tables in the database RentalEase and I want to print them out (physically) so I can look at them easier. I don't care too much about what data is in them, just their structure. How can I do this?

It's an SQL Express server and I'm using Microsoft SQL Server Management Studio Express to manage it. I remember back when I was using MySQL and PHP I could use a DESCRIBE to print it out but I don't remember how I did it. There doesn't seem to be a DESCRIBE for SQL Server


回答1:


try:

sp_help <table_name>



回答2:


You can always inspect the INFORMATION_SCHEMA views to find all the interesting information about tables and their columns.

It's not called "describe" per se - but this query will show you lots of information:

select * from Information_schema.Columns
where table_name = '(your table here)'

Marc




回答3:


In Management Studio,

  1. Click the "+" next to your database expanding the objects below it and click on "Tables"
  2. Open the Tables detail view by selecting "View" -> "Object Explorer Details" from the menu
  3. Now select all tables (on the right hand side in the object details)
  4. right click on any of the selected Tables (on the right hand side)
  5. "Script table As" -> "Create To"
  6. "File" or "Clipboard"

This will produce a script file containing all of the selected file schema definitions.




回答4:


You can use Database Schema Diagram Design Tool. Just drop all the tables there, and you will get the diagram of you database including all keys




回答5:


Here is a script I wrote that simply lists each table and their columns in a format like:

Table Columns

table1 column1,column2...columnX

The script is:

declare @max_tables int
declare @max_columns int
declare @sql nvarchar(400)
declare @x int
declare @y int
declare @table varchar(50)
declare @columns varchar(800)

create table #c ([Table] varchar(50),[Columns] varchar(800))

select ROW_NUMBER() OVER(ORDER BY name) AS Row, name 
into #table_list
from sys.objects 
where type_desc = 'USER_TABLE' 
order by name

set @max_tables = (select count(*) from sys.objects where type_desc = 'USER_TABLE')
set @y = 0

while @y < @max_tables
    begin
        set @y = @y + 1
        set @table = (select name from #table_list where row = @y)

        create table #t (c int)

        set @sql = 'select count(*) as c from Information_schema.Columns where table_name = ''' + @table + ''''
        insert into #t exec sp_executesql @sql

        set @max_columns = (select top 1 c from #t)

        DROP TABLE #t

        set @x = 0
        set @columns = ''

        while @x < @max_columns 
            begin
                set @x = @x + 1
                set @columns = @columns + (select column_name from Information_schema.Columns where table_name = @table and ordinal_position = @x)
                if @x < @max_columns set @columns = @columns + ', '
            end

        insert into #c select @table,@columns

    end

select * from #c

DROP TABLE #c
DROP TABLE #table_List


来源:https://stackoverflow.com/questions/549799/printing-tables-structure-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!