SQL Building Blocks and Server Settings
This chapter covers the building blocks available to the database designer and database user to create and modify database objects and data. The SQL Data Types will be covered along with data representation as literals (constants) and expressions. Also covered are methods to display and change server and database settings including using Enterprise Manager and the sp_configure and sp_dboption system stored procedures.
Throughout the book, the following terms will be used.
-
SQL-92 will refer to the 1992 ANSI/ISO SQL Standard, also called SQL2. SQL-99 will refer to the 1999 ANSI/ISO SQL Standard,1 also called SQL3 and ISO/IEC 9075.
-
SQL Server will refer to Microsoft® SQL Server 2000 (version 8.0).
-
Books Online will refer to Microsoft SQL Server 2000 Books Online.2
It is recommended that Books Online be used in conjunction with this book for coverage of some details not covered here and for different examples. Books Online is an excellent reference. This book, however, gathers related information together and generally provides more examples per topic than does Books Online. Used together they provide quite strong reference material.
2.1 SQL SERVER INSTANCE
Each instance (or installation) of Microsoft SQL Server 2000 is composed of several databases, four system databases (master, model, msdb and tempdb) and as many user databases as specified by the database administrator. SQL Server 2000 supports installing more than one instance on the same host.
2.1.1 The SQL Server Database
Each database consists of a collection of system and user tables and related database objects such as views, indexes, stored procedures, triggers, etc. A database in a Microsoft SQL Server database maintains a list of its database objects in its sysobjects table in that database. Each object type and its sysobjects table symbol are listed in Table 2-1.
Table 2-1. Object Types
Object Type |
Sysobjects Symbol |
---|---|
CHECK constraint |
C |
Default or DEFAULT constraint |
D |
FOREIGN KEY constraint |
F |
Scalar function |
FN |
Inlined table function |
IF |
PRIMARY KEY or UNIQUE constraint |
K |
Log |
L |
Stored procedure |
P |
Rule |
R |
Replication filter stored procedure |
RF |
System table |
S |
Table function |
TF |
Trigger |
TR |
User table |
U |
View |
V |
Extended stored procedure |
X |
By default, two sample user databases, Northwind and Pubs, are installed with SQL Server. These databases will often be used in examples in this book.
Example
List all USER TABLE objects in the pubs database.
SQL |
SELECT * FROM pubs..sysobjects WHERE type = 'U'; |
Other database objects are recorded in a separate table such as sysindexes for indexes and syscolumns for details of table and view columns as well as stored procedure parameters.
2.1.1.1 System Stored Procedures
SQL Server provides some built-in system stored procedures to assist in determining information about databases and database objects and to assist in database administration. System stored procedures are covered as needed, and a complete listing starts on page 168. Two of the more useful are shown here: sp_helpdb and sp_help.
sp_helpdb
This procedure reports information about a specified database or all databases.
Syntax
sp_helpdb [ [ @dbname= ] 'name' ]
Arguments
[@dbname=] 'name' |
This is the name of the database for which to provide information. If a name is not specified, sp_helpdb reports on all databases in master.dbo.sysdatabases. (dbo stands for database owner, the predefined user name in each database that is able to perform all database operations. Any sysadmin server role member becomes dbo inside each database.) |
Return Code Values
0 (success) or 1 (failure)
Example
Show information on database pubs.
SQL |
EXEC sp_helpdb pubs |
Reports db size, owner, file locations and other information for the pubs database.
sp_help
This procedure reports information about a database object, a user-defined data type or a system-supplied data type.
Syntax
sp_help [ [ @objname = ] name ]
Arguments
[@objname =] name |
This is the name of any object in sysobjects or any user-defined data type in the systypes table. name has a default of NULL. (Database names are not acceptable.) |
Return Code Values
0 (success) or 1 (failure)
Example
Show the structure of the titles table of the pubs database.
SQL |
|
---|---|
USE pubs -- must first move the context of SQL client to pubs database go |
|
SQL |
EXEC sp_help titles |
lists all column names and data types, constraints, table owner, etc., of titles table.