Showing posts with label algebra. Show all posts
Showing posts with label algebra. Show all posts

Tuesday, March 30, 2021

Set-Difference Operations in RDBMS - an explanatory blog

   


            Set-Difference Operations in RDBMS

 

* The Set-Difference operation , denoted by "-" allows us to find the tuples that are in one relation but not in another .

 

* The expression , ( r - s ) produces a relation containing those tuples in r but not in s .

 

* One can find all the customers of the bank who have an account but not a loan by writing in the below manner :

 



* In the case of the Union Operation , one must ensure that the set differences are taken between compatible relations .

 

* For a set-difference operation where "r-s" to be valid , one requires that the relations r and s be of the same arity , and that the domains of the ith attribute of r and the ith attribute of s should be the same .

Wednesday, March 17, 2021

Cartesian Product Operation in SQL and RDBMS - concept discussion with example



  Cartesian Product Operation

 

* The Cartesian product operation is denoted by a cross ( X ) which allows us to combine information from any two relations .

 

* One can write the Cartesian Product of Relations R1 and R2 as R1 x R2

 

* A relation is by definition a subset of a Cartesian product of a set of domains .

 

* From the definition , one can have an intuition about the definition of the Cartesian product operation .

 

* Since the same attribute name may appear in both R1 and R2 , one may need to devise a naming schema to distinguish between the attributes .

 

* One can do the attachment to an attribute , the name of the relation from which the attribute originally came from .

 

* For example , the relation schema for

r = borrower * loan is :

 

(

borrower.customer_name ,

borrower.loan_number,

loan.loan_number ,

loan.branch_name ,

loan.branch_name ,

loan.amount

)

 

* With the schema , one can distinguish between borrower.loan_number from loan.loan_number as both the attributes do have the same name but the main relational table for the table are different which are loan and borrower

 

* The naming convention for any of the relations or schemas requires that the relation should have distinct names

 

* In general , if we have two relations r1(R1) and r2(R2) , then r1 x r2 is a relation whose schema is the concatenation of relations R1 and R2 .

 

Relation R contains all the tuples t for which there is a tuple t1 in relation r1 ; and a tuple t2 in r2 for which t|R1| = t|R1| and t|R2| = t2|R2|

 

* Suppose we want to find the names of all the customers who have a loan at the "PerryRidge" branch , then one may need the information in both the loan relation and the borrower relation through the given selection statement .

 

<< FIG - 01 >>

 



 

 From the given statement , one can find that a selection of attribute "branch-name" can be done with a relation existing from the relational tables for borrower and the loan relational table but given that the branch_name is "PerryRidge" over here .

 

* From the above relation , if one wants to find if there is a cartesian product operation that associates every tuple of loan with every tuple of borrower , with the customer having a loan in the “PerryRidge” branch , then there is some tuple in borrower x loan that contains the name of the customers which can be obtained by

criterion / condition from one table borrower to table loan where borrower.loan_number = loan.loan_number

 

Union Operation in RDBMS - Fundamental Relational Algebra Concept

 


            Union Operation - RDBMS

        Fundamental Relational Algebra Concept

=======================================

* Scenario where a Union Operation over a rdbms table could be used : -

Consider a query to find the names of all the bank customers who have either an account or a loan or both in a bank .

 

* To find the names of the customer who have an account and also a deposit account in the bank , the search query would consider the search to take over "depositor" relation table and the "borrower" relation table .

 

* In order to find out the names of all the customers with a loan in the bank , the query that could be used for the operation would be :

 

{figure-01}

The upper relational equation is a form of projection which allows us to produce the relevant relation that returns the argument relation specified within the parenthesis . So as we are interested in finding out the names of the customers who have a loan account , the

relevant result would be fetched from the projection relation as mentioned in the above statement .

 

* Similarly , if we want to know the names of all the customers with an account in the bank , then it can be expressed in the following projection equation :

 




{figure-02}

 

 

* Therefore , in order to find the answer to the raised question in our scenario that is discussed in the opening statement , one needs to do a "Union" operation over the two sets which is : we need all the customer names that appear in either both or two of the relations which can be found out by using the binary operation Union upon both the queries . The relevant relational equation can be represented in the given manner :


 
    {figure-03}

 

* The resulting relation for the query would result in a relation with the relevant tuples from both the tables

 

* The resulting relations are sets from which duplicate values are eliminated .

 

In Hindsight :

In our example , we took the union of two sets , both of which consisted of the attribute "customer_names" values . In general , one must ensure that unions are taken between compatible relations which would generate the appropriate results without duplicates.

 

* Points to note when using a Union Operation over relational sets :

 

If r is a set and s is set , and one needs to find { r U s } , If r is a set and s is set , and one needs to find { r U s } , then the conditions that should satisfy and hold for both relations

1)        The relations r and s must be of the same type and they must have the same number of attributes

 

2)   The domains of the ith attribute of r and ith attribute of s must be the same for all values of i . One may note that , both r and s are either database relations or temporary relations that are the result of relational algebra expressions as given in figure 1 and figure 2 of the article .

Monday, March 15, 2021

Classes and Objects in OOPS - a short introduction in Python

 


     Classes and Objects in OOPS

 

* A Class is a model or plan to create objects . This means , one can write a class with the attributes and actions of objects .

 

* Attributes are represented by variables and actions are performed by methods. Therefore , a class contains both variables and methods .

 

* The same variables and methods are also available within the objects because the objects are created from the class to which the variables and methods belong.

 

* These variables are also called as 'instance variables' because these instance variables can be created inside an instance like an object .

 

* One should remember the difference between a function and a method. A function which is written inside a class is called as a Method.

 

* Generally, a Method is called using one of the following two ways :

1) classname.methodname()

2) instancename.methodname()

 

 

* The general format of a class is given in the following manner:

Class Classname(object):

def _init_(self):

def method01():

def method02():

 

Friday, March 12, 2021

Creating Classes and Objects in Python



     Creating Classes and Objects  in Python

 

* One can create a class "Person" in which "Andy" and "Bandy" are the names of the objects within the class

 

* A Class can be created by using the keyword "class" infront of the name of the class that one is trying to make .

 

* A Class describes the attributes and actions performed by the objects of the class . Therefore , one can write the attributes ( Variables ) and actions ( Functions ) in the class as :

 ===================================================

# This is a Class with the name Person attributes means variables

  Class Person:

  name = 'Andy'

  age = 20

# actions means functions

  def talk(cls):

        print(cls.name)

        print(cls.age)


  

* One can observe from the given code that the Person Class has two variables and one function . The function that is written in the class is called a Method .

 

* If someone wants to use this Class , then one should create an Object for the class first in the given manner .

 p1 = Person()

 Here , p1 is an object of the Person Class . Object 'p1' represents memory to store the actual data . The memory needed to create object 'p1' is provided by the PVM . One may be able to observe the function / method in the class in the given manner:

 

def talk(cls):

 

Here , 'cls' represents a default parameter that indicates the class of the object . Therefore , cls.name refers to the class variable 'Andy' . One can call the "talk()" method to display the object's details as :


p1.talk()

===========================================================

Friday, March 5, 2021

Dictionary Methods in Python for processing of constituent Key and Value Pair Data Objects

 

Dictionary Methods in Python


* There are various methods to process the elements of a dictionary

 * These methods are used for retrieving and manipulating the contents of a dictionary

 * These various methods have been summarised in the given description table


 ===================================

Methods to process Dictionaries in Python

===================================

 1) clear()

dict.clear()

This method removes all the key-value pairs from the dictionary 'd' .

 

2) copy()

d1 = dict.copy()

This method copies all the elements from the dictionary 'd' into a new dictionary object 'd1' .

 

3) fromkeys()

dict.fromkeys(s[,v])

This creates a new dictionary with keys from a given sequence 's' and all the values set to 'v'

 

4) get()

dict.get(k[,v])

This returns all the values associated with the key 'k' . If the key is not found , then it returns 'v'

 

5) items()

dict.items()

This returns an object that contains key-value pairs of dictionary item 'dict' . The key value pairs are stored as tuples in the objects .

 

6) keys()

dict.keys()

This returns a sequence of keys from the dictionary object "dict" .

 

7) values()

dict.values()

This returns a sequence of values from the dictionary object "dict"

 

8) update()

dict.update(x)

This method adds all elements from the dictionary 'x' to the dictionary item object 'dict'

 

9) pop()

dict.pop(k[,v])

This method removes the key 'k' and its associated value from the dictionary object "dict" and returns the value associated with that key "v" is returned . If the key is not found and "v" is not mentioned then "KeyError" exception error is raised .

 

10) setdefault()

dict.setdefault(k[,v])

If the key 'k' is found , then its value is returned and if the key is not found , then the k,v pair is stored into the dictionary 'd'. In the given program , we are going to retrieve the keys from a dictionary object using the keys() method . The keys() method returns dict_keys object that contains only keys . We will be also able to retrieve the values from the dictionary object using the values() method . This method "values()" returns all the values in the form of a dict_values object . Similarly , the items() method can be used to retrieve all the key-value pairs into the "dict_items"

method can be used to retrieve all the key-value pairs into the "dict_items" object .

 

==============================================

 

Program

A Python program to retrieve keys , values and key-value pairs from a dictionary object

# dictionary methods - create a dictionary with employee details

dict =

{

  'Name' : 'D001' ,

  'Id' : '001',

  'Salary' : 1000

}

 

# print the entire dictionary

print(dict)

# display only keys

print(" values in dict = ", dict.values())

# display both key and value pairs as tuples

print(" Items in Dictionary =", dict.items())

 

Output :

{

   'Name' : 'D001' ,

   'Id' : '001',

   'Salary' : 1000

}

Keys in dict =dict_keys(['Name','Id','Salary'])

Values in dict = dict_values(['D001','001','1000'])

Items in dict = dict_items([('Name','D001'),('Id',001),('Salary',1000)])

 

In the given program, we are creating a dictionary by entering the elements from the keyboard and when the user enters the elements from the keyboard inside the curly braces , then the values inside the dictionary object are treated as key value pairs of a dictionary by using the eval() function . Once the elements are entered , one can find the sum of the values using the sum() function over the values of the dictionary .


The screenshot for the used code implemented over Jupyter Notebook is given as below :



An article on - Dictionaries in Python ( with example code and result of Dictionary Creation )

                         Dictionaries in Python


 

* A dictionary represents a group of elements arranged in the form of key-value pairs .

 

* In the dictionary , the first element is considered as "key" and the immediate next element is considered as the "value"

 

* The key and the associated value are separated by the help of a colon (:) operator

 

* All the key-value paris in a dictionary are inserted in curly braces { }

 

* One can take a dictionary by the name "dict" that contains the employee details in the following manner :

 

dict_01 =

{

'Name' : 'Gareeb' ,

'Id' : 001 ,

'Salary' : 0000

}

 

* The name of the dictionary item object in the given example is "dict_01"

 

* The first element in the dictionary is a string object called "Name" and its associated value is "Gareeb" .

The second item in the element is "Id" and its associated value is "001" . The third object in the dictionary object is "Salary" and its associated value is "0000"

 

* The above example is that of a dictionary object with its identifier name as "dict_01" . The dictionary item object is holding three values within it in the form of key and value pair .

 

* From this , we can get to see that there are 3 pairs of keys and values in the dictionary which can be shown in the below figure .


  

 

 

* To briefly explain the process of creation of a dictionary object , one can write a

program in the given manner :

 

# creating dictionary with key-value pairs

dict_01 =

 {

    'Name' : 'Gareeb' ,

    'Id':001 ,

    'Salary':0000

}

# Here a dictionary object is created with the following items in the form of

# key-value pairs - Name , Id and Salary

# If someone wants to retrieve the requisite values from the dictionary then one

# can do the following to retrieve the necessary objects

print(' Name of Employee ',dict_01['Name'])

print(' Id of Employee ',dict_01['Id'])

print(' Salary ',dict_01['Salary'])

 

The Code and Output for the same can be seen in the below given screenshot: