SQL statement queries the table name/column name/primary key/auto-increment instance in the database

Source: Internet
Author: User

SQL statement query the table name/column name/primary key/automatic Growth Value in the database

---- Query tables created by users in the database

---- Jsj01 indicates the database name.

Select name tablename from jsj01.. sysobjects where type = 'U' and name not in ('dtproperties ')

-- Query the field information in the table

--- Docs: Table Name
---- Select * from syscolumns where id = object_id ('docs ')

---- Query all types in the database
---- Select name, xtype from policypes

---- Two-table join query: display all fields and corresponding data types in the table

---- The field 'xtype' in syscolumns corresponds to 'xusertype' in policypes, And the 'name' field in policypes is the data type of the field.
---- Docs indicates the table name.
Select a. name as fieldname, B. name as type from
Syscolumns as
Join policypes as B
On a. xtype = B. xusertype
Where id = object_id ('docs ')

---- Docs is the data table name: queries table fields, types, and descriptions.

Select a. name fieldname, B. name type, c. value comment from
Syscolumns as
Full join policypes as B
On a. xtype = B. xusertype
Full join: fn_listextendedproperty (NULL, 'user', 'dbo', 'table', 'docs', 'column', default) as c ---- This is version 2000, 2005 change user to schema
On a. name = c. objname COLLATE Chinese_PRC_CI_AS ----- sorting rule)
-- Join sysproperties c
-- On a. id = c. major_id
Where id = object_id ('docs ')

---- The primary key in the query table. No primary key is blank. If multiple composite primary keys exist, multiple values pk is the primary key. fk is the foreign key.
--- Jsj01 is the database name. docs is the table name. fk represents the foreign key.

Select column_name as primarykey, * from
[Jsj01]. INFORMATION_SCHEMA.KEY_COLUMN_USAGE
Where Table_name = 'docs' and constraint_name like 'fk _ %'

-- Select * from sysobjects WHERE OBJECT_NAME (sysobjects. parent_obj) = 'docs' -- and xtype = 'pk'
-- Select * from sysconstraints where id = object_id ('docs ')
-- Select * from syscolumns where id = object_id ('docs ')
-- Select * from sysindexes
-- Select * from sysindexkeys

---- Query the Automatically increasing field in the table. It is not empty. If there is only one field
---- Docs indicates the table name.

SELECT a. name column_name, B. name data_type
FROM syscolumns a, policypes B
WHERE a. id = object_id ('docs') and a. xtype = B. xusertype
AND a. autoval is not null


Author: pukuimin1226

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.