This document provides an overview of the relational data model and relational database concepts. It defines what a relational database is and how data is organized into tables with rows and columns. It describes key components like schemas and relational database management systems. The document also covers relational algebra operations like select, project, join, and set operations. Finally, it provides a basic introduction to the structured query language SQL, including some common SQL commands and how it is used to perform queries, updates, and other data operations on relational databases.
3. Relational Database
A relational database is a type of database.
It uses a structure that allows us to identify and access data in relation to
another piece of data in the database.
Often, data in a relational database is organized into tables.
Tables: Rows and Columns
Tables can have hundreds, thousands, sometimes even millions of rows of
data. These rows are often called records.
Tables can also have many columns of data. Columns are labeled with a
descriptive name (say, age for example) and have a specific data type.
For example, a column called age may have a type of INTEGER (denoting the
type of data it is meant to hold).
5. Cont…
In the table above, there are three columns (name, age, and country).
The name and country columns store string data types, whereas age stores
integer data types.
The set of columns and data types make up the schema of this table.
The table also has four rows, or records, in it (one each for Natalia, Ned,
Zenas, and Laura).
What is a Relational Database Management System (RDBMS)?
A relational database management system (RDBMS) is a program that allows
you to create, update, and administer a relational database. Most relational
database management systems use the SQL language to access the database.
6. Relational Algebra
Relational database systems are expected to be equipped with a query
language that can assist its users to query the database instances. There are
two kinds of query languages − relational algebra and relational calculus.
Relational Algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output.
It uses operators to perform queries. An operator can be
either unary or binary.
They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate
results are also considered relations.
7. Cont….
The fundamental operations of relational algebra are as follows −
Select
Project
Union
Set different
Cartesian product
Rename
8. Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic
formula which may use connectors like and, or, and not. These terms may use relational
operators like − =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those
books published after 2010.
9. Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation
Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −
10. Cont….
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary
relation).
For a union operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an
article or both.
11. Set Difference (−)
The result of set difference operation is tuples, which are present in one
relation but are not in the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not
articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
12. Cont….
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by
tutorialspoint.
13. Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The
rename operation allows us to rename the output relation. 'rename' operation
is denoted with small Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
Set intersection
Assignment
Natural join
14. Structured Query Language(SQL)
SQL is a standard language for accessing and manipulating databases.
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in
1986, and of the International Organization for Standardization (ISO) in 1987.
Although SQL is an ANSI/ISO standard, there are different versions of the SQL
language.
However, to be compliant with the ANSI standard, they all support at least the
major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a
similar manner.
15. What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
16. Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:
An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
To use a server-side scripting language, like PHP or ASP
To use SQL to get the data you want
To use HTML / CSS to style the page
17. RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS
SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables. A table is a
collection of related data entries and it consists of columns and rows.
Look at the "Customers" table:
Example
SELECT * FROM Customers;
18. Some of The Most Important SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
19. The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
SELECT Syntax
SELECT column1, column2, ...
FROM table_name;
Here, column1, column2, ... are the field names of the table you want to
select data from. If you want to select all the fields available in the table,
use the following syntax:
SELECT * FROM table_name;