Introduction To Databases - Brief Sketch-Combined
Introduction To Databases - Brief Sketch-Combined
Brief Sketch
Notes I take
3:54
Introduction to Databases 1
Chapter 1 Introduction
Broadly speaking, there are two modes in which databases are used:
data analytics: the processing of data to draw conclusions, and infer rules or
decision procedures, which are then used to drive business decisions.
1.2 Data model: a collection of conceptual tools for describing data, data
relationships, data semantics, and consistency constraints. The data models can be
classified into four different categories.
1- Relational Model: uses a collection of tables to represent both data and the
relationships among those data. Tables are also known as relations. Each tables
contains records of a particular type. Each record type defines a fixed number of
fields, or attributes. The columns of the table correspond to the attributes of the
record type.
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-1/ 1/7
10/26/21, 6:25 PM Introduction to Databases 1 – Brief Sketch
type must have the same set of attributes. JSON and Extensible Markup
Language (XML) are semi-structured data representations.
Data are represented in the form of tables. Each table has multiple columns, and
each column has a unique name. Each row of the table represents one piece of
information.
Physical level: the lowest level of abstraction describes how the data are actually
stored.
Logical level: The next higher-level of abstraction describes what data are stored
in the database, and what relationships exist among those data.
View level: The highest level of abstraction describes only part of the database –
it only shows the relevant information depending on which user is accessing the
database. So it can also “hide” content.
Schema: the overall design of the database. Each database has a physical, logical
and view level schema.
(In practice, DDLs and DMLs are not two separate languages)
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-1/ 2/7
10/26/21, 6:25 PM Introduction to Databases 1 – Brief Sketch
Types of constraints:
1- Domain Constraints: check if the values are of the correct data type.
2- Referential Integrity: check that a value that appears in one relation for a
given set of attributes also appears in a certain set of attributes in another relation.
3:54
Ex. The dept_name value in a course record must appear in the dept_name
attribute of some record of the department relation.
3- Authorization: differentiate among the users the type of access they are
permitted. There are
The processing of DDL statements generates some output. The output of the DDL
is placed in the data dictionary, which contains metadata. The data dictionary
is a special type of table that can be accessed and updated only by the database
system itself (not a regular user). The database system consults the data dictionary
before reading or modifying actual data.
Ex.
(dept_name char(20),
building char(15),
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-1/ 3/7
10/26/21, 6:25 PM Introduction to Databases 1 – Brief Sketch
1- Procedural DMLs: requires users to specify what data are needed and how to
get those data
Ex.
select instructor.name
from instructor
The result of this qurty is a table with a single column laballed name and a set of
rows, each of which contains the name of an instructor whose dept_name is
History.
SQL does not support actions such as input from users, output to displays, or
communication over the network. Such computations must be written in a host
language, such as C/C++, Java, or Python, with embedded SQL queries that access
the data in the database. To access the database, DML statements need to be sent
from the host to the database where they will be executed.
2- choose a data model and translate these requirements into a conceptual schema
of the database. The schema provides a detailed overview of the enterprise, and
describes the data and their relationships.
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-1/ 4/7
10/26/21, 6:25 PM Introduction to Databases 1 – Brief Sketch
The functional components of a database system can be broadly divided into three
parts:
1- storage manager: the server, and the amount of storage it contains
(gigabytes, terabytes).
3:54
2- query processor: simplifies and facilitates access to data.
(use, write) > (query processor) > (storage manager) > (disk storage)
Three-tier architecture: the client machine acts as a front end and does not
contain any direct database calls.Web browsers and mobile applications are the
most commonly used application clients today.
(front end) > (application server) > (database system) > (access the data)
There are three types of database-system users, differentiated by the way they
expect to interact with the system.
1- Naive users: normal users, usually interact with a forms interface where they
fill appropriate fields of the form. They usually read reports generated from the
database.
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-1/ 5/7
10/26/21, 6:25 PM Introduction to Databases 1 – Brief Sketch
Advertisements
You can hide ads completely by upgrading to one of our paid plans.
UPGRADE NOW
DISMISS MESSAGE
3:54
Share this:
Reblog
Like
Related
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-1/ 6/7
10/26/21, 6:25 PM Introduction to Databases 1 – Brief Sketch
Leave a Reply
Enter your comment here...
3:54
Brief Sketch,
Create a free website or blog at WordPress.com.
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-1/ 7/7
10/26/21, 6:26 PM Introduction to Databases 2: The Relational Model – Brief Sketch
Brief Sketch
Notes I take
3:45
relation table
attribute column
In mathematical terminology,
We require that for all relations r, the domains of all attributes of r be atomic,
that is, an attribute does not have a list as its element, but only one element.
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-2-the-relational-model/ 1/6
10/26/21, 6:26 PM Introduction to Databases 2: The Relational Model – Brief Sketch
2.3 Keys
No two tuples in a relation are allowed to have exactly the same value for all
attributes.
A primary key is listed before the other attributes in the relation schema, and it is
also underlined.
Ex.
foreign key: when an attribute (or a set of attributes) from a relation, r1, can only
have values that are included in the primary key of another relation, r2.
Ex. The attribute dept_name from the realtion instructor can only have values
that are included in the attribute dept_name from the relation department (where
dept_name is the primary key of the relation department).
referencing relation: r1
referenced relation: r2
In schema diagrams,
Foreign key constaints appear as arrows from the foreign key attributes of the
referencing relation to the primary key of the referenced relation.
imperative query language: the user instructs the system to perform a specific
sequence of operations on the database to compute the desired result.
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-2-the-relational-model/ 2/6
10/26/21, 6:26 PM Introduction to Databases 2: The Relational Model – Brief Sketch
Pure query languages include: relational algebra, Tuple relational calculus, and
domain relational calculus.
Relational algebra consists of a set of operations that take one or two relations
3:45
as input and produce a new relation as their result.
Unary operations: select, project, and rename operations. They are called
unary operations because they operate on one relation.
The select operation (σ) selects tuples that satisfy a given predicate.
Thus, to select those tuples of the instructor relation where the instructor is in the
“Physics” department, we write:
σdept_name = “Physics”(instructor)
The project operation (Π) is a unary operation that returns its argument relation,
with certain attributes left out. Any duplicate rows are eliminated.
We list those attributes that we wish to appear in the result as a subscript to Π.
Suppose we want to list all instructors’ ID, name, and salary, but we do not care
about the dept_name.
We may also use expressions to modify the values that appear in the table.
Suppose we want the monthly salary of the professors:
Suppose we want to find the names of all instructors in the Physics department.
We write:
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-2-the-relational-model/ 3/6
10/26/21, 6:26 PM Introduction to Databases 2: The Relational Model – Brief Sketch
3:45 in
Assume there are n1 tuples is r1 and n2 tuples in r2. Then there are n1 * n2 tuples
r1 X r2.
r⋈θ s = σθ(r X s)
(2) The types of the ith attributes of both input relations must be the same, for
each i.
Union: a table whose tuples are found either only in r1, or only in r2, or in both r1
and r2.
Ex.
Πcourse_id (σsemester = “Fall” ^ year = 2017 (section)) U Πcourse_id (σsemester = “Spring” ^ year
= 2018 (section))
Ex.
Set-difference: A table whose tuples are in one relation, but not in another.
Ex.
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-2-the-relational-model/ 4/6
10/26/21, 6:26 PM Introduction to Databases 2: The Relational Model – Brief Sketch
The assignment operation, denoted by a left arrow (˿), works like assignment in
programming language. Sometimes it is convenient to assign a relational algebra
expression to temporary relational variables.
returns the result of expression E under the name χ, and with the attributes
renamed to A1, A2, …, An.
Advertisements
You can hide ads completely by upgrading to one of our paid plans.
UPGRADE NOW
DISMISS MESSAGE
Share this:
Reblog
Like
Related
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-2-the-relational-model/ 5/6
10/26/21, 6:26 PM Introduction to Databases 2: The Relational Model – Brief Sketch
Leave a Reply
Enter your comment here...
Brief Sketch,
Blog at WordPress.com.
https://briefsketch97.wordpress.com/2021/10/04/introduction-to-databases-2-the-relational-model/ 6/6
10/26/21, 6:26 PM Introduction to Databases 6.1 – 6.5: Databse Design Using the E-R Model – Brief Sketch
Brief Sketch
Notes I take
3:35
Creating a database application involves the design of the database schema, design
of the programs that access and update the data, and design of a security scheme
to control access to data.
1- Characterize fully the data needs of the prospective users, specify user
requirements.
2- Choose a data model and translate the user requirements into a conceptual
schema of the database. The conceptual schema specifies the entities, attributes of
entities, relationship among entities, and constraints on the entities and
relationships in the database.
3- Specify the functional requirements. The users describe the kinds of operations
or transactions that will be performed on the data.
4- Implementation:
Logical-design phase: map the conceptual schema defined using the entity-
relationship morel into a relation schema.
Physical design phase: specify the physical features of the database (form of
file organization and choice of index structures).
The E-R model employs three basic concepts: entity sets, relationship sets, and
attributes. The E-R model also has an associated diagrammatic representation, the
E-R diagram.
entity: a thing or object in the real world that is distinguishable from all other
https://briefsketch97.wordpress.com/2021/10/06/introduction-to-databases-3-databse-design-using-the-e-r-model/ 1/5
10/26/21, 6:26 PM Introduction to Databases 6.1 – 6.5: Databse Design Using the E-R Model – Brief Sketch
objects. An entity has a set of properties, and the values for some set of properties
must uniquely identify an entity.
entity set: a set of entities of the same type that share the same
properties/attributes. It is represented in an E-R diagram by a rectangle.
Ex.
simple attributes: do not need to be divided into more than one part
https://briefsketch97.wordpress.com/2021/10/06/introduction-to-databases-3-databse-design-using-the-e-r-model/ 2/5
10/26/21, 6:26 PM Introduction to Databases 6.1 – 6.5: Databse Design Using the E-R Model – Brief Sketch
composite attributes: can be divided into subparts. Ex. first name, last name,
etc. It is denoted by an indentation.
multivalued: a value that is a list of values. Ex. list of phone numbers, list of
dependants, etc. It is denoted by curly braces: { multivalued_attribute}
derived attributes: this attribute can be derived from another attribute. Ex. 3:35age,
given birth date; monthly salary, given annual salary.
total: every entity in E must participate in at least one relationship in R. Ex. every
student needs at least one advisor. Indicated by using double lines.
weak entity set: its existence depends on the existence of an identifying entity
set. It must relate to the identifying entity set via a total, one-to-many relationship
set from the identifying to the weak entity set. It is an entity set that does not have
https://briefsketch97.wordpress.com/2021/10/06/introduction-to-databases-3-databse-design-using-the-e-r-model/ 3/5
10/26/21, 6:26 PM Introduction to Databases 6.1 – 6.5: Databse Design Using the E-R Model – Brief Sketch
a primary key on its own. The primary key of a weak entity set is formed by the
primary key of the strong, identifying entity set, plus the weak entity set’s
discriminator. The discriminator (or partial key) of a weak entity set is the set
of attributes that distinguishes among all the entities of a weak entity set. It is
depicted by a double diamond.
3:35
Advertisements
You can hide ads completely by upgrading to one of our paid plans.
UPGRADE NOW
DISMISS MESSAGE
Share this:
Reblog
Like
Be the first to like this.
Related
https://briefsketch97.wordpress.com/2021/10/06/introduction-to-databases-3-databse-design-using-the-e-r-model/ 4/5
10/26/21, 6:26 PM Introduction to Databases 6.1 – 6.5: Databse Design Using the E-R Model – Brief Sketch
Leave a Reply
Enter your comment here...
Brief Sketch,
Blog at WordPress.com.
https://briefsketch97.wordpress.com/2021/10/06/introduction-to-databases-3-databse-design-using-the-e-r-model/ 5/5
10/26/21, 6:26 PM Introduction to Databases 6.6 – 6.8: Databse Design Using the E-R Model – Brief Sketch
Brief Sketch
Notes I take
2:35
For schemas derived from strong entity sets, the primary key of the entity set
serves as the primary key of the resulting schema. This follows from the fact that
each tuple in a relation on this schema corresponds to one entity of the entity set
E.
When a strong entity set has nonsimple attributes, we handle composite attributes
by creating a separate attribute from each of the component attributes.
Let A be a weak entity set. Let B be the strong entity set on which A depends. Let
the primary key of B consist of attributes b1, b2, …, bn.
https://briefsketch97.wordpress.com/2021/10/11/introduction-to-databases-6-6-6-8-databse-design-using-the-e-r-model/ 1/4
10/26/21, 6:26 PM Introduction to Databases 6.6 – 6.8: Databse Design Using the E-R Model – Brief Sketch
We represent the entity set A by a relation schema called A with one attribute for
each member of the set:
6.8.1 Specialization
An entity set may include subgroupings of entities that are distinct in some way
from other entities in the set. Ex. the entity set person may be further classified as
an employee or student.
Each of these person types is described by a set of attributes that includes all the
attributes of entity set person plus possibly additional attributes.
6.8.2 Generalization
The attributes of the higher-level entity sets are said to be inherited by the lower-
https://briefsketch97.wordpress.com/2021/10/11/introduction-to-databases-6-6-6-8-databse-design-using-the-e-r-model/ 2/4
10/26/21, 6:26 PM Introduction to Databases 6.6 – 6.8: Databse Design Using the E-R Model – Brief Sketch
level entity sets. A subclass also inherits participation in the relationship sets in
which its superclass participates.
2:35
Condition-defined (Ex. All customers over 65 years are members of senior-citizen
entity set; senior-citizen ISA person) vs. user-defined.
Constraints on whether or not entities may belong to more than one lower-level
entity set within a single generalization.
Overlapping: an entity can belong to more than one lower-level entity set.
Partial: an entity need not belong to one of the lower-level entity sets.
6.8.5 Aggregation
Aggregation: is an abstraction through which relationships are treated as
higher-level entities.
Advertisements
You can hide ads completely by upgrading to one of our paid plans.
UPGRADE NOW
DISMISS MESSAGE
Share this:
Reblog
Like
Be the first to like this.
https://briefsketch97.wordpress.com/2021/10/11/introduction-to-databases-6-6-6-8-databse-design-using-the-e-r-model/ 3/4
10/26/21, 6:26 PM Introduction to Databases 6.6 – 6.8: Databse Design Using the E-R Model – Brief Sketch
Related
Leave a Reply
Enter your comment here...
Brief Sketch,
Create a free website or blog at WordPress.com.
https://briefsketch97.wordpress.com/2021/10/11/introduction-to-databases-6-6-6-8-databse-design-using-the-e-r-model/ 4/4
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
Brief Sketch
Notes I take
5:44
int: An integer
We define an SQL relation by using the create table command. The following
command creates a relation department in the database:
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 1/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
create table r
(A1 D1,
A2 D2,
…,
An Dn,
5:44
<integrity-constraint1>,
…,
<integrity-constraintk>);
where
primary key (Aj1, Aj2, …, Ajm): The primary key specification attributes are
required to be nonnull and unique;
foreign key (Aj1, Aj2, …, Ajm) references s: The values of the attributes for any
tuple in the relation must correspond to values of the primary key attributes on
some tuple in relation s.
not null: specifies that the null value is not allowed for that attribute.
Updates to tables:
alter table r add A D; : adds attributes to an existing relation. All tuples in the
relation are assigned null as the value for the new attribute
The basic structure of an SQL query consists of three clauses: select, from, and
where. A query takes as its input relations listed in the from clause, operates on
them as specified in the where and select clauses, and then produces a relation
as the result.
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 2/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
Ai: an attribute
Ri: a relation
P: a predicate
The select clause lists the attributes desired in the result of a query. Ex. find5:44
the
names of all instructors:
1 select name
2 from instructor
To force the elimination of duplicates, insert the keyword distinct after select
1 select *
2 from instructor
An attribute can be a literal with no from clause. The result is a table with one
column and a single row with value “X”. We can give the column a name using
An attribute can be a literal with from clause. The result is a table with one
column and N rows (number of tuples in instructors table), each row with value
“A”
1 select 'A'
2 from instructor
The select clause can contain arithmetic expressions involving the operations +, -,
*, and /, and operating on constants or attributes of tuples.
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 3/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
The where clause specifies conditions that the result must satisfy. Ex. to find all
instructors in the Computer Science department
1 select name
2 from instructor
3 where dept_name = "Computer Science"
Comparison results can be combined using the logical connectives and, or, and
not. Ex. To find all instructors in the Computer Science department with salary
greater than $80,0000
1 select name
2 from instructor
3 where dept_name = "Computer Science" and salary > 80000
The from clause lists the relations involved in the query. Ex. Find the Cartesian
product instructor X teaches
1 select *
2 from instructor, teaches
The Cartesian product is not very useful by itself, but it is useful when combined
with the where clause condition. Ex. Find the names of all instructors who have
taught some course, and the course_id
Ex. Find the names of all instructors in the Art department who have taught some
course and the course_id
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 4/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
Pattern matching
5:44
Pattern matching can be performed on strings using the operator like. We
describe patterns by using two special characters:
like “ab\%cd%” escape “\” matches all strings beginning with “ab%cd”
The order by clause causes the tuples in the result of a query to appear in sorted
order. To list in alphabetic order all instructors in the Physics department, we
write:
1 select name
2 from instructor
3 where dept_name = "Physics"
4 order by name;
By default, the order by clause lists items in ascending order. To specify the sort
order, we may specify desc for descending order and asc for ascending order.
1 select *
2 from instructor
3 order by salary desc, name asc
Where-Clause Predicates
SQL includes a between comparison operator. Ex. Find the names of all
instructors with salary between $90,000 and $100,000 (that is, >= $90,000 and
<= $100,000)
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 5/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
1 select name
2 from instructor
3 where salary between 90000 and 100000
union: the set of all courses taught in either the Fall of 2017 or in Spring 2018, or
both:
1 (select course_id
2 from section
3 where semester = "Fall" and year = 2017)
4 union
5 (select course_id
6 from section
7 where semester = "Spring" and year = 2018);
intersect: the set of all courses taught in both the Fall 2017 and Spring 2018:
1 (select course_id
2 from section
3 where semester = "Fall" and year = 2017)
4 intersect
5 (select course_id
6 from section
7 where semester = "Spring" and year = 2018);
except: the courses taught in the Fall 2017 semester but not in the Spring 2018
semester
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 6/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
1 (select course_id
2 from section
3 where semester = "Fall" and year = 2017)
4 except
5 (select course_id
6 from section
7 where semester = "Spring" and year = 2018); 5:44
Average: avg
Minimum: min
Maximum: max
Total: sum
Count: count
Ex: Find the total number of instructors who teach a course in the Spring 2018
semester.
Because we wrote the word distinct, the instructors that teach more than one
course in that semester will count as only 1.
1 select count(*)
2 from course
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 7/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
Find the number of instructors in each department who teach a course in the
Spring 2018 semester
Find the number of instructors in each department who teach a course in the
Spring 2018 semester. For this, we need to join the teaches and instructor
relations.
When we want conditions to apply to groups, not to tuples. The having clause is
applied when a condition does not apply to a single tuple, but rather applies to
each group constructed by the group by clause.
SQL applies predicates in the having clause after groups have been formed.
For example, we might be interested only in the departments that have an average
salary higher than $42,000.
As with the select clause, any attribute that is present in the having clause
without being aggregated must appear in the group by clause, otherwise the
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 8/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
query is erroneous.
To illustrate the use of both a having and a where clause in the same query, we
consider: “For each course section offered in 2017, find the average total credits
(tot_cred) of all students enrolled in the section, if the section has at least 2
students” 5:44
The SQL standard says that the aggregate operators should ignore null values in
its input.
Advertisements
You can hide ads completely by upgrading to one of our paid plans.
UPGRADE NOW
DISMISS MESSAGE
Share this:
Reblog
Like
Be the first to like this.
Related
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 9/10
10/26/21, 6:26 PM Introduction to Databases 3.1 – 3.7: Introduction to SQL – Brief Sketch
5:44
Published by Guilherme Soares
I’m a Computer Science and Public Relations senior student at Rollins College
with a passion for software engineering, healthy living practices, and social
entrepreneurship. View more posts
Leave a Reply
Enter your comment here...
Brief Sketch,
Create a free website or blog at WordPress.com.
https://briefsketch97.wordpress.com/2021/10/12/introduction-to-databases-3-introduction-to-sql/ 10/10
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
Brief Sketch
Notes I take
6:30
A common use of subqueries is to perform tests for set memberbership, make set
comparisons, and determine set cardinality by nesting subqueries in the where
clause.
3.8.7: scalar subqueries and how they can appear wherever an expression
returning a value can occur
in: The in connective tests for set membership, where the set is a collection of
values produced by a select clause.
Ex. Find all the courses taught in both the Fall 2017 and Spring 2018 semesters.
We can take the approach of finding all courses taught in Fall 2017 and that are
also members of the set of courses taught in Spring 2018. We do this by nesting a
subquery in the where clause of an outer query.
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 1/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
Ex. Find all the courses taught in the Fall 2017 semester but not in the Spring 2018
semester
Ex. Find the instructors who are neither Mozart nor Einstein
Ex. Find the total number of distinct students who have taken course sections
taught by the instructor with ID 110011
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 2/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
The phrease “greater than at least one” is represented in SQL by “> some“.
This construct allows us to rewrite the query in a form that resembles closely our
formulation of the query in English
That subquery generates the set of all salary values of all instructors in the Biology
department.
The > some comparison in the where clause is true if the salary value of the
tuple is greater than at least one member of the set of all salary values for
instructors in Biology.
SQL also allows < some, <= some, >= some, = some, and <> some
comparisons, where = some is identical to in.
Now let’s find the names of all instructors that have a salary value greater than
that of each instructor in the Biology department.
The construct > all corresponds to the phrase “greater than all”.
1 select name
2 from instructor
3 where salary > all (select salary
4 from instructor
5 where dept_name = "Biology")
As for some, SQL allows < all, <= all, >= all, = all, and <> all comparisons,
where <> all is identical to not in.
Another examples of set comparisons is: “Find the departments that have the
highest average salary”.
We begin by finding tall average salaries, and then nest it as a subquery of a larger
query that finds those departments for which the average salary is greater than or
equal to all average salaries:
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 3/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
1 select dept_name
2 from instructor
3 group by dept_name
4 having avg(salary) >= all (select avg(salary)
5 from instructor
6 group by dept_name)
6:30
3.8.3 Test for Empty Relations
SQL includes a construct for testing whether a subquery has any tuples in its
result.
Using the exists construct, we can write the query: “Find all courses taught in
both the Fall 2017 semester and in the Spring 2018 semester” in still another way:
1 select course_id
2 from section as S
3 where semester = "Fall" and year = 2017 and
4 exists (select *
5 from section as T
6 where semester = "Spring" and year = 2018 and
7 S.course_id = T.course_id)
A correlation name from an outer query (S in the above query), can be used in a
subquery in the where clause.
To illustrate the not exists operator, consider the query “Find all students who
have taken all courses offered in the Biology department”.
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 4/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
The first subquery finds the set of all courses offered in the Biology department.
The second subquery fids all the courses that student S.ID has taken.
The outer select takes each student and tests whether the set of all courses that
the student has taken contains the set of all courses offered in the Biology 6:30
department.
Ex. Find the total number of (distinct) students who have taken course sections
taught by the instructor with ID 110011
SQL includes a Boolean function for testing whether a subquery has duplicate
tuples in its result. The unique construct returns the value true if the argument
subquery contains no duplicate tuples.
Ex. Find all courses that were offered at most once in 2017
1 select T.course_id
2 from course as T
3 where unique (select R.course_id
4 from section as R
5 where T.course_id = R.course_id and
6 R.year = 2017)
If a course was not offered in 2017, the subquery would return an empty result,
and the unique predicate would evaluate to true on the empty set.
We can test for the existence of duplicate tuples in a subquery by using the not
unique construct.
Ex. Find all courses that were offered at least twice in 2017
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 5/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
1 select T.course_id
2 from course as T
3 where not unique (select R.course_id
4 from section as R
5 where T.course_id = R.course_id and
6 R.year = 2017)
6:30
3.8.5 Subqueries in the From Clause
Ex. Find the average instructors’ salaries of those departments where the average
salary is greater than $42,000
The subquery generates a relation consisting of the names of all departments and
their corresponding average instructors’ salaries. The attributes of the subquery
result can be used in the outer query.
We can also give the subquery result relation a name, and rename the attributes,
using the as clause.
Another example: Find the maximum across all departments of the total of all
instructors’ salaries in each department
1 select max(tot_salary)
2 from (select dept_name, sum(salary)
3 from instructor
4 group by dept_name)
5 as dept_total(dept_name, tot_salary)
The with clause provides a way of defining a temporary relation whose definition
is available only to the query in which the with clause occurs.
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 6/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
1 with max_budget(value) as
2 (select max(budget)
3 from department)
4 select budget
5 from department, max_budget
6 where department.budget = max_budget.value;
6:30
The with clause in the query defines the temporary relation max_budget
containing the results of the subquery defining the relation.
Another example: Find all departments where the total salary is greater than the
average of the total salary at all departments
Ex. List all departments along with the number of instructors in each department
1 select dept_name,
2 (select count(*)
3 from instructor
4 where department.dept_name = instructor.dept_name)
5 as num_instructors
6 from department
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 7/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
3.9.1 Deletion
P: a predicate
r: a relation
delete: finds all tuples t in r for which P(t) is true, and then deletes them from r.
Ex. Delete all tuples in the instructor relation pertaining to instructors in the
Finance department
Ex. Delete all instructors with a salary between $13,000 and $15,000
Ex. Delete all tuples in the instructor relation for those instructors associated with
a department located in the Watson building.
This delete request first finds all departments located in Watson and then deletes
all instructor tuples pertaining to those departments.
3.9.2 Insertion
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 8/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
Or, equivalently
More generally, we might want to insert tuples on the bases of the result of a
query.
Ex. Make each student in the Music department who has earned more than 144
credit hours an instructor in the Music department with a salary of $18,000.
3.9.3 Updates
1 update instructor
2 set salary = salary * 1.05
The preceding update is applied once to each of the tuples in the instructor
relation.
Another ex. Increase salary only to instructors with a salary of less than $70,000.
1 update instructor
2 set salary = salary * 1.05
3 where salary < 70000
As with insert and delete, a nested select within an update statement may
reference the relation that is being updated.
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 9/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
Another ex. Give a 5% raise to instructors whose salary is less than average
1 update instructor
2 set salary = salary * 1.05
3 where salary < (select avg(salary) 6:30
4 from instructor)
case: SQL provides a case construct that we can use to perform multiple updates,
avoiding false updates
1 update instructor
2 set salary = case
3 when salary <= 10000 then salary * 1.05
4 else salary * 1.03
5 end
1 case
2 when pred1 then result1
3 when pred2 then result2
4 ...
5 when predn then resultn
6 else result0
7 end
Ex. Set the tot_cred attribute of each student tuple to the sum of the credits of
courses successfully completed by the student.
1 update student
2 set tot_cred = (
3 select sum(credits)
4 from takes, course
5 where student.ID = takes.ID and
6 takes.course_id = course.course_id and
7 takes.grade <> 'F' and
8 takes.grade is not null)
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 10/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
Advertisements
You can hide ads completely by upgrading to one of our paid plans.
UPGRADE NOW
DISMISS MESSAGE
6:30
Share this:
Reblog
Like
Related
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 11/12
10/26/21, 6:26 PM Introduction to Databases 3.8 – 3.9: Introduction to SQL – Brief Sketch
Leave a Reply
Enter your comment here...
6:30
Brief Sketch,
Create a free website or blog at WordPress.com.
https://briefsketch97.wordpress.com/2021/10/15/introduction-to-databases-3-8-3-9-introduction-to-sql/ 12/12
10/26/21, 6:27 PM Introduction to Databases 4.1 – 4.4: Intermediate SQL – Brief Sketch
Brief Sketch
Notes I take
3:18
The natural join operation operates on two relations and produces a reliation as
the result.
The natural join considers only those pairs of tuples with the same value on those
attributes that appear in the schemas of both relations.
So,
is the same as, *assuming student and takes have no other common attribute
except for ID.
There is a danger, however, of using natural join, which is when more than one
column attribute names match.
join … using
When we use the using keyword, we specify which attributes we want to join in,
https://briefsketch97.wordpress.com/2021/10/25/introduction-to-databases-4-1-4-4-intermediate-sql/ 1/6
10/26/21, 6:27 PM Introduction to Databases 4.1 – 4.4: Intermediate SQL – Brief Sketch
1 select *
2 from student join takes on student.ID = takes.ID
The outer join operation preserves the tuples that would be lost in a join by
creating tuples in the result containing null values.
left outer join preserves tuples only in the relation named before the left outer
join operation.
right outer join preserves tuples only in the relation named after the right
outer join operation.
4.2 Views
To define a view, we must give the view a name and state the query that computes
https://briefsketch97.wordpress.com/2021/10/25/introduction-to-databases-4-1-4-4-intermediate-sql/ 2/6
10/26/21, 6:27 PM Introduction to Databases 4.1 – 4.4: Intermediate SQL – Brief Sketch
the view.
Consider how a clerk needs to access all data in the instructor relation, except
3:18
salary. A view of relation faculty can be made available to the clerk, with the view
defined as follows:
The view relation conceptually contains the tuples in the query result, but it is not
precomputed and stored. Instead, the database stores the query expression
associated with the view relation.
View names may appear in a query any place where a relation name may appear.
If a tuple is added or deleted from a relation, the result of the query defining the
view would change, and the materialized view’s contents must be updated.
In general, an SQL view is said to be updatable if the following conditions are all
satisfied by the query defining the view:
– The select clause contains only attribute names of the relation and does not
have any axpressions, aggregates, or distinct specificiation.
– Any attribute not listed in the select clause can be set to null.
https://briefsketch97.wordpress.com/2021/10/25/introduction-to-databases-4-1-4-4-intermediate-sql/ 3/6
10/26/21, 6:27 PM Introduction to Databases 4.1 – 4.4: Intermediate SQL – Brief Sketch
4.3 Transactions
Atomic transaction
3:18
Either fully executed or rolled back as if it never occured.
In addition to the primary-key constraint, there are a number of other ones that
can be included in the create table command.
– not null
– unique
The unique specification says that attributes Aj1, Aj2, …, Ajm form a superkey; that
is, no two tuples in the relation can be equal on all the listed attributes.
The clause check( P ) specifies a predicate P that must be satisfied by every tuple
in a relation.
https://briefsketch97.wordpress.com/2021/10/25/introduction-to-databases-4-1-4-4-intermediate-sql/ 4/6
10/26/21, 6:27 PM Introduction to Databases 4.1 – 4.4: Intermediate SQL – Brief Sketch
Foreign keys can be specified as part of the SQL create table statement by using
the foreign key clause.
Ensures that a value that appears in one relation for a given set of attributes also
appears for a certain set of attributes in another relation.
3:18
Let R and S be two relations that contain attributes A and where A is the primary
key of S.
Advertisements
You can hide ads completely by upgrading to one of our paid plans.
UPGRADE NOW
DISMISS MESSAGE
Share this:
Reblog
Like
Be the first to like this.
Related
https://briefsketch97.wordpress.com/2021/10/25/introduction-to-databases-4-1-4-4-intermediate-sql/ 5/6
10/26/21, 6:27 PM Introduction to Databases 4.1 – 4.4: Intermediate SQL – Brief Sketch
I’m a Computer Science and Public Relations senior student at Rollins College
with a passion for software engineering, healthy living practices, and social
entrepreneurship. View more posts
Leave a Reply
Enter your comment here...
Brief Sketch,
Blog at WordPress.com.
https://briefsketch97.wordpress.com/2021/10/25/introduction-to-databases-4-1-4-4-intermediate-sql/ 6/6