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.

         

No comments:

Post a Comment