Intro to tsql unit 9


Published on

  • 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

Intro to tsql unit 9

  1. 1. Introduction To SQL Unit 9 Modern Business Technology Introduction To TSQL Unit 9 Developed by Michael Hotek
  2. 2. Data Manipulation <ul><li>Up until this point we have covered numerous ways to get data out of tables </li></ul><ul><li>But, this is only a quarter of the basic SQL picture </li></ul><ul><li>We must have also have a way to add new data to a table, modify existing data, and delete data </li></ul><ul><li>To accomplish this, we will use the insert, update, and delete statements </li></ul><ul><li>These statements are also referred to as DML, data manipulation language </li></ul>
  3. 3. Insert <ul><li>Partial syntax: </li></ul><ul><li>insert [into] table_name [(column_list)] {values (expression [,expression]…}| {select statement} </li></ul><ul><li>The insert statement is used to add data to a table </li></ul><ul><li>It has two clauses, insert and values with an optional columns clause </li></ul><ul><li>The insert specifies which table the data is added to and the values clause specifies what that data is </li></ul>
  4. 4. Insert <ul><li>insert into authors </li></ul><ul><li>values('409-56-7008', 'Bennet', 'Abraham', </li></ul><ul><li>'415 658-9932', '6223 Bateman St.', 'Berkeley', 'CA', '94705') </li></ul><ul><li>Any character or date data must be enclosed within single quotes </li></ul><ul><li>If you are specifying a numeric or monetary amount, do not enclose these in quotes </li></ul><ul><li>If you do not have a value for a particular column, specify the keyword null </li></ul>
  5. 5. Insert <ul><li>Because the previous statement does not have a column_list, the data is inserted into the table in the order of the columns </li></ul><ul><li>For example the result of this statement would be a new row in the table that had the following: </li></ul><ul><li>au_id = 409-56-7008 </li></ul><ul><li>au_lname = Bennet </li></ul><ul><li>au_fname = Abraham </li></ul><ul><li>phone = 415 658-9932 </li></ul><ul><li>address = 6223 Bateman St. </li></ul><ul><li>city = Berkeley </li></ul><ul><li>state = CA </li></ul><ul><li>zip = 94705 </li></ul>
  6. 6. Insert <ul><li>If you did not have a value for the address, the insert statement would be as follows: </li></ul><ul><li>insert into authors </li></ul><ul><li>values ('409-56-7008', 'Bennet', 'Abraham', </li></ul><ul><li>'415 658-9932',null, 'Berkeley', 'CA', '94705') </li></ul><ul><li>The word into is optional, but it is recommended for clarity </li></ul>
  7. 7. Insert <ul><li>You do not always have to insert data in the exact order of the columns in the table, but you must then specify the column list </li></ul><ul><li>insert into authors (au_lname,city,state,au_id, au_fname,zip,address,phone) </li></ul><ul><li>values('Bennet', 'Berkeley', 'CA', </li></ul><ul><li>'409-56-7008', 'Abraham', '94705', </li></ul><ul><li>'6223 Bateman St.','415 658-9932',) </li></ul><ul><li>This will produce the set result as the previous insert statement </li></ul>
  8. 8. Insert <ul><li>You also must use a column list if you do not specify all of the values </li></ul><ul><li>In the authors table, au_id, au_lname, and au_fname are the only columns that require a value (not null) </li></ul><ul><li>We could also write: </li></ul><ul><li>insert into authors (au_id,au_lname, au_fname) values ('409-56-7008', 'Bennet', 'Abraham') </li></ul>
  9. 9. Insert <ul><li>Finally, the values clause can be replaced by a select statement </li></ul><ul><li>The way this works is that the first column of the result set from the select statement is placed in the first column of the column list in the insert </li></ul><ul><li>The second to the second, etc. </li></ul><ul><li>insert into authors (au_id,au_lname, au_fname) select ID,LastName,FirstName from authors_tmp </li></ul>
  10. 10. Insert <ul><li>You can also do this same type of insert without specifying a column list </li></ul><ul><li>The result set from the select statement must match the table you are inserting into in the number of columns, and datatype of columns </li></ul><ul><li>The column names do not have to match </li></ul><ul><li>insert into authors select * from authors_tmp </li></ul>
  11. 11. Insert <ul><li>The select statement that you use for inserting data can be of any variety </li></ul><ul><li>You can use a subquery(s), group by, order by, where, having, multiple tables, etc. </li></ul><ul><li>You can not use a compute or compute by </li></ul>
  12. 12. Update <ul><li>update table_name set column_name1 = {expression1/null | (select statement)} </li></ul><ul><li>[,column_name2 = {expression2/null | (select statement)}…] </li></ul><ul><li>from table_name [where search_conditions] </li></ul><ul><li>An update statement is used to modify existing data in a table </li></ul><ul><li>If you wanted to give a 10% discount on all titles: </li></ul><ul><li>update titles set price = price * .9 </li></ul>
  13. 13. Update <ul><li>You use the where clause to restrict which rows are updated </li></ul><ul><li>If you only wanted to discount those books published before 1/1/87: </li></ul><ul><li>update titles set price = price * .9 </li></ul><ul><li>where pubdate < '1/1/87' </li></ul>
  14. 14. Update <ul><li>You can also change multiple columns at the same time </li></ul><ul><li>update authors set city = 'Oakland West', zip = '94611' where city = 'Oakland' and address like '%College%' </li></ul><ul><li>There is a special type of column in some DBMSs called an identity, autoincrement, sequence, etc. This is beyond the scope of this class, but it is important to know you can not update these columns </li></ul><ul><li>update titles_ident set title_id = 200 where title_id = 1 will return an error </li></ul>
  15. 15. Update <ul><li>When you need to restrict the update to a set of rows that are based upon more than one table, you must use the from clause </li></ul><ul><li>We want to discount only those books that are from publishers in CA </li></ul><ul><li>update titles set price = price * .9 from titles t, publishers p where t.pub_id = p.pub_id and p.state = 'CA' </li></ul>
  16. 16. Delete <ul><li>Finally to delete data from a table, we use a delete statement </li></ul><ul><li>delete table_name [from table_name, table_name…] [where search_conditions] </li></ul><ul><li>If you do not specify any search conditions, you will delete all rows from the specified table </li></ul><ul><li>delete authors </li></ul>
  17. 17. Delete <ul><li>To restrict the rows you delete use a where clause </li></ul><ul><li>delete from authors where state = 'CA' </li></ul><ul><li>To delete rows where the criteria is based on multiple tables, use the from clause </li></ul><ul><li>delete titles from titles t, publishers p where t.pub_id = p.pub_id and p.state = 'CA' </li></ul>
  18. 18. Transaction Logs <ul><li>The full discussion of transaction logs is beyond the scope of this class </li></ul><ul><li>Every change to data (insert, update, and delete) is logged to a special file called a transaction log. </li></ul><ul><li>This is why insert, update, and delete are referred to as logged operations </li></ul>
  19. 19. Truncate <ul><li>You can also delete all of the data in a table without using a delete. </li></ul><ul><li>This is accomplished via a truncate command </li></ul><ul><li>truncate table table_name </li></ul><ul><li>truncate table authors </li></ul><ul><li>is the same as </li></ul><ul><li>delete authors </li></ul><ul><li>as far as the effect on the authors table </li></ul>
  20. 20. Truncate <ul><li>The difference is in how these two commands are handled </li></ul><ul><li>Delete is a logged operation </li></ul><ul><li>Truncate is a nonlogged operation </li></ul><ul><li>This becomes important in recovering a server from a crash </li></ul>
  21. 21. Truncate <ul><li>Generally you should not use a truncate command </li></ul><ul><li>In some client sites, this is a command that is reserved for the DBA (database administrator) </li></ul><ul><li>The truncate will perform faster, because it does not write to the transaction log, but this command should be avoided at all costs by anyone other than a DBA </li></ul><ul><li>It is included here for completeness </li></ul>
  22. 22. Unit 9 Review <ul><li>An insert statement allows you to add data to a table </li></ul><ul><li>The insert can contain a list of columns </li></ul><ul><li>An insert can also obtain it's values from another table </li></ul><ul><li>An update statement is used to change existing data </li></ul><ul><li>An update can not be performed on an identity/autoincrement/sequence column </li></ul><ul><li>A delete will delete data from a table </li></ul><ul><li>Insert, update, and delete part of the DML (Data Manipulation Language) </li></ul><ul><li>Insert, update, and delete are logged operations </li></ul><ul><li>Truncate will also remove all data from a table, but should be avoided at all costs </li></ul>
  23. 23. Unit 9 Exercises <ul><li>Time allotted for exercises is 1/2 hour </li></ul>