intro to tsql unit 9

23
Introduction To SQL Unit 9 Modern Business Technology Introduction To TSQL Unit 9 Developed by Michael Hotek

Upload: syed-asrarali

Post on 25-May-2015

330 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Intro to tsql   unit 9

Introduction To SQLUnit 9

Modern Business Technology

Introduction To TSQLUnit 9

Developed by

Michael Hotek

Page 2: Intro to tsql   unit 9

Data Manipulation

• Up until this point we have covered numerous ways to get data out of tables

• But, this is only a quarter of the basic SQL picture

• We must have also have a way to add new data to a table, modify existing data, and delete data

• To accomplish this, we will use the insert, update, and delete statements

• These statements are also referred to as DML, data manipulation language

Page 3: Intro to tsql   unit 9

Insert

• Partial syntax:insert [into] table_name [(column_list)] {values

(expression [,expression]…}| {select statement}

• The insert statement is used to add data to a table

• It has two clauses, insert and values with an optional columns clause

• The insert specifies which table the data is added to and the values clause specifies what that data is

Page 4: Intro to tsql   unit 9

Insert

insert into authors

values('409-56-7008', 'Bennet', 'Abraham',

'415 658-9932', '6223 Bateman St.', 'Berkeley', 'CA', '94705')

• Any character or date data must be enclosed within single quotes

• If you are specifying a numeric or monetary amount, do not enclose these in quotes

• If you do not have a value for a particular column, specify the keyword null

Page 5: Intro to tsql   unit 9

Insert

• Because the previous statement does not have a column_list, the data is inserted into the table in the order of the columns

• For example the result of this statement would be a new row in the table that had the following:

au_id = 409-56-7008

au_lname = Bennet

au_fname = Abraham

phone = 415 658-9932

address = 6223 Bateman St.

city = Berkeley

state = CA

zip = 94705

Page 6: Intro to tsql   unit 9

Insert

• If you did not have a value for the address, the insert statement would be as follows:

insert into authors

values ('409-56-7008', 'Bennet', 'Abraham',

'415 658-9932',null, 'Berkeley', 'CA', '94705')

• The word into is optional, but it is recommended for clarity

Page 7: Intro to tsql   unit 9

Insert

• 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

insert into authors (au_lname,city,state,au_id, au_fname,zip,address,phone)

values('Bennet', 'Berkeley', 'CA',

'409-56-7008', 'Abraham', '94705',

'6223 Bateman St.','415 658-9932',)

• This will produce the set result as the previous insert statement

Page 8: Intro to tsql   unit 9

Insert

• You also must use a column list if you do not specify all of the values

• In the authors table, au_id, au_lname, and au_fname are the only columns that require a value (not null)

• We could also write:

insert into authors (au_id,au_lname, au_fname) values ('409-56-7008', 'Bennet', 'Abraham')

Page 9: Intro to tsql   unit 9

Insert

• Finally, the values clause can be replaced by a select statement

• 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

• The second to the second, etc.

insert into authors (au_id,au_lname, au_fname) select ID,LastName,FirstName from authors_tmp

Page 10: Intro to tsql   unit 9

Insert

• You can also do this same type of insert without specifying a column list

• The result set from the select statement must match the table you are inserting into in the number of columns, and datatype of columns

• The column names do not have to match

insert into authors select * from authors_tmp

Page 11: Intro to tsql   unit 9

Insert

• The select statement that you use for inserting data can be of any variety

• You can use a subquery(s), group by, order by, where, having, multiple tables, etc.

• You can not use a compute or compute by

Page 12: Intro to tsql   unit 9

Update

update table_name set column_name1 = {expression1/null | (select statement)}

[,column_name2 = {expression2/null | (select statement)}…]

from table_name [where search_conditions]

• An update statement is used to modify existing data in a table

• If you wanted to give a 10% discount on all titles:

update titles set price = price * .9

Page 13: Intro to tsql   unit 9

Update

• You use the where clause to restrict which rows are updated

• If you only wanted to discount those books published before 1/1/87:

update titles set price = price * .9

where pubdate < '1/1/87'

Page 14: Intro to tsql   unit 9

Update

• You can also change multiple columns at the same time

update authors set city = 'Oakland West', zip = '94611' where city = 'Oakland' and address like '%College%'

• 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

update titles_ident set title_id = 200 where title_id = 1 will return an error

Page 15: Intro to tsql   unit 9

Update

• 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

• We want to discount only those books that are from publishers in CA

update titles set price = price * .9 from titles t, publishers p where t.pub_id = p.pub_id and p.state = 'CA'

Page 16: Intro to tsql   unit 9

Delete

• Finally to delete data from a table, we use a delete statement

delete table_name [from table_name, table_name…] [where search_conditions]

• If you do not specify any search conditions, you will delete all rows from the specified table

delete authors

Page 17: Intro to tsql   unit 9

Delete

• To restrict the rows you delete use a where clause

delete from authors where state = 'CA'

• To delete rows where the criteria is based on multiple tables, use the from clause

delete titles from titles t, publishers p where t.pub_id = p.pub_id and p.state = 'CA'

Page 18: Intro to tsql   unit 9

Transaction Logs

• The full discussion of transaction logs is beyond the scope of this class

• Every change to data (insert, update, and delete) is logged to a special file called a transaction log.

• This is why insert, update, and delete are referred to as logged operations

Page 19: Intro to tsql   unit 9

Truncate

• You can also delete all of the data in a table without using a delete.

• This is accomplished via a truncate command

• truncate table table_name

truncate table authors

is the same asdelete authors

as far as the effect on the authors table

Page 20: Intro to tsql   unit 9

Truncate

• The difference is in how these two commands are handled

• Delete is a logged operation

• Truncate is a nonlogged operation

• This becomes important in recovering a server from a crash

Page 21: Intro to tsql   unit 9

Truncate

• Generally you should not use a truncate command

• In some client sites, this is a command that is reserved for the DBA (database administrator)

• 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

• It is included here for completeness

Page 22: Intro to tsql   unit 9

Unit 9 Review

• An insert statement allows you to add data to a table

• The insert can contain a list of columns• An insert can also obtain it's values from

another table• An update statement is used to change

existing data• An update can not be performed on an

identity/autoincrement/sequence column• A delete will delete data from a table• Insert, update, and delete part of the DML

(Data Manipulation Language)• Insert, update, and delete are logged

operations• Truncate will also remove all data from a

table, but should be avoided at all costs

Page 23: Intro to tsql   unit 9

Unit 9 Exercises

• Time allotted for exercises is 1/2 hour