Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

26
Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi

Transcript of Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Page 1: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Chapter 9 Selecting, Updating, and Deleting Data

Syed Rizvi

Page 2: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Retrieving Data Using GUI Right Click on the Table (Customers) Select Top 1000 Rows Above the results, see T-SQL code

generated by SSMSE On Right (F4), see Properties Window for

Connection and Query Details To set fewer than 1,000 rows returned,

set value of TOP clause for the SELECT under SQL Server Object Explorer/Commands

Page 3: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Retrieving Data Using Select Command

Retrieve data in any order, from any number of columns, and from one or more tables

Can perform calculations on that data during data retrieval

Page 4: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Retrieving Data Using Select Command

Page 5: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Retrieving Data Using Select Command

*: Optional—return all the columns from all the tables included in the query. Don’t use on large amounts of data or over a network as network could be busy or there are a large number of columns. Don’t use this against production data. By using this, you are bringing back more information than is required. Wherever possible, you should name the columns instead.

Page 6: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Retrieving Data Using Select Command

Don’t name every column unless required. Typing every column name takes time, but when you start dealing with more complex queries and a larger number of rows, the few extra seconds is worth it.

Avoid using *

Page 7: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Retrieving Data Using Select Command

Which one is a better approach Select * from customers Select firstName, lastName from

customers SELECT CustomerFirstName As ' First Name' ,CustomerLastName AS ' Surname' ,ClearedBalance Balance FROM CustomerDetails. Customers

Page 8: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Varying the Output Display in Query Editor

Results to Grid (Ctrl+D) -as you have seen

Results to Text (Ctrl+T)-within Query Editor

Results to File (Ctrl+Shift+F)-tabulated Word file

Page 9: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Varying the Output Display in Query Editor –Results to Text (Ctrl+T)

Page 10: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Varying the Output Display in Query Editor Varying the Output Display in Query Editor –Results to Text

See how the output is different than the previous query:

SELECT CustomerFirstName As ' First Name'

,CustomerLastName AS ' Surname' ,ClearedBalance Balance FROM CustomerDetails. Customers

Page 11: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Varying the Output Display in Query Editor Varying the Output Display in Query Editor –Results to File

Ctrl+Shift+F

See how the output is different than the previous query:

SELECT CustomerFirstName As ' First Name'

,CustomerLastName AS ' Surname' ,ClearedBalance Balance FROM CustomerDetails. Customers

Page 12: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Varying the Output Display in Query Editor Varying the Output Display in Query Editor –Results to File

Ctrl+Shift+F

Page 13: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Limiting a Search: The Use of WHERE

Press (Ctrl + D) to set the default to gridSelect firstname, lastname From customersWhere firstname = ‘robert’

Page 14: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Limiting a Search: The Use of WHERE

Page 15: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Limiting Number of Rows Returned

SET ROWCOUNT n totally separate statement from the SELECT

statement and can in fact be used with other statements within T-SQL.

limit or reset the number of rows that will be processed for the session in which the statement is executed.

To reset the session so that all rows are taken into consideration, you would set the ROWCOUNT number to 0.

Page 16: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Limiting the Output via ROWCOUNT

Page 17: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Using Transactions

We face situations where more than one data modification has to take place and succeed, and the first one succeeds but a subsequent modification fails.

Transactions allow us to revert to a consistent state after such events occur.

Page 18: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Using Transactions

Page 19: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Using Transactions -Rollback

Page 20: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Using Transactions -Rollback

Page 21: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Deleting Data

Page 22: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Deleting Data

Use transactions for any type of table modification in your application. Imagine that you’re at the ATM and you are

transferring money from your savings account to your checking account. During that process, a transaction built up of many

actions is used to make sure your money doesn’t credit one system and not the other.

If an error occurs, the entire transaction will roll back, and no money will move between the accounts.

Page 23: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Deleting Data using Transaction

Page 24: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Deleting Data using Transaction

Page 25: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Deleting Data using Transaction

Page 26: Chapter 9 Selecting, Updating, and Deleting Data Syed Rizvi.

Deleting Data using Transaction