Download presentation
Presentation is loading. Please wait.
Published byCarol Aubrie Owen Modified over 8 years ago
1
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL
2
2 3 Concepts of Database Management, 5 th Edition Objectives u Introduce the structured query language (SQL) u Use simple and compound conditions in SQL u Use computed fields in SQL u Use built-in SQL functions u Use subqueries in SQL u Group records in SQL
3
3 3 Concepts of Database Management, 5 th Edition Objectives (Con’t) u Join tables using SQL u Perform union operations in SQL u Use SQL to update database data u Use an SQL query to create a table in a database
4
4 3 Concepts of Database Management, 5 th Edition SQL (Structured Query Language) u Provides users with the capability to query a relational database u Must enter commands to obtain desired results u Developed under the name SEQUEL at IBM
5
5 3 Concepts of Database Management, 5 th Edition Table Creation u SQL CREATE TABLE l Used to describe layout of a table u Typical restrictions placed by DBMS l Names cannot exceed 18 characters l Names must start with a letter l Names can contain only letters, numbers, and underscores (_) l Names cannot contain spaces
6
6 3 Concepts of Database Management, 5 th Edition Typical Data Types u INTEGER l Numbers without a decimal point u SMALLINT l Uses less space than INTEGER u DECIMAL(p,q) l P number of digits; q number of decimal places u CHAR(n) l Character string n places long u DATE l Dates in DD-MON-YYYY or MM/DD/YYYY
7
7 3 Concepts of Database Management, 5 th Edition Simple Retrieval u SELECT-FROM-WHERE-- basic form of SQL retrieval command u SELECT clause – lists fields that you wish to display u FROM clause – lists table or tables that contain data to display in query results u WHERE clause – optional section used to list any conditions to be applied to the data to retrieve
8
8 3 Concepts of Database Management, 5 th Edition Figures 3.1-3.2: Simple Retrieval
9
9 3 Concepts of Database Management, 5 th Edition Figures 3.3-3.4: SQL Query to List Part Table
10
10 3 Concepts of Database Management, 5 th Edition Figures 3.5-3.6: SQL Query with Where Condition
11
11 3 Concepts of Database Management, 5 th Edition Simple Condition u Includes the field name, comparison operator, and either another field or a value u Two versions of the “not equal to” operator l and != u Use correct one for the version of SQL u Using wrong version will generate an error
12
12 3 Concepts of Database Management, 5 th Edition Figure 3.7: SQL Comparison Operators
13
13 3 Concepts of Database Management, 5 th Edition Figures 3.8-3.9: SQL Query to Find Customer 148
14
14 3 Concepts of Database Management, 5 th Edition Figures 3.10-3.11: SQL Query to Find All Customers in ‘Grove’
15
15 3 Concepts of Database Management, 5 th Edition Figures 3.12-3.13: Query to find Customers with Credit Limit Exceeding Balance
16
16 3 Concepts of Database Management, 5 th Edition Compound Conditions u Formed by connecting two or more simple conditions using one or both of the operators AND and OR u Can also precede a single condition with NOT operator to negate a condition u With AND all simple conditions must be true for the compound condition to be true u With OR the compound condition will be true when any of the simple conditions are true
17
17 3 Concepts of Database Management, 5 th Edition Figures 3.14-3.15: SQL Query with Compound Condition using ‘AND’
18
18 3 Concepts of Database Management, 5 th Edition Figures 3.16-3.17: SQL Query using ‘OR’
19
19 3 Concepts of Database Management, 5 th Edition Figures 3.18-3.19: SQL Query using ‘NOT’
20
20 3 Concepts of Database Management, 5 th Edition Figures 3.20-3.21: Query with ‘BETWEEN’ Operator
21
21 3 Concepts of Database Management, 5 th Edition Computed Fields u Field whose values are derived from existing fields u Computed fields can involve: l Addition (+) l Subtraction (-) l Multiplication (*) l Division (/)
22
22 3 Concepts of Database Management, 5 th Edition Figures 3.22-3.23: SQL Query with Computed Field
23
23 3 Concepts of Database Management, 5 th Edition Figures 3.24-3.25: SQL Query with Computed Field and Condition
24
24 3 Concepts of Database Management, 5 th Edition Special Operators and Sorting u LIKE operator is used with a wildcard symbol to find non-exact matches u IN operator provides a concise way of phrasing certain conditions u ORDER BY clause is used to display results in desired order u Sort key or key – field on which to sort data
25
25 3 Concepts of Database Management, 5 th Edition Figures 3.26-3.27: SQL Query with ‘LIKE’ Operator
26
26 3 Concepts of Database Management, 5 th Edition Figures 3.28-3.29: SQL Query with ‘IN’ Operator
27
27 3 Concepts of Database Management, 5 th Edition Figures 3.30-3.31: SQL Query to Sort Data
28
28 3 Concepts of Database Management, 5 th Edition Figures 3.32-3.33: SQL Query to Sort on Multiple Fields
29
29 3 Concepts of Database Management, 5 th Edition Built-In Functions u Built-In functions (aggregate functions) are used to l COUNT - calculate number of entries l SUM or AVG – finds sum or average of all entries in a given column l MAX or MIN – finds largest or smallest values respectively
30
30 3 Concepts of Database Management, 5 th Edition Figures 3.34-3.35: SQL Query to Count Records
31
31 3 Concepts of Database Management, 5 th Edition Figures 3.36-3.37: SQL Query to Count Records and Calculate a Total
32
32 3 Concepts of Database Management, 5 th Edition Figures 3.38-3.39: SQL Query to Perform Calculations and Rename Fields
33
33 3 Concepts of Database Management, 5 th Edition Nesting Queries u Nested query - place one query inside another u Subquery – inner query u Subquery is evaluated first u Outer query is evaluated after the subquery
34
34 3 Concepts of Database Management, 5 th Edition Figures 3.40-3.41: SQL Query with Subquery
35
35 3 Concepts of Database Management, 5 th Edition Grouping u Means creating groups of records that share some common characteristic u GROUP BY clause used to indicate grouping in SQL u HAVING clause – is to groups what the WHERE clause is to rows
36
36 3 Concepts of Database Management, 5 th Edition Figures 3.42-3.43: SQL Query to Group Records
37
37 3 Concepts of Database Management, 5 th Edition Figures 3.44-3.45: SQL Query to Restrict Groups
38
38 3 Concepts of Database Management, 5 th Edition Figures 3.46-3.47: SQL Query with ‘WHERE’ and ‘HAVING’ Clauses
39
39 3 Concepts of Database Management, 5 th Edition Joining Tables and Union u Allow queries to locate data from more than one table u Accomplished by entering the appropriate conditions in the WHERE clause u Two tables involved in a union must have the same structure l Same number of fields l Corresponding fields must have same data types
40
40 3 Concepts of Database Management, 5 th Edition Figures 3.48-3.49: SQL Query to Join Tables
41
41 3 Concepts of Database Management, 5 th Edition Figures 3.50-3.51: Query to Restrict Records in Join
42
42 3 Concepts of Database Management, 5 th Edition Figures 3.52-3.53: Query to Join Multiple Tables
43
43 3 Concepts of Database Management, 5 th Edition Figures 3.54-3.55: SQL Query to Perform Union
44
44 3 Concepts of Database Management, 5 th Edition Updating Tables u UPDATE command used to make changes to existing data u INSERT command used to add new data to a table u DELETE command used to delete data from the database
45
45 3 Concepts of Database Management, 5 th Edition Figures 3.56-3.57: SQL Query to Update Data
46
46 3 Concepts of Database Management, 5 th Edition Figures 3.58: SQL Query to Delete Rows
47
47 3 Concepts of Database Management, 5 th Edition Create a Table from a Query u INTO clause used to save the results of a query as a table u Specified before FROM and WHERE clauses
48
48 3 Concepts of Database Management, 5 th Edition Figures 3.59-3.60: SQL Query to Create New Table
49
49 3 Concepts of Database Management, 5 th Edition Summary u SQL (Structured Query Language): language for manipulating relational databases u Three classes of statements: definition, query, and manipulation u You create objects with the CREATE statement u You perform queries with the SELECT statement the condition is part of the WHERE clause
50
50 3 Concepts of Database Management, 5 th Edition Summary (con’t.) u Simple queries use operators such as, etc. to compare field names with the values you are seeking u Compound conditions are formed with logical operators, AND, OR, and NOT u Queries can be nested by using subqueries u The GROUP BY clause can group data in a query u The JOIN clause can combine data from two or more tables
51
51 3 Concepts of Database Management, 5 th Edition Summary u Built-in functions allow you to count rows in a query and summarize data, such as sums or averages u The UNION statement allows you to view data from two queries in one result set u SQL allows you to create tables from queries with the INTO clause u You can delete and update data as well
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.