Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tools of the trade TSQL CIS 407. SQL Server Tools Books on line! Don’t use sql server authentication –Use windows authentication (safer) for developer.

Similar presentations


Presentation on theme: "Tools of the trade TSQL CIS 407. SQL Server Tools Books on line! Don’t use sql server authentication –Use windows authentication (safer) for developer."— Presentation transcript:

1 Tools of the trade TSQL CIS 407

2 SQL Server Tools Books on line! Don’t use sql server authentication –Use windows authentication (safer) for developer version Don’t embed passwords in code (ways to avoid this ) Connection strings in appendix X (for accessing with code Query window –This is where we do almost everything using Tsql statements (chapter 3) –Not just queries – create update tables, system info, etc

3 Query Window Example pg 33 (follow along on your own machine) Open query window Select * from information-schema.tables –Information_schema is a special access path for displaying meta data about your system’s database Hit execute button Show execution plan –Lots of stuff going on to convert SQL to code (loops, ifthenelse, etc). –Why do you care?

4 Transaction SQL Basic –Select –Insert –Update –Delete

5 Basic Select Syntax on page 42 Use northwinds Select * from customers –Select all the rows and columns from customer relation –Using * is expensive – better to specify only those columns you want Select companyName from customers –All rows but just the companyname column from customers

6 Select Statement Select companyName from northwinds.customers –Can use database name before relation name Select companyName, contactName from customers where customerId = 23 –Page 48 details where clause operators =, =>, =, !=, !>, !< and, or, not, between, like, in, all, any, some, exists

7 ‘Where’ clause examples Select * from customers where (city=‘NY’ or city=‘detroit’) and contactTitle = ‘boss’ or contactTitle > ‘flunky’ select * from customers where companyName in (‘snufu’,’acme’,’fubar’)

8 ‘Order by’ Clause Relations are sets of tuples or rows –Sets are not ordered –Cannot assume results of query will be returned in any order !!! If want particular order, then MUST use ‘order by’ clause –Expensive Select * from customers where city = ‘ny’ order by companyName DESC, address

9 Aggregating data Select avg(unitPrice) as ‘averagePrice’ from product as p where p.categoryID = 10 –note alias – ‘as’ optional –Alias has other uses – more later Select count(customerID) from customers c where c.city = ‘NY’ Aggregates: avg, min, max, count,

10 Aggregates and ‘group by’ Aggregates for each value of specified col. Select orderID, min(quantity) from [order details] (!!!!!) where orderID between 11000 and 12002 group by orderID

11 Constraining Groups with ‘Having’ Select employeeID, count(customerID) from Orders where shipCity = ‘ny’ having count <100 List the number of customers with orders shipped to NY for each employee but only if the count is greater than 100. “where” constrains what goes into group ‘having” constrains what goes in result

12 Distinct Relations are sets and so should not have duplicates. But removing duplicates in the results set requires either a complex datastructure or sorting (expensive) –So SQL server does not do this unless you ask Select distinct city from customers Select count(distinct customerId) from [order detail]

13 Insert statement (pg 66) Insert [into] [(column_list)] values (data_values) Insert shippers (shipperID, companyName, Phone) values (1234,’acme’,’(208)2822685’) Listing columns optional: Insert shippers values (1234,’acme’, ’(208)2822685’)

14 Insert Statement Not all columns need be listed (except keys) Insert shippers (shipperid) values (1234) –Nulls or defaults for the rest BUT much better –Insert shippers (shipperID, companyName, Phone) values (1234,default, null) –Page 67: recommend naming every column every time, even if have to use explicit null and default – code more readable.

15 Nulls are BAD What does Null mean? –Not supposed to be there? –Unknown Exec sp_help page 69

16 Combine Insert & Select (pg71) Use Northwind Declare @MyTable TABLE ( OrderID int, CustomerID char(5) ) Insert into @mytable select orderid, customerid from Northwind.dbo.Orders where OrderID between 10240 and 10250 select * from @mytable

17 Update Statement Update set = [, = ]* [from ] [where ] Update shippers set companyName = ‘fubar’ where shipperID = 10

18 Delete Statement DELETE where ] Delete customer where customerID = 10


Download ppt "Tools of the trade TSQL CIS 407. SQL Server Tools Books on line! Don’t use sql server authentication –Use windows authentication (safer) for developer."

Similar presentations


Ads by Google