Database Systems Concepts: Taiz University, 2020
Database Systems Concepts: Taiz University, 2020
Database Systems Concepts: Taiz University, 2020
Lecture 3
Dr. Abdulmalek Alqobaty
Database Schema
department(dept_name, building, budget)
course(course_id, title, dept_name, credits)
prereq(course_id, prereq_id)
instructor(ID, name, dept_name, salary)
student(ID, name, dept_name, tot cred)
classroom(building, room_number, capacity)
section(course_id, sec_id, semester, year, building, room number, time_slot_id)
teaches(ID, course_id, sec_id, semester, year)
takes(ID, course_id, sec_id, semester, year, grade)
advisor(s_ID, i_ID)
time_slot(time_slot_id, day, start_time, end_time)
Intermediate SQL
Join Expressions
We introduced the natural join operation. SQL provides other forms of the join
operation, including the ability to specify an explicit join predicate, and the
ability to include in the result tuples that are excluded by natural join. We shall
discuss these forms of join in this section.
Join Conditions
we saw the join . . . using clause, which is a form of natural join that only requires
values to match on specified attributes. The on condition allows a general
predicate over the relations being joined.
select *
from student join takes on student.ID = takes.ID;
The on condition above specifies that a tuple from student matches a tuple from
takes if their ID values are equal. The join expression in this case is almost the
same as the join expression student natural join takes.
select student.ID as ID, name, dept_name, tot cred, course_id, sec_id,
semester, year, grade from student join takes on student.ID = takes.ID;
The on condition can express any SQL predicate, and thus a join expressions using
the on condition can express a richer class of join conditions than natural join.
Intermediate SQL
There are in fact three forms of outer join:
The left outer join preserves tuples only in the relation named before (to
the left of) the left outer join operation.
select * from student natural left outer join takes;
select ID
from student natural left outer join takes
where course_id is null;
Intermediate SQL
The right outer join preserves tuples only in the relation named after (to the
right of) the right outer join operation.
select * select *
from student join takes using(ID); from student inner join takes using (ID);
The join operations that do not preserve nonmatched tuples are called inner join
operations, to distinguish them from the outer-join operations.
Intermediate SQL
Views
In our examples up to this point, we have operated at the logical-model level. That is, we have
assumed that the relations in the collection we are given are the actual relations stored in the
database.
View Definition
To define a view, we must give the view a name and must state the query that
computes the view.
create view v as <query expression>;
Where the view name is represented by v, and <query expression> is a legal query
expression
create view physics_fall_2009 as
Examples
select course.course_id, sec_id, building, room_number
from course, section
where course.course_id = section.course_id
create view faculty as and course.dept_name = ’Physics’
select ID, name, dept_name and section.semester = ’Fall’
from instructor; and section.year = ’2009’;
Update of a View
Since we allow a view name to appear wherever a relation name is allowed, we,
then, can write:
insert into faculty
values (’30765’, ’Green’, ’Music’);
This insertion must be represented by an insertion into the relation instructor,
since instructor is the actual relation from which the database system
constructs the view faculty.
Intermediate SQL
Under these constraints, the update, insert, and delete operations would be
allowed on the following view:
Integrity Constraints
The integrity constraints guard against accidental damage to the database.
Constraints on a Single Relation
The create table command may also include integrity-constraint statements.
In addition to the primary-key constraint, there are a number of other ones
that can be included in the create table command. The allowed integrity
constraints include:
not null
name varchar(20) not null
budget numeric(12,2) not null
Unique
unique (Aj1 , Aj2, . . . , Ajm )
The unique specification says that attributes Aj1 , Aj2, . . . , Ajm form a candidate
key; candidate key attributes are permitted to be null unless they have explicitly
been declared to be not null.
Integrity Constraints
check (<predicate>)
Index Creation
An index on an attribute of a relation is a data structure that allows the database
system to find those tuples in the relation that have a specified value for that
attribute efficiently, without scanning through all the tuples of the relation. For
example, if we create in index on attribute ID of relation student, the database
system can find the record with any specified ID value, such as 22201, or 44553,
directly, without reading all the tuples of the student relation.
Although the SQL language does not formally define any syntax for creating
indices, many databases support index creation using the syntax illustrated below.
Large-Object Types
SQL therefore provides large-object data types for character data (clob) and
binary data (blob). The letters “lob” in these data types stand for “Large
OBject.” For example, we may declare attributes.
book_review
clob(10KB)
image blob(10MB)
movie blob(2GB)
Intermediate SQL
User-Defined Types
SQL supports two forms of user-defined data types. The first form, which we cover
here, is called distinct types. The other form, called structured data types, allows the
creation of complex data types with nested record structures, arrays, and multisets.
Good type system should be able to detect such assignments or comparisons. To
support such checks, SQL provides the notion of distinct types. The create type clause
can be used to define new types. For example, the statements:
create type Dollars as numeric(12,2) final;
create type Pounds as numeric(12,2) final;
(The keyword final isn’t really meaningful in this context but is required by the
SQL:1999 standard for reasons we won’t get into here; some implementations allow
the final keyword to be omitted.)
create table department
(dept_name varchar (20),
building varchar (15),
budget Dollars);
As a result of strong type checking, the expression (department.budget+20) would not
be accepted since the attribute and the integer constant 20 have different types. Values
of one type can be cast (that is, converted) to another domain, as illustrated below:
cast (department.budget to numeric(12,2))
Intermediate SQL
SQL provides drop type and alter type clauses to drop or modify types that
have been created earlier.
SQL had a similar but subtly different notion of domain (introduced in SQL-92),
which can add integrity constraints to an underlying type. For example, we could
define a domain DDollars as follows.
create domain DDollars as numeric(12,2) not null;
The domain DDollars can be used as an attribute type, just as we used the type
Dollars. However, there are two significant differences:
Domains can have constraints, such as not null, specified on them, and can
have default values defined for variables of the domain type.
Domains are not strongly typed. A
Note that, when applied to a domain, the check clause permits the schema
designer to specify a predicate that must be satisfied by any attribute declared to
be from this domain.
create domain YearlySalary numeric(8,2)
constraint salary_value_test check(value >= 29000.00);
create domain degree_level varchar(10)
constraint degree_level_test check (value in (’Bachelors’, ’Masters’, or ’Doctorate’));
Intermediate SQL
SQL provides drop type and alter type clauses to drop or modify types that
have been created earlier.
SQL had a similar but subtly different notion of domain (introduced in SQL-92),
which can add integrity constraints to an underlying type. For example, we could
define a domain DDollars as follows.
create domain DDollars as numeric(12,2) not null;
The domain DDollars can be used as an attribute type, just as we used the type
Dollars. However, there are two significant differences:
Domains can have constraints, such as not null, specified on them, and can
have default values defined for variables of the domain type.
Domains are not strongly typed. A
Note that, when applied to a domain, the check clause permits the schema
designer to specify a predicate that must be satisfied by any attribute declared to
be from this domain.
create domain YearlySalary numeric(8,2)
constraint salary_value_test check(value >= 29000.00);
create domain degree_level varchar(10)
constraint degree_level_test check (value in (’Bachelors’, ’Masters’, or ’Doctorate’));
Intermediate SQL
create table t1 as
(select *
from instructor
where dept_name= ’Music’)
with data;
By default, the names and data types of the columns are inferred from the query
result. Due to SQL:2003 standard, if the with data clause is omitted, the table is
created but not populated with data.
Intermediate SQL
Authorization
Authorizations on data include:
Authorization to read data.
Authorization to insert new data.
Authorization to update data.
Authorization to delete data.
Each of these types of authorizations is called a privilege.
Granting of Privileges
The SQL standard includes the privileges select, insert, update, and delete. The
privilege all privileges can be used as a short form for all the allowable privileges.
The basic form of this statement is:
grant <privilege list>
on <relation name or view name>
to <user/role list>;
The privilege list allows the granting of several privileges in one command.
grant select on department to Amit, Satoshi;
This grant statement gives users Amit and Satoshi update authorization on the budget
attribute of the department relation:
grant update (budget) on department to Amit, Satoshi;
Intermediate SQL
The insert authorization on a relation allows a user to insert tuples into the
relation. The insert privilege may also specify a list of attributes; then inserts to
the relation must specify only these attributes. The delete authorization on a
relation allows a user to delete tuples from a relation.
The user name public refers to all current and future users of the system. Thus,
privileges granted to public are implicitly granted to all current and future
users.
Revoking of Privileges
To revoke an authorization, we use the revoke statement. It takes a form almost
identical to that of grant:
revoke <privilege list>
on <relation name or view name>
from <user/role list>;
Authorizations on Schema
The SQL standard specifies a primitive authorization mechanism for the
database schema: Only the owner of the schema can carry out any modification
to the schema, such as creating or deleting relations, adding or dropping
attributes of relations, and adding or dropping indices. However, SQL includes a
references privilege that permits a user to declare foreign keys when creating
relations. The following grant statement allows user Mariano to create relations
that reference the key branch name of the branch relation as a foreign key:
grant references (dept_name) on department to Mariano;
Transfer of Privileges
A user who has been granted some form of authorization may be allowed to pass
on this authorization to other users. If we wish to grant a privilege and to allow
the recipient to pass the privilege on to other users, we append the with grant
option clause to the appropriate grant command.
grant select on department to Amit with grant option;
Intermediate SQL
Revoking of Privileges
The following revoke statement revokes only the grant option, rather than the
actual select privilege: