Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://www.LearnNowOnline.com
Objectives
Learn basic relational database design principles Create a SQL Server database based on sound design principles Build tables using the SQL Server Management Studio designers Learn about SQL Server data types Create constraints, triggers, and indexes Create a database diagram and define relationships to enforce referential integrity
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Agenda
Relational Database Design Principles Implementing Database Design
Database Storage
Pages
The minimum unit of storage: 8 KB Maximum size of a row
Except long values: text, ntext, image, varchar(max), nvarchar(max),
Extents
8 pages per Extent New objects at first stored in spare pages in existing extents
Database Storage
Files
.MDF, .NDF, .LDF
Filegroups
Can locate data or indexes in additional files and on
Data Integrity
The Transaction Log
Automatically created with a database Records all activity in a database Data pages are loaded into a buffer cache Any updates are made to the copy in the buffer A log record is created in a log cache Checkpoint process saves the log record to disk Only then is the data saved to disk
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Recovery Models
Simple
Log is truncated at checkpoint Recovery to the point of the last full or differential backup
Full
All operations are fully logged Recovery to any point in time
Bulk-logged
Faster bulk-logged operations (indexing, bulk load) Recovery limited to logged transactions after the last
backup
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Creating Databases
Use Management Studio or Transact-SQL
SSMS uses T-SQL behind the scenes Almost all SSMS actions follow this pattern Most of the time you can access that code
Additional characters: letters, numbers, #, $, _ Use square brackets or double quotes if identifier has spaces or other special characters No use of reserved keywords as identifiers
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Creating Tables
New database doesnt contain tables Table names must be unique
General format:
Schemas
Before SQL Server 2005, schema names based on user names
Except for dbo Close tie was inconvenient
time 3 to 5 bytes
Range: 00:00:00.0000000 to 23:59:59.9999999 Precision up to 100 nanoseconds
datetime2 6 to 8 bytes
Range and precision of date and time types No time zone or daylight savings time information
datetimeoffset 8 to 10 bytes
datetime2 with time zone information
YYYY-MM-DDThh:mm:ss[.nnnnnnn][{+|-}hh:mm] YYYY-MM-DDThh:mm:ss[.nnnnnnn]Z
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
{40700425-0080-11d2-851f-00c04fc21759}
Used by SQL Server for replication newid() function as default generates random IDs newsequentialid() better for indexing
Used to track data changes (compare old and current values to see
timestamp Useful for name/value pairs where the data type for the values may vary More overhead than any other data types
Stores meta data as well as data:base data type,
xml) Special methods for querying and modifying xml Special indexes store individual xml nodes and paths to the nodes from the root T-SQL supports a subset of the emerging XQuery standard for querying xml data
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Computed Columns
Let you create a column that automatically updates values
Based on expression that you define Can include references to other columns
Creating Constraints
Can restrict data that users enter in columns
Set restrictions, or constraints Can place on columns as part of creating tables
Or add them later
Types
Default Constraints
Enables a value to be entered automatically into a new row
When no other data is explicitly entered in that
Check Constraints
Checks the data before saving record
If incoming data violates constraint, record isnt
saved If value of constraint expression is False, constraint is violated If null, will save data
Triggers
Available on INSERT, UPDATE, DELETE Access to special views: deleted, inserted Can access and alter values in other tables Uses:
Business rules, Audit trails, roll back Originally used for referential integrity
Caution: Maintenance and perf headaches Instead, use stored procedures for data modification if possible
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Creating Indexes
Indexes let users query data efficiently
SQL Server can use a shortcut to find data Instead of scanning entire table
Questions?
http://www.LearnNowOnline.com