Tampilkan postingan dengan label sql. Tampilkan semua postingan
Tampilkan postingan dengan label sql. Tampilkan semua postingan

Oracle 10g Pagination Query SQL Example for Java Programmer

Kamis, 27 Februari 2014

Many time we need SQL query which return data page by page i.e. 30 or 40 records at a time, which can be specified as page size. In fact Database pagination is common requirement of Java web developers, especially dealing with largest data sets.  In this article we will see how to query Oracle 10g database for pagination or how to retrieve data using paging from Oracle. Many Java programmer also use display tag for paging in JSP which supports both internal and external paging. In case of internal paging all data is loaded in to memory in one shot and display tag handles pagination based upon page size but it only suitable for small data where you can afford those many objects in memory. If you have hundreds of row to display than its best to use external pagination by asking databaseto do pagination. In pagination, ordering is another important aspect which can not be missed. It’s virtually impossible to sort large collection in Java using Comparator or Comparablebecause of limited memory available to Java program, sorting data in database using ORDER BY clause itself is good solution while doing paging in web application.
Read more »
Read More..

SQL Interview Questions and Answers on deleting records

Senin, 10 Februari 2014



The following is a very popular SQL job interview question.

Q. What is the difference between "Truncate" and "Delete" commands?
A. 
  • TRUNCATE TABLE_NAME always locks the table and page but not each row, whereas  DELETE statement is executed using a row lock, each row in the table is locked for deletion.
  • Truncate removes all the records in the table whereas delete can be used with WHERE clause to remove records conditionally. That is remove only a handful number of records.
  • Truncate performance is much faster than Delete, as its logging is minimal wheres the Delete command logs every record.
  • Truncate does not retain the identity, whereas DELETE command retains the identity. When you use Truncate, If the table contains an identity column, the counter for that column is reset to the seed value that is defined for the column.
  • Truncate cleans up the object statistics and clears the allocated space whereas Delete retains the object statistics and allocated space.
  • TRUNCATE is a DDL (Data Definition Language) and DELETE is a DML (Data Manipulation Language). 
  • Data removed by TRUNCATE command cannot be generally rolled back unless the database server specifically supports it. The DELETE command can rollback a transaction.
  • The TRUNCATE command does not fire any triggers, whereas the DELETE command fires any triggers defined on the table. For example, to keep an audit trail of records that have been deleted by inserting the deleted records into an audit table via the DELETE triggers.

Q. When will you use a truncate command?
A.TRUNCATE is useful for purging a table with huge amount of data. Alternatively, you can drop the table and recreate it that makes sense. Firing a delete command instead of a truncate command to empty a table with millions of records can result in locking the whole table and also can take longer time to complete, and at times cause the machine to hang.

The truncate command is executed as shown below.

TRUNCATE TABLE table_name



Q. Which command will you use to periodically purge data from your tables as part of a house keeping job?
A.  Use a DELETE command within a transaction with a WHERE clause to remove data that are older than 7 years. Remove  large amount of data in batches as opposed to in a single transaction.

Q. How will you delete a few records from single table
A.

DELETE FROM parent p WHERE p.parent_name = Peter


Q
. How will you delete a few records from parent and child tables where the parent table with parent_name = Peter?
A.

Firstly, you need to delete the child records because the integrity constraint wont let you delete the parent record when there are child records.

DELETE child

FROM  parent p, child c

WHERE p.parent_id = c.parent_id

  AND p.parent_name = Peter


Now, the parent table can be deleted as shown below

DELETE FROM  parent p WHERE p.parent_name = Peter


Note: Please note the difference in syntax when you make a join with the child. When there is only a single table involved, it is "DELETE FROM table_name", but when there is a join, it is "DELETE table_name" and then the "FROM" with the join clauses.


Q.  What do you do with the PURGE command?
A. The purge command is used to clear the recycle bin. It is generally used with the DROP command. For example,

drop table tablename purge;


the above  command will clear away the table from database as well as from the recycle bin. After executing  the purge command, you cannot retrieve the table using a flashback query. 



    You may also like:
    Read More..

    SEO Networking SQL Books

    Kamis, 06 Februari 2014

    Note: If you want any thing download then go to link and press CTRL + S. Link is given on content name.


    Read More..

    What all new Oracle users should know about SQL Plus

    Senin, 03 Februari 2014

    Ok, I realize anyone who’s used oracle much will already know this, but I still get asked this question a lot. Usually its from people who know SQL, but havent worked a lot with Oracle.

    If you are using SQL Plus with Oracle, you may be surprised by the odd way it displays results. This is because by default SQL Plus assumes your screen is only 80 characters wide, and 14 rows tall, and wraps accordingly.

    You can fix this by entering the following in SQL Plus:
    set pagesize 50000 
    set linesize 32767

    By default those values are 14 and 80 respectively. 32767 is the maximum size for linesize and 50000 is the maximum size for pagesize.
    Read More..