KEMBAR78
Lecture2 Relational Model | PDF | Relational Model | Sql
0% found this document useful (0 votes)
3 views44 pages

Lecture2 Relational Model

The document provides an overview of the relational model and SQL, detailing data modeling concepts, relational keys, and integrity rules. It outlines the objectives and importance of SQL, including its history and structure for writing SQL commands. Additionally, it includes examples of SQL statements for data retrieval and manipulation.

Uploaded by

enkherden2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views44 pages

Lecture2 Relational Model

The document provides an overview of the relational model and SQL, detailing data modeling concepts, relational keys, and integrity rules. It outlines the objectives and importance of SQL, including its history and structure for writing SQL commands. Additionally, it includes examples of SQL statements for data retrieval and manipulation.

Uploaded by

enkherden2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Relational model

Б.Наранчимэг
Мэдээлэл, компьютерийн ухааны тэнхим
ХШУИС, МУИС
naranchimeg@seas.num.edu.mn
Data modeling
• A data model is a collection of concepts for describing data.
• Data model
• Data structures
• Integrity constraints
• Operations
• Data modeling
• ER diagram of EER diagram
• Relational Model
Objectives
• Define the term of data model
• Terminology of relation data model
• How tables are used to represent data
• Properties of database relations
• How to identify candidate, primary and foreign keys
• Meaning of entity integrity and referential integrity
Data model
• Integrated collection of concepts for describing data,
relationships between data, constraints on the data.
• Has three components
• A structural part
• A manipulative part
• A set of integrity rules
Relational model terminology
• Relation: table with columns and rows
• Attribute: named column of a relation
• Domain: set of allowable values for one or more attributes
• Tuple: record of a relation
• Relational Database – collection of normalized relations with distinct
relation names
Instances of the Branch and Staff relations
Instances of the Branch and Staff relations
Domains for some attributes of Branch and
Staff
Alternative terminology
• Relation, attribute, Tuple ->
• Table, column, record ->
• File, field, row
Properties of Relations
• Table name is distinct from all other table names in the database
• Each cell of table contains exactly one atomic (single) value
• Each column has a distinct name
• Values of a column are all from the same domain
Properties of Relations
• Each record is distinct; there are no duplicate records
• Order of columns has no significance
• Order of records has no significance
Relational keys
• Superkey
• A column, or a set of columns, that uniquely identifies a record within a table
• Candidate key
• Superkey (K) such that no proper subset is a superkey within a table
• In each record, values of K uniquely identify that record (uniqueness)
• No proper subset of K has the uniqueness property (irreducibility)
Relational keys
• Primary key
• Candidate key selected to identify records uniquely within table
• Alternative key
• Candidate key that are not selected to be primary key
• Foreign key
• Column, or set of columns, within one table that matches candidate keys of
some (possibly same) table.
Relational integrity
• Null
• Represents value for a column that is currently unknown or not applicable for
record
• Deals with incomplete or exceptional data
• Represents the absence of a value and is not the same as zero or spaces,
which are values
Relational integrity
• Entity integrity
• In a base table, no column of a primary key can be null
• Referential integrity
• If FK exists in a table, either FK value must match a candidate key value of
some record in its home table of FK values must be wholly null
Relational integrity
• Business rules
• Rules that define or constrain some aspect of the organization
Structured Query Language
Select query

Б.Наранчимэг
Мэдээлэл, компьютерийн ухааны тэнхим
ХШУИС, МУИС
naranchimeg@seas.num.edu.mn
Objectives
• Purpose and importance of SQL.
• How to retrieve data from database using SELECT and:
• Use compound WHERE conditions.
• Sort query results using ORDER BY.
• Use aggregate functions.
• Group data using GROUP BY and HAVING.
• Use subqueries.
Objectives of SQL
• Ideally, database language should allow user to:
• create the database and relation structures;
• perform insertion, modification, deletion of data from relations;
• perform simple and complex queries.
• Must perform these tasks with minimal user effort and
command structure/syntax must be easy to learn.
• It must be portable.
Objectives of SQL

• SQL is a transform-oriented language with 2 major


components:
• A DDL for defining database structure.
• A DML for retrieving and updating data.

• Until SQL3, SQL did not contain flow of control commands.


These had to be implemented using a programming or job-
control language, or interactively by the decisions of user.
Objectives of SQL
• SQL is relatively easy to learn:
• it is non-procedural - you specify what information you require, rather
than how to get it;
• it is essentially free-format.
Objectives of SQL
• Consists of standard English words:
1) CREATE TABLE Staff(staffNo VARCHAR(5),
lName VARCHAR(15),
salary DECIMAL(7,2));
2) INSERT INTO Staff VALUES (‘SG16’, ‘Brown’, 8300);
3) SELECT staffNo, lName, salary
FROM Staff
WHERE salary > 10000;
Objectives of SQL
• Can be used by range of users including DBAs, management,
application developers, and other types of end users.

• An ISO standard now exists for SQL, making it both the formal and de
facto standard language for relational databases.
History of SQL
• In 1974, D. Chamberlin (IBM San Jose Laboratory) defined
language called ‘Structured English Query Language’
(SEQUEL).
• A revised version, SEQUEL/2, was defined in 1976 but name
was subsequently changed to SQL for legal reasons.

24
History of SQL
• In late 70s, ORACLE appeared and was probably first commercial
RDBMS based on SQL.
• In 1987, ANSI and ISO published an initial standard for SQL.
• In 1989, ISO published an addendum that defined an ‘Integrity
Enhancement Feature’.
• In 1992, first major revision to ISO standard occurred, referred to as
SQL2 or SQL/92.
• In 1999, SQL3 was released with support for object-oriented data
management.
Importance of SQL
• SQL is used in other standards and even influences development of
other standards as a definitional tool. Examples include:
• ISO’s Information Resource Directory System (IRDS) Standard
• Remote Data Access (RDA) Standard.

26
Writing SQL Commands
• SQL statement consists of reserved words and user-defined words.
– Reserved words are a fixed part of SQL and must be spelt exactly as
required and cannot be split across lines.
– User-defined words are made up by user and represent names of various
database objects such as relations, columns, views.

27
Writing SQL Commands
• Most components of an SQL statement are case insensitive, except for
literal character data.
• More readable with indentation and lineation:
• Each clause should begin on a new line.
• Start of a clause should line up with start of other clauses.
• If clause has several parts, should each appear on a separate line and be
indented under start of clause.
Writing SQL Commands
• Use extended form of BNF notation:
- Upper-case letters represent reserved words.
- Lower-case letters represent user-defined words.
- | indicates a choice among alternatives.
- {} Curly braces indicate a required element.
- [] Square brackets indicate an optional element.
- … indicates optional repetition (0 or more).

29
Literals
• Literals are constants used in SQL statements.

• All non-numeric literals must be enclosed in single quotes (e.g. ‘London’).

• All numeric literals must not be enclosed in quotes (e.g. 650.00).


SELECT Statement
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM TableName [alias] [, ...]
[WHEREcondition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
SELECT Statement
FROM Specifies table(s) to be used.
WHERE Filters rows.
GROUP BY Forms groups of rows with same column value.
HAVING Filters groups subject to some condition.
SELECT Specifies which columns are to appear in output.
ORDER BY Specifies the order of the output.

32
SELECT Statement
• Order of the clauses cannot be changed.

• Only SELECT and FROM are mandatory.


Example 5.1 All Columns, All Rows

List full details of all staff.

SELECT staffNo, fName, lName, address,


position, sex, DOB, salary, branchNo
FROM Staff;

• Can use * as an abbreviation for ‘all columns’:


SELECT *
FROM Staff;
Example 5.1 All Columns, All Rows
Example 5.2 Specific Columns, All Rows

Produce a list of salaries for all staff, showing only staff number,
first and last names, and salary.

SELECT staffNo, fName, lName, salary


FROM Staff;

36
Example 5.2 Specific Columns, All Rows
Example 5.3 Use of DISTINCT

List the property numbers of all properties that have been


viewed.

SELECT propertyNo
FROM Viewing;
Example 5.3 Use of DISTINCT

• Use DISTINCT to eliminate duplicates:

SELECT DISTINCT propertyNo


FROM Viewing;
Example 5.4 Calculated Fields

Produce a list of monthly salaries for all staff, showing staff number, first
and last names, and salary details.
SELECT staffNo, fName, lName, salary/12
FROM Staff;
Example 5.4 Calculated Fields

• To name column, use AS clause:

SELECT staffNo, fName, lName, salary/12


AS monthlySalary
FROM Staff;
Example 5.5 Comparison Search Condition

List all staff with a salary greater than 10,000.

SELECT staffNo, fName, lName, position, salary


FROM Staff
WHERE salary > 10000;
Summary
• Relational model
• SQL
Thank you!
Any questions?

You might also like