chapter 9: creating database conventions & standards mcitp administrator: microsoft sql server...

26
Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Upload: lucinda-harper

Post on 23-Dec-2015

226 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Chapter 9: Creating Database Conventions & Standards

MCITP Administrator: Microsoft SQL Server 2005 Database Server

Infrastructure Design Study Guide (70-443)

Page 2: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Benefits of Naming Conventions

• Easily identify an object’s purpose , type, and function

• Helps integrate new devlopers into development team

• Learning curve shortened

© Wiley Inc. 2006. All Rights Reserved.

Page 3: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Naming Convention Guidelines

• Establish for all major type of database objects

• Document & Disseminate conventions

• Enforce conventions

© Wiley Inc. 2006. All Rights Reserved.

Page 4: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: Tables

• Tables typically represent entities such as Customer or Order.

• Use the name of the entity that the table

• Use singular names whenever possible.

© Wiley Inc. 2006. All Rights Reserved.

Page 5: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: Columns

• Use meaningful name for each column in the database. (For example, use LastName for a column holding the last name of an employee in the Employee table.)

© Wiley Inc. 2006. All Rights Reserved.

Page 6: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: Views

• Views typically join several tables or other views together to generate or summarize information.

• Use names that indicate the purpose of the information they return.

• It is common to use a standard prefix such as “vw_” for view names to distinguish them from tables.

© Wiley Inc. 2006. All Rights Reserved.

Page 7: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: Stored Procedures

• Stored procedures express actions.

• Use a meaningful name combining verbs and objects that describe their action.

• Avoid confusion with system-stored procedures anddo not use the “sp_” prefix; consider using “usp” instead.

© Wiley Inc. 2006. All Rights Reserved.

Page 8: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: User Defined Functions

• Use a meaningful name that describes the calculations that they perform.

• A common convention is to prefix the name with “ufn” to distinguish them from columns or views in SQL statements.

© Wiley Inc. 2006. All Rights Reserved.

Page 9: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: Triggers

• Combine the name of the table and the trigger event type.

• Indicate whether the trigger is an AFTER or INSTEAD OF trigger by including “After” or “InsteadOf” in the name, e.g., dAfterOrder.

© Wiley Inc. 2006. All Rights Reserved.

Page 10: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: Indexes

• Combine the name of the table and the names of the columns and frequently include a prefix such as a “IX_” prefix.

• Augment the prefix to indicate whether the index is clustered or nonclustered, a unique index, and so on.

© Wiley Inc. 2006. All Rights Reserved.

Page 11: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: Constraints

• name the constraint after the rule it enforces or the column it operates on.

• Add a prefix indicating the type of constraint (check, primary key, foreign key, unique constraint, and so on).

© Wiley Inc. 2006. All Rights Reserved.

Page 12: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Typical Naming Conventions: Schema

• Use schemas to group database objects by functionality and to partition objects into protected domains.

• Add a prefix that identifies a name as a schema so that there is no confusion that it might be a table.

© Wiley Inc. 2006. All Rights Reserved.

Page 13: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Naming Conventions: Bad Practices

• Using sp_ in a user-defined stored procedure name

• Inconsistent use of upper and lowercase• Using spaces or nonalphanumeric

characters• Naming tables with tbl prefix• Including datatype abbreviation in column

name• Using shortened or abbreviated object

names• Using reserved words as object names

© Wiley Inc. 2006. All Rights Reserved.

Page 14: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Vendor Naming Conventions

• Wherever possible vendor should use local naming convention

• Do not allow vendor naming conventions to supplant your system

© Wiley Inc. 2006. All Rights Reserved.

Page 15: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Documenting & Disseminating Naming Conventions

• Document adopted standards

• Disseminate – Paper documents– Microsoft SharePoint Portal– Post on intranet

• Assure contractors are provided and follow naming conventions

© Wiley Inc. 2006. All Rights Reserved.

Page 16: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Database Standards

• Standards used to minimize chaos• Standards must be:

– Clear– Sensible– Enforced

• Can reduce learning curves• Can reduce failure by requiring

activity through known working methods

© Wiley Inc. 2006. All Rights Reserved.

Page 17: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

T-SQL Coding Standards

• T-SQL should be considered true source code

• Use standard source code control and standards for coding

• Use templates for each type of object

© Wiley Inc. 2006. All Rights Reserved.

Page 18: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

T-SQL Coding Stylistic Standards

• Prefix every reference to a database object with the name of the schema it belongs to

• Indent every block of code appropriately

• Use UPPERCASE for all SQL and SQL server keywords

© Wiley Inc. 2006. All Rights Reserved.

Page 19: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

T-SQL Coding Functional Standards

• Ensure code in triggers can handle multiple inserts, updates or deletes

• Never use UDFs to perform searches on other tables

• Avoid using cursors inside stored procedures

• Require stored procedures avoid creating/using temporary tables

• Use TRY…CATCH constructs to perform error handling

© Wiley Inc. 2006. All Rights Reserved.

Page 20: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Coding Standards

• Document adopted standards

• Disseminate – Paper documents– Microsoft SharePoint Portal– Post on intranet

• Assure contractors are provided and follow coding standards

© Wiley Inc. 2006. All Rights Reserved.

Page 21: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Database Access Standards

• Direct Access not normally a good practice

• Indirect access can be established by– Specifying applications use stored

procedures– Restrict all data acces to views

© Wiley Inc. 2006. All Rights Reserved.

Page 22: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Indirect Access: Stored Procedure - Advantages

• Applications aren’t tightly coupled to database schema

• Can shield operations that may expose sensitive data

• Can optimize and tune queries without affecting/modifying applications

• Reduces network traffic through encapsulating logic on server rather client applications

© Wiley Inc. 2006. All Rights Reserved.

Page 23: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Indirect Access Through Views - Advantages

• Can hide complex SQL loic from applications

• Reduces coupling between application and database

• Can be selective about information provided

• Applications can be seelctive about what data they retrieve

© Wiley Inc. 2006. All Rights Reserved.

Page 24: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Communicating Database Access Standards

• Document adopted standards

• Disseminate – Paper documents– Microsoft SharePoint Portal– Post on intranet

• Assure contractors are provided and follow database access standards

© Wiley Inc. 2006. All Rights Reserved.

Page 25: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Deployment Process Standards

• Define roles for – Development database– Testing database– Production database

© Wiley Inc. 2006. All Rights Reserved.

Page 26: Chapter 9: Creating Database Conventions & Standards MCITP Administrator: Microsoft SQL Server 2005 Database Server Infrastructure Design Study Guide (70-443)

Protecting Production Data During Deployment

• Allow only production database administrators to access production database

• Make changes to database only by using T-SQL scripts

• Backup all affected databses before deployment

• Define roles and responsibilities of staff members during deployment

• Record all changes in a Run Biook

© Wiley Inc. 2006. All Rights Reserved.