{"id":2808,"date":"2012-10-17T19:15:28","date_gmt":"2012-10-17T19:15:28","guid":{"rendered":"http:\/\/craftydba.com\/?p=2808"},"modified":"2012-10-24T14:49:55","modified_gmt":"2012-10-24T14:49:55","slug":"database-files-object-part-2","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=2808","title":{"rendered":"Database Files &#038; Objects &#8211; Part 2"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/black-binary-data.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/black-binary-data-150x128.jpg\" alt=\"\" title=\"black-binary-data\" width=\"150\" height=\"128\" class=\"alignleft size-thumbnail wp-image-2794\" \/><\/a><\/p>\n<p>Today, I will be continuing my talk on database files and database objects that can be viewed through the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms180163.aspx\">system catalog views<\/a> or <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187961.aspx\">system stored procedures<\/a>.<\/p>\n<p>A Microsoft SQL Server database is composed of at least two operating system files: a primary data file and a transaction log file.  A secondary data files can be used to implement table partitioning or storage of data on different disks by filegroup.<\/p>\n<p>If we want to get a simple list of all the databases on the server, one can execute the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms178568.aspx\">sp_helpdb<\/a> system stored procedure.<\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- Older system stored proc - all databases\r\nsp_helpdb\r\ngo\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_db_all.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_db_all.jpg\" alt=\"\" title=\"sp_help_db_all\" width=\"912\" height=\"232\" class=\"aligncenter size-full wp-image-2802\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_db_all.jpg 912w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_db_all-300x76.jpg 300w\" sizes=\"auto, (max-width: 912px) 100vw, 912px\" \/><\/a><\/p>\n<p>Detailed information can be obtained by calling the stored procedure with the database name that we are interested in.<\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- Older system stored proc - just one database\r\nsp_helpdb @dbname = 'MATH';\r\ngo\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_db_one.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_db_one.jpg\" alt=\"\" title=\"sp_help_db_one\" width=\"745\" height=\"218\" class=\"aligncenter size-full wp-image-2803\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_db_one.jpg 745w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_db_one-300x87.jpg 300w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><\/a><\/p>\n<p>If we want to get a simple list of all the files that make up a database, one can execute the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms174307.aspx\">sp_helpfile<\/a> system stored procedure.<\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- Must select database\r\nuse [MATH]\r\ngo\r\n\r\n-- Older system stored proc - all files for current db\r\nsp_helpfile\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_file_all.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_file_all.jpg\" alt=\"\" title=\"sp_help_file_all\" width=\"744\" height=\"126\" class=\"aligncenter size-full wp-image-2804\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_file_all.jpg 744w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_file_all-300x50.jpg 300w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><\/a><\/p>\n<p>Detailed information can be obtained by calling the stored procedure with the file name that we are interested in.<\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- Must select database\r\nuse [MATH]\r\ngo\r\n\r\n-- Older system stored proc - just one file\r\nsp_helpfile @filename = 'FN_MATH_P1'\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_file_one.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_file_one.jpg\" alt=\"\" title=\"sp_help_file_one\" width=\"659\" height=\"48\" class=\"aligncenter size-full wp-image-2805\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_file_one.jpg 659w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/sp_help_file_one-300x21.jpg 300w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/a><\/p>\n<p>There is an old <a href=\"http:\/\/en.wikipedia.org\/wiki\/Clich%C3%A9\">cliche<\/a> that states &#8216;There are many ways to skin a cat&#8217;.  There are system catalog views in newer versions of SQL Server that supply the same or more information than the system stored procedures.<\/p>\n<p>A query of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms178534.aspx\">sys.databases<\/a> catalog view will show all databases on the server.  Please note that this view contains more information than the stored procedure.  Information on database attributes can be used to determine what databases do not your standard operating procedures (SOP) for your company.<\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- Must select database\r\nuse [MATH]\r\ngo\r\n\r\n-- What is the properties all databases\r\nselect * from sys.databases\r\ngo\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-databases.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-databases.jpg\" alt=\"\" title=\"select-sys-databases\" width=\"1421\" height=\"241\" class=\"aligncenter size-full wp-image-2800\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-databases.jpg 1421w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-databases-300x50.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-databases-1024x173.jpg 1024w\" sizes=\"auto, (max-width: 1421px) 100vw, 1421px\" \/><\/a><\/p>\n<p>If you query the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms174397.aspx\">sys.database_files<\/a> catalog view, you will see each file that makes up the database.<\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- Must select database\r\nuse [MATH]\r\ngo\r\n\r\n-- Where is the properties of my current database\r\nselect * from sys.database_files\r\ngo\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-database-files.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-database-files.jpg\" alt=\"\" title=\"select-sys-database-files\" width=\"1429\" height=\"131\" class=\"aligncenter size-full wp-image-2822\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-database-files.jpg 1429w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-database-files-300x27.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-database-files-1024x93.jpg 1024w\" sizes=\"auto, (max-width: 1429px) 100vw, 1429px\" \/><\/a><\/p>\n<p>Every database has hidden tables used by service broker messaging, change data capture, file stream, table file, full text indexing, extended indexes, etc.  Some of these tables appear when advance features are turned on.  The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187799.aspx\">sys.internal_tables<\/a> catalog view lists these internal tables associated with the current database.<\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- hidden tables - current database\r\nselect * from sys.internal_tables\r\ngo\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-internal-files.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-internal-files.jpg\" alt=\"\" title=\"select-sys-internal-tables\" width=\"1418\" height=\"142\" class=\"aligncenter size-full wp-image-2801\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-internal-files.jpg 1418w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-internal-files-300x30.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-sys-internal-files-1024x102.jpg 1024w\" sizes=\"auto, (max-width: 1418px) 100vw, 1418px\" \/><\/a><\/p>\n<p>Last but not least, there are two catalog views that have detailed information on all files that are part of the SQL database server.  The view <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186782.aspx\">sys.master_files<\/a> contains all the visible system databases as well as the user databases.  I filter this for the MATH and visible system databases.  <\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- Must select database\r\nuse [MATH]\r\ngo\r\n\r\n-- One row in master per every database\r\nselect * from sys.master_files where database_id <= 7\r\ngo\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-master-files.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-master-files.jpg\" alt=\"\" title=\"select-master-files\" width=\"1458\" height=\"377\" class=\"aligncenter size-full wp-image-2799\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-master-files.jpg 1458w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-master-files-300x77.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-master-files-1024x264.jpg 1024w\" sizes=\"auto, (max-width: 1458px) 100vw, 1458px\" \/><\/a><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms181338.aspx\">sys.sysaltfiles<\/a> catalog view shows the hidden resource database.  It appears as datbase id 32767 with no name.<\/p>\n<pre><span style=\"color: #008000; font-size: small;\">-- Must select database\r\nuse [MATH]\r\ngo\r\n\r\n-- This view includes resource db\r\nselect * from sys.sysaltfiles where dbid <=7 or dbid = 32767\r\ngo\r\n<\/span><\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-alt-files.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-alt-files.jpg\" alt=\"\" title=\"select-alt-files\" width=\"1176\" height=\"400\" class=\"aligncenter size-full wp-image-2798\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-alt-files.jpg 1176w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-alt-files-300x102.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/10\/select-alt-files-1024x348.jpg 1024w\" sizes=\"auto, (max-width: 1176px) 100vw, 1176px\" \/><\/a><\/p>\n<p>To recap this discussion, system stored procedures can be used grab information about the logical databases and physical files.  Use system catalog views instead to get detailed information on both.  In the future, I am sure I will be writing TSQL scripts using these commands to do maintenance tasks where third party vendors come up short.  <\/p>\n<p>Next time, I will be talking about using system stored procedures and catalog views to look at the objects inside my user defined (sample) database named [MATH].<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I will be continuing my talk on database files and database objects that can be viewed through the system catalog views or system stored procedures. A Microsoft SQL Server database is composed of at least two operating system files: a primary data file and a transaction log file. A secondary data files can be used to implement table partitioning or storage of data on different disks by filegroup. If we want to get a simple list of all the databases on the server, one can execute the sp_helpdb system&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[31,12,15,583,584,28,320,585,586,587,588,568,567,29],"class_list":["post-2808","post","type-post","status-publish","format-standard","hentry","category-db-dev","tag-database-developer","tag-free-code","tag-john-f-miner-iii","tag-sp_helpdb","tag-sp_helpfile","tag-sql-server","tag-sys-databases","tag-sys-database_files","tag-sys-internal_tables","tag-sys-master_files","tag-sys-sysaltfiles","tag-system-catalog-views","tag-system-stored-procedures","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/2808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2808"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/2808\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}