Docsity

Advanced Database Management: SQL, NoSQL, Cloud, and Distributed Systems, Lecture notes of Database Management Systems (DBMS)

Partial preview of the text

Download Advanced Database Management: SQL, NoSQL, Cloud, and Distributed Systems and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

Relational

Database

Management

System

Advanced Data Management: FoR SQL, Nosql, Cloud and Distributed Databases

The relational data model is based on the concept of storing records of data as rows inside tables. Each row represents an entity of the real world with table columns being attributes (or properties) of interest of these entities. The relational data model has been the predominant data model of database systems for several decades. Relational Database Management Systems (RDBMSs) have been a commercial success since the 1980s. There are powerful systems on the market with lots of functionalities.

Introduction

Relational Data Model

Database and Relation Schemas

Table 1 Definition of the attribute names Ai for the relation symbol R is called a relation schema The set of the relation schemas of all relation symbols in the database is then called a database schema

  • (^) A table is then filled row-wise with values that represent the state of an entity; that is, the rows are tuples of values that adhere to the predefined attribute domains as shown in Table 1
  • (^) Each table hence corresponds to the mathematical notion of a relation in the sense that the set of tuples in a relation are a subset of the cartesian product of the attribute domains: if r is the set of tuples in a table, then r dom (A 1 ) Ɨ … Ɨ dom (An )

Relational Data Model

Database and Relation Schemas

Table 1 These constraints describe which dependencies between the stored data exist Intrarelational constraints describe dependencies inside a single table Interrelational constraints describe dependencies between different tables Intrarelational constraints Example: be functional dependencies – and in particular keyconstraints: the key attributes BookID and ReaderID in our ER diagram will be keys in the corresponding database tables and hence serve as unique identifiers for books and readers Intrarelational constraints Example: inclusion dependencies – and in particular foreign keyindex constraints: when using the ID of a book in another table (for example a table for all the book lendings), we must make sure that the ID is included in the Book table Each relation schema can have intra relational constraints, and the database schema can have inter relational constraints

Relational Data Model

Mapping ER Models to Schemas

An ER diagram can be

translated into a database

schema

Each entity name

corresponds to a relation

symbol

Entity attributes

correspond to relation

attributes

Relational Data Model

Mapping ER Models to Schemas

Example, The multi-valued attribute Author must be translated into a new relation BookAuthors with attributes BookID and Author and a foreign key constraint BookAuthors.BookID āŠ† Book.BookID. relational data model does not allow multi-valued and composite attributes. In the case of multi-valued attributes, a new relation schema for each multi- valued attribute created containing additional foreign keys (to connect the new relation schema to the original relation schema) However,

Relational Data Model

Mapping ER Models to Schemas

Example, We have a BookLending relation in our database with the attribute ReturnDate. In order to be able to map the values from the entities connected by the relationship together, the relation also contains the key attributes of the entities participating in the relationship. That is why the BookLending relation also has a BookID attribute and a ReaderID attribute with foreign key constraints on them. Relationships are also translated into a relation schema.

Relational Data Model

Mapping ER Models to Schemas

Database schema: Relation schemas: Library = ({Book, BookAuthors, Reader, BookLending}, { BookAuthors.BookID āŠ† Book.BookID, BookLending.BookID āŠ† Book.BookID, BookLending.ReaderID āŠ† Reader.ReaderID}) Book = ({BookID, Title, Year}, {BookID → Title, Year}) BookAuthors = ({BookID, Author}, {}) Reader = ({ReaderID, Name, Email}, {ReaderID → Name, Email}) BookLending = ({BookID, ReaderID, ReturnDate}, {BookID, ReaderID → ReturnDate})

Referential Integrity

Referential integrity means that values of the attributes that belong to the

foreign key indeed exist as values of the primary key in the referenced table –

if there is more than one option to choose a key, it suffices that the referenced

attributes are a candidate key.

Referential integrity must be ensured when inserting or updating tuples in the referencing table; but also deleting tuples from the referenced table as well as updating the primary key (or candidate key) in the referenced table affects referential integrity. Table 3. Unnormalized relational table

Referential Integrity

Insert tuple into referencing table: Whenever we insert a tuple in a table that has foreign key attributes, we must make sure that the values inserted into the foreign key attributes are equal to values contained in the referenced primary key or candidate key. Update tuple in referencing table: The same applies when values of foreign keys are updated. Table 3. Unnormalized relational table

Relational Query Languages

The Structued Query Language (SQL) is the standardized language to communicate with RDBMSs; it is a standardized language for data definition, data manipulation and data querying. Commonly used SQL statements: CREATE SCHEMA … CREATE TABLE … INSERT INTO … VALUES … DELETE FROM … WHERE … UPDATE … SET … WHERE … SELECT … FROM … WHERE … SELECT … FROM … GROUP BY … SELECT … FROM … ORDER BY … SELECT COUNT(*) FROM …

Relational Query Languages

Other (more mathematical) ways to express queries on relational tables, would be the logic- based relational calculus , or the operator-based relational algebra. Projection Ļ€ (restricting a table to some attributes). For example, IDs of readers currently having borrowed a book: Selection σ (with condition on answer tuples). For example, all books to be returned before 29-10-2016: Renaming ρ (giving a new name for an attribute). For example, rename ReturnDate to DueDate:

Relational Query Languages

Example: Let us now look at the different ways to express queries in a comparative example by asking a query over the BookLending table: "Find all books that must be returned before 29-10-2016". In calculus, x = BookID, y = Reader ID, z = ReturnDate

Relational Query Languages

Example: Let us now look at the different ways to express queries in a comparative example by asking a query over the BookLending table: "Find all books that must be returned before 29-10-2016". In relational algebra we would use the selection operator σ,