Sunday, 27 October 2013

SQL Command Types



As we've seen in the last discussion, the heart of any database lies in the query language. This is similar to the case that sql is the heart of database systems. There are three types of SQL Commands
  • Data Definition Language
  • Data Manipulation Language
  • Data Control Language
Data Definition Language:
This is what a database needs to be created: a basic set of commands used to create, and drop the data from the database. We use these commands with one flexibility, that they are auto saved commands. This is one we need to look at now:

There's a command called commit, in the Oracle's Sql list. This command saves the previously modified data. Since its all related to Data manipulation, every DML command must be accompanied by a commit command.


DDL command are auto saved in this sense: once they are executed, they are auto committed. Hence we need not explicitly save them.


The commands in the DDL class are:


CREATE

This command creates and entirely new table, we specify the attributes of the table along with the entity at this level. we also use the data types for each attribute.

Syntax:

                                  CREATE TABLE table_name (
                                              data_1 TYPE,
                                              data_2 TYPE,
                                              data_3 TYPE,
                                                        .
                                                        .
                                                        .
                                                        .
                                   ); 

Example: 


CREATE TABLE student (sno number(5), sname varchar2(10), course varchar2(5), avg number(5,2));       

One thing we have to observe here is that Oracle command line is dependent on the ';' used. Unless and until you put a ; all the thing written will be treated as a single statement only!

ALTER:

this command alters the things such as column names, types and new columns. we can use two commands in sync with this command, they are:
  • ADD
  • MODIFY
  • DROP

ADD command adds a new attribute to the table, this can be used to append new columns to the created table.

MODIFY command can be used to change the data type or the name of the column, in an already created table.

DROP command can be used to simply drop a column form the table. The data associated with it, will also be deleted.

syntax


ALTER TABLE table_name ADD data_1 type1;

ALTER TABLE table_name MODIFY data_name new_type; 

ALTER TABLE table_name DROP column_name;

DROP:

this command simply drops the entire table, it deletes all the data and associated structures, and the deletion is unchangeable. This means once dropped, we cannot bring back the data. 

Syntax
DROP TABLE table_name;

A Summary of the Data Definition Language commands is given below:



Transforming Schema into Programming

Till now we've been discussing the various concepts and qualities of a relational databases. These were the most efficient and most used databases now a days. Now we'll throw some light on the practical database examples.

In general, a database has a programming environment apart from the traditional File management systems. This was first developed by IBM corporation in the 20th century, and later was adapted by other companies too! This programming language used for creating, manipulating and accessing database is called Structured Query Language (SQL). This was a query type language, this means the commands we give to the database is in the form of a query, a structured command that runs with the database System. 


The various database systems available in the market are:

  • Oracle
  • My SQL
  • SQL Server
  • DB 2
  • MS access etc.,
The benefit of SQL is that its a universally accepted language for databases, this means that though the interfaces may change: all the database systems use the same language for the processing. Here our scope of learning is limited to oracle. After the completion of this, we'll move on to another database system: My SQL.

Coming to the History of Oracle, Its an American Software and Hardware Manufacturer co-Founded by its CEO Lary Ellison in 1977. It operates from California, USA. Its the 3rd Largest Corporation in the world in terms of its revenue. The First released product from it was Oracle 2, in 1979. From then, till now Oracle has been developing products for Database Systems. The Latest Product launched is Oracle 11g. Oracle has also won acquisition of Sun Microsystems in 2010. Now its the official owner of Java!



In SQL Programming, there are mainly 3 types of Language Commands. These are the major commands used in Creating, manipulating and accessing a database. They are:
  • DDL :    Data Definition Language
  • DML :   Data Manipulation Language
  • DCL :   Data Control Language

Refining The Database Schema



Till now we've seen various models, and ways of creating a database. we've primly discussed about  the evolution of the ER Model, and the different types of keys that are seen in a relation. Today we'll see one of the most important step in the construction of a database. If we buy something, don't we check before using it? if we don't i don't say its bad, but the chances of bad quality are not null anyways. Hence in every aspect we check and try to purify things for refining and optimizing its efficiency.

In the same trend, once we create a relation for a given problematic data, its not directly implemented. Its first checked for some hidden errors that may cause the database to have run time disorders. First of all, we'll have a close look at the errors that might cause the following limitations of database:
  • Delayed Processing of Requests
  • Errors in responses
  • Sometimes database may fail to result in the right response.
Redundancy is the prime of all the errors that occurs in a database, its simply the occurrence of  duplicates of a data. This may result in many further disorders in the relation.

There are three main errors that creep into a database, due to bad design of relation. they are:
  • Insert
  • Delete
  • Update
These errors are called Anomalies, in the database terminology.

Insert Anomaly occurs, when data cannot be inserted without the presence of other information.

Delete Anomaly occurs, when deletion of a data causes loss of some other information.

Update Anomaly occurs, when updating a data needs to be done again and again.

The method of refining a relation by removing the redundancies that creep into the database, is called Normalization. Here we refine and if needed trim the relation, so that the relation in turn becomes simple and with no anomalies.

A Relation when satisfies a rule of Normalization is said to be called a Normal Form.

There are basically 5 Normal Forms, each having a rule to be satisfied to be called so:
  • 1 NF
  • 2 NF
  • 3 NF
  • BCNF
  • 4 NF
1st Normal Form: The Relation should have a single value in each cell, multiple values should be removed or made into new rows.

2nd Normal Form: The Relation should be made free of Functional Dependency, there by causing the anomalies to be flushed off.

3rd Normal Form: The Relation should be freed of Partial and Transitive Dependencies.

3.5th Normal Form or Boyce- Codd Normal Form (BCNF)

A Relation is in BCNF iff an FD X->A is such that: 

  • A belongs to X and 
  • X is a Super Key
4th Normal Form: The relation should not contain multi valued dependencies.

We'll have a close look at each of the Normal Forms in the next Posts....

The ER Model



In our previous sessions, we've been some what brief to the relational database models. Toady, we'll have an in detail examination of what is what. Basically, in R DBMS everything that is data before is now a relation. Hence, a relational database consists of relation aka tables which have rows and columns.

  • Rows are called Tuples.
  • Columns are called attributes.
  • Number of rows in a table constitute the cardinality.
  • Number of columns in a table constitute the arity.
  • The intersection of a row and a column is called a cell.
Hence, we work entirely on the cells, at each tuple of an attribute. the relational database was constructed based on a theory called the ER model. The ER model describes how a relation can be established, given a problem. The entities are the things that are under our examination. here we can assume an entity to be a table. An attribute describes the entity well. hence it is a description of the entity. we can assume this attribute to the columns of our table. A Relation is the thing that relates two entities: an intersection of two entities. We try to work out the model in our construction of a table. The model uses a diagrammatic approach for different cases of entities and attributes, and for a relation. The main elements of ER model are:

  • Entity
  • Attribute
  • Relation
Entity is represented by a rectangular box, an attribute by an ellipse and relationship is done by a Rhombus. A small example of ER model is discussed below:

Problem: a Student Joins in a College

Model:

There are principally 3 types of relationships, by their nature. They are:

  • One-to-One
  • One-to-Many/ Many-to-One
  • Many-to-Many
One to One relationship involves two entity relationship, One to Many involves more relations while many to many involves even more relations.

The Above problem can be modified to all the types like:

a Student Joins in a College (1-1)




many Students Join a College (m-1)



many Students Join many Colleges (m-m)




Another important element in the construction of a database, is the selection of the keys. A key is an attribute of a relation that uniquely identifies each tuple. For example if we take all the students information in a relation, then obviously student roll number or his admission number or his registration number will be unique of each student. hence they are all alternatives for the key.

One such attribute selected is called a Primary Key. If two relation have a common key that uniquely identifies its Tuples, is called a foreign key. This is similar to a Parent-Child relationship in class and objects concept: inheritance. If two or more keys are combined in case of forming a single key for a table, its called  a composite key. Hence there are:

  • Primary Key
  • Foreign Key
  • Composite Key
  • Candidate Key
in a table. The ER model of a relation is mapped in order to construct a database table. Hence we involve the following steps in the construction of a database:

  • Referring the Problem
  • Identifying the Entities, attributes and Relations
  • Framing a model for the problem
  • Mapping the model for our database table.

Relational Database Management Systems


We've seen one of the factors that throw a great impact on the efficiency of data and types of database management schemes present in the development and manipulation of an efficient database. One thing must be clear for you by this time that a database is simply a powerful storage than a Hard Disk, made so because of the management we have for it. DBMS is the software version of such management/ governing scheme. There are Some goals that an efficient DBMS must aim at:
  • Creation of a Database
  • Efficient Handling of manipulations like add, modify or delete on the data
  • Employ constraints for Database Security
Hence our DBMS must somehow satisfy these needs to have an efficient handling of data. One thing is that how much large the data may be, it must be stored such that the operations must be responded very quickly. For Example let us consider a reservation database, that handles the seats in a vehicle, a Bus say. If our database is so slow that each query takes considerable time in responding, passengers will sure have to struggle a lot. Because since the tickets are such a data which can't be shared, if a ticket is issued to two people, then we'll have a fight!!!

 Relational database solves such problems by employing table constructs on the data construction. The rows are all entirely data, and columns map to the owners or the identifier variables. The cell is an intersection of a row and a column. For example in the above figure, tutorial_id, title, company are the variables which hold the values below in the respective columns. In RDBMS, the table is a relation, a row and a column being the values of the relation. Here we need to know about two things about the entries in the table, they are 
  • Entity
  • Attribute
An Entity is the item being described in the table, and attribute being the values. Hence a table consists of Entities and attributes for the entities. A Relational Database is represented in the form of a relation between the attributes, the tables we generally speak about will be of the form

D(d1,d2,d3,.......dn) where d1, d2,... are all attributes which hold the column values.

Edgar F. Codd was the man behind the origin of a Relational Database Management Systems. He defined a relation as a one that satisfies 12 rules to be called so. We'll pause our discussion at this point and we'll discuss them in their full view next time...

Effect of Physical Media on Database Performance



In our previous discussions, we've seen things such as some typical fields of applications and Driving Force behind databases. You Can refer them Here.

Driving back to our King, like all other things we have some factors that influence the efficient utilization of a database. We'd now look at one of them: the Physical medium of Communication between the Database and the user. User can basically need to connect to a database before getting served by it. This connection can be in two ways materially, One is a Physical Link, and the other is a Virtual link.

A Physical Link is one connection that is visible and can be accessed physically  What i'm trying to say is that physical links are generally wired media. Our cables, are the best examples for it. 

A Virtual Link is one link that can't be viewed, its a software type. For example consider a Bluetooth connection between two devices, can we see the connection between them? We can't view them, but in the devices, the transfer can be witnessed. This is what, the most easy technique but at the same time, can't be wholly reliable. This is because that, the packets of data we share can't be traced once they are sent out of the device. Since the entire transmission is a medium independent, there's no scope for reliability. All we can see here is Hardware simplicity and fast access. WLAN, Bluetooth, etc., work on this technique. On the counter side, Ethernet is one best example of a wired type communication.

Actually, this topic needs more clarification because there are many related concepts of Data Communications with the discussed one above. I've related them to our discussion though they're not of great importance, because that we're here thinking about factors and the efficiency of a database depends on these links since these vary with the data rates.

Lets get back to our discussion, a Database is generally a collection of logically related data. The data are stored in database in the form of rows and columns of a table. Hence, a Database typically contains tables. The data are retrieved and manipulated based on these tables. 

Database management means use of tools in creating a database and maintaining it. There are two main ways in database management, the first one is the traditional management system. its called Files Management System, that uses programmatic way in creating and manipulating tables. Examples are the Files in C, C++ etc. but this is less user friendly and has its limitations too!

Hence we move on to an advanced method called Database Management Systems (DBMS). This uses the so discussed, Table format for a database management. This is widely used nowadays. In the DBMS, there are two more approaches, or the sub classes for it. They are the Relational DBMS and Object Oriented Databases.



DBMS:-
  •  RDBMS
  • OODBMS or ODBMS 
Relational Database is one which has a relation between the values of the table. The table is itself treated as a relation and we apply our relational mathematics on it. Object Oriented approach of the RDBMS is the next type called ODBMS. We'll have a detail account of these things and also beginning of the syntactical approach in our next discussion.

Database Systems










Previous Class: Here

I hope that you might have well understood the need of a database in present day applications.
we'll have a brief discussion on the applications of  database in various fields. Then we'll throw 
some flash on the underlying software that handles the king: Database.

1. As we've assumed previously, the main aim of database is to provide data sharing among several users. The aim of database has extended to such a manner that its now providing data sharing at a remote level. For example we know that we all access our mail accounts, send and receive mails. But we would wonder that these mails are not stored in our computer. right? We can access them anywhere and anytime, irrespective of the device we use: we can have the same mails accessed through our desktops and even from our mobiles. I'm just talking about the data we access, not how its being accessed. This example might confuse you a little bit because, database is generally a software driven Hardware. So we should talk about the software while mentioning the hardware too!

Well we'll leave the other things out of our scope here, such as the way in which we access them: data communications. here actually each person or the owner of mail has his mailbox or more naively, his very own virtual postbox in the database located somewhere else in this world. He'd log on to his account through some client address, and then checks his mails. Here the Database acts merely as a combo of a Huge Post Office and Post Box.

2. Internet, is all we're using somehow in our work. Even you are reading my bundles of notes through your internet right? but the internet is just a software: its a network. The driving force of Storage behind internet is the large use of Database Servers. A Server is a software and database is a storage. Together they provide the functionality of inter networking. The things you see on an internet are all simply web scripts: pages, bundles and bundles written in some language other than English :) These web pages are stored in the dedicated databases which are accessed remotely by the client systems when need some page to load. The browser acts as a dish in which your potato fry: i mean the pages are served by the server: in my sense database....
Just don't mingle Server and database here, we'll see that some other time.

3. Cloud, is the next level of database computing, in which the database is remotely accessed and offers several advantages like Single application being run on many systems etc.,

Now that you are aware of the application aspects of a database. if still the thing's not clear, just have a search on this thing. because applications include reservation systems, and many other online services, I've classified them in another form: the way in which a database functions.

Now we'll return to our concept of Database Systems. A System is generally a physical entity. But in this context we'll imagine this to be a Driving force. A Database is simply a Jumbo size storage disk for a Computer. But the thing's that even though we own a car for us, what's the driving force for it? Just don't say simply fuel, its the car keys!

In the same way, a Database needs a key to control it: a software that governs the functionality of the database. I just don't want to confuse and prolong it anymore, in the straight way, its the database system. Its a software installed to provide the functionality of a database. To be a successful thing, you must put some effort in your efficiency. In the same way, in order to build a successful database, we should concentrate on building an efficient software. 



We'll pause our story of King DB here, and next time we'll see DB System in detail and have a brief introdution of DBMS.