Second Unit 3
Second Unit 3
Relational Model is the most widely used model. In this model, the data is
maintained in the form of a two-dimensional table. All the information is
stored in the form of row and columns. The basic structure of a relational
model is tables. So, the tables are also called relations in the relational
model. Example: In this example, we have an Employee table.
Tuples: Each row in the table is called tuple. A row contains all the
information about any instance of the object. In the above example, each
row has all the information about any specific individual like the first
row has information about John.
Attribute or field: Attributes are the property which defines the table or
relation. The values of the attribute should be from the same domain. In
the above example, we have different attributes of the employee like
Salary, Mobile_no, etc.
Advnatages of Relational Model
. Relationship
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then it is
known as one to one relationship.
For example, A female can marry to one male, and a male can marry to one
female.
b. One-to-many relationship
When only one instance of the entity on the left, and more than one instance of
an entity on the right associates with the relationship then this is known as a
one-to-many relationship.
For example, Scientist can invent many inventions, but the invention is done by
the only specific scientist.
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one instance of
an entity on the right associates with the relationship then it is known as a
many-to-one relationship.
For example, Student enrolls for only one course, but a course can have many
students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than one
instance of an entity on the right associates with the relationship then it is
known as a many-to-many relationship.
For example, Employee can assign by many projects and project can have
many employees.
Insert Operation
The insert operation gives values of the attribute for a new tuple which
should be inserted into a relation.
Update Operation
You can see that in the below-given relation table CustomerName=
‘Apple’ is updated from Inactive to Active.
Delete Operation
To specify deletion, a condition on the attributes of the relation selects the
tuple to be deleted.
In the above-given example, CustomerName= “Apple” is deleted from
the table.
The Delete operation could violate referential integrity if the tuple which
is deleted is referenced by foreign keys from other tuples in the
same database.
Select Operation
In the above-given example, CustomerName=”Amazon” is selected
Types of attributes
Multi-valued Attribute
Attribute having a set of values for a single entity is called a multi-valued
attribute.
For example −
e-mail
Tel.No
Hobbies
Derived Attributes or stored Attributes
When one attribute value is derived from the other is called a derived attribute.
For example: Age can be derived from date of birth, where,
Age is the derived attribute.
DOB is the stored attribute.
Complex Attribute
Nesting of composite and multi-valued attributes forms a complex attribute.
For example
If a person has more than one house and each house has more than one phone.
Then, that attribute phone is represented as a complex attribute.
----------------------------------------------------------------------------------
A relational database collects different types of data sets that use tables, records,
and columns. It is used to create a well-defined relationship between database
tables so that relational databases can be easily stored. For example of relational
databases such as Microsoft SQL Server, Oracle Database, MYSQL, etc.
For example, there are many people involved in each project, and every person
can involve more than one project.
Difference between a database and a relational database
Relational Database Database
A relational database can store and It is used to store the data as files.
arrange the data in the tabular form like
rows and columns.
In a relational database, the values are Generally, it stores the data in the
stored as tables that require a primary hierarchical or navigational form.
keys to possess the data in a database.
11 Andrew Johnson
22 Tom Wood
33 Alex Hale
1. Super Key
2. Primary Key
3. Candidate Key
4. Alternate Key
5. Foreign Key
6. Compound Key
7. Composite Key
8. Surrogate Key
Example:
Example:
In this table, StudID, Roll No, Email are qualified to become a primary key. But
since StudID is the primary key, Roll No, Email becomes the alternative key.
Candidate key Example: In the given table Stud ID, Roll No, and email are
candidate keys which help us to uniquely identify the student record in the table.
DeptCode DeptName
001 Science
002 English
005 Computer
In this key in dbms example, we have two table, teach and department in a
school. However, there is no way to see which search work in which
department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can
create a relationship between the two tables.
In this example, OrderNo and ProductID can’t be a primary key as it does not
uniquely identify a record. However, a compound key of Order ID and Product
ID could be used as it uniquely identified each record.
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of
values for an attribute.
o The data type of domain includes string, character, integer, time, date,
currency, etc. The value of the attribute must be available in the
corresponding domain.
Example:
2. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows
in relation and if the primary key has a null value, then we can't identify
those rows.
o A table can contain a null value other than the primary key field.
Example:
3. Referential Integrity Constraints
o A referential integrity constraint is specified between two tables.
o In the Referential integrity constraints, if a foreign key in Table 1 refers
to the Primary Key of Table 2, then every value of the Foreign Key in
Table 1 must be null or be available in Table 2.
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set
uniquely.
o An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the
relational table.
Example:
---------------------------------------------------------------------------------------------
The relational algebra is a relation-at-a-time (or set) language where all tuples
are controlled in one statement without the use of a loop. There are several
variations of syntax for relational algebra commands, and you use a common
symbolic notation for the commands and present it informally.
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Derived Operations:
If you understand little bit of SQL then you can think of it as a where clause in
SQL, which is used for the same purpose.
Syntax of Select Operator (σ)
σ Condition/Predicate(Relation/Table name)
Select Operator (σ) Example
Table: CUSTOMER
σ Customer_City="Agra" (CUSTOMER)
Output:
Customer_Name Customer_City
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi
Union operator is denoted by ∪ symbol and it is used to select all the rows
(tuples) from two tables (relations).
Lets discuss union operator a bit more. Lets say we have two relations R1 and
R2 both have same columns and we want to select all the tuples(rows) from
these relations then we can apply the union operator on these relations.
Note: The rows (tuples) that are present in both the tables will only appear once
in the union set. In short you can say that there are no duplicates present after
the union operation.
table_name1 ∪ table_name2
Union Operator (∪) Example
Table 1: COURSE
Course_Id Student_Name Student_Id
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:
Student_Name
Aditya
Carl
Paul
Lucy
Rick
Steve
Note: As you can see there are no duplicate names present in the output even
though we had few common names in both the tables, also in the COURSE
table we had the duplicate name itself.
Lets say we have two relations R1 and R2 both have same columns and we
want to select all those tuples(rows) that are present in both the relations, then in
that case we can apply intersection operation on these two relations R1 ∩ R2.
Note: Only those rows that are present in both the tables will appear in the
result set.
table_name1 ∩ table_name2
Intersection Operator (∩) Example
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:
Student_Name
Aditya
Steve
Paul
Lucy
Set Difference (-)
Set Difference is denoted by – symbol. Lets say we have two relations R1 and
R2 and we want to select all those tuples(rows) that are present in Relation R1
but not present in Relation R2, this can be done using Set difference R1 – R2.
table_name1 - table_name2
Set Difference (-) Example
Lets take the same tables COURSE and STUDENT that we have seen above.
Query:
Lets write a query to select those student names that are present in STUDENT
table but not present in COURSE table.
Student_Name
Carl
Rick
R1 X R2
Cartesian product (X) Example
Table 1: R
Col_A Col_B
AA 100
BB 200
CC 300
Table 2: S
Col_X Col_Y
XX 99
YY 11
ZZ 101
Query:
Lets find the cartesian product of table R and S.
RXS
Output:
AA 100 XX 99
AA 100 YY 11
AA 100 ZZ 101
BB 200 XX 99
BB 200 YY 11
BB 200 ZZ 101
CC 300 XX 99
CC 300 YY 11
CC 300 ZZ 101
Note: The number of rows in the output will always be the cross product of
number of rows in each table. In our example table 1 has 3 rows and table 2 has
3 rows so the output has 3×3 = 9 rows.
Rename (ρ)
Lets say we have a table customer, we are fetching customer names and we are
renaming the resulted relation to CUST_NAMES.
Table: CUSTOMER
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
Steve
Raghu
Chaitanya
Ajeet
Carl
Join is a combination of a Cartesian product followed by a selection process. A
Join operation pairs two tuples from different relations, if and only if a given
join condition is satisfied.
We will briefly describe various join types in the following sections.
Theta join combines tuples from different relations provided they satisfy the
theta condition. The join condition is denoted by the symbol θ.
Notation
R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn)
such that the attributes don’t have anything in common, that is R1 ∩ R2 = Φ.
Theta join can use all kinds of comparison operators.
Student
101 Alex 10
102 Maria 11
Subjects
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail −
STUDENT ⋈Student.Std = Subject.Class SUBJECT
Student_detail
Equijoin
Natural join does not use any comparison operator. It does not concatenate the
way a Cartesian product does. We can perform a Natural Join only if there is at
least one common attribute that exists between two relations. In addition, the
attributes must have the same name and domain.
Natural join acts on those matching attributes where the values of attributes in
both the relations are same.
Courses
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE
HoD
Dept Head
CS Alex
ME Maya
EE Mira
Courses ⋈ HoD
Outer Joins
Theta Join, Equijoin, and Natural Join are called inner joins. An inner join
includes only those tuples with matching attributes and the rest are discarded in
the resulting relation. Therefore, we need to use outer joins to include all the
tuples from the participating relations in the resulting relation. There are three
kinds of outer joins − left outer join, right outer join, and full outer join.
All the tuples from the Left relation, R, are included in the resulting relation. If
there are tuples in R without any matching tuple in the Right relation S, then the
S-attributes of the resulting relation are made NULL.
Left
A B
100 Database
101 Mechanics
102 Electronics
Right
A B
100 Alex
102 Maya
104 Mira
Courses HoD
A B C D
All the tuples from the Right relation, S, are included in the resulting relation. If
there are tuples in S without any matching tuple in R, then the R-attributes of
resulting relation are made NULL.
Courses HoD
A B C D
All the tuples from both participating relations are included in the resulting
relation. If there are no matching tuples for both relations, their respective
unmatched attributes are made NULL.
Courses HoD
A B C D
DBMS SQL
It allows the user to perform operations such as create, delete etc. on tables
in database
It also allows operations such as read, insert, update, delete etc. on the data
stored in the tables.
All popular relational database management softwares such as MySQL,
PostgreSQL, Oracle, Microsoft Access, IDBM Db2, SQLite etc. use SQL
to manage the database.
SQL is a simple english like language which is used to query the database
to perform various operations on the database.
1. Only use lowercase letters, numbers and underscores for database, schema,
column and table name.
2. Use simple & meaningful, table and column name. This is because
sometimes, we refer the attribute of a table from another table as foreign
key.
3. SQL is not a case sensitive language, however the keywords used in the
SQL query are generally written in uppercase.
4. Using SQL queries you can perform various operations on the database.
SQL Processing
Characteristics of SQL
In this article, we will discuss the advantages of SQL. We have already seen
the several features of SQL in characteristics of SQL. Here, we will cover some
of the advantages that we get while using SQL as a database language.
You can quickly retrieve large amount of data from database using SQL. The
response time of a SQL query is very fast.
2. Requires No coding
Learning SQL is easy and doesn’t require any prior coding or programming
knowledge. The syntax of SQL is very simple and close to english so learning
curve is smooth.
3. Portable
4. Standardised language
SQL is been used for over the years and has wide variety of well maintained
documentation. This language is so standardised that the same syntax can be
used on various different platforms.
5. Intergation
SQL server can connect to third party backends like Oracle, IDM Db2, MySQL
etc. using drivers. These drivers allow the smooth integration.
6. Secure
SQL allows to set permissions in tables, this makes it secure as a user with no
permission cannot read, write or modify the data in database. SQL also has the
concept of constraints that ensures what type of data can be inserted into the
tables. All these features make SQL a secure database language.
7. Scalable
It is easy to add or drop tables in the database using SQL. Also, the database
size doesn’t affect the performance of SQL that much and it works pretty great
with large databases as well. It is easy to add several new tables into the
database as well as drop multiple tables from database.
8. Supports Transactions
Transactions are the logical units or sequence of tasks that either needs to be
completed fully or none to maintain database integrity. SQL commands such as
COMMIT, ROLLBACK, SAVEPOINT etc. support transactions.
For example, you want to store student data in a table “student”. The attributes
of this table are: student_id, student_name, student_age, student_address. The
primary key is a set of one or more of these attributes to uniquely identify a
record in the table. In the case, since student_id is different for each student, this
can be considered a primary key.
1. Minimal
The primary key should contain minimal number of attributes. The example we
seen above, where student_id is able to uniquely identify a record, here
combination of two attributes such as {student_id, student_name} can also
uniquely identify record. However since we should choose minimal set of
attribute thus student is chosen as primary key instead of {student_id,
student_name}.
2. Unique
The value of primary key should be unique for each row of the table. The
column(s) that makes the key cannot contain duplicate values. This is because
non-unique value would not help us uniquely identify record. If two students
have same student_id then updating a record of one student based on primary
key can mistakenly update record of other student.
3. Non Null
The attribute(s) that is marked as primary key is not allowed to have null values.
The primary key value should not change over time. It should remain as it is
until explicitly updated by the user.
5. Easily accessible
The primary key of the record should be accessible to all the users who are
performing any operations on the database.
While creating table you can define primary key like this:
-------------------------------------------------------------------------------------------
{Emp_SSN}
{Emp_Number}
{Emp_SSN, Emp_Number}
{Emp_SSN, Emp_Name}
{Emp_SSN, Emp_Number, Emp_Name}
{Emp_Number, Emp_Name}
{Emp_SSN}
{Emp_Number}
Only these two sets are candidate keys as all other sets are having redundant
attributes that are not necessary for unique identification.
Lets take an example of table “Employee”. This table has three attributes:
Emp_Id, Emp_Number & Emp_Name. Here Emp_Id & Emp_Number will be
having unique values and Emp_Name can have duplicate values as more than
one employees can have same name.
Emp_IdEmp_Number Emp_Name
Lets select the candidate keys from the above set of super keys.
Definition: Foreign keys are the columns of a table that points to the primary
key of another table. They act as a cross-reference between tables.
For example:
In the below example the Stu_Id column in Course_enrollment table is a foreign
key as it points to the primary key of the Student table.
Course_enrollment table:
Course_Id Stu_Id
C01 101
C02 102
C03 101
C05 102
C06 103
C07 102
Student table:
101 Chaitanya 22
102 Arya 26
103 Bran 25
104 Jon 21
Note: Practically, the foreign key has nothing to do with the primary key tag of
another table, if it points to a unique column (not necessarily a primary key) of
another table then too, it would be a foreign key. So, a correct definition of
foreign key would be: Foreign keys are the columns of a table that points to
the candidate key of another table.
Composite key in DBMS
Definition of Composite key: A key that has more than one attributes is known
as composite key. It is also known as compound key.
Note: Any key such as super key, primary key, candidate key etc. can be called
composite key if it has more than one attributes.
Table – Sales
Column cust_Id alone cannot become a key as a same customer can place
multiple orders, thus the same customer can have multiple entires.
Column order_Id alone cannot be a primary key as a same order can contain
the order of multiple products, thus same order_Id can be present multiple
times.
Column product_code cannot be a primary key as more than one customers can
place order for the same product.
Column product_count alone cannot be a primary key because two orders can
be placed for the same product count.
Based on this, it is safe to assume that the key should be having more than one
attributes:
Key in above table: {cust_id, product_code}
This is a composite key as it is made up of more than one attributes
As we have seen in the candidate key guide that a table can have multiple
candidate keys. Among these candidate keys, only one key gets selected
as primary key, the remaining keys are known as alternative or secondary
keys.
Table: Employee/strong>
DBA (Database administrator) can choose any of the above key as primary key.
Lets say Emp_Id is chosen as primary key.
Many of the calculus expressions involves the use of Quantifiers. There are
two types of quantifiers:
Free and bound variables may be compared with global and local variable of
programming languages.
Types of Relational calculus:
Notation:
Where
For example:
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential
(∃) and Universal Quantifiers (∀).
For example:
Output: This query will yield the same result as the previous one.
Table: Student
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Lets write relational calculus queries.
Query to display the last name of those students where age is greater than
30
Last_Name
Singh
Query to display all the details of students where Last name is ‘Singh’
Ajeet Singh 30
Chaitanya Singh 31
Notation:
Where
For example:
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Query to find the first name and age of students where student age is greater
than 27
Output:
First_Name Age
Ajeet 30
Chaitanya 31
Carl 28
Structure of Index
We can create indices using some columns of the database.
The search key is the database’s first column, and it contains a duplicate
or copy of the table’s candidate key or primary key. The primary key
values are saved in sorted order so that the related data can be quickly
accessible.
The data reference is the database’s second column. It contains a group of
pointers that point to the disk block where the value of a specific key can
be found.
Methods of Indexing
Ordered Indices
To make searching easier and faster, the indices are frequently arranged/sorted.
Ordered indices are indices that have been sorted.
Example
Let’s say we have a table of employees with thousands of records, each of
which is ten bytes large. If their IDs begin with 1, 2, 3,…, etc., and we are
looking for the student with ID-543:
We must search the disk block from the beginning till it reaches 543 in
the case of a DB without an index. After reading 543*10=5430 bytes, the
DBMS will read the record.
We will perform the search using indices in the case of an index, and the
DBMS would read the record after it reads 542*2 = 1084 bytes, which is
significantly less than the prior example.
Primary Index
Sparse Index
Only a few items in the data file have index records. Each and every item points
to a certain block. Rather than pointing to each item in the main database, the
index, in this case, points to the records that are present in the main table that is
in a gap.
The index record appears only for a few items in the data file. Each item
points to a block as shown.
To locate a record, we find the index record with the largest search key
value less than or equal to the search key value we are looking for.
We start at that record pointed to by the index record, and proceed along
with the pointers in the file (that is, sequentially) until we find the desired
record.
Clustering Index
An ordered data file can be defined as a clustered index. Non-primary key
columns, which may or may not be unique for each record, are sometimes
used to build indices.
In this situation, we’ll join two or more columns to acquire the unique
value and generate an index out of them to make it easier to find the
record. A clustering index is a name for this method.
Records with comparable properties are grouped together, and indices for
these groups are constructed.
Example
Assume that each department in a corporation has numerous employees.
Assume we utilise a clustering index, in which all employees with the same
Dept_ID are grouped together into a single cluster, and index pointers refer to
the cluster as a whole. Dept_Id is a non-unique key in this case.
Because one disk block is shared by records from various clusters, the previous
structure is a little unclear. It is referred to as a better strategy when we employ
distinct disk blocks for separate clusters.
Secondary Index
When using sparse indexing, the size of the mapping grows in sync with the
size of the table. These mappings are frequently stored in primary memory to
speed up address fetching. The secondary memory then searches the actual data
using the address obtained through mapping. Fetching the address becomes
slower as the mapping size increases. The sparse index will be ineffective in this
scenario, so secondary indexing is used to solve this problem.
Another level of indexing is introduced in secondary indexing to reduce the size
of the mapping. The massive range for the columns is chosen first in this
method, resulting in a small mapping size at the first level. Each range is then
subdivided into smaller groups. Because the first level’s mapping is kept in
primary memory, fetching the addresses is faster. The second-level mapping, as
well as the actual data, are kept in secondary memory (or hard disk).
PL/SQL Full Form
PL/SQL stands for “Procedural Language extensions to the Structured Query
Language.” PL/SQL is Oracle Corporation’s procedural extension for SQL and
the Oracle relational database. It is a high-performance, highly integrated
database language.
Architecture of PL/SQL
The Below PL/SQL Example is a pictorial representation of PL/SQL
Architecture.
PL/SQL Architecture Diagram
1. PL/SQL Block
2. PL/SQL Engine
3. Database Server
PL/SQL block:
Anonymous Block
Function
Library
Procedure
Package Body
Package Specification
Trigger
Type
Type Body
PL/SQL Engine
Database Server:
This is the most important component of Pl/SQL unit which stores the
data.
The PL/SQL engine uses the SQL from PL/SQL units to interact with the
database server.
It consists of SQL executor which parses the input SQL statements and
execute the same.
Disadvantages of PL/SQL
SQL PL/SQL
Rules:
What Is SQL?
SQL plays a crucial role in retrieving relevant data from databases, which can
later be used by various platforms such as Python or R for analysis purposes.
SQL can manage several data transactions simultaneously where large volumes
of data are written concurrently.
1. Parsing: This refers to a process that cross-checks the syntax of the query.
2. Binding: This step involves verifying query semantics before executing it.
3. Optimization: The final step generates the query execution plan. The
objective here is to identify an efficient query execution plan that runs in
minimal time. This implies that the shorter the response time for the SQL query,
the better the results. Several combinations of plans are generated to have a
practical end execution plan.
How does SQL work?
SQL is the go-to-choice of most database users due to its easy usability and how
the queries can carry out varied functions on vast amounts of structured data.
1. Keywords
2. Clauses
3. Expressions
4. Predicates
5. Queries
Elements of SQL
Let’s understand the role of each element in SQL programming:
For example, let’s say we need to identify the names of families living in the
Boston area who have ‘Luis’ as their last name. The following SQL query will
fetch the relevant results for this problem statement:
2. Clauses: Clauses refer to the in-built functions that filter out data and retrieve
the required data from the database or table. It is suitable when handling large
databases. Clauses are a part of the SQL statement.
Let’s consider a use case where you need to select age, email, and address from
the database. One would then represent the clause as ‘SELECT Age,
Email, and Address,’ where SELECT is a keyword and age, email, and address
reveal certain information to run the SQL query and retrieve the required data.