390 likes | 485 Views
System Catalog for SQL Server. Syscolumns Syscomments Sysconstraints (view) Sysdepends Sysfilegroups Sysfiles Sysforeignkeys Sysfulltextcatalogs sysindexes. Sysindexkeys Sysmembers Sysobjects Syspermissions Sysprotects Sysreferences Systypes sysusers. Database System Tables.
E N D
Syscolumns Syscomments Sysconstraints (view) Sysdepends Sysfilegroups Sysfiles Sysforeignkeys Sysfulltextcatalogs sysindexes Sysindexkeys Sysmembers Sysobjects Syspermissions Sysprotects Sysreferences Systypes sysusers Database System Tables
Sysobjects • Contains one row for each object (constraint, default, log, rule, stored procedure, and so on) created within a database. • Xtype values • C = CHECK constraintD = Default or DEFAULT constraintF = FOREIGN KEY constraintL = LogFN = Scalar functionIF = Inlined table-functionP = Stored procedurePK = PRIMARY KEY constraint (type is K) • RF = Replication filter stored procedure S = System tableTF = Table functionTR = TriggerU = User tableUQ = UNIQUE constraint (type is K)V = ViewX = Extended stored procedure
Important Sysobject Columns • Name – name of object • Id – object identification number • Xtype – object type • Uid – user ID of object owner
Syscolumns • Contains one row for every column in every table and view, and a row for each parameter in a stored procedure. This table is in each database.
Important Syscolumns Columns • Name – name of column • Id – object ID of the table to which the column belongs • Xtype – physical storage type from systypes table • Length – maximum physical storage length from systypes table • Colid – column ID • Domain – ID of the rule or check constraint
Sysindexes • Contains one row for each index and table in the database. This table is stored in each database.
Important Columns for Sysindexes • ID - ID of table (for indid= 0 or 255). Otherwise, ID of table to which the index belongs. • Indid – ID of index • 1 = clustered index • 2 = nonclustered index • 3 = entry for tables that have text/image data • Minlen – Minimum row size • Keycnt – number of keys • Groupid – Filegroup ID where the object was created • Dpages - For indid = 0 or indid = 1, dpages is the count of data pages used. For indid=255, it is set to 0. Otherwise, it is the count of index pages used. • Name - Name of table (for indid= 0 or 255). Otherwise, name of index.
Important Columns for Sysindexes • Rowcnt - Data-level rowcount based on indid = 0 and indid = 1. For indid = 255, rowcnt is set to 0. • Rowmodctr - Counts the total number of inserted, deleted, or updated rows since the last time statistics were updated for the table. • OrigFillFactor - Original fillfactor value used when the index was created. This value is not maintained; however, it can be helpful if you need to re-create an index and do not remember what fillfactor was used. • Keys - List of the column IDs of the columns that make up the index.
Sysindexkeys • Contains information for the keys or columns in an index. This table is stored in each database.
Important Columns in Sysindexkeys • ID – ID of the table • Indid – ID of the index • Colid – ID of the column • Keyno – position of the column in the index
Sysconstraints • Contains mappings of constraints to the objects that own the constraints. This system catalog is stored in each database. • Sysconstraints is a view
Important Columns in Sysconstraints • Constid – constraint number • Id – ID of the table that owns the constraint • Colid – ID of the column on which the constraint is defined, 0 if a table constraint. • Status - Bitmap indicating the status.
Types of Constraints • Look at status • 1 = PRIMARY KEY constraint. • 2 = UNIQUE KEY constraint. • 3 = FOREIGN KEY constraint. • 4 = CHECK constraint. • 5 = DEFAULT constraint. • 16 = Column-level constraint. • 32 = Table-level constraint.
Sysforeignkeys • Constid - ID of the FOREIGN KEY constraint. • Fkeyid - Object ID of the table with the FOREIGN KEY constraint • Rkeyid - Object ID of the table referenced in the FOREIGN KEY constraint. • Fkey – ID of the referencing column • Rkey – ID of the referenced columne • Keyno – Position of the column in the reference column list.
Sysreferences • Contains mappings of FOREIGN KEY constraint definitions to the referenced columns. This table is stored in each database.
Important columns in Sysreferences • Constid • Fkeyid • Rkeyid • Rkeyindid – Index ID of the unique index on the referenced table covering the referenced key-columns • Keycnt – number of columns in the key • Fkey1-16 – Column ID of the referencing columns • Rkey1-16 – Column ID of the referenced columns
Access to the Catalog • Enterprise Manager • Select Statements • System Stored Procedures
System Stored Procedures • These are stored procedures that are provided with SQL Server • They are identifiable by the ‘sp_’ prefix that the begins the name
Store Procedures • Sp_tables • Sp_columns • Sp_pkeys • Sp_fkeys • Sp_statistics
Sp_tables • Lists the tables that are in a database • sp_tables [ [ @table_name = ] 'name' ] [ , [ @table_owner = ] 'owner' ] [ , [ @table_qualifier = ] 'qualifier' ] [ , [ @table_type = ] "type" ] • Valid @table_type • Table • View • System Table
Sp_table examples • sp_tables syscolumns, dbo, pubs, "'SYSTEM TABLE'"; • sp_tables @table_owner=dbo; • sp_tables @table_type="'table'";
Sp_columns • Return information about the columns in a table • sp_columns [ @table_name = ] object[ , [ @table_owner = ] owner ] [ , [ @table_qualifier = ] qualifier ] [ , [ @column_name = ] column ] [ , [ @ODBCVer = ] ODBCVer ]
Sp_columns example • sp_columns @table_name = 'sales';
Sp_pkeys • Returns information about a table’s primary keys • sp_pkeys [ @table_name = ] 'name' [ , [ @table_owner = ] 'owner' ] [ , [ @table_qualifier = ] 'qualifier' ]
Sp_pkeys example • sp_pkeys @table_name = 'titleauthor';
Sp_fkeys • Returns logical foreign key information for the current environment. • sp_fkeys [ @pktable_name = ] 'pktable_name' [ , [ @pktable_owner = ] 'pktable_owner' ] [ , [ @pktable_qualifier = ] 'pktable_qualifier' ] { , [ @fktable_name = ] 'fktable_name' } [ , [ @fktable_owner = ] 'fktable_owner' ] [ , [ @fktable_qualifier = ] 'fktable_qualifier' ]
Sp_fkeys examples • sp_fkeys @pktable_name = 'titles'; • sp_fkeys @fktable_name = 'titleauthor';
Sp_statistics • Returns a list of all indexes and statistics on a specified table or indexed view. • sp_statistics [@table_name =] 'table_name'[,[@table_owner =] 'owner'] [,[@table_qualifier =] 'qualifier'] [,[@index_name =] 'index_name'] [,[@is_unique =] 'is_unique'] [,[@accuracy =] 'accuracy']
Sp_statistics example • sp_statistics @table_name = 'titles';
Which should be used? • Enterprise Manager is easiest • Stored Procedures are always there • Write your own SQL for special cases
Example • Take a few minutes to look at what was put into the catalog for the example we did earlier • Let’s look at Enterprise Manager • Let’s issue some stored procedures • Sp_tables • Sp_columns • Sp_statistics
Data Pages • Data pages are structures that contain all a table’s nontext and nonimage data • Does not include indexes • Are a fixed size of 8K (8192 bytes)
Data Page Components • Page Header • Data Rows • Row Offset Array
Page Header • 96 bytes • Identifies the following • File number and page number of the page • Next page (with clustered index) • Previous page (with clustered index) • Object that owns the page • Freedata • Freecnt • Other data
Row Offset Array • This is a block of 2 byte entries which indicates the offeset of the page where the corresponding data row begins • The row offset array indicate the logical order of rows on a page. • A row is located by knowing its page and slot numbers
Data Rows • The data row structure is pictured on p. 237 • Status bits A (1 byte) • Status bits B (1 byte) • Length of fixed length portion of rows (2 bytes) • Fixed length data (n bytes) • Number of columns (2 bytes) • NULL bitmap (1 bit for each column) • Number of variable length columns (2 bytes) • Column offset array (2 bytes for each varchar) • Data for variable length columns
Look at a page • DBCC traceon (3604) • Returns the data to the client • DBCC page(pubs, 1, 96, 1, 1) • See results on p. 234 in book