Database System Lecturing v 3
Database System Lecturing v 3
and
Relational Database Model
Yan Watequlis Syaifudin
Master course’s presentation, Okayama University, Japan
Concept of Database
Query Language
OUTLINE
Database Development
Q&A
Post Test
1. Concept of Database
DATA PROCESSING IN THE WORLD
Definitions
Data: stored representations of meaningful objects and
events or
Referred to facts concerning objects and events that
5
Types of Data
INFOGINEERING MODEL
Understanding
Principles
???
Terminology
in Relational
Database
Relation (Table)
Two-dimensional structure with rows and columns.
Table also called a relation because the relational model’s
creator, Codd, used the term relation as a synonym for table.
A relation represent a single entity.
Each table must have an attribute to uniquely identify each row.
Column values all have same data type.
Order of the rows and columns is immaterial to the DBMS.
Rules of Relation (Table)
1. There are no duplicate tuples (rows).
• The body of the relation is a mathematical set (i.e., a set of tuples),
and sets in mathematics do not include duplicate elements.
• If a "relation" contains duplicate tuples, then it is not a relation.
2. Tuples (rows) are unordered (top to bottom).
• Sets in mathematics are not ordered. So, even if a relation A's tuples
are reversely ordered, it is still the same relation.
• Thus, there is no such thing as "the 5th tuple" or the last tuple. In
other words, there is no concept of positional addressing.
3. Attributes (columns) are unordered (left to right).
• The heading of a relation is also defined as a set.
• There is no such thing as "5th attribute (column)" or the last
attribute.
4. All attribute values are atomic.
• At every row-and-column position within the table, there always
exists precisely one value, never a list of values. Or equivalently,
relations do not contain repeating groups.
• A relation satisfying this condition is said to be in First Normal Form.
Keys
Consists of one or more attributes that determine other
attributes.
Primary key (PK) is an attribute, or collection of attributes,
whose values uniquely identify each tuple in a relation.
To being unique, a PK must be minimal (contain no
unnecessary attributes) and must not change in value.
Key’s role is based on determination
• If you know the value of attribute A, you can look up (determine)
the value of attribute B
21
Foreign Key
• A relation schema may have an attribute that corresponds to
the primary key of another relation. The attribute is called a
foreign key.
• It is an attribute (or combination of attributes) in one table
whose values must either match the primary key in another
table or be null.
• In relational database, foreign key represent the relationship
between 2 or more relations/tables.
Relationships within Relations/Tables
(Cardinality)
a) 1:M (one to many) relationship
• Relational modeling ideal
• Should be the norm in any relational database design
b) 1:1 (one to one) relationship
• Should be rare in any relational database design
c) M:N (many to many) relationships
• Cannot be implemented as such in the relational model
• M:N relationships can be changed into two 1:M relationships
Lets identify
23
The 1:M Relationship
• Relational database norm
• Found in any database environment
Q : Give example?
24
The 1:1 Relationship
• One entity can be related to
only one other entity, and vice
versa
• Sometimes means that entity
components were not defined
properly
• Could indicate that two entities
actually belong in the same
table
• As rare as 1:1 relationships
should be, certain conditions Q : Give example?
absolutely require their use
25
The M:N Relationship
• Yields required M:N to 1:M conversion
• Linking table must contain the primary keys of original tables
• L.T. contains multiple occurrences of the foreign key values
• Additional attributes may be assigned as needed
Q : Give example? 26
RD Schema: a case study
3. Query Language
Data Query Language
• Query languages or data query languages (DQLs)
are computer languages used to communicate with DBMS.
• The most popular relational database query language is SQL
(Structured Query Language), created by IBM in 1974.
• The standard of SQL is ANSI SQL.
Implementation of SQL in RDBMS
Many types of SQL exist, including MySQL, PostgreSQL, PL/SQL
(Procedural Language/SQL, used by Oracle), T-SQL and ANSI SQL
(used by Microsoft SQL), and many others.
Type of SQL Statements
Data Definition Language (DDL)
Use to define the database structure or table.
Statement Description
CREATE Create new database/table.
ALTER Modifies the structure of database/table.
DROP Deletes a database/table.
TRUNCATE Remove all table records including allocated
table spaces.
RENAME Rename the database/table.
Data Manipulation Language (DML)
Use for managing data within table object.
Statement Description
SELECT Retrieve data from the table.
INSERT Insert data into a table.
UPDATE Updates existing data with new data within a table.
DELETE Deletes the records rows from the table.
MERGE MERGE (also called UPSERT) statements to INSERT new
records or UPDATE existing records depending on
condition matches or not.
LOCK TABLE LOCK TABLE statement to lock one or more tables in a
specified mode. Table access denied to a other users
for the duration of your table operation.
Data Control Language (DCL)
Use to give privileges to access limited data.
Statement Description
GRANT Gives privileges to user for accessing database
data.
REVOKE Take back for given privileges.
ANALYZE ANALYZE statement to collect statistics
information about index, cluster, table.
AUDIT To track the occurrence of a specific SQL
statement or all SQL statements during the
user sessions.
COMMENT Write comment to the data table.
SQL: A Case Study
Student Room
id name id name capacity
1000 James Smith C01 Class Room 01 24
1001 Ada Lovelace M01 Meeting Room 20
1002 小嶋陽菜 C02 Class Room 02 24
1003 James Smith G01 General Room 01 100
Enrolled
Course
student_id course_id grade
id name room_id
1000 IT101 A
IT101 Mathematics C01
1000 IT102 B+
IT102 Programming 1 C02
1003 IT301 A+
IT301 Network System C01
1003 IT302
IT302 English Lecture M01
1002 IT402
IT402 Advanced Network
1001 IT402 A+
Creating a table
CREATE TABLE table_name (
Basic syntax column1_name column1_type,
column2_name column2_type,
…
);
Student Room
id name id name capacity
Adding a single row / rows (Insert)
Basic syntax
INSERT INTO table_name VALUES (val1,val2,val3,…);
Student Room
id name id name capacity
1000 James Smith C01 Class Room 01 24
1001 Ada Lovelace M01 Meeting Room 20
1002 小嶋陽菜 C02 Class Room 02 24
1003 James Smith G01 General Room 01 100
Show table content (Select)
Basic syntax SELECT column_list FROM table_name
[WHERE condition] [ORDER BY column_list];
DBMS
Physical View of Data Logical View of Data
(Tables) (Forms)