Monday, March 10, 2014

Difference between inner join and equi join and natural join

SQL join clause is used to to retrieve data from two or more database tables. In previous article, I have explained the Different Types of SQL Joins. In this article, I would explain the difference among inner join, equi join and natural join.

Inner Join

This is the most used join in the SQL. this join returns only those records/rows that match/exists in both the database tables.

Inner Join Example

  1. SELECT * FROM tblEmp JOIN tblDept
  2. ON tblEmp.DeptID = tblDept.DeptID;
In the join condition, you can also use other operators like <,>,<>.

Equi Join

Equi join is a special type of join in which we use only equality operator. Hence, when you make a query for join using equality operator then that join query comes under Equi join.

Equi Join Example

  1. SELECT * FROM tblEmp JOIN tblDept
  2. ON tblEmp.DeptID = tblDept.DeptID;
  3. --Using Clause is not supported by SQL Server
  4. --Oracle and MySQL Query
  5. SELECT * FROM tblEmp INNER JOIN tblDept USING(DeptID)

Note

  1. Inner join can have equality (=) and other operators (like <,>,<>) in the join condition.
  2. Equi join only have equality (=) operator in the join condition.
  3. Equi join can be an Inner join, Left Outer join, Right Outer join
  4. The USING clause is not supported by SQL Server and Sybase. This clause is supported by Oracle and MySQL.

Natural Join

Natural join is a type of equi join which occurs implicitly by comparing all the same names columns in both tables. The join result have only one column for each pair of equally named columns.

Natural Join Example

  1. --Run in Oracle and MySQL
  2. SELECT * FROM tblEmp NATURAL JOIN tblDept

In the above join result we have only one column "DeptID" for each pair of equally named columns.

Note

  1. In Natural join, you can't see what columns from both the tables will be used in the join. In Natural join, you might not get the desired result what you are expecting.
  2. Natural join clause is not supported by SQL Server, it is supported by Oracle and MySQL.

Clear List of Recent SQL Server Connection From SQL Server Management Studio

While working with SQL Server, we connect to the different-different data server. As a result of this SSMS has a list of diffrenet SQL Server which we have used. Sometimes this list create confusion and we can try to make the connection to SQL Server which does not exist or we does not have access to it. To resolve this issue, we should clear this server list from SSMS.

Clear recent connection list from SQL Server 2005

First of all make sure that the SSMS is closed. Now remove or rename the file "mru.dat" from the location "C:\Documents and Settings\\Application Data\Microsoft\Microsoft SQL Server\90\Tools\Shell\mru.dat" or "C:\Documents and Settings\[user]\Application Data\Microsoft\Microsoft SQL Server\90\Tools\ShellSEM\mru.dat"
If you are not able to find the file "mru.dat" at above locations then search this file in system drive and remove or rename it.After removing or renaming, launch the SSMS and you will not see any of the most recently used servers in the list.

Clear recent connection list from SQL Server 2008 or 2008r2

First of all make sure that the SSMS is closed. Now remove or rename the file "mru.dat" from the location "C:\Documents and Settings\\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin" or "C:\Documents and Settings\[user]\Application Data\Microsoft\Microsoft SQL Server\100\Tools\ShellSEM\SqlStudio.bin"
If you are not able to find the file "SqlStudio.bin" at above locations then search this file in system drive and remove or rename it.After removing or renaming, launch the SSMS and you will not see any of the most recently used servers in the list.

Note

  1. Before removing above "mru.dat" or "SqlStudio.bin" file, please note down the exist or required SQL Server connections so that you can add these server on re-open of SSMS.

Calculate Running Total, Total of a Column and Row

Many times, you required to show information of each transaction and also keep a Running Total and Final Total like GridView in Asp.Net. In this article, I am going to explain, how can you achieve this using SQL Query in simple and easy way.
Suppose you have the below CustomerOrders table and has the data as shown below:
  1. CREATE TABLE CustomerOrders
  2. (
  3. OrderID int identity,
  4. Amount Decimal(8,2),
  5. OrderDate SmallDatetime default getdate()
  6. )
  7.  
  8. Go
  9. INSERT INTO CustomerOrders(Amount) Values(120.12)
  10. INSERT INTO CustomerOrders(Amount) Values(20.12)
  11. INSERT INTO CustomerOrders(Amount) Values(10.12)
  12. INSERT INTO CustomerOrders(Amount) Values(30.12)
  13. INSERT INTO CustomerOrders(Amount) Values(40)
  14.  
  15. GO
  16. SELECT * FROM CustomerOrders

Calculating Running Total

Let's see how to calculate the running total using SQL Query as given below:
  1. select OrderID, OrderDate, CO.Amount
  2. ,(select sum(Amount) from CustomerOrders
  3. where OrderID <= CO.OrderID)
  4. 'Running Total'
  5. from CustomerOrders CO

Calculating Final Total

Let's see how to calculate the final total using ROLLUP with in SQL Query as given below:
  1. SELECT OrderID, SUM(Amount) AS Amount
  2. FROM CustomerOrders
  3. GROUP BY OrderID WITH ROLLUP

Calculating Total of All Numeric columns in a row

Let's see how to calculate the total of all numeric fields with in a row using SQL Query as given below:
  1. SELECT OrderID, Amount, SUM(OrderID+Amount) AS RowNumericColSum
  2. FROM CustomerOrders
  3. GROUP BY OrderID,Amount
  4. ORDER BY OrderID

Difference between document.ready and window.onload or pageLoad

We think pageLoad() and jQuery’s $(document).ready() events do the same. Both methods seem too similar in simple demo example. But $(document).ready() and pageLoad() methods are very much differ in functioning.In this article, I will explain the major differences between $(document).ready() and pageLoad() methods.

Introducing $(document).ready()

JQuery’s document.ready() method gets called as soon as DOM is ready (means browser has parsed the HTML and built the DOM tree). It is cross browser compatible means behave equally on all browsers. If your web page has large images, it will not wait for loading of images completely. Hence it may called before pageLoad() method. We can have multiple document.ready() methods on a web page that will be called in coming sequence.

Introducing pageLoad()

pageLoad() method gets called when images and all associated resources of the page have been fully loaded. Suppose your web page has large size images then until all the images are not fully loaded on the page, pageLoad() method will not called. pageLoad() method is not browser compatible. We can have only one pageLoad() method on a web page.

Introducing Update Panel Partial PostBack with pageLoad() and $(document).ready()

Since we know, in asp.net update panel gets partially postback to the server. Hence If you are using $(document).ready() and pageLoad() methods on the page, it is mandatory to know the difference between both the methods.
pageLoad() methods is called each and every partial postback of update panel but $(document).ready() is not called each and every partial postback of update panel. $(document).ready() is called only one time (during first time of page loading). Hence code written in $(document).ready() method will not be initialized each and every partial postback.
  1. ID="ScriptManger1" runat="server" />
  2. ID="UpdatePanel1" runat="server">

An ASP.NET AJAX alternative to $(document).ready()

If you are using AJAX on your asp.net web page then you can use Application.add_init() method in place of $(document).ready() for one time initialization.
  1. ID="ScriptManager1" runat="server" />
Note that to call Application.add_init, we need to place it after the ScriptManager. This is required because the ScriptManager injects its reference to MicrosoftAjax.js at that location. Attempting to reference the Sys object before Script Maanager will throw a javascript error "sys is undefined"

Note

  1. $(document).ready()

    1. Best for onetime initialization.
    2. Called as soon as DOM is ready; may called slightly before than pageLoad().
    3. Cross browser compatible.
    4. Unable to re-attach the functionality to elements/controls of the page affected by partial postbacks.
  2. pageLoad()

    1. Not best for onetime initialization if used with UpdatePanel.
    2. Not Cross browser compatible.
    3. Best to re-attach the functionality to elements/controls of the page affected by partial postbacks with UpdatePanel.
  3. Application.Init()

    1. Useful for one time initialization if only ASP.NET AJAX is available.
    2. More work required to wire the event up.
    3. "sys is undefined" javascript error may occurs if you are not careful.
Summary
In this article I try to expose document.ready() and window.onload/pageLoad(). I hope after reading this article you will be able to understand the use of both these methods. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Disable right click on web page and images

Sometimes it is required to prevent your web page images from being copied by another one. You can secure your images by disabling right click or by disabling image context menu on images only. This is an effective approach to prevent your images from being copied or stolen. Here I am going to share the tricks how can you achieve this. This trick will disable right click only on images. Add the below event handler to the img or image tag as shown below:

Disable right click on images

  1. ID="img1" runat="server" ImageUrl=""../ImgLoc/1.png"" oncontextmenu="return false;" />
  2. alt="MyImage" src="../ImgLoc/2.png" oncontextmenu="return false;"/>

Note

  1. It is impossible to prevent images from being stolen completely since there are so many tools and software through which any one can steal yours images. But something is better than nothing.

Disable right click on web page

Similarly we can disable right click on whole page by adding oncontextmenu handler in body tag of webpage. Now this will disable right click on each and every control of a webpage.
  1. ...
  • oncontextmenu="return false;" >
  • ...
  • Show alert on right click

    1. oncontextmenu=" return disableRightClick();">
    2. ...
    Summary
    In this article, I explain how can you disable right click on webpage and images. I hope after reading this article you will be able to do this. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

    Difference Between GridView and DataGrid and ListView

    GridView, DataGrid and ListView Data-bound controls are used to display and modify data in your Asp.Net web application. These controls also contains others Asp.Net controls like as Label, TextBox, DropdownList etc. into a single layout. In this article, I am going to expose the difference among these three.

    Difference between GridView and DataGrid

    GridView
    DataGrid
    It was introduced with Asp.Net 2.0.
    It was introduced with Asp.Net 1.0.
    Built-in supports for Paging and Sorting.
    For sorting you need to handle SortCommand event and rebind grid required and for paging you need to handle the PageIndexChanged event and rebind grid required.
    Built-in supports for Update and Delete operations.
    Need to write code for implementing Update and Delete operations.
    Supports auto format or style features.
    This features is not supported.
    Performance is slow as compared to DataGrid
    Performance is fast as compared to GridView.

    Difference between ListView and GridView

    ListView
    GridView
    It was introduced with Asp.Net 3.5.
    It was introduced with Asp.Net 2.0.
    Template driven.
    Rendered as Table.
    Built-in supports for Data grouping.
    Need to write custom code.
    Built-in supports for Insert operation.
    Need to write custom code.
    Provides flexible layout to your data.
    Need to write custom code.
    Performance is fast is compared to GridView.
    Performance is slow as compared to ListView.

    Object-Oriented Programming Concepts

    OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts.Below are object oriented programming concepts :
    1. Object

      Object is representative of the class and is responsible for memory allocation of its data members and member functions.An object is a real world entity having attributes (data type) and behaviors (functions).
      An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object behavior. For example, the hand can grip something or a Student (object) can give the name or address
    2. Class

      Class is a data structure that contains data members (constants files, events), member function methods, properties, constructor, destructor, indexers and nested type.Basically
      1. It is a user defined data type.
      2. It is a reference type.
      3. Infact class is a tag or template for object.
    3. Encapsulation

      Encapsulation is a mechanism of binding the data member & member function into a single unit known as class.Encapsulation provides a way for abstraction.In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes.
    4. Data abstraction

      Data abstraction is a mechanism to provide the essential features without describing the background details. Means provide the functions to access the hidden (private) data.
      The importance of abstraction is derived from its ability to hide irrelevant details and from the use of names to reference objects. Abstraction is essential in the construction of programs. It places the emphasis on what an object is or does rather than how it is represented or how it works. Thus, it is the primary means of managing complexity in large programs.
    5. Data Hiding

      Data hiding is a mechanism to hide the internal structure of an object from rest of the program.In a class private members are hidden from the rest of the program, hence it supports data hiding. Data hiding is also a way to implement data abstraction.
    6. Polymorphism

      Polymorphism means one thing in many form.Basically polymorphism is capability of one object to behave in multiple ways. Example : A man role changes at home, college, and outside the home. There are following types of polymorphism :
      1. Static polymorphism(compile time) :It is achieved using function overloading and operator overloading.
      2. Dynamic polymorphism(runtime time) : It is achieved using function overriding means using virtual function.

    Difference between function and method

    In programming langauages we have two concepts functions and methods. functions are defined in structural language and methods are defined in object oriented langauge. The difference between both is given below :

    Functions

    1. Functions have independent existence means they can be defined outside of the class. Ex:- main() function in C, C++ Language
    2. Functions are defined in structured languages like Pascal,C and object based language like javaScript
    3. Functions are called independently.
    4. Functions are self describing unit of code.
    1. //function main in C
    2. void main()
    3. {
    4. int a,b,c;
    5. a=5;
    6. b=6;
    7. c=a+b;
    8. printf("Sum is : %d",c);
    9. }

    Methods

    1. Methods do not have independent existence they are always defined with in class. Ex:- main() method in C# Language that is defined with in a class
    2. Methods are defined in object oriented languages like C#, Java
    3. Methods are called using instance or object.
    4. Methods are used to manipuate instance variable of a class.
    1. //method sum in C#
    2. class demo
    3. {
    4. int a,b,c;
    5. public void sum()
    6. {
    7. a=5;
    8. b=6;
    9. c=a+b;
    10. Console.WriteLine("Sum is : {0}",c);
    11. }
    12. }