Sunday, August 21, 2011

SQL statements Part 1 (DML)...

In this post I want to discuss about DML statements in SQL.

DML ::


       DML means Data Manipulation Language. These statements will be used to modify the data present in the tables. These statements will work on the data, there is no relation for these statements with the structure of the tables. Data manipulation involves Inserting data into tables, modifying the data present in the tables and removing the data from the tables. DML statements require Transaction control.

     Any change which is made to the database by a DML statement will be called as a transaction. So, any change made by DML statement needs to be controlled by TCL statements (Transaction Control). I will discuss about these TCL statements in my future posts. In order to handle the changes done to the database by DML statements , we need transaction control.

    We have 4 types of DML statements available in the SQL. They are,

                  1. Insert
                  2. Delete
                  3. Update
                  4. Merge

1. Insert ::


        Insert will be used to insert new data into the tables. In other words we can say, Insert will be used to create new rows in the tables. We cannot effect the existing rows from the table using Insert statement.

   We have two different syntax's for writing the Insert statement. They are,
         1. INSERT INTO tab_name values ( val1, val2, ...... valn);
         2. INSERT INTO tab_name (col1, col2, col3, ....., coln) VALUES (val1, val2, ..... , valn);

 First syntax is the general syntax of insert statement. In this type of syntax we must pass values to all the columns of the table, and that also in the order of presence of these columns in the table. Otherwise, we will get error. (In this post I don't want to discuss about duplicate rows, not null values... etc. This I will discuss while discussing the constraints of the table).

   For Example, Let us say we have a table called EMP in the database. The columns present in this table are Empno, Ename, Sal and Deptno. Now If I want to insert new row into this table using first syntax then,

   INSERT INTO Emp VALUES ( 1111, 'RAM', 5000, 20);

Now, I will discuss about the 2nd syntax. In this second syntax we can enter values into the columns of the table while creating new rows in our own desired order irrespective of the order of presence of these columns in the table. This we can achieve by mentioning the our desired order between tab_name and VALUES as shown in the syntax. In this syntax we can also give values to specific columns of the table only. This means, If  we have 4 columns in the table and while creating the new row If you want to give values only to 3 columns we can do that by using second syntax.

 Ex : INSERT INTO Emp ( Empno, Ename, Sal) Values (2222, 'RAJ', 2000);
        INSERT INTO Emp (Empno, Sal, Ename) Values (3333, 1500, 'KRISH');

 We have one more syntax for creating new rows in the table using Insert statement.  That is shown below.

      INSERT INTO Emp VALUES ( &val1, &val2, ..... &valn).

     In this syntax, if we run the above statement then system will ask us to enter the values one by one. after entering all the values row will be created in the table. after that if we hit '/' at the command prompt then system will ask us to enter the values again to create another row. Like this we can create n number of rows.

 In this way we can create new rows in the table using different syntax's.

2. Delete ::

           Delete will be used to remove the data from the tables. Using Delete we can remove single row or multiple rows from the table. We have two syntax's for Delete statement.

    1. DELETE FROM tab_name;
    2. DELETE FROM tab_name WHERE condition ;

If we use first syntax then all the rows from the table will be removed. If we want to remove specific rows only then we have to use the second syntax.

  Ex :  DELETE FROM Emp;

     If we execute this statement then all the rows from the EMP table will be removed.

           DELETE FROM Emp WHERE empno = 1111;

    If we execute this statement then the row belongs to employee with Empno 1111 will be removed. So, using this syntax we can remove specific rows from the table by specifying the Where clause.

    Using Delete we can remove entire row from the table only. we cannot remove values from specific columns of the table using Delete statement.

3. Update ::


           Update will be used to modify the data present in the tables. using single update statement we can modify the data present in the single column or multiple columns of the table. Similarly, using single update we can modify data present in single row or multiple rows.

    Different syntax's for Update statement are,

     1. UPDATE tab_name SET col_name = val ;
     2. UPDATE tab_name SET col_name = val WHERE condition;
     3. UPDATE tab_name SET col1 = val1, col2 = val2 , .... ;
     4. UPDATE tab_name SET col1 = val1, col2 = val2,... WHERE condition;

  If we use the first syntax, then in all the rows of the table the column specified (col_name) will be updated to the provided value (val).
  If we use second syntax, then for the rows which will satisfy the condition specified in the WHERE clause column specified (col_name) will be updated to the provided value(val).
 Third and Fourth syntax's are similar to first and second syntax's only , only difference here is instead of updating single column we are updating multiple columns of the table.

 EX : UPDATE Emp SET deptno = 20; (This will update deptno to 20 in all the rows of Emp table).
         UPDATE Emp SET sal = 5500 WHERE ename = 'RAM'; (This will update sal of Ram to 5500).
         UPDATE Emp SET sal = 6000, deptno = 30 ;
         UPDATE Emp SET sal = 7000, deptno = 40 WHERE ename = 'RAM';

 As I discussed before, If we want to insert value to a particular column of existing row we cannot use Insert statement.We have to use Update statement to update the column to that particular value. Similarly If we want to remove values from specific column then we cannot do it using Delete statement. We have to update that column or columns to null.

    Ex : UPDATE Emp SET deptno = null WHERE empno = 3333;

4. Merge ::


           Merge statement will be used to combine data from two similar tables into single table. In order to do merge operation two tables containing similar columns  are required. Generally we will use merge statement to insert data from one table into other similar table. While doing this If same row is present in both the tables then we cannot perform insert operation due to the constraints of the table. This is where the merge operation is very useful.

         Merge is the combination of both Insert and Update operations. So, while merging data from one table into another table, If we have same row in both the tables then we will perform UPDATE operation. If different row is present, then we will perform INSERT operation. This is the concept involved with MERGE statement.

   Syntax for writing the merge statement is,

         MERGE INTO tab1
         USING tab2
         ON condition
         WHEN MATCHED THEN
         UPDATE SET tab1. col  = tab2. col
         WHEN NOT MATCHED THEN
         INSERT VALUES ( tab2. col1, tab2.col2, ........ );

   Let me explain this merge statement with an example. Let us say we have two similar tables Emp and Emp1 . I want to merge data from Emp1 to Emp table. Then statement used for this is,

      MERGE INTO Emp
      USING Emp1
      ON emp.empno = emp1.empno
      WHEN MATCHED THEN
      UPDATE SET emp.sal = emp1.sal
      WHEN NOT MATCHED THEN
      INSERT VALUES ( emp1.empno, emp1.ename, emp1.sal, emp1.deptno);

 So, here I am merging data from Emp1 table into Emp table. ON will be used to check the common thing between the two tables to decide when to do update and when to do Insert. Here using Empno column I am deciding this. Generally we will use primary key columns where unique values will  be present to compare in ON condition.

    Now if the condition is satisfied then, WHEN MATCHED condition will be satisfied and the UPDATE statement will be performed. Here we are taking sal from emp1 table and updating it to emp table. If condition is not satisfied then WHEN NOT MATCHED condition will be satisfied and new row will be inserted into Emp table from Emp1 table.

    So, This is how Merge statement will be worked.  So, in case of Merge operation if similar rows are there we will perform Update operation and if different row is there then we will perform Insert operation.
     
  So, these are the different DML statements available in SQL.

Overview of SQL topics....

I am going to discuss lot of topics in SQL in my  future posts.... In this post I want to give overview of those topics....

1. SQL Statements :: 
           SQL statements are the base for SQL . In this topic I will discuss about different types of SQL statements available in SQL and their usage. To give some idea, we have 5 types of SQL statements.
                           1. DRL (Select)
                           2. DML ( Insert, Delete, Update, Merge)
                           3. DDL (Create, Alter, Truncate, Drop, Rename)
                           4. DCL (Grant, Revoke)
                           5. TCL (Commit, Rollback, Savepoint)
        These statements are very useful while working with the database. We will not write any statement in SQL without using any of these statements.

2. SQL Functions ::
            There are many types of SQL functions which are very useful while writing the SQL queries.
       Depending on number of results they are producing for each row / group of rows, functions are divided into two types.
       1. Single row functions      2. Multiple row functions
   These functions again depending on type of data they are effecting divided into,
       1. Numeric Functions like Round, Trunc ....
       2. Character Function like Substr, Instr ....
       3. Date functions like Months_between, last_day......
   We have some more functions like,
       Conversion Functions used for data conversions like to_date, to_char, ........
       General functions used for handling null values like NVL, NVL2 .....
       Conditional Functions like Case and Decode
       Group Functions like Sum, Avg, .....

3.  Where , Group by, Order by and Having clauses..

4.  Joins which will be used to get data from one or more tables
      We will discuss about different joins like,
       Inner Join, Outer Join, Self Join, Equi Join, Non Equi Join........
     This is one of the most important topics of SQL. Because in real life most of the times queries which we will write will contain joins only.....

5. Sub Queries :: This is also a most important topic of SQL. We have two types of Sub queries.
             1. Correlated Sub queries    2. Non Correlated Sub queries

6. Set Operators :: 
            Set operators will be used to combine the results from 2 queries into single result.
      Different Set Operators in SQL are,
          Union, Union All, Minus, Intersect

7.  Views :: In this we will discuss about what are views , why we need views and how we can create them.


8.  Indexes :: These are very important in improving the performance of the queries. We have different indexes like,
               Function Based Indexes
               Bit map Indexes
               B-tree Indexes

9.  Synonyms :: used for aliasing


10. Sequences :: Used for generating the unique sequence of values.

11. ROWNUM and ROWID pseudo columns

12. Analytical Functions ::
                 This topic will come under advanced SQL. In this we will discuss about Rank and Dense_Rank functions.

13. Hierarchical Retrieval ::
              This is also advanced SQL topic. In this we will discuss about Connect By Prior clause.

14. Constraints ::

              In this we will discuss about Primary key, Foreign Key, Check constraint, Unique key and Not Null constraints.

15. Datatypes ::
     
            In this we will discuss about some useful datatypes in SQL. like Number, Date, ... etc.

        Apart from these topics I will also discuss about design models like ER Modeling,... etc. Also, I will try to post about different advanced topics in SQL.
           
  

Sunday, August 14, 2011

Introduction to SQL.....

            Before learning the concepts of SQL and PL/SQL we need to know the importance of the ORACLE language and also about few introductory things. So, In this post I am going to give Introduction to SQL which will give  the better understanding of why to learn this language. Also I will provide some basic information about different topics related to SQL which I am going to discuss about in my upcoming posts.
           As I mentioned in my previous post we need to have a database in our system to practice the SQL or PL/SQL. So, for us database is the most important thing. So, we need to know about what is a Database and why we need it.

What is a Database :: ?
            The answer to this question is very simple. Database is the place where we can store our data. The Oracle database is designed in such a way that any data we want to store in it, we can store it in the form of 2 dimensional tables (In database terminology we will call them as Relations).
            A table will contain Rows (In database terminology we will call them as Tuples) and Columns (In database terminology we will call them as Attributes). I will explain this concept with small example.
            Basically tables will contain structure and data. The structure of the table includes Name of the table, Columns of the table(along with their names and data types along with constraints if any). The name of the table should be unique (duplicate names are not allowed). The name should be in such a way that by seeing the name itself we must be able to identify the type of information  stored in the tables.
 For EX: See the following table.

               EMP:    Empno
Empno
Ename
Deptno
Hire_date
Sal
1
Ram
10
10-jul-2011
25000
2
Raj
20
15-jul-2011
15000
3
Rao
30
20-jul-2011
10000
 
  Name of this table is EMP, means employee. So, by seeing this name itself we will know that Employee details were stored in this table.
     This table contain 5 columns. Generally each column of a table will specify one unique attribute of the data which we are going to store in the table. If you observe this table we have columns named as Empno (which will give us employee number of the employee), Ename (name of employee), Deptno (dept to which employee belongs), Hire_date( joining date of employee) and Sal(sal of employee). Like this each column is specifying the unique feature of a particular employee. Regarding data type we will discuss about it in coming posts. But to give some idea, The data type will specify the type of data which we are going to store in that particular column (Ex : Numeric, character.... etc).
      Now coming to the data part of the table, Rows will contain the data in the tables. Each row will contain some unique information of the type of the data which we are going to store. In our example each row will contain unique information of  an employee of the company. Generally we will maintain only one row for an employee in the table. (This can be maintained with the constraints about which we will discuss in coming posts)
      We can store any type of data in the database in form of tables . We can store pictures and files also.
So, By this we have got some idea about what is a database. Now next question is why we need database?

Why we need a Database :: ?

    Let me explain this with small example. Let us take the example of GMAIL, through which we can send mails, we can chat with friends.... by logging in with our credentials(Username and Password).
    Here in Gmail our Usernames should be unique. While registering it will ask to enter unique user name by checking the availability and each username will be associated with a password. So, where this information will be stored?? This information will be stored in the tables of a database server which is located at the company. So, each time when you are trying to login then it will validate the information with the data stored in the database and it will do appropriate action.
   Similarly in real life almost in all the places database becomes a mandatory thing as we can store data into it from any place and we can access that data from any place. Take Banking, Railways ticket reservations, Bus ticket reservations, Cinema ticket reservations and information, Hospitals..... any place you take... they need a database to maintain the information.

So, Now we understand the importance of the database in our real life. Now, here comes most importane question. What is SQL?

What is SQL :: ?
   
      SQL means Structured Query Language. The statements which we are going to write in this language will be called as Queries. Each query will be having its own structure. That is why this language is called as Structured Query Language. Ok fine. Now we know what is SQL. The next question is Why we need SQL?

Why we need SQL :: ?

     So as discussed in the above sections,we will store the information in the database in the form of tables. How will you store?? we cannot take it by hand and we cannot put it in the database right.. Similarly How will you see the information?? How will you modify the information?? To do all these things we need a mode of communication with the database. We need to interact with the database by some medium. Here comes the usage of a Database language by which we can perform all the above mentioned tasks on the database. One such type of Language is SQL.
     So, SQL is a communication language which will be used to interact with the database.
So, As part of SQL we are going to learn about how to perform all the above mentioned tasks. 

So, this is a small introduction to what we are going to discuss in my upcoming posts. If you have any queries you can post them through comments.


Saturday, August 13, 2011

Oracle Database Installation - Links and Steps...


     As I mentioned in my previous post, I am going to place many helpful posts to understand the concepts of SQL and PL/SQL (ORACLE). Oracle is the simplest language of all the programming languages  to learn. But to get command on this language we need to do lot of practice. So, I am going to place posts related to different practice exercises also.

    In order to practice Oracle we need Oracle Database (9i or10g or 11g) to be installed in our systems. So, In this post I am providing links and steps for installing the Oracle Database.

   We can download the Oracle database10g from the following links. Actually, the database file which we are going to download for installation will depend on the type of OS in our systems. 

    If your are using Vista(32-bit) or Windows 2007(32-bit) OS then the file in the following link is compatible.

you need to download only the following file from the above link.
                         10203_vista_w2k8_x86_production_db.zip(797,326,161bytes)
While downloading, It may ask for registration. So, register in WWW.ORACLE.COM to download the file. It will take only few minutes.
Similarly for Vista(64-bit) or Windows 7(64-bit) you can download from following link.
   If you are using MAC OS then you can download from following link,
    For Windows XP(32-bit) OS, following link can be used to download the compatible file,
   For Windows XP(64-bit) following link can be referenced,
    Before downloading files from any of the above links, you need to accept the license agreement.
   Check the following link for steps to install the Oracle Database in the system containing MAC OS.
              For remaining OS, refer following link for installation steps.
        If you have any queries related to download or installation you can post it in your comments.





Saturday, August 6, 2011

Few things about the blog....

  Hi, This blog is exclusively for those who want to learn SQL and PL/SQL easily. Some important information about this blog is listed below.

---> I will add many posts for the different topics in SQL and PL/SQL along with detailed explanation
        including examples etc....
---> I will also add Power point presentation to different topics which are prepared by me.
---> I will try to add audio and video tutorials also which are prepared by me.
---> If you have any queries about anything you can update me. I will provide the required solutions or  
        information for the queries in this blog.
---> you can subscribe to the posts in this blog by entering your email address in the home page of this blog.

  Requesting your support to maintain this blog effectively and informatively. Hope to hear from you soon.