Professional Documents
Culture Documents
StudlD
is a Primary Key.
StudID Roll No FirstName LastName Email
1 pa Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com
What is the Alternate key?
ALTERNATE KEYS is a column or group of columns in a table that uniquely
identify every row in that table. A table can have multiple choices for a primary
key but only one can be set as the primary key. All the keys which are not
primary key are called an Alternate Key.
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.
StudID Roll No First Name LastName Email
1 il Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREWhat is a Candidate Key?
CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no repeated attributes. The Primary key
should be selected from the candidate keys. Every table must have at least a
single candidate key. A table can have multiple candidate keys but only a single
primary key
Properties of Candidate key:
+ Itmust contain unique values
+ Candidate key may have multiple attributes
« Must not contain null values.
+ It should contain minimum fields to ensure uniqueness
+ Uniquely identify each record ina table
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.
studi Roll No First Name LastName Email
1 oa Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com
1 11] Tom Price ynailcom
2 12|Nick Wright v2t@omailcom
3 13|Dana Natan |mno@yahoo com
What is the Foreign key?
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREFOREIGN KEYis a column that creates a relationship between two tables. The
purpose of Foreign keys is to maintain data integrity and allow navigation
between two different instances of an entity. It acts as a cross-reference
between two tables as it references the primary key of another table.
Exampl
DeptCode DeptName
001 Science
002 English
005 Computer
Teacher ID Fname Lname
Bo02 David Warner
8017 Sara Joseph
Bo009 Mike Brunton.
In this key in doms 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.
Teacher ID DeptCode Fname Lname
Booz 002 David Warner
BO17 002 Sara Joseph
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREBo009 001 Mike Brunton
This concept is also known as Referential Integrity.
What is the Compound key?
COMPOUND KEY has two or more attributes that allow you to uniquely
recognize a specific record. itis possible that each column may not be unique by
itself within the database. However, when combined with the other column or
columns the combination of composite keys become unique. The purpose of the
compound key in database is to uniquely identify each recordin the table.
Example:
OrderNo PorductiD Product Name Quantity
005, JAP102459 Mouse 5
B005, DKT321573 usB 10
005 0MG446789 LCD Monitor 20
B004 DKT321573 USB 15
B002 0MG446789 Laser Printer 3
In this example, OrderNo and ProductlD can't be a primary key asit 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.
What is the Composite key?
COMPOSITE KEY is a combination of two or more columns that uniquely
identify rows in a table. The combination of columns guarantees uniqueness,
though individually uniqueness is not guaranteed. Hence, they are combined to
uniquely identify records in a table.
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREThe difference between compound and the composite keys that any part of the
compound key can be a foreign key, but the composite key may or maybe not a
part of the foreign key.
What is a Surrogate key?
SURROGATE KEYS is An artificial key which aims to uniquely identify each
record is called a surrogate key. This kind of partial key in dbms is unique
because itis created when you don't have any natural primary key. They donot
lend any meaning to the data in the table. Surrogate key is usually an integer. A
surrogate key is a value generated right before the record is inserted into a
table.
Fname Lastname
Anne Smith
Jack Francis
Anna McLean
Shown Willam.
Start Time
09:00 18:00
08:00 17:00
11:00 20:00
14:00 23:00
Above, given example, shown shift timings of the different employee. In this
example, a surrogate key is needed to uniquely identify each employee.
Surrogate keys in sql are allowed when
+ No property has the parameter of the primary key.
+ Inthe table when the primary key is too big or complicated,
Difference Between Primary key & Foreign key
values.
Primary key Foreign Key
® Helps youto uniquely identifya| « Itisa fieldin the table thatis
recordin the table. the primary key of another
© Primary Key never accept null table.
* Aforeign key may accept
multiple null values.
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORE
End Time© Primary key isa clustered index
and data in the DBMS table are
physically organized in the
sequence of the clustered
index.
* Youcan have the single
Primary key ina table.
+ Aforeign key cannot
automatically create an index,
clustered or non-clustered.
However, you can manually
create an index on the foreign
key.
* You can have multiple foreign
keysin a table.
25. What is Relational Algebra? Explain different operations performed in relational
algebra with examples.
RELATIONAL ALGEBRA is a widely used procedural query language. It collects
instances of relations as input and gives occurrences of relations as output. It uses
various operations to perform this action. SQL Relational algebra query operations
are performed recursively on a relation. The output of these operations is a new
relation, which might be formed from one or more input relations.
Basic SQL Relational Algebra Operations
Relational Algebra devided in various groups
Unary Relational Operations
+ SELECT (symbol: 0)
+ PROJECT (symbol: n)
* RENAME (symbol: p)
Relational Algebra Operations From Set Theory
UNION (u)
+ INTERSECTION (),
DIFFERENCE (-)
CARTESIAN PRODUCT ( x)
Binary Relational Operations
+ JOIN
+ DIVISION
Let's study them in detail with solutions:
SELECT (a)
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREThe SELECT operation is used for selecting a subset of the tuples according to a
given selection condition. Sigma(o)Symbol denotes it. Its used as an
expression to choose tuples which meet the selection condition. Select operator
selects tuples that satisfy a given predicate.
ote)
cis the predicate
x stands for relation which is the name of the table
pis prepositional logic
Example 1
6 topic = natamser (Tutorials)
='Database’.
Output - Selects tuples from Tutorials where topi
Example 2
© topic = ataise nd ator «“guvsae( Tutordals)
Output- Selects tuples from Tutorials where the topic is ‘Database’ and ‘author’
is guru99.
Example 3
6 ates» soae (Customers)
Output- Selects tuples from Customers where sales is greater than 50000
Projection(m)
The projection eliminates all attributes of the input relation but those
mentioned in the projection list. The projection method defines a relation that
contains a vertical subset of Relation.
This helps to extract the values of specified attributes to eliminates duplicate
values. (pi) symbol is used to choose attributes from a relation. This operator
helps you to keep specific columns from a relation and discards the other
columns.
Example of Projection:
Consider the following table
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORE= CustomerName | Status
1 Google Active
2 Amazon Active
3 Apple
Here, the projection of CustomerName and status will give
1 cavtnenae, states (Custoners
CustomerName
Rename (p)
Rename isa unary operation used for renaming attributes of a relation.
p (a/b)R will rename the attribute ‘b’ of relation by ‘a’.
Union operation (vu)
UNION is symbolized by U symbol. It includes all tuples that are in tables A or in
B. Italso eliminates duplicate tuples. So, set A UNION set B would be expressed
as:
The result AUB
BCA STUDY MATERIAL 1VISIT WWWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREFor a union operation to be valid, the following conditions must hold -
+ Rand Smust be the same number of attributes.
+ Attribute domains need to be compatible.
+ Duplicate tuples should be automatically removed.
Example
Consider the following tables.
AUB ce
TableAUB
Set Difference (-)
- Symbol denotes it. The result of A- B, is a relation which includes all tuples that
are in Abut not inB.
+ The attribute name of A has to match with the attribute name in B.
BCA STUDY MATERIAL 1VISIT WWWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORE+ The two-operand relations A and B should be either compatible or Union
compatible.
+ Itshould be defined relation consisting of the tuples that are in relation A,
but notin B.
Example
AB
Table A-B
column 1 — | column2
Intersection
An intersection is defined by the symbol N.
ANB
Definesa relation consisting of a set of all tuple that are in both A and B.
However, A and B must be union-compatible.
O™
Only matching rows Visual
Definition of intersection
Example:
Ans
Table ANB
column 1 column 2
1 1
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORECartesian Product(X) in DBMS
Cartesian Product in DBMS is an operation used to merge columns from two
relations. Generally, a cartesian product is never a meaningful operation when it
performs alone. However, it becomes meaningful when itis followed by other
operations. Itis also called Cross Product or Cross Join.
Example - Cartesian product
© column 2= "(A X B)
Output- The above example shows all rows from relation A and B whose
column 2 has value 1
column 2="1' (AXB)
column 1 | column 2
1 1
1 1
Join Operations
Join operation is essentially a cartesian product followed by a selection
criterion.
Join operation denoted by ®.
JOIN operation also allows joining variously related tuples from different
relations.
Types of JOIN:
Various forms of join operation are:
Inner Joins:
+ Theta join
+ EQUI join
+ Naturaljoin
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREOuter join:
+ Left Outer Join
+ Right Outer Join
+ Full Outer Join
Inner Joi
In an inner join, only those tuples that satisfy the matching criteria are included,
while the rest are excluded. Let's study various types of Inner Joins:
Theta Join:
The general case of JOIN operation is called a Thetajoin. Itis denoted by
symbol®
Example
A seo 8
Theta join can use any conditions in the selection criteria.
For example:
A 0 cohen 2> s.cclom 2 (B)
Ave A.column 2 > B.column 2 (B)
column 1 column 2
1 2
EQUI join:
When a theta join uses only equivalence condition, it becomes a equi join.
For example:
A 00 column 2+ s.cctom 2 (B)
Ava A.column 2 = B.column 2 (B)
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREcolumn 1 | column 2
1 L
EQUI join is the most difficult operations to implement efficiently using SQLin
an RDBMS and one reason why RDBMS have essential performance problems.
NATURAL JOIN (03)
Natural join can only be performed if there isa common attribute (column)
between the relations. The name and type of the attribute must be same.
Example
Consider the following two tables
i 7
CuD
BCA STUDY MATERIAL 1VISIT WWWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORENum Square Cube
2 4 4
3 9 27
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria, we also
include some or all tuples that do not match the criteria.
Left Outer Join(A 2B)
In the left outer join, operation allows keeping all tuple in the left relation.
However, if there is no matching tuples found in right relation, then the
attributes of right relation in the join result are filled with null values.
Left Outer
OO*
All vows from Left Table.
Consider the following 2 Tables
A
Num Square
2 4
3 9
4 16
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORECube
Right Outer Join: (AXLB)
In the right outer join, operation allows keeping all tuple in the right relation.
However, if there is no matching tuple is found in the left relation, then the
attributes of the left relation in the join result are filled with null values.
BCA STUDY MATERIAL 1VISIT WWWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREAmB
Num | Cube Square
2 8 4
3
5
Full Outer Join: ( A> B)
In a full outer join, all tuples from both relations are included in the result,
irrespective of the matching condition.
BCA STUDY MATERIAL 1VISIT WWWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORE26. write relational algebra for the following relations:
Employee
Emp id | Name ‘Address Salary Dept_id
Department
Dept_id Dept_Name Floor
i, Select name and address of employees whose salary is between 10000 and
20000.
ji, Select employee id, employee name and department name of employees
‘working in first floor.
ili, Select all records of department which are in second floor.
Select name, address and department name of employees which are from
Birtamode.
v. Select employee id and name of employees having salary more than 10000
‘and from Kathmandu.
Unit-1V
27. What is Normalization? Explain advantages and disadvantages of Normalization.
ADVANTAGES OF NORMALIZATION and Types of normal form?
‘Normalization
Normalization is the process of organizing the data in the database.
Normalization is used to minimize the redundancy from a relation or set of relations.
It is also used to eliminate the undesirable characteristics like Insertion, Update and
Deletion Anomalies.
© Normalization divides the larger table into the smaller table and links them using
relationship.
© The normal form is used to reduce redundancy from the database table.
Here we can see why normalization is an attractive prospect in RDBMS concepts.
1) A smaller database can be maintained as normalization eliminates the duplicate
data. Overall size of the database is reduced as a result.
2) Better performance is ensured which can be linked to the above point. As,
databases become lesser in size, the passes through the data becomes faster and
shorter thereby improving response time and speed.
3) Narrower tables are possible as normalized tables will be fine-tuned and will
have lesser columns which allows for more data records per page.
4) Fewer indexes per table ensures faster maintenance tasks (index rebuilds).
5) Also realizes the option of joining only the tables that are needed.
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREDISADVANTAGES OF NORMALIZATION
1) More tables to join as by spreading out data into more tables, the need to join
tables increases and the task becomes more tedious. The database becomes
harder to realize as well.
2) Tables will contain codes rather than real data as the repeated date will be
stored as lines of codes rather than the true data. Therefore, there is always @
need to go to the lookup table.
3) Data model becomes extremely difficult to query against as the data model is
‘optimized for applications, not for ad hoc querying. (Ad hoc query is a query that
cannot be determined before the issuance of the query. It consists of an SQL that
is constructed dynamically and is usually constructed by desktop friendly query
tools.). Hence it Is hard to model the database without knowing what the
customer desires.
4) As the normal form type progresses, the performance becomes slower and
slower.
'5) Proper knowledge is required on the various normal forms to execute the
normalization process efficiently. Careless use may lead to terrible design filled
with major anomalies and data inconsistency.
Types of Normal Forms
There are the four types of normal forms:
Normal Form
ANE
g
I
i
2
Description
A relation is in 1NF if it contains an atomic value.
A relation will be it i
attributes are fully functional dependent on the primary key.
2NF
NF and all non-key
A relation will be in 3NF if it is in 2NF and no transition dependency exists.
A relation will be in 4NF if it is in Boyce Codd
normal form and has no multi-valued dependency.
A relation is in SNF if it is in 4NF and not contains any join dependency and
Joining should be lossless.
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORE‘Normal Forms
28, What are differernt anomalies? Explain each of them with suitable examples.
2. What is functional dependency? Explain different types of functional
dependencies with example.
What is Functional Dependency?
Functional Dependency (FD) is a constraint that determines the relation of one
attribute to another attribute in a Database Management System (DBMS).
Functional Dependency helps to maintain the quality of data in the database. It
plays a vital role to find the difference between good and bad database design.
A functional dependency is denoted by an arrow ">". The functional
dependency of X on Yis represented by X > Y. Let's understand Functional
Dependency in DBMS with example.
Example:
Employee number Employee Name
1 Dana
2 Francis
3 Andrew
Salary
50000
38000
25000
City
San Francisco
London
Tokyo
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREIn this example, ifwe know the value of Employee number, we can obtain
Employee Name, city, salary, etc. By this, we can say that the city, Employee
Name, and salary are functionally depended on Employee number.
Rules of Functional Dependencies
Below are the Three most important rules for Functional Dependency in
Database:
+ Reflexive rule -. IfXisa set of attributes and Yis_subset_of x, then x holds
avalue of ¥.
+ Augmentation rule: When x->y holds, and c is attribute set, then ac-> be
also holds. That is adding attributes which do not change the basic
dependencies.
+ Transitivity rule: This rule is very much similar to the transitive rule in
algebraifx->y holds and y > z holds, then x > z also holds. X->y is called
as functionally that determines y.
Types of Functional Dependencies in DBMS
There are mainly four types of Functional Dependency in DBMS. Following are
the types of Functional Dependencies in DBMS:
+ Multivalued Dependency
+ Trivial Functional Dependency
+ Non-Trivial Functional Dependency
+ Transitive Dependency
Multivalued Dependency in DBMS
Multivalued dependency occurs in the situation where there are multiple
independent multivalued attributes in a single table. A multivalued dependency
is a complete constraint between two sets of attributes in a relation. It requires
that certain tuples be present in a relation. Consider the following Multivalued
Dependency Example to understand.
Example:
Car_model Maf_year Color
HOO1 2017 Metallic
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREHOO1 2017 Green
Hoos 2018 Metallic
HOS 2018 Blue
HOLO 2015 Metallic
Ho33 2012 Gray
In this example, maf_year and color are independent of each other but
dependent on car_model. In this example, these two columns are said to be
multivalue dependent on car_model.
This dependence can be represented like this:
car_model-> maf_year
car_model-> colour
Trivial Functional Dependency in DBMS
The Trivial dependency is a set of attributes which are called a trivial if the set of
attributes are included in that attribute.
So, X->Y isa trivial functional dependency if Y isa subset of X. Let's understand
with a Trivial Functional Dependency Example.
For example:
Emp_id Emp_name
ASS5S Harry
AS81L George
S999 Kevin
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREConsider this table of with two columns Emp_id and Emp_name.
{Emp_id, Emp_name} -> Emp_id isa trivial functional dependency as Emp.
a subset of (Emp_id,Emp_name}.
Non Trivial Functional Dependency in DBMS
Functional dependency which also known as a nontrivial dependency occurs
when AB holds true where B is not a subset of A. In a relationship, if attribute B
is not a subset of attribute A, then it is considered as a non-trivial dependency.
Company CEO Age
Microsoft Satya Nadella 51
Google Sundar Pichai 46
Apple Tim Cook 87
Exampl
(Company}-> {CEO} (if we know the Company, we knows the CEO name)
But CEO is nota subset of Company, and hence it's non-tri
dependency.
Transitive Dependency in DBMS
A Transitive Dependency is a type of functional dependency which happens
when tis indirectly formed by two functional dependencies. Let's understand
with the following Transitive Dependency Example.
Example:
Company CEO Age
Microsoft Satya Nadella 51
Google Sundar Pichai 46
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREAlibaba Jack Ma 54
{Company} > {CEO} (if we know the compay, we know its CEO's name)
{CEO } -> {Age} If we know the CEO, we know the Age
Therefore according to the rule of rule of transitive dependency:
{Company} -> {Age} should hold, that makes sense because if we know the
company name, we can know his age.
Note: You need to remember that transitive dependency can only occur in a
relation of three or more attributes.
What is Normalization?
Normalization is a method of organizing the data in the database which helps
youto avoid data redundancy, insertion, update & deletion anomaly. It is a
process of analyzing the relation schemas based on their different functional
dependencies and primary key.
Normalization is inherent to relational database theory, It may have the effect of
duplicating the same data within the database which may result in the creation
of additional tables.
Advantages of Functional Dependency
+ Functional Dependency avoids data redundancy. Therefore same data do
not repeat at multiple locations in that database
It helps you to maintain the quality of data in the database
+ Ithelps you to defined meanings and constraints of databases
+ Ithelps you to identify bad designs
+ Ithelps you to find the facts regarding the database design
30. Explain INF, 2NF, 3NF, BCNF and 4NF with suitable example
‘What is Normalization?
Normalization is a database design technique that reduces data redundancy and
eliminates undesirable characteristics like Insertion, Update and Deletion Anomalies.
Normalization rules divides larger tables into smaller tables and links them using
relationships. The purpose of Normalization in SQL is to eliminate redundant
(repetitive) data and ensure data is stored logically.
The inventor of the relational model Edgar Codd proposed the theory of
normalization of data with the introduction of the First Normal Form, and he
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTOREcontinued to extend theory with Second and Third Normal Form. Later he joined
Raymond F. Boyce to develop the theory of Boyce-Codd Normal Form.
Database Normal Forms
Here is a list of Normal Forms
+ NF (First Normal Form)
+ 2NF (Second Normal Form)
‘© 3NF (Third Normal Form)
+ BCNF (Boyce-Codd Normal Form)
+ 4NF (Fourth Normal Form)
+ SNF (Fifth Normal Form)
# _ 6NF (Sixth Normal Form)
‘The Theory of Data Normalization in SQL server is still being developed further. For
example, there are discussions even on 6" Normal Form. However, in most practical
applications, normalization achieves its best in 3° Normal Form. The evolution of
SQL Normalization theories is illustrated below-
Database Normal Forms
Database Normalization With Examples
Database Normalization Example can be easily understood with the help of 2 case
study. Assume, a video library maintains a database of movies rented out. Without
any normalization in database, all information is stored in one table as shown below.
Let's understand Normalization in database with tables example:
Here you see Movies Rented column has multiple values. Now let's move into 1st
Normal Forms:
:1NF (First Normal Form) Rules
‘+ Each table cell should contain a single value.
‘+ Each record needs to be unique.
The above table in INF-
NF Example
Before we proceed let's understand a few things ~
What is a KEY?
A KEY isa value used to identify a record in a table uniquely. A KEY could be a single
column or combination of multiple columns
Note: Columns in a table that are NOT used to identify a record uniquely are called
non-key columns.
What is a Primary Key?
Primary Key
‘A primaryis a single column value used to identify a database record uniquely.
It has following attributes
+ Aprimary key cannot be NULL
[BCA STUDY MATERIAL 1VISIT WWW.BCANOTESNEPAL.COM AND ALSO APP AVALIABLE ON PLAYSTORE