EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial Metadata in SQL
 

Metadata in SQL

Roja Metla
Article byRoja Metla
EDUCBA
Reviewed byRavi Rathore

Updated March 13, 2023

Metadata in SQL

 

 

Introduction to Metadata in SQL

Metadata in simple words describe as data about data. Usually, the metadata returns the information about the database, db objects, db files, etc., in the SQL server. In relational databases metadata is said to be consisting the information regarding the schema, storage, etc., Metadata in databases for schema consists of information regarding tables, columns, constraints, foreign keys, indexes, and sequences. It also consists of information regarding the views, procedures, functions, and triggers. Access to this metadata is provided in the form of a set of tables or views called system catalog or data dictionary.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

To find ID of a schema object below syntax format is used:

SELECT OBJECT_ID(N'dbo.tablename');

Example:

SELECT OBJECT_ID(N'dbo.staging_employee');

To find the information about all the columns in db we use the below syntax: –

SELECT nameas "TABLE COLUMN NAMES",column_id AS " COLUMN ID"
FROM sys.columns/* - - - Predefined columns- - - */
WHERE object_id = OBJECT_ID(N’dbo.tablename');

Example:

SELECT name as " TABLE COLUMN NAMES",column_id AS " COLUMN ID"
FROM sys.columns/* - - - Predefined columns - - - */
WHERE object_id = OBJECT_ID(N'dbo.staging_employee');

To find the information related to the table we use the below syntax:

SELECT name AS "COLUMN NAME",max_column_id_used "MAX COLUMN ID USED"
FROM sys.tables

To find the information related to the schema we use the below syntax:

SELECT name AS "SCHEMA NAME",schema_id AS "SCHEMA ID"
FROM sys.schemas   /* - - - Predefined Schema - - - */
WHERE principal_id = DATABASE_PRINCIPAL_ID(N'DATABASENAME');

Example:

SELECT name AS "SCHEMA NAME",schema_id AS "SCHEMA ID"
FROM sys.schemas/* - - - Predefined Schema - - - */
WHERE principal_id=DATABASE_PRINCIPAL_ID(N'dbo');

How does Metadata Work in SQL?

Metadata related result set operations are used in our daily application. For example, we have data types of a column the data type related information can be found in the metadata. We can also get the information related to the length of the column and how much space storage has been taken.

The main usage of metadata is especially in ad hoc queries; we won’t know the metadata of the result set. To get those metadata we need to use the functions SQLNumResultCols, SQLDescribeCol, and SQLColAttribute.

To retrieve result set metadata, we use SQLDescribeCol and SQLColAttribute. SQLDescribeCol always returns the same five pieces of information (a column’s name, data type, precision, scale, and null ability. A single piece of information is returned by the SQLCOLAttribute. However, SQLColAttribute can return a sophisticated selection of metadata, including the information of the column’s case-sensitivity, display size, updatability, and search ability.

Most of the applications that require only the metadata returned by SQLDescribeCol. SQLDescribeCol is faster than the SQLColAttribute because the information is returned in a single call. Rest of the application use SQLColAttribute to require additional metadata and use both the functions.

Metadata is often very expensive to get it from the data source. Because of this reason, we need drivers to cache any metadata. This driver will retrieve it from the server and hold it. It would be easier for the application to request only the metadata that is absolutely needed.

Examples of Metadata in SQL

1. To find ID of a schema object below syntax format is used.

SELECT OBJECT_ID(N'dbo.tablename');

Example for the above:

SELECT OBJECT_ID(N'dbo.staging_employee')as "OBJECT ID";

Metadata in SQL-1.1

SELECT OBJECT_ID(N'dbo.staging_product')as "OBJECT ID";

Metadata in SQL-1.2

SELECT OBJECT_ID(N'dbo.staging_customer')as "OBJECT ID";

Metadata in SQL-1.3

SELECT OBJECT_ID(N'dbo.staging_purchaseorder')as "OBJECT ID";

Metadata in SQL-1.4

2. To find the information about all the columns in db we use the below syntax:

SELECT name as "TABLE COLUMN NAMES",column_id AS " COLUMN ID"
FROM sys.columns
WHERE object_id = OBJECT_ID(N'dbo.tablename');

Example for the above:

SELECT name as "TABLE COLUMN NAMES",column_id AS " COLUMN ID"
FROM sys.columns /* - - - Predefined column table - - - */
WHERE object_id = OBJECT_ID(N'dbo.staging_employee');

Metadata in SQL-2.1

SELECT name as "TABLE COLUMN NAMES",column_id AS " COLUMN ID"
FROM sys.columns /* - - - Predefined column table - - - */
WHERE object_id = OBJECT_ID(N'dbo.staging_product');

Metadata in SQL-2.2

SELECT name as "TABLE COLUMN NAMES",column_id AS " COLUMN ID"
FROM sys.columns /* - - - Predefined column table - - - */
WHERE object_id = OBJECT_ID(N'dbo.staging_customer');

Output-2.3

SELECT name as "TABLE COLUMN NAMES",column_id AS " COLUMN ID"
FROM sys.columns
WHERE object_id = OBJECT_ID(N'dbo.staging_purchaseorder');

Output-2.4

3. To find the information related to the table we use the below syntax:

SELECT name AS "COLUMN NAME",max_column_id_used "MAX COLUMN ID USED"
FROM sys.tables

Output-3.1

4. To find the information related to the schema we use the below syntax:

SELECT name AS "SCHEMA NAME",schema_id AS "SCHEMA ID"
FROM sys.schemas
WHERE principal_id = DATABASE_PRINCIPAL_ID(N'DATABASENAME');

Example for the above:

SELECT name AS "SCHEMA NAME",schema_id AS "SCHEMA ID"
FROM sys.schemas
WHERE principal_id = DATABASE_PRINCIPAL_ID(N'dbo');

Conclusion

Metadata in simple words describe as data about data. Usually, the metadata returns the information about the database, db objects, db files, etc., in the SQL server. In relational databases metadata is said to be consisting the information regarding the schema, storage, etc., Metadata in databases for schema consists of information regarding tables, columns, constraints, foreign keys, indexes, and sequences.

Recommended Articles

We hope that this EDUCBA information on “Metadata in SQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. ROLLUP in SQL
  2. CUBE in SQL
  3. Types of NoSQL Databases
  4. What is MySQL Schema?
  5. Guide to SQL While Loop
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW