Thursday, 9 August 2012

Ruby


              Ruby is a scripting language which is need no be compiled. The code will be interpreted. It supports Object Oriented programming. Even the basic data types are objects. It is dynamically typed as in Javascript. It is a server side scripting language.
              The class of each object can be known using the ".class" command.
            For example : 10.class will return Number.
            Similarly "10".class will return Fixnum.
             Each class is associated with member functions. For example we can convert a string object into a Fixnum using the to_i method.
              The methods can either be for a class or for an instance of the class. The class methods will have self keyword. They can only be accessed by the class name. The instance methods will be accessed by the instance names.
              Similar to the constructors in the C++. there is a special method called as initializer in the Ruby. This method will be invoked  whenever a new instance is created.
             There are private and public variables too like c++. But they have different syntax.
             There are access specifiers such as attr_reader which allows public read of the particular variable. attr_writer which allows public write of the variable. There is attr_accessor which allows the variable to be both read and written in public.

Containers :
              The container objects available in the language are array and hash. We should specify the key and value pairs in for the hash. If we want to access the value all we need to do is use the key to access the variable.
             The syntax for the array is ,
              a = [1,2,3,4,5]
             and for a hash,
              h={"1"=>prem,"2"=>anandh}
             In the above we can access like,
             puts a[0]
             puts h["1"]
             The output is,
             1
             prem
             
Control Structures and loops:
              The control structures include if(condition), elsif(condition) statements doing the job of the if(condition) and the else if(condition) of the C++. There is an option called as unless structure. The working

             if(condition)                      unless( ! condition )
             #some_code                      #some_code
            end                                     end

             If the condition is true in if the #some_code will execute and the opposite is there in unless.

              There is a syntax called as
               case (option) when match1 when match2 end
                This is similar to the switch case in ruby. It is a multi branching statement unlike if else.
                 The loops in ruby are,
                 for, while and until. The first two are familiar as they are similar to the loops in C++. The for loop has syntax like,
#  If you're used to C or Java, you might prefer this.
for element in collection
  ...
end

#  A Smalltalk programmer might prefer this.
collection.each {|element|
  ...
}

Coming  to until .
       until (condition1)                     
       ....                                             
       end                                           
        In the until loop the loop will continue till the condition is true.

There are options for loops like:
   5.times {p "*" } #print "*" 5 times.
   3.upto 6 {p "*" } #prints "*" 3 ( 3 to 6) times.

Classes and objects:
         Classes have their name preceded by the "Class" keyword. We can create the objects using the "new".
          object = Class_name.new

Inheritance:
             Inheritance in ruby is done by the "<". Here there is a base class
              class some_class < parent_class
              ...
              end
               Here the class some_class is inherited from the parent class.

Procs and Lambda and Blocks:
               Well i might call Procs as generic functions which can be passed as parameters and then they could be called from there. The syntax and working of these are listed at the site http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
                Procs and Lambda are similar but the only two differences between them are,
  1. Lambda's check the number of parameters getting passed.
  2. Lambda's let the method execute even after it has returned a value but  Procs don't.  Procs will return the control the out of the function once it returns the value.
I also came to know about meta programming in ruby and ROR and how well these languages support meta programming. It still was confusing and thought that I will know in depth once I am used to programming in ruby and ROR.

A block is also a Proc.

Scope in Ruby :

Name Begins WithVariable Scope
$ A global variable
@ An instance variable
[a-z] or _ A local variable
[A-Z] A constant
@@A class variable

                 The above mentioned table contains the table having the ruby variables and their scope. For clarifications in details visit http://www.techotopia.com/index.php/Ruby_Variable_Scope

Ranges :
          Ruby allows a new concept of ranges where you can specify the start and end of the range and you the intermediate values will be automatically generated.
              Ranges can be used as conditions, intervals and sequences.
            For example :
             if  marks == 0..49
                  puts "fail"
              else
                   puts "pass"
           Thus the above code will give "fail" is the mark is between 0 and 49. There are two ways of creating ranges.
             using "start..end"  and "start...end".  The first range is from start to end and the second one is from start to value_previous_to_end.

Iterators :
         Ruby offers two iterators. They allow us to traverse through the collections. They are .each and .collect.
        .each will not return any individual element while the .collect returns a new collection.
        For example :
        ary = [1,2,3,4,5]
        ary.each do |i|
           if( i%2==0 ) puts i
        end
        The output is,
        2
        4
        And for the .collect operator.

        ary = [1,2,3,4,5]         new_ary = ary.collect{ | x |  10*x }
        puts new_ary
        The output is,
        10
        20
        30
        40
        50
        Thus we can manipulate the values in the original array and return the new array to new_array.

Tuesday, 7 August 2012

JS-OOP and event handling

OOP:
    In Javascript everything you can manipulate are objects. They include all the Strings, Numbers, Arrays, functions and Objects.

    var x="prem"
    prem.length

    here x is an object which has value as "prem" and property as length.
    We can also create objects such as
    var prem = Object.create(null);

    Here the prem is an object. But where is the class...????
    There is no class, just objects.
    Here we need to think apart from the traditional OOPS where you need to create classes and instantiate the objects using the classes as in java and c#.

  To add the properties

    There are options to allow the get and set methods in the JS. They can be set by using the following syntax,

// () → String // Returns the full name of object. 
function get_full_name() 
{ return this.first_name + ' ' + this.last_name }  
// (new_name:String) → undefined 
// Sets the name components of the object, from a full name. 
function set_full_name(new_name)
 { 
    var names names = new_name.trim().split(/\s+/) 
    this.first_name = names[⁣'0'] || ''  
    this.last_name = names['1'] || '' 
  } 

Object.defineProperty(prem, 'name', { get: get_full_name , set: set_full_name , configurable: true , enumerable: true })


In the above example there is a property called "name". 

prem.name="prem anandh" // set_full_name is called
now,
prem.first_name is "prem"
and
prem.first_name is "anandh"
and of-course,
prem.name //get_full_name is called

Do Classes even exist in JS...?
        Yes. They do. But slightly different from the rest of the languages in which they are used. The syntax is,
       function name( some_variable )
       {
             this.val=some_variable;
        }
       


   

Thursday, 2 August 2012

Javascript - an intro


What :
       Javascript is a programming language which can exist on its own and it can interact with the DOM elements. It goes well with HTML and CSS to give so many good things in the web page. These are all facts about the language. Now we can see about some working in that language.

To declare any variable in this language we can use the "var" keyword.

  • var name="prem"; // String type has double quotes( "" )
  • var age=15;// Numeric type don't have double quotes.
  • var nothing; //undefined type
  • var something=null; // null type

To create and call a function.

var add = function(a,b) // function definition
{
       return a+b;
};

add(10,15);  // invoking the function

The most used output function is,
 document.write("something");
To print the output onto the console we use
 console.log( add(10,15) );
To print the output onto the screen as a pop-up
 confirm( add(10,15) ); // here the return will be a boolean
To output without the use of a return value we can use alert box
  alert( add(10,15) );

To get inputs directly from the user we use the prompt(); function.
It takes two parameter first is the text you need to appear on the screen and the second is the default value in case user doesn't enter anything.
var name=("type your name","prem");

The conditional statements and the looping statements will work in the same way as in C language.
Note : There is a difference between operators "==" and "===".
      E.g;
       5=="5" and 5==5 will return true
       but
        5==="5" returns false while 5===5 returns true. i.e, here both the value and type are checked.

Objects:
      Javascript supports objects. An object have properties and methods.
      For example for a string,
      "prem".length // is a property
      and
      "prem" .toUpperCase() // is a method.
       Method is doing something over the given object while properties are the attributes associated with an object.
        Date  is an object in the JS(shorthand for JavaScript) and the date object can be obtained by,
          var today = new Date ();
           Now, to have the seperate date, hours, minutes and second we can use the code below,
          var date=today.getDate();
          var month=today.getMonth();
          var h=today.getHours();
          var m=today.getMinutes();
          var s=today.getSeconds();


Wednesday, 1 August 2012

TDD-Test Driven Development

TDD
       So before beginning this we shall see about the usual manner in which we code. When we are given the problem description we will start to code. End up with a solution. Then give test cases to check if the code works well. But in TDD,
  • We take a requirement and write a test case that matches it.
  • Run the test so that it fails.
  • Modify the existing code so that the test passes.
  • Refactor the code so that the code is more efficient and manageable.
       The diagram below explains the working of the TDD.


























Scenario:
        So lets consider a scenario where you are asked to write a code to play music using a music player.
        music_player()
        {
        }

        Test 1: should play a song.
        The above test will fail as there is no option to play the song.
        The code is changed to,
          music_player()
          {
                 play_audio(); // it will contain the code to play a particular file
           }

           Test 2: Should allow user to select a song.
            The above code will fail for this test. So we will need to add a new piece of code to choose a file.
           music_player()
          {
                int song_id = select(); //to select a song from the list of songs
                 play_audio( song_id );

           }

           Test 3: Should not list non audio/video files.
           The code will fail as there is such an option is not created.
           music_player()
           {
                show_list();  // list only audio/video files.
                int song_id = select();

                play_audio( song_id );

            }


           Test 4: Should not play a deleted song.
           The code will fail as there is no option to check if the given song id is in the list.
           music_player()
           {
                show_list();
                int song_id = select();
                if( check_updates(song_id) ) // This will check if the song is still in the list
                {
                        play_audio( song_id );
                 }
            }

            Test 5: Should play videos for video songs.
            The code fails as there is no special provision for playing videos. Then we have to change the code as,
            music_player()
           {
                show_list();
                int song_id = select();
                if( check_updates(song_id) )
               {
                     int type = get_type(song_id);
                     play( type, song_id ); //generic play function
                }
            }
            play( type , song_id )
            {
                   switch(type)
                   {
                             case AUDIO: play_audio(song_id);
                             case VIDEO: play_video(song_id); 
                   }
            }

            Here we assume that select(), play(), play_audio() and play_video() are already tested units so we need not check their functionality again. We are concentrating only on the music player().
 
Advantages:
            The good thing about this is if a new programmer wants to add some other features such as categorize() the music files. He will write a test case for it and add the feature and run the entire test cases on the music_player().

            So if there is a problem he can know due to which test case there is a failure and overcome the problem. This gives the programmers some assurance over the code.

              NEGATIVE TEST CASES: These test cases are here to handle situations when the test cases are outside what is defined for the system. For the above music_player() scenario providing non audio/video files and selecting id's of deleted files.
  
             POSITIVE TEST CASES: These test cases are to know how system should react to proper inputs. For the above music_player() giving files to play and selecting files are examples of positive test cases.


Monday, 30 July 2012

CSS

CSS, an abbreviation of Cascading Style Sheet.

Why ?
       CSS came to existence to solve the problem of overloading the HTML contents with all the formatting options. When all the formatting options came into use there was a problem of mentioning the options in each and every tag. This lead to having a big HTML document which is not a good method for a programmer. Hence people came up with the idea for separating the formatting style options into a separate file. Thus came the CSS.

Like is said in my previous blogs the css can be included,
  1. Inline(within each tags). 
  2. In the style tag of the head section.
  3. From an external CSS file.
 The last one is the preferred way. If multiple styles are set to a particular selector then the style will be inherited with number three having the highest priority and the priority decreases as we move up in the above mentioned list.

CSS styling includes adding attributes to background, text, font, links, list tables.
  • In Background we can specify either a color or a picture. We can specify the position of the image. There is also an interesting property called as attachment. This specifies if the image should scroll with the rest of the page or if it should be fixed on the screen.
  • In Text section we can specify the space between the letters, lines, words. Indentation of the words. Text decoration( underline,blink, etc.,).
  • In  Font section we can format the fonts we want. There are options for specifying the font size, family, weight(degree of boldness).
  • In the Links section there are four categories link-normal unvisited link, visited-any visited link, hover- when the user mouses over the link, active- the moment the link is clicked. We can give properties depending on the links.
  • In Table we can set attributes such as border format(type, color, width, collapse), we can also use the above mentioned options within the table contents as it can contain those elements too.
The Box model:
          The box model consists of four elements. They are given in the above diagram. The Margin and the padding are transparent elements. The former is to specify clarity between the element and adjacent elements while the latter is to have some space between the border and the contents.
         The margin and padding will take the amount of space in all the four directions. The Border will have style and width attribute.

Fluid design :
          Before you start to design any website there is a need to know on which device the web page will be accessed. The normal desktop monitor has a resolution of about 1024 * 768 pixels. But for a smart phone it will not be the same. So if you have specified the dimensions in pixels it wont have the proper in the phone. So the concept of fluid came into picture. Here the dimensions are given as percentage corresponding to the browser. This helps the page to be displayed properly in case it is getting accessed by a hand held device.

Selectors :
       We can select HTML elements from CSS using their id, class or the name of the tags.
         To select a particular element belonging to a particular class we can use
 element_name.class_name. The same cannot be applied over an id as an id will be unique to an element. We can have the same style for more than one selector using ',' operator. E.g,   
        #style_one , #style_two
       {
         color:black;
       }
      If you need to select a particular element like "<p>"which is inside a class "some_class" of element like <div> which in-turn is inside a id "some_id". Then we can select the element using the following way,
        #some_id div.some_class p
        {
             text-align: right;
         }

OOPS CSS:
         OOPS CSS is combining the OOPS concepts into the CSS. CSS doesn't support the OOPS concepts like other languages. But they are still applied in the CSS. How is this possible and why is this necessary..?
       The need to go for such a concept is that you should not duplicate the same css code.
        For example, There are different types of boxes in a HTML file. One can be round, Rectangle. These can further be classified as narrow and wide. So inseted of having classes like,
       .box_round_narrow{...}
       .box_round_wide{...}
       .box_rectangle_narrow{...}
       .box_rectangle_wide{...}
       The above mentioned option can be done but we will unnecessarily using the same "box"  code four times and code for the "rectangle", "round", "narrow", "wide" two times each. This is not a good way right.
      So lets apply some OOPS here. Split each into different classes.
       .box{...}
       .round{...}
       .rectangle{...}
       .narrow{}
       So if we need a box of type "narrow round ". We can include it in the HTML by giving the class in the following manner.
       <tag_name class=" box narrow round"> some code</tag_name>
        I didn't say that the number of classes will be reduced all i am saying is that unnecessary repetition of codes will be reduced.

        We can also apply the OOPS concept when we need to
1) Separate structure and skin :
          Skin is the appearance of the HTML element i.e, color, font attributes, border color, style etc., This can be same for both the table and a paragraph even if they are not within a single parent container. Thus the skin can be a class and it can be reused over different elements.
         I have a doubt about the structures so i will explain about that to you people later.

2)Separate container and content

         Here the container is the base container and the content is the content the container holds. For E.g, in case where  

<div class="some"><img src=url('www.x.html')> </img></div>.

     The CSS can have .some{border-color:black;border-style:bold;}. Instead of the above design we should alter it as

<div><img src=url('www.x.html') class="bordered"> </img></div>

and in the CSS file we can have,.bordered{border-color:black;border-style:bold;}.

  The use of the above is that we can have another image within the <div>  which doesn't require the border and we can also have a bordered image in any part of the HTML file.


SPRITES 

   Sprites are image sprites which can be used to create animations of moving objects by moving the image quickly over the screen. For example the image at http://www.google.co.in/logos/2011/gumby11-gumby.jpg can be used to create an animation. 

        This can be done if you could enlarge the above picture we will see an animated image which changes itself as you scroll through the image. All we need to do now is to create a viewable portion of the image in the screen and change its position by setting the intervals. We can do this using CSS.

Friday, 27 July 2012

DOM, Page layouts

The HTML-DOM tree

HTML DOM:
         The HTML DOM interprets the HTML document as a tree structure. The DOM is like an API which interprets a HTML document in the following format. The DOM has a tree structure which has the document as the parent. 
             
                     The DOM helps in retrieving individual elements in the script language using the structure provided above. In the above mentioned tree the attribute, text, elements and comments are placed in the nodes of the tree. HTML DOM allows scripting languages to get the nodes and to process or apply changes over them.

For example (javascript) : we can use Document.getElementbyID('name'); will get an element from the HTML document which contains the give ID.
                 There also exists relationship between the nodes.
                 The above picture gives a clear picture of the relationships between different nodes in the tree. Each node is associated with some properties and methods. Properties are the values of nodes which can be extracted to use or changed. Methods are the methods which can be done over the nodes.

Document.getElementbyID('something').innerHTML; here getting the element is a method and the innerHTML is a property which is being retrieved.

Browser Specific Elements:
         
       Browser is a rendering machine. It gets the HTML page is interpreted it and displays the contents in the programmer desired format.  The rendering of the web page has the following process.






        The Render tree is built by combining the HTML file along with the CSS file which is included by the HTML page. Each browser has its own method of creating the web page by interpreting the DOM. Not all browsers will interpret the web page in the same manner. The difference may arise in the compatibility of the web browser.
       Most of the HTML4 commands will execute properly in all the standard web browsers. But the recently developed HTML5 is not widely supported and the same page will not be rendered properly in all browsers. Hence it is better to go for HTML4 for the time being till HTML5 is well established so that it could be used in all the web browsers.

Thursday, 26 July 2012

HTML as i know......


         HTML as you all know is a language that lets us design web pages. It contains tags which help in presenting such good visuals on the screen. There are a lot of browsers being used currently. Though all the browsers display the properties in the same way there are some browser specific attributes too.

          There are three parts in a web page. The HTML part, script part, css part. The HTML part contains all the tags which will appear on the screen. The script part helps in the web page being dynamic. This part will be similar to any other programming languages. The most popular one being javascript. We shall see about this in future.
         The css part helps in assigning the formatting to the tags in the HTML. There are class and id assigned to each element/tags in the HTML document. They both serve the same purpose, helping to assign the formatting to particular elements using css.
       The css and scripts can be either included in the same HTML document or written as separate files and include them into the HTML file.
       Id is is used in elements where the formatting will appear for only that element alone. Class is used when there are many elements which will contain the same formatting. For example : The text box whenever they appear on the screen will need to have the same formatting. So they will be having the same class name.
       The elements in HTML are either block elements or inline elements. <dev> is a block level element which acts as a container to contain other block level and inline elements. The formatting style for the block elements will be inherited in all the elements which it contains.
       <span> is an inline element which is used to span the formatting style in the text which it contains.
          To create the layout of the web page we can either go for <div> or <table>. Though it is now established that <div> is the proper way to do create the layout.
         Some characters are reserved in HTML. It is not possible to use the less than (<) or greater than (>) signs in your text, because the browser will mix them with tags. To actually display reserved characters, we must use character entities in the HTML source code. For example we can use @lt to make '<' to print on the screen.
        There are various events which are there to monitor the web page. Sometimes you may have to sign in in a new website. You will be giving a new password. If your password is small or not proper the website will print the problem once you leave the text box. This can be done by combining events and corresponding client side scripts.
        The webpage http://w3schools.com/html/html_quick.asp will contain quick view of syntax of all the available tags and entities available in the HTML.

Wednesday, 25 July 2012

Document based database

 Hi,
     Document base database. From the name you can get that it has something to do with a database and document. Initially i thought it as database with proper documentation. But it turned out to be database which uses documents to store the data. They can be opened and read like any other file.
      Many people who have been using RDBMS like mysql will find it difficult to switch on to this type. Because this is an entirely new concept where you need to unlearn things. We need to think apart from normalization and relations.So lets get started.

Why Document-based:
       Well each new concept has a reason behind it. And the reason for the document-based is to overcome the rigidity of the RDBMS. By rigid we mean the strict rules in terms of schema.DBD also prevent joining tables as the details which need to be retrieved by joining multiple tables can be provided  within the same document.
       There is no schema in DBD. Each document can contain its own set of attributes which can vary from one document to another(even though it is preferred to have similar attributes in the document ).

Comparing RDBMS and DBD using example:

       For example (in MongoDB):
       db.users.({name:'prem', likes['facebook','twitter'] });
       db.users.({name:'praveen',cars['ducati','ferrari']});
      
        The above statements adds two documents to the collection users with names "prem" and "praveen". These both documents have two different attributes i.e, likes and cars. Thus we can add each document different data types in runtime. Sounds good right....

       If we need the same thing to be done using MySQL the schema would be,
       create table users (name varchar(10),likes1 varchar,likes1 varchar,cars1  
       varchar, cars2 varchar);

      Then for prem there would be two NULL columns in cars1 and cars2. The same would be case for praveen in the likes column. If we need to increase the number of cars owned by praveen we need to alter the entire table in RDBMS. But in DBD only the particular document will be affected.

       Consider a query where you need to "find number of people who like facebook". The query can be done in DBD too. But searching each attribute of document to address the query is not a proper way. So you may need to rethink the structure of the documents.

Storage in DBD:
        Well another advantage of DBD is that the documents are in text files. So the processing of these documents are extremely fast compared to the relational database.Here the Documents are analogous to records and the collections are analogous to the tables.

Searching with JASON:
       Each documents are addressed by a key-value pair. For each document a unique id is generated. Thus search for a document is faster. DBD allows API's such as REST using JSON which is pretty much faster than RDBMS( JDBC , ODBC ). Each document is a object of JASON. In the previous example the two user objects 'prem' and 'praveen' are JSON. For good knowledge on JSON refer http://www.json.org/.
        The use of JASON makes the DBD to support javascript very much. They also form effective means of communication between server and client as the message transfered will be in the form of JASON rather than XML documents.
       Primarily the DBD came into use to manage huge amounts of data. The DBD allows writing very fast and hence they are used in write intensive application. Even in reads it is better to use rather than RDBMS.

Transactions:
        But the problem in this is that we cannot go for transactions in DBD. Even though there are facilities to support transactions these days. DBD are not preferred  for transactions. There is no possibility to go for locks in DBD.
        There can be situations when you need to split the collection into two with a common value to link the two collections(foreign key in RDBMS). Even though we can implement the above scenario using DBD it is not advisable.
       
          

Tuesday, 24 July 2012

Unix commands and working

The audio blog for the blog is at http://yourlisten.com/channel/content/16906239/Linux_commands

Before getting into the commands let us see the Linux directory structure.

/ :
 This is the root directory. Every other folders are under the root directory. This usually contains the sub folders so its best no to keep any direct files in it.

< /boot >
       This folder contains the information it needs when it is getting booted.

< /bin, /usr/bin >
        This folder contains important program like ls,grep etc.,

< /usr >
 It is the largest directory in the linux.
 Inside the usr directory we have these following folders.

doc : Documentation for the user apps, in many file formats.
share : Config files and graphics for many user apps.
src: Source code files for the system's software, including the Linux kernel.
include : Header files for the C compiler. The header files define structures and constants that are needed for building most standard programs. A subdirectory under  contains headers for the C++ compiler.

 </var>
          It has the data which will be constantly changing during the system execution. They contain directories like , log - to to contain the log information,
mail - to have the sent and received mails , spool - to contain the jobs which are queued up for process(eg. printing).

</dev>
        It has the various devices attached to the file system.

Now lets brush up a few linux commands which we normally get to use,

  • ls - to list all the files and directories in the current directory.
  • mv- moving file names.
  • rm- remove the file.
  • rmdir - remove directory.
  • rm -r - removes the directory with all the files ans sub directory inside it.
  • cp filename filename2 - creates the same file with the name as specified in the second parameter.
  • scp filename destination@ip_address - this will upload the given file into the destination address.
  • scp destination_name@ip_address:/path/filename - this will get the file from the destination to our machine.
  • ssh ip_address : This will let us to access the system of the given ip_address through the terminal.
  • chown - to change owner of a file
  • chmod - to change the mode of a given file
  • To start any any network application we can use /etc/init.d/application_name start.
  • ping url - used to check whether the given website is working.
  • ps -elf | grep application name -this will give the details of a particular process.
  • tail -n - prints the last n lines of the file
  • head -n - prints the first n lines of the file
  • top - it is simliar to ps but it shows the real time usage while ps is shows average values over a given snapshot.During top if you press shift + p it will give
  • kill - to kill a particular process given the process id. The process id can be found by using grep command along with the application name.
  • cat , less ,more - these are display commands which is used to display the given file in th terminal.
  • df - disk usage of the file system.
  • du - disk usage of each processes. 
       To know about more commands check this given link
       http://www.oreillynet.com/linux/cmd/



       

Monday, 23 July 2012

Digging deep....DB!!!

Hi guys in this blog we shall see about advanced features in Database.

JOINS:
          Joins are queries which are often executed when you need to fetch data from more than one tables. We can also execute queries without joins, but internally cross join will be getting executed. They are some joins which i might list,

This image give better explanation than any written statements. Thanks to Mr.Hariharan for providing the image.

UNION :
         Union clause is used to get the union of two or more queries. The distinct values will be getting listed. The result of a join and a union may look similar. But the list which is getting returned will be taller in UNION.
         select <columns> from  table1 union select <columns> from table2.

Import and Export :

             To export a database into a dump we can use the following command.
      mysqldump -u mysql_username -p name_of_database > databasefile.sql.
           
           This can be retrieved and used to create a new database.For doing it the following command should be executed.
        mysql -u mysql_username -p name_of_database < databasefile.sql
            These dumps are used as a backup along withe the export and import purposes.

Subqueries :
           The subqueries are queries within queries. It is used when you need to get the results of one query and apply another set of query operation over it.
          The syntax for subquery es,
          select <columns> from <tables> where column(s) {=|>|<|<=|>=|<>}{ANY | ALL | SOME} / { EXISTS | IN } {QUERY};

Normalization :
        Normalization is used in DBMS to overcome the redundancy, anomalies and dependency. This is done by splitting a larger table into smaller tables so that insertions, deletions and modifications can be made in a single table and the changes can be made to known to other tables by defining relationship between the tables. There are various normalized forms. They are,

2NF:
        This is done when there exists partial dependency. XY->Z be a relation. In addition if Y->Z is another relation that is existing. So its better if we could create a separate table having relation Y->Z.

3NF:
         This is done when there is a dependency of the give form. X->Y is one relation and Y->Z is another relation. This implies that X->Z. So we should split the table such that the relations X->Y and Y->Z appear in separate tables.

BCNF:
         This is done when the following scenario appears. When XY->K and another relation exists which states that K->Y. In that case we can remove the column Y appearing in the right side of the second relation. and create a separate table for the relation K->Y and another for X->K. The scenarios are (a)there are multiple candidate keys, (b) The keys are composed of multiple attributes, (c) There are common attributes between the keys.
           In this manner we can apply the normalization over the given tables.

SCENARIO :

We can see about the above mentioned NF's here.
Consider the table with columns,

person_name, company_name, designation, salary.

         here the person_name, company_name --> designation, salary. Logically we can also deduce that company_name, designation --> salary because all the persons in the company will have the same salary. So instead of repeating the group(company_name, designation --> salary) in each employee record we can separately have a table with these columns alone.
               Another problem which is avoided here is, if salary for a particular designation in the company is incremented we need not search for each employee with given designation in the given company and update their salary column. This problem is avoided with a single update in our new model.

              This is the use of normalization.

Concept of views, triggers and stored procedures.

 VIEWS:
          They are stored queries which can be accessed by the name. They are virtual tables and they do not exist in the physical form. Views are created to view certain portion if the tables along with aggregation applied over it. Whenever a view is accessed the query will be getting executed and it helps in abstracting the underlying complexity to the user.

  This can be done by the syntax,
           Create view as select <columns> from ... ; 

           Create view  total_customers as select comp_name as name,count(*)  as 
           no_of_customers from company, customer where 
           comp.cust_id=customer.cust_id group_by(comp_name) ;

           Whenever the above query is run the number of customers in each company will be listed. This can be done by simple running the query,
          select * from total_customers;


           Inside the query for the view is executed and the result is passed onto the query using the view.

           The view can be updatable when the view doesn't contain complex values such as 3.56, col1*col2, col1+3. Any updates in the view / table will be reflected back in the corresponding table / view.

TRIGGERS:
         Triggers are stored procedures which will be getting executed in case of an event. The triggers are used usually to provide integrity in a database for example, when a withdrawal from account causes a negative balance then such transactions should be prevented. This can be accomplished using Triggers. MySQL provides three types of triggers. They are Insert, Update and Delete.The syntax is,

CREATE TRIGGER <trigger_name> 
BEFORE/AFTER UPDATE/INSERT/DELETE ON <table_name> 
REFERENCING NEW ROW AS n, OLD ROW AS o FOR EACH ROW IF n.col_name <comparison_operator> o.col_name  
THEN  
END IF;

STORED PROCEDURES:
           Stored Procedures are piece of code to be encapsulated into a single procedure name and the  code will get invoked from another procedure or from a trigger. These are stored in server.
It has the following syntax,
 DELIMITER //
CREATE PROCEDURE Get_All_cars(IN input varchar(10),OUT output int)
BEGIN
SELECT count(*) INTO output FROM products where type=input;
END //
DELIMITER ; 
          The parameters can be of type IN, OUT or INOUT. In the IN type, the given input will be only for processing. In the OUT type a value will be getting stored in the variable and it will be used in the place from where it was called. INOUT is a combination of both the IN and OUT types.
         The procedure can contain loops and case statements.
        The procedure can be called using by the following syntax,
         CALL Get_All_cars('name',@output) 
          select @output ; // this will give the number of cars of given type.


There are both pros and cons. The pros are user abstraction and sharing logic. The cons are repeating logic in the server and increased in the server.

Saturday, 21 July 2012

RELATING OOPS, DATA STRUCTURE ,DBMS ,WEB


               This blog tries to relate the topics that are being blogged about. So lets try to relate all these things to get the big picture of them all.

                Lets take a journey from the web services and we can move through the rest of the topics.Consider a user accessing a web page to download a picture. The web services starts from a client request to the server.  So lets guess how this works.
             The client sends a message which reaches the server. There can be many paths from client to server and the client must choose the optimal path. This is done by using path finding algorithms. The information will be stored in the networking devices. Proper data structures should be used to give the optimal path to the request from the client.
             The web page from which the request starts will contain many components. Each component will have particular attributes and behavior. This can be easily established using Object Oriented Programming Languages. Here each component will be represented as an object.
           Once the request reaches the server the web server will get the request and will forward it to the application server. This application server will be used to get the picture.
           The picture will be stored in a database. The DBMS will abstract the application programs from the manner in which the data is stored in a database. There will be different data structures which will be used to retrieve the data efficiently. The most preferred data structures used here are indexes and trees(B+).
           Database concentrates more on the data while DS concentrates more about organizing the data.
          Anything which is getting stored will require a data structure. Hence the data structure will be an essential in a programming language like OOPS. The OOPs is related to RDBMS. Because the master tables can be represented by using the objects. There also exists OODBMS which purely depends on the Object orientation.

Friday, 20 July 2012

INSERTION AND RETRIEVE DATA

INSERTION:
        *) The insert command works with the syntax,
        insert into <table_name> values(.....);

        *) There can also be bulk insert where you can insert multiple values into the database in a single query.
         insert into <table_name>  values (field1,field2,....) values
                           (value11,value12,....) , (value21,value22,....) ,...;

         *) There is also an option of inserting data from other tables using the following command.
         insert into table1 select * from table2;

         *) You can also insert particular values into the table1 from table2,
        insert into table1 select id,name,age from table2;

         *) If  you need to insert data from another table you can do it by mentioning the database name of the table before the table name.
          insert into db1.table select * from db2.table2;

RETRIEVAL OF DATA:
        The retrieval of data is done using select command. The where command is used to set the conditions for selecting the columns. Now let us discuss those commands.
        select <columns> from <tables> where <conditions>;
        To select all the columns use the ' * ' symbol. To select the tables list the tables. In the conditions, you can use '=' operator to check for equality, '>' greater than, '<' less than.
        To compare the results of various operators we can have AND and OR conditions attached to them.
        ORDER BY is a clause used to order the rows retrieved based on the columns specified on the ORDER BY clause.
        GROUP BY is a clause used to group the rows containing the columns into a single row.
         The following functions are usually used along with GROUP BY clause and they can be used independently also.
         *) COUNT function is used to return the number of values of a particular column.
         *) SUM function is used to add the values of a particular column.
         *) AVG function is used to find average values of a particular column.
         *) MAX function is used to find the maximum in a particular column.
         *) MIN function is used to find the minimum in a particular column.
         The LIKE operator is  used in the following manner,
         SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern.
           The pattern can have a '%' symbol either in the beginning or in the end or both the ends. The '%' symbol will be substituted by one or many characters.

         

Thursday, 19 July 2012

MODIFYING TABLE

Modifying Table :
                     Modifying table is modifying the schema or contents of the table. The schema can be altered by using ALTER,UPDATE,DELETE command. Modifying involves,

ALTER:      
        ALTER command changes the schema of the tables.
        1) Altering the data type or adding new constraints or changing names of
            column ,
            ALTER TABLE <table_name> CHANGE COLUMN <column_name>
            <new_column_name>  data_type constraints ;

            For_example : If i want to change the car_id from largeint to int with
             AUTO INCREMENT constraint we can do this by,
             ALTER TABLE cars CHANGE COLUMN car_id car_id INT
             PRIMARY_KEY AUTO INCREMENT;

        2) We can also change the column name ,
             ALTER TABLE cars ADD <column_name> AFTER/BEFORE
             <column_name>

             For_example : If we want to add car_color column after the car_id we
             can add it using,
                ALTER TABLE cars ADD car_color after car_id;

 UPDATE:
              Update table is used to change the values inside the table. This is done by selecting particular rows and changing the attributes in those rows.
            UPDATE TABLE <table_name> set <column_name1>=value WHERE
             <column_name1>=value1;
             For_example : If we want to change the color of car having id=102 to
             'Yellow'
             UPDATE TABLE cars set car_color='Yellow' WHERE
             car_id=102;
  DELETE:
             DELETE is used to delete either selected records from the table or the entire records in a table. The DELETE command can be used in either way as follows
             DELETE FROM <table_name> where.....
             The above mentioned is used to delete the selected records depending on the where clause.
              DELETE * FROM <table_name>
              The above mentioned will delete all the columns from the table.

 TRUNCATE:
               TRUNCATE is used to delete the entire contents from the table. The syntax is,
                TRUNCATE TABLE <table_name>

 TRUNCATE VS DELETE:
                Applying DELETE for all records in a table and TRUNCATE may seem similar. They give the same output when they execute. But there is a difference between the both. DELETE logs before deleting each record. DELETE is costlier as it involves resources like locking and logging. But DELETE is more safe to apply than TRUNCATE.

DROP:
             DROP differ from TRUNCATE and DELETE because DROP deletes the schema along with the entries. DROP is applied either to a column or a table. When the schema is deleted all the integrity constraints, access privileges will be deleted.
          Lets quote two examples to differentiate between DROP and TRUNCATE.
           eg.1: TRUNCATE TABLE CAR;
                    SELECT count(*) from car;
                     output :
                      count(*)
                     --------------
                        0
          e.g.2: TRUNCATE TABLE CAR;
                    SELECT count(*) from car;
                     output :
                     ERROR at line 1: ORA-00942: table or view does not exist.

Creating a Table and the commands to work on it..


Here we will be dealing about the various commands to work with in the DBMS. The DBMS which we are using here is MYSQL.

Creating and Deleting a Database :
                 The DBMS can contain any number of databases. Here we can consider a database for cars. So how will you create one...?
                   CREATE DATABASE [ IF NOT EXISTS ] cars ;
                  This command helps in creating a database called cars. the IF NOT EXISTS is used to check the database whether another database by the same name exists. This is optional but helps in preventing errors in-case another database by the same name exists. To use this database we should use the command,
                   USE cars ;
                   This helps in fetching the database cars and then we can create tables in the database and work on it.
                     To delete a database from the DBMS we can use the command,
                     DROP DATABASE [ IF EXISTS ] cars ;
                     The IF EXISTS is again an optional command where you can check in prior whether the database already exists or not.  DROP is a risky command to use as it deletes the database along with the entire tables inside the database.


Creating the tables :
             Once you enter into the database you can create tables inside it. You need to be clear about various things before you create a table. We will discuss various things in detail before we know about the syntax of create tables.


a ) Data Types .
                 Data Types offer a lot of data types to work on. Each column will contain a particular data type. The data types can be NUMERIC, STRING, DATE AND TIME, SPATIAL. Picking a data type for a column determines on 
            1) How do you want to represent the data
            2) Whether it is a fixed length or variable length.
            3) If there is a need to index the data
            4) How MySQL will compare the values in the data types.

b) Storage Engines .
             MySQL offers various types of storage engines. The type of storage engines which are offered innoDB, MyISAM, BDB, HEAP, MERGE. The default engine which will be used is MyISAM. It provides full support to indexing but it is not transaction safe. Only innoDB and BDB are transaction safe.
             MyISAM is used when you need fast access of data without any need to concentrate on foreign key constraints. InnoDB supports foreign key constraints.
            We can change the table type(engine) on your need.
   InnoDB vs MyISAM :
  • InnoDB provides referential integrity(foreign key constraints)
  • InnoDB provides row level locking. Hence it is used in write intensive   applications.
  • MyISAM is simple and hence it is generally preferred for read intensive applications.
  • Genrally MyISAM is faster as it uses full-text indexing.
c) Identifying Primary key and Foreign key.
              Primary key is the key which makes it possible to uniquely identify a record in a table. The primary key can be an attribute(column) or a set of attributes(columns).
        Example: A car database can contain a car table. The car table needs a attribute to differentiate between two records. This can be achieved using having a car_id. This id should be unique and NOT_NULL.
              Usually a table should contain primary key. If you don't have any primary key it wont be an error. Languages like Microsoft Access will maintain a default Primary key with an auto number data type.
  
               Foreign keys are primary keys of another table. Foreign key comes to use to maintain relationships between tables. Foreign keys are used to cross reference between two tables.

               Example: Coming back to the car database. There can be a manufacturer table which contains a manufacturer_id as primary key in it. Each car will have a manufacturer associated with it. This relation can be maintained by having a new column in car table known as manufacturer_id. Now, it will be easy to know the manufacturer of the car and also no car can contain unregistered manufacturer.

d) Identifying Nullable and NOT NULL fields in database :
                     There are certain situations when you don't need to specify certain fields and there are certain attributes which should not be left unfilled. For example a car may have a owner or may not have a owner. So it is not always needed to fill this attribute of the record. But each car should have tires so the tire_company_id should be filled and should not be left unfilled.
                       We can also have a Default field which allows a record to take a default value in-case it wasn't filled during insertion.

e) Identify exact data type for fields :
                   This is an important aspect in creating the table. Now, lets come back to car table. Here car_id can be a largeint as there is a need to store large number of cars and each record should have a unique identifier. The car_model indicating the model of the car should be a string data type. In that it can be a a varchar(10) - 10 letter characters. Because no model of a car can be more than 10 letters. So having some common sense and practical knowledge will let you pick the data types and fields.

f) Naming Conventions for Column Names:
                  The basic things to follow for naming columns are
                1) To check whether the naming makes sense to you,
                2) To check if the naming makes sense to others,
                The first point is because if you see your code code few months later you may get confused. The second is because when many people are working with you the naming should be clear to both and the other people who uses it.
                    The preferred way of naming are using singular words and preventing mentioning the table names as a prefix of the attribute.

e) How to determine which field to index :
                        Creating index will slow down the entire database as there is a need to maintain index and there is overheads involved in creating and deleting the index. Generally primary fields are indexed but non primary fields are sometimes indexed too.
                       Index helps in improving the search if the value searched is same as the value required.
              Example: If the car_id is indexed queries requiring the car_id's can be executed fast. But if there is a query regarding car_id and the model_name then the index wont be useful.
                     Create index by keeping in mind the query performance and the maintenance of the records.