Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Monday, 18 May 2015

What is Database?

A database is a systematic collection of data. Databases support storage and  manipulation of data. Databases make data management easy. Let's discuss few examples.
An online telephone directory would definitely use database to store data pertaining to people, phone numbers, other contact details, etc.
Your electricity service provider is obviously using a database to manage billing , client related issues, to handle fault data, etc.
Let's also consider the facebook. It needs to store, manipulate and present data related to members, their friends, member activities, messages, advertisements and lot more.
We can provide countless number of examples for usage of databases .

What is a Database Management System (DBMS)

Database Management System (DBMS) is a collection of programs which enables its users to access database, manipulate data, reporting / representation of  data . It also helps to control access to the  database.
Database Management Systems are not a new concept and as such had been first implemented in 1960s. Charles Bachmen's Integrated Data Store(IDS) is said to be the first DBMS in history. With time database technologies evolved a lot while usage and expected functionalities of databases have been increased immensely.

Types of DBMS

Let's see how the DBMS family got evolved with the time. Following diagram shows the evolution of DBMS categories.
alt
There are 4 major types of DBMS. Let's look into them in detail.
  • Hierarchical - this type of DBMS employs the "parent-child" relationship of storing data. This type of DBMS is rarely used nowadays. Its structure is like a tree with nodes representing records and branches representing fields. The windows registry used in Windows XP is an example of a hierarchical database. Configuration settings are stored as tree structures with nodes.
  • Network DBMS - this type of DBMS supports many-to many relations. This usually results in complex database structures.  RDM Server is an example of a database management system that implements the network model.
  • Relational DBMS - this type of DBMS defines database relationships in form of tables, also known as relations. Unlike network DBMS, RDBMS does not support many to many relationships.Relational DBMS usually have pre-defined data types that they can support. This is the most popular DBMS type in the market. Examples of relational database management systems include MySQL, Oracle, and Microsoft SQL Server.
  • Object Oriented Relation DBMS - this type supports storage of new data types. The data to be stored is in form of objects. The objects to be stored in the database have attributes (i.e. gender, ager) and methods that define what to do with the data. PostgreSQL is an example of an object oriented relational DBMS.
 
1.What is Structured Query language (SOL)




Structured Query language (SQL) pronounced as "S-Q-L" or sometimes as "See-Quel"is actually the standard language for dealing with Relational Databases. SQL can be effectively used to insert, search, update, delete database records. That doesn't mean SQL cannot do things beyond that. In fact it can do lot of things including, but not limited to ,optimizing and maintenance of databases.  Relational databases like MySQL, Oracle, Ms SQL server, Sybase, etc uses SQL !  SQL syntaxes used in these databases are almost similar, except the fact that some are using few different syntaxes and even proprietary SQL syntaxes.

What is NoSQL ?

NoSQL is an upcoming category of Database Management Systems..Its main characteristic is its non-adherence to Relational Database Concepts. NOSQL means "Not only SQL".
Concept of NoSQL databases grew with internet giants such as Google, Facebook, Amazon etc who deal with gigantic volumes of data.When you use relational database for massive volumes of data , the system starts getting slow in terms of response time. To overcome this , we could of course "scale up" our systems by upgrading our existing hardware. The alternative to the above problem would be to distribute our database load on multiple hosts as the load increases. This is known as "scaling out".
 NOSQL database are non-relational databases that scale out better than relational databases and are designed with web applications in mind. They do not use SQL to query the data and do not follow strict schemas like relational models.With NoSQL, ACID (Atomicity, Consistency, Isolation, Durability) features are not guaranteed always.

Relational databases have the following advantages over NOSQL databases;
  • SQL(relational) databases have a mature data storage and management model . This is crucial for enterprise users.
  • SQL databases support the notion of views which allow users to only see data that they are authorized to view. The data that they are not authorized to see is kept hidden from them.
  • SQL databases support stored procedures which allow database developers to implement part of the business logic into the database.
  • SQL databases have better security models compared to NoSQL databases.


What is Normalization?

Normalization is a database design technique which organizes tables in a manner that reduces redundancy and dependency of data.
It divides larger tables to smaller tables and link them using relationships.
The inventor of the relational model Edgar Codd proposed the theory of normalization with the introduction of First Normal Form and he continued to extend theory with Second and Third Normal Form. Later he joined with Raymond F. Boyce  to develop the theory of Boyce-Codd Normal Form.
Theory of Normalization is still being developed further. For example there are discussions even on 6th Normal Form. But in most practical applications normalization achieves its best in 3rd Normal Form. The evolution of Normalization theories is illustrated below-

alt



Let's learn Normalization with practical example -
Assume a video library maintains a database of movies rented out. Without any normalization all information is stored in one table as shown below.

alt
Table 1

Here you see Movies  Rented column has multiple values.
Now let's move in to 1st Normal Form

1NF Rules

  • Each table cell should contain single value.
  • Each record needs to be unique.
The above table in 1NF-
alt
Table 1 : In 1NF Form
Before we proceed lets understand a few things --

What is a KEY ?

A KEY  is a value used to uniquely identify a record in a table. A KEY could be a single column or combination of multiple columns
Note: Columns in a table that are NOT used to uniquely identify a record are called non-key columns.

What is a primary Key?

A primary is a single column values used to uniquely identify a database record.
It has following attributes
  • A primary key cannot be NULL
  • A primary key value must be unique
  • The primary key values can not be changed
  • The primary key must be given a value when a new record is inserted.

What is a composite Key?

A composite key is a primary key composed of multiple columns used to identify a record uniquely
In our database , we have two people with the same name Robert Phil but they live at different places.
alt
Hence we require both Full Name and Address to uniquely identify a record. This is a composite key.
Let's move into 2NF
2NF Rules
  • Rule 1- Be in 1NF
  • Rule 2- Single Column Primary Key
It is clear that we can't move forward to make our simple database in 2nd Normalization form unless we partition the table above.
alt
Table 1

alt
Table 2
We have divided our 1NF table into two tables viz. Table 1 and Table2. Table 1 contains member information. Table 2 contains information on movies rented.
We have introduced a new column called Membership_id which is the primary key for table 1. Records can be uniquely identified in Table 1 using membership id

Introducing Foreign Key!

In Table 2, Membership_ID is the foreign Key

alt


Foreign Key references primary key of another Table!It helps connect your Tables

  • A foreign key can have a different name from its primary key
  • It ensures rows in one table have corresponding rows in another
  • Unlike Primary key they do not have to be unique. Most often they aren't
  • Foreign keys can be null even though primary keys can not 
alt
Why do you need a foreign key ?
Suppose an idiot inserts a record in Table B such as
You will only be able to insert values into your foreign key that exist in the unique key in the parent table. This helps in referential integrity.
alt
The above problem can be overcome by declaring membership id  from Table2  as foreign key of membership id  from Table1
Now , if somebody tries to insert a value in the membership id  field that does not exist in the parent table , an error will be shown!

What is a transitive functional dependencies?

A transitive functional dependency is when changing a non-key column , might cause any of the other non-key columns to change
Consider the table 1. Changing the non-key column Full Name , may change Salutation.
alt
Let's move into 3NF

3NF Rules

  • Rule 1- Be in 2NF
  • Rule 2- Has no transitive functional dependencies
To move our 2NF table into 3NF we again need to need divide our table.
alt
TABLE 1

alt
Table 2

alt
Table 3
We have again divided our tables and created a new table which stores Salutations.
There are no transitive functional dependencies and hence our table is in 3NF
In Table 3 Salutation ID is primary key and in Table 1 Salutation ID is foreign to primary key in Table 3
Now our little example is in a level that cannot further be decomposed to attain higher forms of normalization. In fact it is already in higher normalization forms. Separate efforts for moving in to next levels of normalization are normally needed in complex databases.  However we will be discussing about next levels of normalizations in brief in the following.

Boyce-Codd Normal Form (BCNF)
Even when a database is in 3rd Normal Form, still there would be anomalies resulted if it has more than one Candidate Key.
Sometimes is BCNF is also referred as 3.5 Normal Form.

4th  Normal Form

If no database table instance contains two or more, independent and multivalued data describing the relevant entity , then it is in 4th Normal Form.

5th  Normal Form

A table is in 5th Normal Form only if it is in 4NF and it cannot be decomposed in to any number of smaller tables without loss of data.

6th  Normal Form

6th Normal Form is not standardized yet however it is being discussed by database experts for some time. Hopefully we would have clear standardized definition for 6th Normal Form in near future.