Вы находитесь на странице: 1из 5

11/6/2015

UsingMetadataSQLTeam.com

Search:

Home|Weblogs|Forums|SQLServerLinks

Go

ActiveForumTopics|PopularArticles|AllArticlesbyTag|SQLServerBooks|About

UsingMetadata
ByBillGraziano

on24March2003|2Comments|Tags:DatabaseDesign

ThesimplestdefinitionIcanfindformetadataissimply"dataaboutdata".SQLServerhasanumberof
differentfunctionsthatyoucanusetoqueryyourdatabasestructure.Thisarticlesdiscussesthe
InformationSchemaviewsandfunctionssuchasObjectPropertyandColumnProperty.
TheInformationSchemaviewsarepartoftheSQL92standard.Wepublishedashortarticleonthem
earlier.TheSQL92standarddefinedanumberofviewsthatwouldprovideinformationaboutthe
database.Forexample,there'saviewcalledTABLESthatprovidesinformationaboutthetablesina
database.Youcanqueryitjustlikeanyotherview.Thequery
select *
from pubs.information_schema.tables

Subscribeto
SQLTeam.com
WeeklySQLServernewsletter
witharticles,forumposts,
andblogpostsviaemail.
Subscribersreceiveour
whitepaperwith
performancetipsfor
developers.

Subscribe
SQLTeam.comArticlesvia
RSS
SQLTeam.comWeblogvia
RSS

willreturninformationonallthetablesandviewsinthepubsdatabase:
Advertisement

TABLE_CATALOG
--------------pubs
pubs
...
pubs

TABLE_SCHEMA
--------------dbo
dbo

TABLE_NAME
------------------------authors
discounts

TABLE_TYPE
---------BASE TABLE
BASE TABLE

dbo

titleview

VIEW

Resources
SQLServerResources

Myresultsetalsoincludedsomesystemobjectsthatareusedforreplication.Theseincludetablessuch

AdvertiseonSQLTeam.com
SQLServerBooks
SQLTeam.comNewsletter

http://www.sqlteam.com/article/usingmetadata

1/5

11/6/2015

UsingMetadataSQLTeam.com

assysarticlesandsyspublications.TheTABLE_CATALOGisthedatabasenameandTABLE_SCHEMAisthe
objectowner.BesuretoincludeINFORMATION_SCHEMAastheowneroftheview.

ContactUs
AbouttheSite

AnotherinterestingviewistheCOLUMNSview.Thefollowingquery
select

TABLE_CATALOG,
TABLE_SCHEMA,
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH
from pubs.information_schema.columns
where table_name = 'authors'
willreturninformationaboutthecolumnsintheauthorstable:
TABLE_CATALOG
------------pubs
pubs
pubs
pubs
pubs
pubs
pubs
pubs
pubs

TABLE_SCHEMA
-----------dbo
dbo
dbo
dbo
dbo
dbo
dbo
dbo
dbo

TABLE_NAME
---------authors
authors
authors
authors
authors
authors
authors
authors
authors

COLUMN_NAME
----------au_id
au_lname
au_fname
phone
address
city
state
zip
contract

DATA_TYPE
---------varchar
varchar
varchar
char
varchar
varchar
char
char
bit

CHARACTER_MAXIMUM_LENGTH
-----------------------11
40
20
12
40
20
2
5
NULL

There'sactuallyquiteabitmoreinformationthanthatbutthisisallIcouldfitonthescreen.Thereare
columnsforcolumnordinalposition,nullability,numericprecision,defaults,characterset,sortorder
andanyinformationonuserdefineddatatypes.BelowisatablelistingalltheINFORMATION_SCHEMA
views:
ViewName

Description

CHECK_CONSTRAINTS

Holdsinformationaboutconstraintsinthedatabase

COLUMN_DOMAIN_USAGE

Identifieswhichcolumnsinwhichtablesareuserdefineddatatypes

http://www.sqlteam.com/article/usingmetadata

2/5

11/6/2015

UsingMetadataSQLTeam.com

COLUMN_PRIVILEGES

Hasonerowforeachcolumnlevelpermissiongrantedtoorbythe
currentuser

COLUMNS

Listsonerowforeachcolumnineachtableorviewinthedatabase

CONSTRAINT_COLUMN_USAGE Listsonerowforeachcolumnthathasaconstraintdefinedonit
CONSTRAINT_TABLE_USAGE

Listsonerowforeachtablethathasaconstraintdefinedonit

DOMAIN_CONSTRAINTS

Liststheuserdefineddatatypesthathaverulesboundtothem

DOMAINS

Liststheuserdefineddatatypes

KEY_COLUMN_USAGE

Listsonerowforeachcolumnthat'sdefinedasakey

PARAMETERS

Listsonerowforeachparameterinastoredprocedureoruserdefined
function

REFERENTIAL_CONSTRAINTS

Listsonerowforeachforeignconstraint

ROUTINES

Listsonerowforeachstoredprocedureoruserdefinedfunction

ROUTINE_COLUMNS

Containsonerowforeachcolumnreturnedbyanytablevalued
functions

SCHEMATA

Containsonerowforeachdatabase

TABLE_CONSTRAINTS

Listsonerowforeachconstraintdefinedinthecurrentdatabase

TABLE_PRIVILEGES

Hasonerowforeachtablelevelpermissiongrantedtoorbythe
currentuser

TABLES

Listsonerowforeachtableorviewinthecurrentdatabase

VIEW_COLUMN_USAGE

Listsonerowforeachcolumninaviewincludingthebasetableofthe
columnwherepossible

VIEW_TABLE_USAGE

Listsonerowforeachtableusedinaview

VIEWS

Listsonerowforeachview

BooksOnlinehasdetailsofeachviewincludingacompletedescriptionoftheresultseteachview
returns.
MetaDataFunctions
SQLServeralsohasanumberoffunctionsthatreturninformationaboutobjectsinthedatabase.One
thatIrecentlyhadanopportunitytouseistheCOLUMNPROPERTYfunction.Runningthefollowingquery
http://www.sqlteam.com/article/usingmetadata

3/5

11/6/2015

UsingMetadataSQLTeam.com

inNorthwind
SELECT COLUMNPROPERTY( OBJECT_ID('Categories'),'CategoryID','IsIdentity')
returns1whichindicatesthatCategoryIDisanidentitycolumn.Thereareadditionalfunctionsthat
returninformationabouttheidentitycolumn.TheColumnPropertyfunctionhasquiteafewpropertiesit
cancheckincludingnullability,precision,scale,etc.Manyofthesearealsointheinformationschema
viewsbutsomearen't.BooksOnlinehasthecompletelist.
WealsousedtheOBJECT_IDfunctioninthatquery.ManyofthesefunctionsonlyacceptanobjectID
andweusethisfunctiontoreturnanobjectIDgivenanobjectname.TheOBJECT_NAMEfunctionwill
returnthenamegivenanobjectID.
AnotherhandyfunctionistheObjectPropertyfunction.ItworksliketheColumnPropertybuthasmany
morepropertiesitcancheck.Forexample,thefollowingquerywillshowyouwhichtableshave
identities,clusteredindexesandprimarykeys.
select

table_name,
IDNTY = objectproperty(object_id(TABLE_NAME), 'TableHasIdentity'),
CLSTRD = objectproperty(object_id(TABLE_NAME), 'TableHasClustIndex'),
PK = objectproperty(object_id(TABLE_NAME), 'TableHasPrimaryKey')
from information_schema.tables
where table_type = 'base table'
Youcanalsocheckpropertiesforwhetherornotaprimarykeyisaclusteredindex.Prettyhandyona
projectwheredeveloperscancreatetheirowntables.AdditionalfunctionsincludeIndexProperty,
DatabaseProperty,FileGroupProperty,FullTextPropertyandafewothers.BooksOnlinehasadditional
informationaboutthese.
That'smylittletourthroughsomeofthemetadatafunctionsthatSQLServerprovidestogiveyou
informationaboutwhatthestructureofthedatabaselookslike.

Discussthisarticle:2Commentssofar.PrintthisArticle.

Ifyoulikethisarticleyoucansignupforourweeklynewsletter.There'san
optoutlinkatthebottomofeachnewslettersoit'seasytounsubscribeatany
http://www.sqlteam.com/article/usingmetadata

4/5

11/6/2015

UsingMetadataSQLTeam.com

time.
EmailAddress:

Subscribe

RelatedArticles

OtherRecentForumPosts

UsingSETNULLandSETDEFAULTwithForeignKey
Constraints(12August2008)

Corruptedinnodbtablecrashingmysql
(3Replies)

ImplementingTableInterfaces(19May2008)
ImplementingTableInheritanceinSQLServer(20
February2008)
TheDailyDatabaseBuild(23August2004)
HOWTO:MoveaDatabaseDiagram(12December2003)
DatabaseDesignThoughtsforPackagedApplications(26
October2003)
Thecurrentstateofdatabaseresearch(16September
2003)
DatabaseDesignConcepts(3June2002)

Corrupted.dbfdatabasefiles(3Replies)
Helpwithcount(0Replies)
HowtorecoverdatafromcorruptSQL
database?(5Replies)
Findduplicaterows(0Replies)
RepairCorruptAccessDatabaseFile
(7Replies)
Howtoextractapartofastringinthe
columnre(2Replies)
pdftogif(jpeg)(3Replies)

20002015SQLTeamPublishing,LLC|PrivacyPolicy

http://www.sqlteam.com/article/usingmetadata

5/5

Вам также может понравиться