Intro to tsql unit 6


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 1
  • 45
  • 47
  • Intro to tsql unit 6

    1. 1. Introduction To SQL Unit 6 Modern Business Technology Introduction To TSQL Unit 6 Developed by Michael Hotek
    2. 2. Temporary Tables <ul><li>Temp tables are used to hold intermediate result sets for further processing </li></ul><ul><li>These are used when you need to perform a process that can not be done in a single SQL statement </li></ul><ul><li>They come in two different types </li></ul><ul><li>local </li></ul><ul><ul><li>accessible only to that connection </li></ul></ul><ul><ul><li>preceeded by a # ex: #authors </li></ul></ul><ul><ul><li>dropped when the session disconnects </li></ul></ul><ul><li>global </li></ul><ul><ul><li>accessible to all connections </li></ul></ul><ul><ul><li>must be explicitly dropped </li></ul></ul><ul><ul><li>preceeded by ## ex: ##authors </li></ul></ul><ul><li>Names can only be 13 characters long </li></ul>
    3. 3. <ul><li>Temp tables can be a powerful addition to your code </li></ul><ul><li>They should be minimized at all costs </li></ul><ul><li>Many people overuse temp tables </li></ul><ul><ul><li>Poor database design </li></ul></ul><ul><ul><li>Lack of SQL knowledge </li></ul></ul><ul><ul><li>Lazy </li></ul></ul><ul><li>Temp tables can impose a significant drain on system resources </li></ul>Temporary Tables
    4. 4. Select Into <ul><li>Select into can be used to create a new table based upon a select statement </li></ul><ul><li>Creates the table to match the structure of the select list </li></ul><ul><li>Adds any data from the select </li></ul><ul><li>The database option select into/bulkcopy needs to be turned on </li></ul><ul><li>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] </li></ul>
    5. 5. Select Into <ul><li>To create an empty table, include a where clause that will always be false </li></ul><ul><li>select * into temptitles from titles </li></ul><ul><li>where 1 = 2 </li></ul><ul><li>It is highly recommended to do this and then insert the data into the table in a separate operation </li></ul><ul><li>Rules, defaults, constraints, indexes, triggers, etc. are not associated with the new table </li></ul>
    6. 6. Views <ul><li>Views are nothing more than a name for a select statement that is stored in the database </li></ul><ul><ul><li>Behave exactly like a table </li></ul></ul><ul><ul><li>Do not store any data </li></ul></ul><ul><ul><li>May include more than one table </li></ul></ul><ul><ul><li>Simplify and/or customize data </li></ul></ul><ul><ul><li>Provides independence from the database schema </li></ul></ul><ul><ul><li>Provides security </li></ul></ul><ul><li>The main factor in using security should usually be security </li></ul>
    7. 7. Views <ul><li>CREATE VIEW [ owner . ] view_name [ ( column_name [ , column_name ]... ) ] [WITH ENCRYPTION] AS select_statement [WITH CHECK OPTION] </li></ul><ul><li>Always explicitly specify the columns that will be included </li></ul><ul><li>With encryption means that the definition of the view is encrypted when it is stored </li></ul>
    8. 8. With Check Option <ul><li>The with check option restricts the data that can be inserted and updated through a view </li></ul><ul><li>Leaving this out, allows inserts and updates to the underlying data that might not show up in the view due to the where clause </li></ul><ul><li>When this is included, the insert and update must conform to the where clause of the view </li></ul>
    9. 9. Restrictions <ul><li>Another view can be included in the definition of a view, but only 16 tables/views can be emcompassed </li></ul><ul><li>Clauses not allowed </li></ul><ul><ul><li>order by </li></ul></ul><ul><ul><li>compute/compute by </li></ul></ul><ul><ul><li>select into </li></ul></ul><ul><li>Updates can be performed through a view as long as only one table at a time is updated </li></ul><ul><li>Updates can not be performed is a distinct, group by, or aggregate is included in the view </li></ul>
    10. 10. Unit 6 Review <ul><li>Temporary tables can be created to hold intermediate result sets </li></ul><ul><li>Temp tables are either local or global </li></ul><ul><li>Select into can be used to create either a permanent or temporary table </li></ul><ul><li>Views can be defined to provide alternate access to data </li></ul><ul><li>Views primary consideration should be security </li></ul>
    11. 11. Unit 6 Exercises <ul><li>Time allotted for exercises is 30 minutes </li></ul>