Вы находитесь на странице: 1из 26

Module 3

Designing a Physical Database


Model

Module Overview
Selecting Data Types
Designing Database Tables
Designing Data Integrity

Lesson 1: Selecting Data Types


Considerations for Selecting Standard Column Data Types
Considerations for Selecting New SQL Server 2008 Data Types
Considerations for Using CLR User-Defined Data Types
Considerations for Using Spatial Data Types
Guidelines for Using the XML Data Type
Establishing Naming Standards for Database Objects
Discussion: Working with Data Tables

Considerations for Selecting Standard Column


Data Types

Integer
Integer versus
versus GUID
GUID primary
primary keys
keys

Fixed
Fixed versus
versus variable
variable length
length columns
columns

VARCHAR
VARCHAR (MAX),
(MAX), NVARCHAR
NVARCHAR (MAX),
(MAX), and
and VARBINARY
VARBINARY (MAX)
(MAX) data
data types
types

Character
Character column
column collations
collations and
and Unicode
Unicode and
and non-Unicode
non-Unicode data
data types
types

Transact-SQL
Transact-SQL user-defined
user-defined data
data types
types

Considerations for Selecting New SQL Server 2008


Data Types
Date
Date // Time
Time

Datetime2
Datetime2 //
Datetimeoffset
Datetimeoffset

Date:
Date: From
From 01/01/0001
01/01/0001 to
to 12/31/9999
12/31/9999
Date:
Date: No
No time
time component
component
Time:
Time: Based
Based on
on 24
24 hour
hour clock,
clock, NO
NO date
date
component
component
Time:
Time: Accurate
Accurate to
to 100
100 ns
ns

Hierarchyid
Hierarchyid

FILESTREAM
FILESTREAM

To
To create
create tables
tables with
with aa hierarchical
hierarchical

When
When aa table
table contains
contains aa FILESTREAM
FILESTREAM

structure
structure
To
To query
query and
and perform
perform work
work with
with
hierarchical
data
by
using
T-SQL
hierarchical data by using T-SQL

Same
Same date
date precision
precision as
as date
date
Same
Same time
time precision
precision as
as time
time
DateTimeOffset
DateTimeOffset is
is time
time zone
zone aware
aware

column
column
When
When objects
objects are
are larger
larger than
than 11 MB
MB
Where
Where fast
fast read
read access
access is
is important
important
Where
Where you
you use
use aa middle
middle tier
tier

Considerations for Using CLR User-Defined Data Types


Use
Use common
common language
language runtime
runtime (CLR)
(CLR) user-defined
user-defined data
data types
types for
for
nonstandard
nonstandard or
or proprietary
proprietary data
data types
types

Avoid
Avoid excessively
excessively complex
complex data
data types
types

Consider
Consider the
the overhead
overhead of
of row-by-row
row-by-row processing
processing

Consider
Consider the
the risks
risks of
of tightly
tightly coupling
coupling aa CLR
CLR user-defined
user-defined data
data types
types
and
and the
the database
database

Considerations for Using Spatial Data Types


Spatial
Spatial Data:
Data:
Spatial
Spatial Data
Data
Represents
Represents information
information on
on the
the

Geometry
Data Type

Geography Data
Type

location
location and
and shape
shape of
of geometric
geometric
objects
objects
Can
Can be
be of
of two
two types
types including
including
geometry
geometry and
and geography
geography data
data types
types
Implemented
Implemented as
as .NET
.NET common
common
language
language runtime
runtime data
data types
types in
in SQL
SQL
Server
Server
Supports
Supports eleven
eleven spatial
spatial data
data objects,
objects,
or
or instance
instance types
types

Guidelines for Using the XML Data Type

Use
Use the
the XML
XML data
data type
type for
for data
data that
that is
is not
not frequently
frequently updated
updated

Use
Use typed
typed XML
XML columns
columns

Use
Use the
the XML
XML data
data type
type for
for data
data that
that is
is not
not relationally
relationally structured
structured

Use
Use the
the XML
XML data
data type
type for
for configuration
configuration information
information
< xml >

Use
Use the
the XML
XML data
data type
type for
for data
data with
with recursive
recursive structures
structures

Establishing Naming Standards for Database Objects


Use names
names that
that comply
comply with
with the
the rules
rules for
for forming
forming SQL
SQL Server
Server
Use
2008
2008 identifiers
identifiers
Use descriptive
descriptive terms
terms
Use
Use only
only standard
standard abbreviations
abbreviations in
in names
names
Use
Use models
models for
for naming
naming standards
standards
Use
Name intersection
intersection tables
tables consistently
consistently
Name
Be consistent
consistent across
across all
all objects
objects
Be
Record and
and communicate
communicate naming
naming standards
standards
Record
Use prefixes
prefixes ONLY
ONLY when
when itit provides
provides value
value
Use
Use policy-based
policy-based management
management
Use

Discussion: Working with Data Tables


Scenario:
Some developers argue to use a simple table structure containing
only four columnsa unique row identifier, a field identifier
(analogous to a column name), a datatype indicator, and the data
itself stored as a sqlvariant.
Some say that the application could reconstitute all data fields, and
pivot them into a virtual table.
The argument often goes further to whether developers should be
involved in working with the database schema, since they can create
new data properties (fields) as needed.
Question:
What is the fallacy in these arguments?

Lesson 2: Designing Database Tables


Guidelines for Determining Table Width
What Are Sparse Columns?
Demonstration: How To Create a Table By Using Sparse Columns
Guidelines for Using Computed Columns
Discussion: Using Computed Columns

Guidelines for Determining Table Width

Large Object Types

Data Overflow

Capacity Planning

What Are Sparse Columns?


Sparse
Sparse columns
columns are
are ordinary
ordinary columns
columns that
that have
have an
an optimized
optimized storage
storage for
for NULL
NULL values.
values.
They
can
be
used
with
column
sets
and
filtered
index.
They can be used with column sets and filtered index.

Properties
Properties of
of Sparse
Sparse Columns
Columns
The
The SQL
SQL Server
Server Database
Database Engine
Engine uses
uses the
the SPARSE
SPARSE keyword
keyword in
in aa column
column

definition
definition to
to optimize
optimize the
the storage
storage of
of values
values in
in that
that column.
column.
Catalog
Catalog views
views for
for aa table
table that
that has
has sparse
sparse columns
columns are
are the
the same
same as
as for
for aa typical
typical
table.
table.
The
The COLUMNS_UPDATED
COLUMNS_UPDATED function
function returns
returns aa varbinary
varbinary value
value to
to indicate
indicate all
all the
the
columns
columns that
that were
were updated
updated during
during aa DML
DML action.
action.

Data
Data types
types that
that you
you cannot
cannot specify
specify as
as sparse
sparse are
are geography,
geography, geometry,
geometry, image,
image, ntext,
ntext, text,
text,
timestamp,
timestamp, and
and user-defined
user-defined data
data types.
types.

Demonstration: How To Create a Table By Using


Sparse Columns
In this demonstration, you will see how to:
Create a table by using sparse columns

Guidelines for Using Computed Columns

Usage
Persisted
Usage
of of
Persisted
Computed
Computed
Usage
of Computed
Columns
Exceptions
in Columns
Column
Columns
Data

Avoid
the overhead
of to
complex
functions
in computed
columns
Use
persisted
computed
computed
columns
columns
derive results
for performance
from
other columns
Avoid persisted computed columns on active data
Computed
Protect against numeric overflow
andColumns
divide by zero errors
Usage of Computed
Columns

Exceptions in Column
Data

Discussion: Using Computed Columns


What kind of problems do computed columns solve?
When is a computed column actually computed?

Lesson 3: Designing Data Integrity


Guidelines for Designing Column Constraints
Guidelines for Designing Table Constraints
Guidelines When Implementing DDL Triggers
Discussion: Identifying the Best Options for Column Data Types

and Data Constraints

Guidelines for Designing Column Constraints

(HireDate int
NOT NULL...)

Declare columns as NOT NULL

(CONSTRAINT Qty
DEFAULT 0...)

Use ANSI default constraints

CONSTRAINT
chkQty CHECK
(Amount > 0)...)
Check
Constraint

Use column CHECK Constraints

Bound
Rules

Use CHECK constraints instead of


bound rules

Guidelines for Designing Table Constraints


Use
Use DRI
DRI for
for data
data integrity
integrity in
in aa database
database

Specify
Specify cascading
cascading levels
levels and
and options
options

Use
Use ANSI-standard
ANSI-standard options
options

ON DELETE

Use
Use triggers
triggers to
to enforce
enforce referential
referential integrity
integrity

Use
Use table-level
table-level CHECK
CHECK constraints
constraints
Check Constraint

Guidelines for Designing Database Constraints by


Using DDL Triggers

Use
Use DDL
DDL triggers
triggers for
for auditing
auditing

Use
Use DDL
DDL triggers
triggers to
to support
support security
security

Use
Use DDL
DDL triggers
triggers to
to prevent
prevent database
database changes
changes

Guidelines When Implementing DDL Triggers

Use
Use DDL
DDL triggers
triggers with
with transactions
transactions

Use
Use DDL
DDL triggers
triggers scope
scope to
to control
control database
database operations
operations or
or objects
objects that
that
activates
activates the
the trigger
trigger

Avoid
Avoid creating
creating DDL
DDL Trigger
Trigger on
on both
both CREATE_SCHEMA
CREATE_SCHEMA and
and
CREATE_TABLE
CREATE_TABLE event
event

Use
Use value()
value() instead
instead of
of query()
query() when
when querying
querying data
data returned
returned from
from
EVENTDATA
EVENTDATA

Discussion: Identifying the Best Options for Column


Data Types and Data Constraints
Scenario:
QuantamCorp is a local company conducting business only in USA
and Canada. You need to create a single table to contain the
following information.
1.Time of the day for a 24 hour clock
2.Company name
3.E-mail address
4.Postal code
5.Product information
6.Product descriptive brochure
7.Telephone number

Question:
What are the best options for column data types and data
constraints?

Lab 3: Designing a Physical Database Model


Exercise 1: Specifying Database Object Naming Standards
Exercise 2: Converting a Logical Database Model into a Physical

Database Model

Logon Information

User name

Administrator

Password

Pa$$w0rd

Estimated time: 40 minutes

Lab Scenario
You are a lead database designer at QuantamCorp. You are working on the Human
Resources Vacation and Sick Leave Enhancement (HR VASE) project that is designed to
enhance the current HR system of your organization. This system is based on the
QuantamCorp2008 sample database in SQL Server 2008.
The main goals of the HR VASE project are as follows:
Provide managers with current and historical information about employee vacation and

sick-leave data.
Provide permission to individual employees to view their vacation and sick-leave

balances.
Provide permission to selected employees in the HR department to view and update

employee vacation and sick-leave data.


Provide permission to the HR manager to view and update all the data.
Standardize employee job titles.
In this lab, you will build a physical database model based on the logical model created

earlier.

Lab Review
Can you explain the purpose of creating naming standards and

having a Naming Standards policy?

What kind of issues may arise if a Naming Standards policy does

not exist?

Module Review and Takeaways


Review Questions
Real-world Issues and Scenarios

Вам также может понравиться