Download Structure Query Language

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
SI 654
Database Application Design
Winter 2003
Dragomir R. Radev
1
© 2002 by Prentice Hall
Database Processing
Eighth Edition
Structured
Query
Language
2
Chapter 9
David M. Kroenke
© 2002 by Prentice Hall
Structure Query Language
• Structure Query Language is known
as either
– Its acronym, SQL, or
– SEQUEL, the name of the original
version of SQL
• SEQUEL was developed by IBM in
the mid-1970s.
3
© 2002 by Prentice Hall
SQL, not a Procedural
Programming Language
• SQL is not a programming language
itself, it is a data access language
• SQL may be embedded in traditional
procedural programming languages
(like COBOL)
4
© 2002 by Prentice Hall
SQL Syntax
SQL is not case sensitive.
SELECT field(s) ‘what columns will be
retrieved
FROM table(s); ‘which table contains the
column data
e.g.,
SELECT Name, Phone
FROM Student;
5
© 2002 by Prentice Hall
The DISTINCT Qualifier
• Eliminating duplicate rows on the
output…
SELECT DISTINCT StateAddress
FROM Employee;
6
© 2002 by Prentice Hall
The WHERE Clause
• Reducing the output based on
specified criteria…
SELECT StudentName
FROM Students
WHERE GradePointAverage >= 3.0;
7
© 2002 by Prentice Hall
Comparison Operators
Equals =
Not equals <>
Greater than >
Less than <
Greater than or equal to >=
Less than or equal to <=
Within a list of values IN
A logical NOT
Within a range BETWEEN
8
© 2002 by Prentice Hall
IN a List of Values
SELECT StudentName
FROM Student
WHERE State IN [‘PA’, ‘MA’, ‘CA’];
9
© 2002 by Prentice Hall
The Logical NOT
SELECT StudentName
FROM Students
WHERE State NOT IN [‘NJ’, ‘NM’, ‘NY’];
SELECT StudentName
FROM Students
WHERE NOT GradePointAverage >= 3.0;
10
© 2002 by Prentice Hall
Within a Range of Values
SELECT StudentName
FROM Student
WHERE StudentID BETWEEN 250 and 300;
11
© 2002 by Prentice Hall
Using Wildcard Character
Substitutions
• The LIKE keyword is used in place of
the = sign when you use wildcard
characters.
• The underscore character (_) is a
single character substitution
• The percent character (%) is a multicharacter substitution
12
© 2002 by Prentice Hall
Using LIKE
SELECT StudentID
FROM Student
WHERE StudentName LIKE ‘K%’;
SELECT PartName
FROM Part
WHERE PartNumber LIKE ‘_ABC%’;
13
© 2002 by Prentice Hall
NULL Means Nothing
• A NULL character means that nothing
has been entered. This is different
from a space or a zero.
SELECT Name
FROM Student
WHERE Major IS NULL;
14
© 2002 by Prentice Hall
ORDER BY… Sorting Outputs
• Sorting in descending order…
SELECT StudentID, Name
FROM Student
ORDER BY Name DESC;
• Sorting in ascending order…
SELECT StudentID, Name
FROM Student
ORDER BY Name ASC;
15
© 2002 by Prentice Hall
Built-in Functions
•
•
•
•
Counting number of rows COUNT
Adding the values in a column SUM
Averaging the values in a column AVG
Finding the maximum value in a column
MAX
• Finding the minimum value in a column MIN
16
© 2002 by Prentice Hall
Built-in Functions
SELECT Count (*)
FROM Student
WHERE State = ‘WI’;
SELECT Sum (Amount)
FROM SalesReceipt;
SELECT Max (Score)
FROM Assignments;
17
© 2002 by Prentice Hall
Grouping the Output
SELECT Name, State
FROM Student
GROUP BY State;
18
© 2002 by Prentice Hall
Reducing the Groups
Displayed
SELECT Name, State
FROM Student
GROUP BY State
HAVING Count (*) > 4;
19
© 2002 by Prentice Hall
Sub-Queries
SELECT Name
FROM Student
WHERE SID IN
(SELECT StudentNumber
FROM Enrollment
WHERE ClassName = ‘MIS445’);
20
© 2002 by Prentice Hall
Joining Tables
SELECT Student.SID, Student.Name,
Enrollment.ClassName
FROM Student, Enrollment
WHERE Student.SID =
Enrollment.StudentNumber AND
Student.State = ‘OH’;
21
© 2002 by Prentice Hall
EXISTS
SELECT DISTINCT StudentNumber
FROM Enrollment A
WHERE EXISTS
(SELECT *
FROM Enrollment B
WHERE A.StudentNumber =
B.StudentNumber
AND A.ClassName NOT = B.ClassName);
22
© 2002 by Prentice Hall
Entering Data
INSERT INTO Enrollment
VALUES (400, ‘MIS445’, 44);
23
© 2002 by Prentice Hall
Deleting Data
DELETE Student
WHERE Student.SID = 100;
24
© 2002 by Prentice Hall
Modifying Data
UPDATE Enrollment
SET SeatNumber = 44
WHERE SID = 400;
25
© 2002 by Prentice Hall
Related documents