Download presentation
Presentation is loading. Please wait.
Published byJoan Morton Modified over 9 years ago
2
Industry standard for interacting with relational databases Statements to Enter, Retrieve, Modify, Remove & Display data stored in database tables ◦ Pose Ad-Hoc Queries ◦ Create Various Objects ◦ Perform Database Maintenance Works with SQL-Server and almost all other data management sytems
3
Query Statements Data Definition Language Statements Data Manipulation Language Statements Transaction Control Statements Data Control Language Statements
4
Retrieve rows from one or more tables ◦ Select Statement SELECT FROM WHERE GROUP BY HAVING ORDER BY
5
Define Structures that make up the database ◦ Tables ◦ Views CREATE ALTER DROP RENAME TRUNCATE
6
Modify the contents of tables ◦ INSERT ◦ UPDATE ◦ DELETE
7
Tools to make changes to rows permanent, or to revoke the changes ◦ COMMIT ◦ ROLLBACK ◦ SAVEPOINT
8
Who has access to what tables Who can log into the SQL Server System Access and Privileges for each user for various tables ◦ GRANT ◦ REVOKE
9
Write SQL statements to accomplish a database task SQL-Server SQL Server Management Studio Interface Can be written on multiple lines (cannot split words) Cannot abbreviate words Separate words by at least one white space Not Case Sensitive Commas separate lists Period delimits two names (tableName.fieldName) Single Quotation marks enclose literal character strings (including dates) Semicolon indicates the end of a SQL Statement
10
11/30/201510 SQL SELECT Statement Column References ORDER BY Clause WHERE Clause SQL Statement String Construction
11
11/30/201511 Syntax: SELECT [DISTINCT | ALL] { * | } FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ]
12
11/30/201512 Syntax: [[AS] ] Derived column may ◦ Be a column ◦ Contain an expression referencing one or more columns ◦ Contain function arguments The ‘AS’ is for an alias, or alternate name, for the derived column result
13
11/30/201513 SELECT First, Last, City SELECT Player.First, Team.Location SELECT Price * Qty AS InvoiceAmount SELECT First, [Last Name] AS Last SELECT Max(TeamId) SELECT Avg(Price) SELECT IsNull(Name) SELECT DISTINCT State SELECT * SELECT Player.*, Team.Nickname
14
11/30/201514 Specifies which records from the tables listed in the FROM clause are affected by the SELECT statement Only those records satisfying are included in the result is a logical expression which may include: ◦ One or more of four types of logical predicates ◦ the logical operators AND, OR, NOT
15
11/30/201515 WHERE clause has four logical predicates: ◦ comparisons w/relational operators ( =, >, <>) expression1 comparison-operator expression2 ◦ LIKE expression LIKE pattern ◦ IN expression [NOT] IN (value1, value2,...) ◦ BETWEEN expression [NOT] BETWEEN value1 AND value2
16
11/30/201516 WHERE City = ‘New Orleans’‘* relational ops WHERE Rate > 5.50 WHERE State LIKE ‘?L’‘* LIKE predicate WHERE [Last Name] LIKE ‘S*’ WHERE State IN (‘AL’, ‘MS’, ‘FL’)‘* IN predicate ‘* BETWEEN predicate WHERE Birthday BETWEEN #7/1/78# AND #7/30/78# WHERE [Pay Rate] BETWEEN 5.50 AND 10.00 ‘* multiple comparisons with AND, OR, NOT WHERE City=‘New Orleans’ AND Name LIKE ‘L*’ WHERE NOT State=‘AL’ AND Major=‘CIS’ WHERE Class=‘Union’ OR Rate>10.00
17
11/30/201517 Syntax: ORDER BY Examples: ORDER BY LastName, FirstName ORDER BY HR DESC ORDER BY TeamId ASC, HR DESC
18
11/30/201518 Strings can be constructed in code with concatenation and variables can be concatenated into the string Dim Sql As String Sql = “Select LName, FName From Emp” Sql = Sql & “Where Title = ‘“ & cboTitle.Text & “‘;” When variable contains a string surround it with quotes Sql = “Select... WHERE City=‘Jacksonville‘;” or Sql = “Select... WHERE City=‘“ & txtCity.Text & “‘;” When variable contains numeric data do not use quotes Sql = “Select... WHERE Age > “ & intAge & “ ORDER BY Age”
19
11/30/201519 SQL INSERT statement ◦ Adds one or more rows to a table SQL UPDATE statement ◦ Modifies one or more columns of one or more rows of a table SQL DELETE statement ◦ Removes one or more rows from a table
20
11/30/201520 Syntax: INSERT INTO [( [{, }…])] VALUES ( [{, }…]) ◦ Second line is optional If you omit, then values list must be complete and in order of field creation If you include column list, values list must match column list in number and order (but not in field creation order
21
11/30/201521 INSERT INTO Team (League, Location, Nickname, Stadium) VALUES (‘AL’, ‘Mobile’, ‘Bay Bears’, ‘Hank Aaron Stadium’)
22
11/30/201522 Syntax: UPDATE SET [{, }…] [WHERE ] ◦ syntax: = ◦ Omitting WHERE updates all rows
23
11/30/201523 UPDATE Team SET Stadium=‘Citizens Bank Park’ WHERE Stadium=‘Veterans Stadium’ UPDATE Player SET HR=HR + 1 WHERE PlayerId=22
24
11/30/201524 Syntax: DELETE FROM [WHERE ] ◦ Omission of WHERE removes all rows
25
11/30/201525 DELETE FROM Team WHERE Nickname=‘Bay Bears’ DELETE FROM Team WHERE League NOT IN ('AL', 'NL')
26
11/30/201526 A function that generates a single value from a group of values ◦ often used with Group By and Having clauses ◦ a.k.a. set function Examples: ◦ Avg, Count, Max, Min, and Sum
27
11/30/2015 27 Aggregate functionDescription AVG(expr) Average of the values in a column. The column can contain only numeric data. COUNT(expr), COUNT(*) A count of the values in a column (if you specify a column name as expr) or of all rows in a table or group (if you specify *). COUNT(expr) ignores null values, but COUNT(*) includes them in the count. MAX(expr) Highest value in a column (last value alphabetically for text data types). Ignores null values. MIN(expr) Lowest value in a column (first value alphabetically for text data types). Ignores null values. SUM(expr) Total of values in a column. The column can contain only numeric data.
28
11/30/201528 A query (SQL statement) that summarizes information from multiple rows by including an aggregate function such as Sum or Avg ◦ For example, you can create a query that averages the contents of a price column SELECT Avg(Price) AS AvgPrice FROM Book
29
11/30/201529 Aggregate queries can also display subtotal information by creating groups of rows that have data in common ◦ An example would be a query that displays the average price of a book for each publisher ◦ Use the GROUP BY clause SELECT PublisherID, Avg(Price) As AvgPrice FROM Book GROUP BY PublisherID
30
11/30/201530 Combines records with identical values in the specified field list into a single record. Syntax: A summary value is created for each record if you include an SQL aggregate function, such as Sum or Count, in the SELECT statement. For example total the home runs for each team: SELECT TeamId, Sum(HR) As TeamTotalHR FROM Player Group By TeamId SELECT SELECT FROM FROM [WHERE ] [GROUP BY ] [HAVING ]
31
11/30/201531 Use the WHERE clause to exclude rows you don't want grouped, and use the HAVING clause to filter records after they've been grouped. For example, team’s with more than 100 homers: SELECT TeamId, Sum(HR) As TotalTeamHR FROM Player GROUP BY TeamId HAVING Sum(HR)>100 Or, a count of each team’s players with less than 10 homers: SELECT TeamId, Count(*) As LowHRcount FROM Player WHERE HR<10 GROUP BY TeamId
32
Direct Execution ◦ Communicate directly through a client application SQL Server Management Studio Module Binding ◦ Create blocks of SQL statements and combined with a complete application through a linker program Embedded SQL ◦ Statements are placed directly into the host programming language (C++ or Java) ◦ Requires a SQL Server preprocessor CLI (Call-Level Interface) ◦ Invoke SQL statements by passing the statements directly to the subroutines that process them ◦ Executed directly by the DBMS
33
Rich, Easy to Use environment for creating and executing SQL instructions Windows dialog box GUI Interface – closely matches Visual Studio.Net 2005 interface
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.