Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Tandem Computers wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Relational algebra wikipedia , lookup
Clusterpoint wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
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 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 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? Transaction SQL • Basic – Select – Insert – Update – Delete 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 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 ‘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’) ‘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 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, 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 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 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] Insert statement (pg 66) • Insert [into] <table> [(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’) 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. Nulls are BAD • What does Null mean? – Not supposed to be there? – Unknown • Exec sp_help <name> page 69 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 Update Statement • Update <table name> set <column> = <value> [,<column> = <value>]* [from <source table(s)>] [where <restrictive condition>] • Update shippers set companyName = ‘fubar’ where shipperID = 10 Delete Statement • DELETE <table_Name> where <search condition>] • Delete customer where customerID = 10