540 likes | 630 Views
Chapter Five. Creating SQL Server 2000 Databases. Objectives. Define the different types of databases SQL Server 2000 supports Create and manage a SQL Server 2000 database Create and manage database objects. Databases. Database
E N D
Chapter Five Creating SQL Server 2000 Databases
Objectives • Define the different types of databases SQL Server 2000 supports • Create and manage a SQL Server 2000 database • Create and manage database objects
Databases • Database • Logical collection of data and objects that are used to support maintenance and use of that data • There are two general types of databases: • User-defined databases • Created to store application-specific data • System-defined databases • Collection of databases that store vital system information
Databases • The four system databases in SQL Server 2000: • master database • Stores all system-level information for SQL Server 2000 installation • tempdb database • Used as temporary processing location • model database • Used as template for new database creation • msdb database • Used by SQL Server Agent to store scheduled alerts and jobs information
Databases • In addition to system databases, the pubs and Northwind sample databases are created for every SQL Server 2000 installation • These two databases are referenced extensively in SQL Server Books Online • You can review information about the databases for a given SQL Server 2000 installation by running the sp_helpdb system-stored procedure in SQL Query Analyzer
Files • Files • Physical operating system file that stores a database or transaction log • SQL Server 2000 supports three file types: • Primary data files • Holds the data and system tables for the database • Secondary data files • Allows databases to be expanded onto more than one file • Log files
Files • Information from primary file would be used in following scenarios: • When attaching to an existing database • When restoring master database • When upgrading from SQL Server 7.0 • Regular identifiers • Set of naming rules for objects • Delimited identifiers • Names that do not follow rules for regular identifiers must be delimited with double quotes or square brackets
Filegroups • Filegroups are designed to help administer data files • Primary filegroup • Contains the primary file and any other files not associated with another filegroup • User-defined filegroup • Contains any additional files as specified during database creation or later alteration • Default filegroup • Filegroup where space is allocated when a table or index does not explicitly define a filegroup
Creating a Database Figure 5-1: Viewing database information with SQL Enterprise Manager
Creating a Database Figure 5-2: Viewing database information with SQL Query Analyzer
Creating a Database Figure 5-3: General tab of Database Properties dialog box
Managing Files and Filegroups Figure 5-4: Data Files tab of Database Properties dialog box
Managing Files and Filegroups Figure 5-5: Transaction Log tab of Database Properties dialog box
Creating Databases with T-SQL Statements • You may also create databases by using the CREATE DATABASE T_SQL command • To use this command, open a query window with SQL Query Analyzer
Modifying Databases • Expanding databases • Databases can be configured for automatic file expansion by using growth parameters for the databases • To increase file sizes for databases using SQL Enterprise Manager, right-click the database and choose Properties from the context-sensitive menu • This loads the the Orders Properties dialog box
Modifying Databases Figure 5-6: Orders Properties
Modifying Databases Figure 5-7: Expanding database file size
Modifying Databases • If you would like automatic file growth, check the Automatically grow file check box in the File properties section and specify the various properties associated with automatic file growth • If you prefer to handle the file sizes on your own, uncheck the Automatically grow file check box
Modifying Databases • Shrinking a database • The simplest way to shrink a database is to have it done automatically • Automatic database shrinking occurs as a background process and should not affect normal usage • Since a database set to automatically shrink cannot shrink below its initial size, make sure your initial database size makes sense
Modifying Databases Figure 5-8: Shrink Database dialog box
Modifying Databases Figure 5-9: Shrink Files dialog box
Changing the Default Filegroup • The default filegroup is where new pages are allocated when a table or index does not explicitly define a filegroup • To change the default filegroup, right-click on your database in SQL Enterprise Manager and choose Properties from the context-sensitive menu • This loads the Properties dialog box
Changing the Default Filegroup Figure 5-10: Database filegroups
Configuration Settings Figure 5-11: Database options
Configuration Settings Table 5-1: Database options
Attaching and Detaching Databases • You may move databases from server to server by attaching and detaching • When a database is detached it becomes inaccessible • Once detached, the physical database files may be moved to another server where they can be attached for use with that server
Attaching and Detaching Databases Figure 5-12: Detach Database dialog box Figure 5-13: Attach Database dialog box
Modifying Databases • Databases can be renamed using the sp_renamedb system-stored procedure • To remove a database, right-click the database in SQL Enterprise Manager and choose delete from the context-sensitive menu Figure 5-14: Delete Database dialog box
Database Objects Figure 5-15: Database objects
General Considerations • General guidelines to consider when creating database objects: • Ownership • Object naming • Permissions • Viewing object information • Deleting objects • Renaming objects
Ownership • In general, the user name of the user who creates an object is the object’s owner • The full name of the object is the object name prefaced by the owner name • All references to objects need to specify owner information • Exception to this rule is if the user name you are logged in with owns the object
Object Naming • All database objects created by a user need to follow the rules for identifiers • First letter of the name must be a letter, underscore, at sign (@) or pound sign (#) • Rest of the name must follow first letter rules but may also include numbers • No spaces are allowed • Name cannot be a Transact-SQL reserved word • Names must be less than 128 characters
Permissions • A user needs explicit permissions to create objects in a database • By default, permission to create an object is granted to members of the db_owner and db_ddladmin fixed database roles • In most cases, members of the sysadmin fixed server role can give users explicit permissions to create objects
Permissions Figure 5-16: Database permissions
Viewing Object Information • To view information about an object in the database with SQL Enterprise Manager: • Expand your database by left-clicking on the plus sign next to the database name • Left-click on the object of interest • You can also use the sp_help system-stored procedure in SQL Query Analyzer to gain information about database objects
Deleting Objects • To remove an object from the system with SQL Enterprise Manager: • Expand your database by left-clicking the plus sign next to the database name • Left-click on the object of interest • Right-click to choose Delete from the context-sensitive menu
Deleting Objects Figure 5-17: Drop objects
Renaming Objects • To rename an object with SQL Enterprise Manager: • Expand your database by left-clicking on the plus sign next to the database name • Left-click on the object containing the items to be renamed • Right-click on the object you want to rename in the right-hand panel and choose rename from the context-sensitive menu
Creating Tables Figure 5-18: New tables dialog box
Adding Columns to Database Tables • Extended property • User-defined value that can be attached to almost any object • Precision • Determines the total number of decimal digits that can be stored • Scale • Determines the maximum number for decimal digits to the right of the decimal point • Globally Unique Identifier (GUID) • Value that is unique across all databases in the world
Adding Columns to Database Tables Figure 5-19: Table with multiple-column primary key
Managing Table Properties • Tables tab lets you manage high-level items on your table Figure 5-20: Tables tab of the Table and Index Properties dialog box
Defining Table Relationships • Relationships tab is used to define primary and foreign keys Figure 5-21: Relationships tab of Table and Index Properties dialog box
Managing Table Indexes • Fill factor • Determines how full the index pages will be Figure 5-22: Indexes/Keys tab of Table and Index Properties dialog box
Managing Check Constraints Figure 5-23: Check constraints tab of Table and Index Properties dialog box
Creating and Modifying Database Objects with T-SQL • Tables can also be added using the CREATE TABLE Transact-SQL command • Once a table is created, the ALTER TABLE statement can be used to modify it • Indexes are created using the CREATE INDEX statement
Creating Views in Enterprise Manager Figure 5-24: New View Screen
Creating Views in Enterprise Manager • To add tables to the Diagram Pane, use the Add Table button on the toolbar • This opens the Add Table dialog box Figure 5-25: Add Table dialog box
Creating Views in Enterprise Manager Figure 5-26: One to many join
Creating Views in Enterprise Manager Figure 5-27: Join properties Figure 5-28: View properties