intro to tsql unit 6

11
Introduction To SQL Unit 6 Modern Business Technology Introduction To TSQL Unit 6 Developed by Michael Hotek

Upload: syed-asrarali

Post on 29-Apr-2015

381 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Intro to tsql   unit 6

Introduction To SQLUnit 6

Modern Business Technology

Introduction To TSQLUnit 6

Developed by

Michael Hotek

Page 2: Intro to tsql   unit 6

Temporary Tables

• Temp tables are used to hold intermediate result sets for further processing

• These are used when you need to perform a process that can not be done in a single SQL statement

• They come in two different types• local

– accessible only to that connection– preceeded by a # ex: #authors– dropped when the session disconnects

• global– accessible to all connections– must be explicitly dropped– preceeded by ## ex: ##authors

• Names can only be 13 characters long

Page 3: Intro to tsql   unit 6

• Temp tables can be a powerful addition to your code

• They should be minimized at all costs

• Many people overuse temp tables– Poor database design– Lack of SQL knowledge– Lazy

• Temp tables can impose a significant drain on system resources

Temporary Tables

Page 4: Intro to tsql   unit 6

Select Into

• Select into can be used to create a new table based upon a select statement

• Creates the table to match the structure of the select list

• Adds any data from the select• The database option select

into/bulkcopy needs to be turned onSELECT [ALL | DISTINCT] select_list

[INTO [new_table_name]] [FROM {table_name | view_name}[(optimizer_hints)]

[[, {table_name2 | view_name2}[(optimizer_hints)]

[..., {table_name16 | view_name16}[(optimizer_hints)]]] [WHERE clause] [GROUP BY clause][HAVING clause][ORDER BY clause]

Page 5: Intro to tsql   unit 6

Select Into

• To create an empty table, include a where clause that will always be false

select * into temptitles from titles

where 1 = 2

• It is highly recommended to do this and then insert the data into the table in a separate operation

• Rules, defaults, constraints, indexes, triggers, etc. are not associated with the new table

Page 6: Intro to tsql   unit 6

Views

• Views are nothing more than a name for a select statement that is stored in the database– Behave exactly like a table– Do not store any data– May include more than one table– Simplify and/or customize data– Provides independence from the

database schema– Provides security

• The main factor in using security should usually be security

Page 7: Intro to tsql   unit 6

Views

CREATE VIEW [owner.]view_name[(column_name [, column_name]...)][WITH ENCRYPTION]AS select_statement [WITH CHECK OPTION]

• Always explicitly specify the columns that will be included

• With encryption means that the definition of the view is encrypted when it is stored

Page 8: Intro to tsql   unit 6

With Check Option

• The with check option restricts the data that can be inserted and updated through a view

• Leaving this out, allows inserts and updates to the underlying data that might not show up in the view due to the where clause

• When this is included, the insert and update must conform to the where clause of the view

Page 9: Intro to tsql   unit 6

Restrictions

• Another view can be included in the definition of a view, but only 16 tables/views can be emcompassed

• Clauses not allowed– order by– compute/compute by– select into

• Updates can be performed through a view as long as only one table at a time is updated

• Updates can not be performed is a distinct, group by, or aggregate is included in the view

Page 10: Intro to tsql   unit 6

Unit 6 Review

• Temporary tables can be created to hold intermediate result sets

• Temp tables are either local or global• Select into can be used to create either a

permanent or temporary table• Views can be defined to provide alternate

access to data• Views primary consideration should be

security

Page 11: Intro to tsql   unit 6

Unit 6 Exercises

• Time allotted for exercises is 30 minutes