Chapter-3
Chapter-3
Introduction to SQL
3.1
4. View definition.
– The SQL DDL includes commands for defining views.
5. Transaction control.
– SQL includes commands for specifying the
beginning and ending of transactions.
The SQL language has several parts:
7. Authorization.
– The SQL DDL includes commands for specifying
access rights to relations and views.
3.2
2. varchar(n):
– A variable-length character string with user-specified
maximum length n.
– The full form, character varying, is equivalent.
3. int:
– An integer (a finite subset of the integers that is machine
dependent).
– The full form, integer, is equivalent.
4. smallint:
– A small integer (a machine-dependent subset of the integer
type).
Basic Types
5. numeric(p, d):
– A fixed-point number with user-specified precision.
– The number consists of p digits (plus a sign), and d of
the p digits are to the right of the decimal point.
• Thus, numeric(3,1) allows 44.5 to be stored exactly, but neither
444.5 or 0.32 can be stored exactly in a field of this type.
7. float(n):
– A floating-point number, with precision of at least n
digits.
Basic Schema Definition
Basic Schema Definition
Basic Schema Definition
drop table
• To remove a relation from an SQL database,
we use the drop table command.
Introduction to SQL
3.4
• Concatenating:
– Fname || lname
• Select fname || lname as Name from
emp;
String Operations
• Converting strings:
– Upper(s)
– Lower(s)
• Select ID from emp where
upper(name)=“ALI”;
– ’%Comp%’
• matches any string containing “Comp” as a substring
– ’_ _ _ ’
• matches any string of exactly three characters.
– ’_ _ _%’
• matches any string of at least three characters.
String Operations [Con.]
• Find the names of all departments whose
building name includes the substring
‘Watson’.
Escape Character \
• like ’ab\%cd%’:
– Matches all strings beginning with “ab%cd”.
• like ’ab\\cd%’:
– Matches all strings beginning with “ab\cd”.
Attribute Specification in Select
Clause
Ordering the Display of Tuples
Where Clause Predicates
• Find the names of instructors with salary
amounts between $90,000 and $100,000
Where Clause Predicates
• Find the instructor names and the courses
they taught for all instructors in the Biology
department who have taught some course.
Chapter 03
Introduction to SQL
3.5
SET OPERATIONS
Set Operations
• The SQL operations union, intersect, and
except operate on relations and correspond
to the mathematical set-theory operations
∪, ∩, and −.
Set Operations
• The set of all courses taught in the Fall 2009
semester:
NULL VALUES
Null
• Null values present special problems in
relational operations:
– Arithmetic operations.
– Comparison operations.
– Set operations.
Arithmetic Expression Involving Null
• The result of an arithmetic expression
(involving, for example +, −, ∗, or /) is null if
any of the input values is null.
Introduction to SQL
3.7
AGGREGATE FUNCTIONS
Aggregate Functions
• Aggregate functions are functions that take
a collection (a set or multiset) of values as
input and return a single value.
4
Chapter 03
Introduction to SQL
3.8
NESTED SUBQUERIES
Set Membership
• The in connective tests for set membership.
– Where the set is a collection of values produced by a select clause.
• The not in connective tests for the absence of set membership.
Example : Find all the courses taught in the both the Fall 2009 and
Spring 2010 semesters.
Set Membership
• Example, to find all the courses taught in the
Fall 2009 semester but not in the Spring
2010 semester, we can write:
Set Membership
• Selects the names of instructors whose
names are neither “Mozart” nor “Einstein”
Introduction to SQL
82
3.9
83
Deletion
84
Deletion
85
Insertion
86
Updates
87
Updates
• Suppose that all instructors with salary over
$100,000 receive a 3 percent raise, whereas
all others receive a 5 percent raise.
Note that the order of the two
update statements is important. If
we changed the order of the two
statements, an instructor with a
salary just under $100,000 would
receive an over 8 percent raise.
88
Updates
• Suppose that all instructors with salary over
$100,000 receive a 3 percent raise, whereas
all others receive a 5 percent raise.
89