Sunday 5 July 2015

Basic Structural aspects of RDBMS systems(SQL)

In the age of Big Data where storing unstructured data has brought to the forefront NoSQL systems like MongoDB,RDBMS systems are slowly loosing their relevance.However,they are still largely used for structured data storage and manipulation.In this article we will take a look at some basic structural aspects of SQL databases like relationships,foreign key constraints .
DatabaseDiagram:



A database diagram is used to depict the relationships between related tables in a database.As can be seen in the above diagram Employees,Territories and EmployeeTerritories are related by the keys Employee ID and TerritoryID.
Primary Key & Foreign Key:
In the diagram the EmployeeID key is a primary key in Employee table but a foreign key in Employee Territories.
Definition:
"The primary key of a relational table uniquely identifies each record in the table. It can either be a normal attribute that is guaranteed to be unique (such as Social Security Number in a table with no more than one record per person) or it can be generated by the DBMS (such as a globally unique identifier, or GUID, in Microsoft SQL Server). Primary keys may consist of a single attribute or multiple attributes in combination."
Well that is a big definition but let's try and understand that with an example.
In the above database diagram the Key EmployeeID is a primary key and is used to uniquely identify records in the table.
As can be seen here the key EmployeeID is unique for each row.
Now lets see what a foreign key is.
Here,all the keys are foreign keys,which means that they are primary keys in some other table/s and are used here in this way to create a mapping of records in multiple tables as was depicted in the database diagram.This is done primarily to avoid data redundancy and is called normalization,which has to be dealt separately.
Schema:
"database schema (/ˈski.mə/ skee-mə) of a database system is its structure described in a formal language supported by the database management system (DBMS) and refers to the organization of data as a blueprint of how a database is constructed (divided into database tables in the case of Relational Databases). The formal definition of databaseschema is a set of formulas (sentences) called integrity constraints imposed on a database. These integrity constraints ensure compatibility between parts of the schema. All constraints are expressible in the same language. A database can be considered a structure in realization of the database language.[1] The states of a created conceptual schema are transformed into an explicit mapping, the database schema. This describes how real world entities are modeled in the database."-Source: Wiki.
A schema imposes a structure on the database which is a mainstay of RDBMS systems.It imposes constraints which prevent accidental deletion of records which are related.For example if we try to delete the records where EmployeeID = 1 in table Employees we will get the following error message:

This is done because if we delete a primary key record,the foreign key in another table/s which references it becomes invalid.This is a strong structural imposition on the data.

Conclusion:
In this article we have seen some of the basics on how data is structured in a RDBMS system like SQL and some of it's salient features.In a future post I will try to share some basic operations in SQL like record addition,delete,update,retrieval etc.Please feel free to comment.









1 comment:

  1. good start. please put the links in the right had sidebar ... see layout, add gadgets

    ReplyDelete