0% found this document useful (0 votes)
13 views59 pages

Lecture 02

The document covers the relational model and entity-relationship model in database management, explaining key concepts such as relations, keys, primary keys, foreign keys, and integrity constraints. It details the structure of databases, the use of data definition language (DDL), and provides examples of SQL queries for data manipulation. Additionally, it discusses the process of designing a database, including requirement analysis and conceptual database design using ER models.

Uploaded by

bapex28875
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)
13 views59 pages

Lecture 02

The document covers the relational model and entity-relationship model in database management, explaining key concepts such as relations, keys, primary keys, foreign keys, and integrity constraints. It details the structure of databases, the use of data definition language (DDL), and provides examples of SQL queries for data manipulation. Additionally, it discusses the process of designing a database, including requirement analysis and conceptual database design using ER models.

Uploaded by

bapex28875
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/ 59

CSE 412 Database Management

Lecture 02
Relational Model and Entity-Relationship Model
Bharatesh Chakravarthi

1
Review Relational Model

2
What is a Relation?
• Relation: made up of 2 parts:
• Schema: specifies name of relation, plus name and type of each column.
• Instance: a table with rows and columns.
• #rows = cardinality
• #columns = arity
arity

cardinality

3
Take-away Messages
• A database is a collection of relations (or tables)
• Each relation has a set of attributes (or columns)
• Each attribute has a name and a domain (or type)
• Each relation contains a set of tuples (or rows)

4
What is a key?
• A set of attributes K is a key for a relation R if
• In no instance of R will two different tuples agree on all attributes of K
• That is, K can serve as a “tuple identifier”
• If K can serve as a “tuple identifier”, it is called as a Superkey
• No proper subset of K satisfies the above condition
• That is, K is minimal
• Minimal superkey is called as a key
• Example:
Student (sid, name, login, age, gpa)
{sid, name}: superkey
{sid}: superkey, AND key
{name}: not superkey

5
More examples

• Is name a key of User?


• Yes? Seems reasonable for this instance
• No! User names are not unique in general

6
More examples

• Is name a key of User?


• Yes? Seems reasonable for this instance
• No! User names are not unique in general
• {uid} is the key
7
More examples

• Group (gid, name)


• Which set of attributes is the key?

8
More examples

• Group (gid, name)


• {gid} is the key

9
More examples

• Member (uid, gid)


• Which set of attributes is the key?

10
More examples

• Member (uid, gid)


• {uid, gid} is the key
• A key can contain multiple attributes
11
Primary Keys
• A relation can have multiple keys!
• Student: {ssn}, {student-id}, {driving-license#, state}
• Employee: {ssn}, {employee-id}, {phone#}
• computer: {mac-address}, {serial#}
• DBA (Database Admin) typically picks one as the “primary” key, and
underline all its attributes
• Other keys are called candidate keys

12
Primary Keys
• Example:
• Address (street_address, city, state, zip)
• {street_address, city, state}
• {street_address, zip}
• DBA typically picks one as the “primary” key, and underline all its attributes,
e.g., Address (street_address, city, state, zip)
• Other keys are called candidate keys.
e.g., {street_address, city, state}

13
What is a Database?
A database is a collection of relations (or tables)
Example 1

14
What is a Database?
A database is a collection of relations (or tables)
Example 2

15
What is a Database?
A database is a
collection of relations
(or tables)

Example 3

16
Foreign Keys, Referential Integrity
• Foreign Key: Set of attributes ‘referring’ to a tuple in another
relation.
• Must correspond to the primary key of the other relation
• Like a ‘logical pointer’
• Foreign key constraints enforce referential integrity (i.e., no dangling
references)

17
Data Definition Language
(DDL)

18
Data Definition Language (DDL)

• Column-Definition: Comma separated list of column names


with types.
• Constraints: Primary key, foreign key, and other meta-data
attributes of columns.
• Table-Options: DBMS-specific options for the table (not SQL-
92).
19
Data Definition Language (DDL)

20
Common Data Types
• CHAR(n) (fixed length), VARCHAR(n) (variable length)
• TINYINT (1 byte), SMALLINT (2 bytes), INT (4 bytes), , BIGINT (8 bytes),
• NUMERIC(p,d) (precision (no. of digits) and scale (digits to right of the
decimal point), FLOAT, DOUBLE, REAL
• DATE, TIME
• BINARY(n), VARBINARY(n), BLOB (image, video, files)

21
Useful Non-standard Types
• TEXT
• BOOLEAN
• ARRAY
• Geometric primitives
• XML/JSON
• Some systems also support user-defined types.

22
Integrity Constraints

23
Primary Keys
• Single-column primary key:

• Multi-column primary key:

24
Key declarations are part of the schema
CREATE TABLE enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid, cid))

PRIMARY KEY == UNIQUE, NOT NULL

25
Foreign Key References
• Single-column reference:

• Multi-column reference:

26
Foreign Keys in SQL
CREATE TABLE Enrolled (
sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid)
REFERENCES Students )

27
Data Manipulation Language
Basic SQL

28
Basic Queries: SFW statements
• SELECT A1, A2, …, An
FROM R1, R2, …, Rm
WHERE condition;
• Also called as SPJ (selection-projection-join) query - (selecting
rows, projecting columns, and joining tables) Select (σ), Project (π), Join (⋈)

• Can correspond to (but not exactly equivalent to) relational


algebra query:

29
Example Database

30
Example: reading a table
• SELECT * FROM User;
• Single-table query, so no cross product here
• WHERE clause is optional
• * is a short hand for “all columns”

31
Example: selection and projection
• Name of users under 18
• SELECT name
FROM User
WHERE age < 18
• When was Lisa born?
• SELECT 2025 - age
FROM User
WHERE name = 'Lisa’;
• SELECT list can contain expressions
• Can also use built-in functions such as SUBSTR, ABS, etc.
• String literals (case sensitive) are enclosed in single quotes
32
Example: join
• ID’s and names of groups with a user whose name contains
“Simpson”

33
Example: join
• ID’s and names of groups with a user whose name contains
“Simpson”
• SELECT Group.gid, Group.name
FROM User, Member, Group
WHERE User.uid = Member.uid
AND Member.gid = Group.gid
AND User.name LIKE '%Simpson%’;
• LIKE matches a string against a pattern
• % matches any sequence of zero or more characters
• Okay to omit table_name in table_name.column_name if column_name is
unique
34
Assignment 0

35
https://asu.instructure.com/courses/213606/assignments/6030571

36
37
Challenge:
How to Design a Database?

38
Scenario
• http://www.myimdb.com wants to store information about movies
• The steps:
• Requirement Analysis
• Film (title, type, year, actors, director)
• Movie person (ID, name, address, birthday)
• Actor(ID, picture, salary for each film)
• Director (ID)
• Award (name, year, film, organization)
• Organization (name, phone number)
• Conceptual Database Design: High level description of data to be stored (ER model)
• Logical Database Design: Translation of ER diagram to a relational database schema
(description of tables)
39
Scenario
• http://www.myimdb.com wants to store information about movies
• The steps:
• Requirement Analysis
• Conceptual Database Design: High level description of data to be stored using
Entity-Relationship model (ER model)
• Logical Database Design: Translation of ER diagram to a relational database
schema (description of tables)

40
The Output ER Model of
Conceptual Database Design
• Will explain how to
understand and
design the ER model

41
Entities, Entity Sets
• Entity: An object in
the world that can
be distinguished
from other objects
• Entity set: A set of
similar entities
• Examples of entity
sets:

 Entity sets are


drawn as rectangles

42
Entities, Entity Sets
• Entity: An object in
the world that can
be distinguished
from other objects
• Entity set: A set of
similar entities
• Examples of entity
sets:

 Entity sets are


drawn as rectangles

43
Attributes
• Attributes: Used to
describe entities
• All entities in the set
have the same
attributes
• A minimal set of
attributes that
uniquely identify an
entity is called a key
 Attributes are drawn using
ovals
 The names of the
attributes which make up
a key are underlined
44
Attributes
• Attributes: Used to
describe entities
• All entities in the set
have the same
attributes
• A minimal set of
attributes that
uniquely identify an
entity is called a key
 Attributes are drawn using
ovals
 The names of the attributes
which make up a primary key
are underlined
45
Example

id birthday
Actor

name address

46
Another Option for a Key?

birthday
id
Actor

name address

47
Relationships, Relationship Sets
• Relationship:
Association among two
or more entities
• Relationships may have
attributes
• Relationship Set: Set of
similar relationships

 Relationship sets are


drawn using diamonds

48
Relationships, Relationship Sets
• Relationship:
Association among two
or more entities
• Relationships may have
attributes
• Relationship Set: Set of
similar relationships

 Relationship sets are


drawn using diamonds

49
Example
title
birthday

id
Actor Acted In Film year

name

address type

Where does the salary salary


attribute belong?
50
Recursive Relationships
• An entity set can participate more than once in a relationship
• In this case, we add a description of the role to the ER-diagram

phone
number
manager

id
Employee Work for
worker
name

address

51
n-ary Relationship
• An n-ary relationship set R involves exactly n entity sets: E1, …, En.
• Each relationship in R involves exactly n entities: e1 E1, …, en  En

id Director name

id
Actor Produced Film title

name

Ternary Relationship
52
Key Constraints
• Key constraints specify
whether an entity can
participate in one, or more
than one, relationships in a
relationship set
• When there is no key
constraint, an entity can
participate any number of
times
• When there is a key
constraint, the entity can
participate at most one time
 Key constraints are drawn
using an arrow from the entity
set to the relationship set

53
Key Constraints
• Key constraints specify
whether an entity can
participate in one, or more
than one, relationships in a
relationship set
• When there is no key
constraint, an entity can
participate any number of
times
• When there is a key
constraint, the entity can
participate at most one time
 Key constraints are drawn
using an arrow from the entity
set to the relationship set

54
One-to-Many
✓A film is directed by at most one director
✓A director can direct any number of films

id
Director Directed Film title

name

55 Director Directed Film


Many-to-Many
✓A film is directed by any number of directors
✓A director can direct any number of films

id
Director Directed Film title

name

56 Director Directed Film


One-to-One
✓A film is directed by at most one director
✓A director can direct at most one film

id
Director Directed Film title

name

57 Director Directed Film


Question

One to One One to Many Many to Many


58
59

You might also like