Sei sulla pagina 1di 22

Module 6

Database Objects
Module 6 Overview

• Tables
• Views
• Stored Procedures, Triggers, and Functions
Lesson 1: Tables

• Introduction to Data Types


• Numeric Data Types
• Character String Data Types
• Other Data Types
• Keys and Constraints
• The Identity Property
• Schemas
Introduction to Data Types
Numeric Data Types

• Exact numeric data types:


• Tinyint 0 to 255
• Smallint -32767 to 32767
• Int -2,147,000,000 to 2,147,000,000
• Bigint -922,000,000,000,000 to 922,000,000,000,000
• money
• smallmoney
• Decimal / Numeric Decimal( 10 , 2 )
• Bit 0 OR 1

• Approximate numeric data types:


• Real -1x10e-304 - 1x10e304
• Float -1x10e-34 - 1x10e34
Character String Data Types

• Non-Unicode character string data types:


• char(8000) (8000) - maximum
• varchar(8000)
• varchar(max) 2GB of text- maximum

• Unicode character string types:


• nchar(4000) (4000) - maximum
• nvarchar(4000)
• nvarchar(max) 2GB of text- maximum

• TEXT, NTEXT
Other Data Types

• Date and time data types


• Date, Time, DateTime, DateTime2, SmallDateTime

• Binary data types


• Spatial data types
• Other data types
• User-defined data types
Keys and Constraints
• PRIMARY KEY constraints- ensure uniqueness of
ROWS

• FOREIGN KEY constraints- maintain referential


integrity

• UNIQUE constraints- ensure uniqueness for non-


primary key columns

• DEFAULT constraints- provide a default value

• CHECK constraints- check values against defined


criteria
The Identity Property

• The IDENTITY property in a CREATE TABLE


statement automatically generates integer values
for a column … IDENTITY(100000 ,10)
• The seed value is the starting point of the numerical
sequence
• The increment value is the amount that the value
increases by for each row
• IDENTITY does not ensure uniqueness
• Use a PRIMARY KEY or UNIQUE constraint

• Only one column per table can include the


IDENTITY property
Schemas

• A schema is a named object that represents a logical


container for tables
• A schema is also a security boundary
• Configuring permissions at the schema level reduces
administrative effort and improves security
• OtherServer.OtherDatabase.SALES.Orders (Fully-Qualified Name)
• SALES.Customers
• SALES.OrderLineItems
• SALES.vAllOrderDetails
• SALES.uspAddNewOrder

GRANT SELECT ON SCHEMA::SALES


TO SalesPersonsRole
Lesson 2: Views

• What Is a View?
• Views and Security
• Views and Performance
What Is a View?
Views and Security

• End users only require permission on the view,


not on the underlying tables
• Benefits of this approach include:
• Users can only access columns that the view references
• Reduces the risk of users exploring data that they
should not access
• Simplifies permission management for database
administrators
Views and Performance

• Creating an indexed view persists the result set of


a view in the database
• Indexed views can improve query performance
for some types of queries
• Indexed views are not updated automatically if
the base tables change
• To index a view, you MUST use the WITH
SCHEMABINDING option in the view definition
• Balance the benefits of using indexed views
against the cost of maintaining them
Lesson 3: Stored Procedures, Triggers, and Functions

• Stored Procedures
• Benefits of Stored Procedures
• Input Parameters
• Triggers
• Functions
Stored Procedures
Benefits of Stored Procedures

• Modularization
• Performance
• Security
• Standardization and code reuse
Input Parameters
Triggers
• Data Manipulation Language (DML) Triggers- at
the TABLE Level
• AFTER triggers
• INSTEAD OF triggers

• Data Definition Language (DDL) triggers- at the


DATABASE Level

• Logon triggers

• Use a CREATE TRIGGER Transact-SQL statement


to create triggers
Functions

• Performs a specific SELECT task


• Built-in function types:
• Scalar functions – operate on a single value and return
a single scalar result
• Aggregate functions – operate on a range of values
and return a single result
• User-defined function types:
• Scalar functions – operate on a single value and return
a single scalar result
• Inline table-valued functions – return a table populated
by a single SELECT statement
• Multi-statement table-valued functions – return a table
populated by the results of multiple statements
Functions Example

• User-defined function types-


• Inline table-valued functions – return a table populated
by a single SELECT statement
Lab 6: Using SQL Server

• Exercise 1: Creating a Table and Constraints


• Exercise 2: Creating a View
• Exercise 3: Creating a Stored Procedure

Logon Information
Ensure that the 10985C-MIA-DC and 10985C-MIA-SQL
virtual machines are both running, and then log on to
10985C-MIA-SQL as ADVENTUREWORKS\Student with
the password Pa55w.rd.

Estimated Time: 60 minutes

Potrebbero piacerti anche