02-Introduction To Relational Model and SQL-SCD
02-Introduction To Relational Model and SQL-SCD
02 Introduction to Relational
Model
CRI2E4 Basis Data
2
Learning Outcomes
Students can explain and understand the Relational Model
Outline
3
Database Schema
Keys
Schema Diagram
4
Structure of Relational
Databases
Basic Concepts
5
• The set of allowed values for each attribute is called the domain of the
attribute
• Attribute values are (normally) required to be atomic; that is, indivisible
• The special value null is a member of every domain. Indicated that the value
is “unknown”
• The null value causes complications in the definition of many operations
Example of a Instructor Relation
8
attributes
(or columns)
tuples
(or rows)
9
Database Schema
Database Schema
10
Keys
Keys
12
• Let K R
• K is a superkey of R if values for K are sufficient to identify a unique tuple of
each possible relation r(R)
• Superkey K is a candidate key if K is minimal
• One of the candidate keys is selected to be the primary key. Primary key
attributes are also underlined.
• The primary key should be chosen such that its attribute values are never, or
are very rarely, changed.
• Foreign key constraint: Value in one relation must appear in another
• Referencing relation
• Referenced relation
• Note that in a foreign-key constraint, the referenced attribute(s) must be the
primary key of the referenced relation.
13
Schema Diagram
Schema Diagram
14
• A database schema, along with primary key and foreign-key constraints, can
be depicted by schema diagrams.
15
References
02 Introduction to SQL
CRI2E4 Basis Data
2
Learning Outcomes
Students can explain and understand DDL, DML, Basic query
structure
Outline
3
• IBM Sequel language developed as part of System R project at the IBM San
Jose Research Laboratory
• Renamed Structured Query Language (SQL)
• ANSI and ISO standard SQL:
• SQL-86
• SQL-89
• SQL-92
• SQL:1999 (language name became Y2K compliant!)
• SQL:2003
• Many products now support the SQL language.
• SQL has clearly established itself as the standard relational database
language.
SQL Parts
6
• Insert
• insert into instructor values ('10211', 'Smith', 'Biology', 66000);
• Delete : Remove all tuples from the relation
• delete from r
• Drop Table : Remove a relation from an SQL database
• drop table r
• Alter
• alter table r add A D
• where A is the name of the attribute to be added to relation r and D is the domain of
A.
• All exiting tuples in the relation are assigned null as the value for the new attribute.
• alter table r drop A
• where A is the name of an attribute of relation r
• Dropping of attributes not supported by many databases.
13
• Ai represents an attribute
• Ri represents a relation
• P is a predicate.
• The result of an SQL query is a relation.
The Select Cause
15
• The select clause lists the attributes desired in the result of a query
• corresponds to the projection operation of the relational algebra
• Example: find the names of all instructors:
select name
from instructor
• NOTE: SQL names are case insensitive (i.e., you may use upper- or lower-case letters.)
E.g., Name ≡ NAME ≡ name
The Select Cause (cont.)
16
• The select clause can contain arithmetic expressions involving the operation, +, –, , and
/, and operating on constants or attributes of tuples.
• The query:
select ID, name, salary/12
from instructor
would return a relation that is the same as the instructor relation, except that the value
of the attribute salary is divided by 12.
• Can rename “salary/12” using the as clause:
select ID, name, salary/12 as monthly_salary
The Where Clause
19
• The where clause specifies conditions that the result must satisfy
• Corresponds to the selection predicate of the relational algebra.
• To find all instructors in Comp. Sci. dept
select name
from instructor
where dept_name = 'Comp. Sci.'
• SQL allows the use of the logical connectives and, or, and not
• The operands of the logical connectives can be expressions involving the comparison
operators <, <=, >, >=, =, and <>.
• Comparisons can be applied to results of arithmetic expressions
• To find all instructors in Comp. Sci. dept with salary > 70000
select name
from instructor
where dept_name = 'Comp. Sci.' and salary > 70000
The From Clause
20
• Rename operations
• String operations
• Ordering the Display of Tuples
• Where-Clause Predicates
Rename Operations
24
• The SQL allows renaming relations and attributes using the as clause:
old-name as new-name
• Find the names of all instructors who have a higher salary than some instructor
in 'Comp. Sci'.
• select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = 'Comp. Sci.’
• We may specify desc for descending order or asc for ascending order, for each
attribute; ascending order is the default.
• Example: order by name desc
• Tuple comparison
• select name, course_id
from instructor, teaches
where (instructor.ID, dept_name) = (teaches.ID, 'Biology');
29
Modification of the
Database
Deletion
30
• Delete all tuples in the instructor relation for those instructors associated with a
department located in the Watson building.
delete from instructor
where dept name in (select dept name
from department
where building = 'Watson');
Insertion
31
• or equivalently
insert into course (course_id, title, dept_name, credits)
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
References