EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQL Tutorial Metadata in SQL

Metadata in SQL

Roja Metla
Article byRoja Metla
Priya Pedamkar
Reviewed byPriya Pedamkar

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
 204+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

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

*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 Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more