Querying SQL table structure information including primary and foreign keys and auto-increment _ MySQL
Source: Internet
Author: User
Parse the SQL table structure information to query the data dictionary that needs to be done in the latest project including the primary and foreign keys and the self-increasing bitsCN.com. the table structure information is required. I have read a lot of queries on table structure information on the Internet, and I feel pretty bad. The better thing is 《 Detailed description of querying the upgraded version based on SQL2005 SQL2008 table structure information (including foreign key information)But there is a small problem here, and a small bug occurs when a filter is missing. Query Results of the Address table in the AdventureWorks2012 database
We can add the following information to the query filter:
AND g. class_desc = 'object _ OR_COLUMN'
The modified SQL statement is as follows:
SELECT table name = case when a. colorder = 1 THEN d. name
ELSE''
END,
Table description = case when a. colorder = 1 then isnull (f. value ,'')
ELSE''
END,
Field No. = a. colorder,
Field name = a. name,
Id = case when columnproperty (a. id, a. name, 'isidentity ') = 1 then' √'
ELSE''
END,
Primary key = case when exists (SELECT 1
FROM dbo. sysindexes si
Inner join dbo. sysindexkeys sik ON si. id = sik. id
AND si. indid = sik. indid
Inner join dbo. syscolumns SC ON SC. id = sik. id
AND SC. colid = sik. colid
Inner join dbo. sysobjects so ON so. name = so. name
AND so. xtype = 'PK'
WHERE SC. id = a. id
AND SC. colid = a. colid) THEN '√'
ELSE''
END,
Foreign key = case when tony. fkey IS NOT NULL
AND tony. fkey = a. colid THEN '√'
ELSE''
END,
Foreign key table = case when tony. fkey IS NOT NULL
AND tony. fkey = a. colid THEN OBJECT_NAME (tony. fkeyid)
ELSE''
END,
Foreign key field = case when tony. fkey IS NOT NULL
AND tony. fkey = a. colid
THEN (SELECT name
FROM syscolumns
WHERE colid = tony. fkey
AND id = tony. fkeyid
)
ELSE''
END,
Type = B. name,
Length = a. length,
PRECISION = COLUMNPROPERTY (a. id, a. name, 'Precision '),
Decimal places = ISNULL (COLUMNPROPERTY (a. id, a. name, 'scale'), 0 ),
Allow null = case when a. isnullable = 1 THEN '√'
ELSE''
END,
Default value = ISNULL (e. text ,''),
Field description = ISNULL (g. [value], ''),
Creation time = d. crdate,
Change time = case when a. colorder = 1 THEN d. refdate
ELSE NULL
END
FROM dbo. syscolumns
Left join dbo. policypes B ON a. xtype = B. xusertype
Inner join dbo. sysobjects d ON a. id = d. id
AND d. xtype = 'u'
AND d. status> = 0
Left join dbo. syscomments e ON a. cdefault = e. id
Left join sys. extended_properties g ON a. id = g. major_id
AND a. colid = g. minor_id
AND g. class_desc = 'object _ OR_COLUMN'
Left join sys. extended_properties f ON d. id = f. major_id
AND f. minor_id = 0
Left join sysobjects htl ON htl. parent_obj = d. id
AND htl. xtype = 'F'
Left join sysforeignkeys tony ON htl. id = tony. constid
WHERE d. name = 'address' -- enter the conditions that contain the table name.
Order by d. id,
A. colorder
Running result
I don't like its "type" information very much. generally, varchar will have length information, and this query is not supported for new data types in SQL 2012. There are many nested queries in this SQL statement, so I re-wrote an SQL statement.
We recommend that you use the INFORMATION_SCHEMA.XXX view instead of the sys. XXX view.
The new SQL statement is as follows:
SELECT
-- OBJECT_ID (a. TABLE_SCHEMA + '.' + a. TABLE_NAME) AS [object_id],
Case when a. ORDINAL_POSITION = 1
THEN a. TABLE_SCHEMA + '.' + a. TABLE_NAME
ELSE''
End as TABLE_NAME,
Case when (a. ORDINAL_POSITION = 1
AND p1.value IS NOT NULL
) THEN p1.value
ELSE''
End as TABLE_Description,
A. COLUMN_NAME,
Case when (CHARINDEX ('char ', a. DATA_TYPE)> 0
Or charindex ('binary ', a. DATA_TYPE)> 0
)
AND a. CHARACTER_MAXIMUM_LENGTH <>-1
)
THEN a. DATA_TYPE + '('
+ CAST (a. CHARACTER_MAXIMUM_LENGTH as varchar (4) + ')'
WHEN (CHARINDEX ('char ', a. DATA_TYPE)> 0
Or charindex ('binary ', a. DATA_TYPE)> 0
)
AND a. CHARACTER_MAXIMUM_LENGTH =-1
) THEN a. DATA_TYPE + '(max )'
WHEN (CHARINDEX ('numeric ', a. DATA_TYPE)> 0)
THEN a. DATA_TYPE + '(' + CAST (a. NUMERIC_PRECISION as varchar (4 ))
+ ',' + CAST (a. NUMERIC_SCALE as varchar (4) + ')'
ELSE a. DATA_TYPE
End as COLUMN_TYPE,
Case when c. IS_IDENTITY = 1 THEN 'yes'
ELSE 'no'
End as IS_IDENTITY,
A. IS_NULLABLE,
Case when a. COLUMN_DEFAULT is null then''
ELSE a. COLUMN_DEFAULT
End as Default_Value,
Case when p. value is null then''
ELSE p. value
End as [COLUMN_Description],
Case when o. name is null then''
ELSE '√'
End as Is_PrimaryKey,
Case when f. parent_column_id is null then''
ELSE '√'
End as Is_Foreignkeys,
Case when referenced_object_id is null then''
ELSE OBJECT_NAME (referenced_object_id)
End as Foreign_Table,
Case when referenced_object_id is null then''
ELSE (SELECT name
FROM sys. columns
WHERE object_id = f. referenced_object_id
AND column_id = f. referenced_column_id
)
End as Foreign_key
FROM INFORMATION_SCHEMA.COLUMNS
Inner join sys. columns c ON OBJECT_ID (a. TABLE_SCHEMA + '.'
+ A. TABLE_NAME) = c. OBJECT_ID
AND a. COLUMN_NAME = c. NAME
Left join INFORMATION_SCHEMA.KEY_COLUMN_USAGE B ON a. TABLE_SCHEMA = B. TABLE_SCHEMA
AND a. TABLE_NAME = B. TABLE_NAME
AND a. COLUMN_NAME = B. COLUMN_NAME
Left join sys. sysobjects o ON o. name = B. CONSTRAINT_NAME
AND o. xtype = 'PK'
Left join sys. extended_properties p ON OBJECT_ID (a. TABLE_SCHEMA + '.'
+ A. TABLE_NAME) = p. major_id
AND a. Ordinal_position = p. minor_id
AND p. class_desc = 'object _ OR_COLUMN'
Left join sys. extended_properties p1 ON OBJECT_ID (a. TABLE_SCHEMA + '.'
+ A. TABLE_NAME) = p1.major _ id
AND p1.minor _ id = 0
Left join sys. foreign_key_columns f ON OBJECT_ID (a. TABLE_SCHEMA + '.'
+ A. TABLE_NAME) = f. parent_object_id
AND a. ORDINAL_POSITION = f. parent_column_id
WHERE a. TABLE_NAME = 'address'
-- A. TABLE_NAME IN (SELECT name FROM sys. tables)
Order by a. TABLE_SCHEMA, a. TABLE_NAME, a. ORDINAL_POSITION
Running effect
If something is wrong, please make a brick! Thank you! BitsCN.com
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service