Introduction To SQL Unit 6 Modern Business Technology Introduction To TSQL Unit 6 Developed by Michael Hotek
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
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
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 on SELECT [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]
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
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
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
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
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
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
Unit 6 Exercises Time allotted for exercises is 30 minutes

Intro to tsql unit 6

  • 1.
    Introduction To SQLUnit 6 Modern Business Technology Introduction To TSQL Unit 6 Developed by Michael Hotek
  • 2.
    Temporary Tables Temptables 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
  • 3.
    Temp tables canbe 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
  • 4.
    Select Into Selectinto 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 on SELECT [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]
  • 5.
    Select Into Tocreate 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
  • 6.
    Views Views arenothing 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
  • 7.
    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
  • 8.
    With Check OptionThe 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
  • 9.
    Restrictions Another viewcan 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
  • 10.
    Unit 6 ReviewTemporary 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
  • 11.
    Unit 6 ExercisesTime allotted for exercises is 30 minutes

Editor's Notes