Sunday, August 26, 2012

DBMS - Relational Model

Relational Model was proposed by E.F. Codd to model data in the form of relations or tables. After designing the conceptual model of Database using ER diagram, we need to convert the conceptual model in the relational model which can be implemented using any RDMBS languages like Oracle SQL, MySQL etc. So we will see what Relational Model is.
What is Relational Model?
Relational Model represents how data is stored in Relational Databases.  A relational database stores data in the form of relations (tables). Consider a relation STUDENT with attributes ROLL_NO, NAME, ADDRESS, PHONE and AGE shown in Table 1.
STUDENT
ROLL_NONAMEADDRESSPHONEAGE
1RAMDELHI945512345118
2RAMESHGURGAON965243154318
3SUJITROHTAK915625313120
4SURESHDELHI18
TERMINOLOGIES
  1. Attribute: Attributes are the properties that define a relation. e.g.; ROLL_NONAME
  2. Relation Schema: A relation schema represents the name of the relation with its attributes. e.g.; STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is relation schema for STUDENT. If a schema has more than 1 relation, it is called Relational Schema.
  3. Tuple: Each row in the relation is known as a tuple. The above relation contains 4 tuples, one of which is shown as:
1RAMDELHI945512345118
  1. Relation Instance: The set of tuples of a relation at a particular instance of time is called as relation instance. Table 1 shows the relation instance of STUDENT at a particular time. It can change whenever there is an insertion, deletion or updation in the database.
  2. Degree: The number of attributes in the relation is known as the degree of the relation. The STUDENT relation defined above has degree 5.
  3. Cardinality: The number of tuples in a relation is known as cardinality. The STUDENT relation defined above has cardinality 4.
  4. Column: Column represents the set of values for a particular attribute. The column ROLL_NO is extracted from relation STUDENT.
ROLL_NO
1
2
3
4
  1. NULL Values: The value which is not known or unavailable is called NULL value. It is represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.
CONSTRAINTS
While designing Relational Model, we define some conditions which must hold for data present in the database are called Constraints. These constraints are checked before performing any operation (insertion, deletion, and updation) in the database. If there is a violation in any of constraints, the operation will fail.
Domain Constraints: These are attribute level constraints. An attribute can only take values which lie inside the domain range. e.g,; If a constraint AGE>0 is applied on STUDENT relation, inserting a negative value of AGE will result in failure.
Key Integrity: Every relation in the database should have at least one set of attributes which defines a tuple uniquely. Those set of attributes is called key. e.g.; ROLL_NO in STUDENT is a key. No two students can have the same roll number. So a key has two properties:
  • It should be unique for all tuples.
  • It can’t have NULL values.
Referential Integrity: When one attribute of a relation can only take values from another attribute of same relation or any other relation, it is called referential integrity. Let us suppose we have 2 relations,
STUDENT
ROLL_NONAMEADDRESSPHONEAGEBRANCH_CODE
1RAMDELHI945512345118CS
2RAMESHGURGAON965243154318CS
3SUJITROHTAK915625313120ECE
4SURESHDELHI18IT
 BRANCH
BRANCH_CODEBRANCH_NAME
CSCOMPUTER SCIENCE
ITINFORMATION TECHNOLOGY
ECEELECTRONICS AND COMMUNICATION ENGINEERING
CVCIVIL ENGINEERING
BRANCH_CODE of STUDENT can only take the values which are present in BRANCH_CODE of BRANCH which is called referential integrity constraint. The relation which is referencing to other relation is called REFERENCING RELATION (STUDENT in this case) and the relation to which other relations refer is called REFERENCED RELATION (BRANCH in this case).
ANOMALIES
An anomaly is an irregularity or something which deviates from the expected or normal state. When designing databases, we identify three types of anomalies: Insert, Update, and Delete.
Insertion Anomaly in Referencing Relation:
We can’t insert a row in REFERENCING RELATION if referencing attribute’s value is not present in referenced attribute value. e.g.; Insertion of a student with BRANCH_CODE ‘ME’ in STUDENT relation will result in error because ‘ME’ is not present in BRANCH_CODE of BRANCH.
Deletion/Updation Anomaly in Referenced Relation:
We can’t delete or update a row from REFERENCED RELATION if the value of REFERENCED ATTRIBUTE is used in the value of REFERENCING ATTRIBUTE. e.g; if we try to delete tuple from BRANCH having BRANCH_CODE ‘CS’, it will result in error because ‘CS’ is referenced by BRANCH_CODE of STUDENT, but if we try to delete the row from BRANCH with BRANCH_CODE CV, it will be deleted as the value is not been used by referencing relation. It can be handled by following method:
ON DELETE CASCADE: It will delete the tuples from REFERENCING RELATION if  the value used by REFERENCING ATTRIBUTE is deleted from REFERENCED RELATION. e.g;, if we delete a row from BRANCH with BRANCH_CODE ‘CS’, the rows in STUDENT relation with BRANCH_CODE CS (ROLL_NO 1 and 2 in this case) will be deleted.
ON UPDATE CASCADE: It will update the REFERENCING ATTRIBUTE in REFERENCING RELATION if attribute value used by REFERENCING ATTRIBUTE is updated in REFERENCED RELATION. e.g;, if we update a row from BRANCH with BRANCH_CODE ‘CS’ to ‘CSE’, the rows in STUDENT relation with BRANCH_CODE CS (ROLL_NO 1 and 2 in this case) will be updated with BRANCH_CODE ‘CSE’.

Saturday, August 25, 2012

DBMS - Introduction

Database Management System is a system software for easy, efficient and reliable data processing and management. It can be used for:
  • Creation of a database.
  • Retrieval of information from the database.
  • Updating the database.
  • Managing a database.

  • It provides us with the many functionalities and is more advantageous than the traditional file system in many ways listed below:
    1) Processing Queries and Object Management:
    In traditional file systems, we cannot store data in the form of objects. In practical world applications, data is stored in objects and not files. So in a file system, some application software maps the data stored in files to objects so that can be used further.
    We can directly store data in the form of objects in a database management system. Application level code needs to be written to handle, store and scan through the data in a file system whereas a DBMS gives us the ability to query the database.
    2) Controlling redundancy and inconsistency:
    Redundancy refers to repeated instances of the same data. A database system provides redundancy control whereas in a file system, same data may be stored multiple times. For example, if a student is studying two different educational programs in the same college, say ,Engineering and History, then his information such as the phone number and address may be stored multiple times, once in Engineering dept and the other in History dept. Therefore, it increases time taken to access and store data. This may also lead to inconsistent data states in both places. A DBMS uses data normalization to avoid redundancy and duplicates.
    3) Efficient memory management and indexing:
    DBMS makes complex memory management easy to handle. In file systems, files are indexed in place of objects so query operations require entire file scans whereas in a DBMS , object indexing takes place efficiently through database schema based on any attribute of the data or a data-property. This helps in fast retrieval of data based on the indexed attribute.
    4) Concurrency control and transaction management:
    Several applications allow user to simultaneously access data. This may lead to inconsistency in data in case files are used. Consider two withdrawal transactions X and Y in which an amount of 100 and 200 is withdrawn from an account A initially containing 1000. Now since these transactions are taking place simultaneously, different transactions may update the account differently. X reads 1000, debits 100, updates the account A to 900, whereas X also reads 1000, debits 200, updates A to 800. In both cases account A has wrong information. This results in data inconsistency. A DBMS provides mechanisms to deal with this kind of data inconsistency while allowing users to access data concurrently. A DBMS implements ACID (Atomicity, Consistency, Isolation, Durability) properties to ensure efficient transaction management without data corruption.
    5) Access Control and ease in accessing data:
    A DBMS can grant access to various users and determine which part and how much of the data can they access from the database thus removing redundancy. Otherwise in file system, separate files have to be created for each user containing the amount of data that they can access. Moreover, if a user has to extract specific data, then he needs a code/application to process that task in case of file system, e.g. Suppose a manager needs a list of all employees having salary greater than X. Then we need to write business logic for the same in case data is stored in files. In case of DBMS, it provides easy access of data through queries, (e.g., SELECT queries) and whole logic need not be rewritten. Users can specify exactly what they want to extract out of the data.
    6) Integrity constraints: Data stored in databases must satisfy integrity constraints. For example, Consider a database schema consisting of the various educational programs offered by a university such as(B.Tech/M.Tech/B.Sc/M.Sc/BCA/MCA) etc. Then we have a schema of students enrolled in these programs. A DBMS ensures that it is only out of one of the programs offered schema , that the student is enrolled in, i.e. Not anything out of the blue. Hence, database integrity is preserved.
    Apart from the above mentioned features a database management also provides the following:
    • Multiple User Interface
    • Data scalability, expandability and flexibility: We can change schema of the database, all schema will be updated according to it.
    • Overall the time for developing an application is reduced.
    • Security: Simplifies data storage as it is possible to assign security permissions allowing restricted access to data.