SQL Experience Sharing (2) obtaining all the table names, field names, and tables in the database

Source: Internet
Author: User

2.1 obtain the names of all tables in the database.

Sample Data:


Statement:

Select T. Name 'table name' from sysobjects t where objectproperty (T. ID,
N 'isusertable') = 1

Or use select name from sysobjects where type = 'U'

Execution result:

2.2 obtain the names of all fields in all tables

Statement:

Select distinct C. Name 'field name'From sysobjects
T, syscolumns C
Where T. ID = C. ID 
AndObjectproperty (T. ID, n'isusertable') = 1

Execution result:


Note: There are many fields, which are omitted below

 

2.3 query all tables and fields

Statement: Select T. Name 'table name', C. name' field name'From
Sysobjects T, syscolumns C
Where T. ID = C. ID 
AndObjectproperty (T. ID, n'isusertable') =
1Group by T. Name, C. Name

Execution result:


Note: There are many fields, which are omitted below

 

2.4 query all fields in a table

Original table_1 data:


Statement:
Select T. Name, C. Name 'field name'From sysobjects t,
Syscolumns C
Where T. ID = C. ID 
AndObjectproperty (T. ID, n'isusertable') =
1
AndT. Name = 'table _ 1'

Execution result:

 

2.5 check which table the field belongs to (that is, find the table containing the same field)

Queries which tables have name Columns

Statement:

Select distinct T. name from sysobjects T, syscolumns C
Where T. ID = C. ID 
AndObjectproperty (T. ID, n'isusertable') =
1
And C. Name in ('name ')

Execution result:

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.