In Oracle if you want to limit the records based on ROWNUM(mostly while doing pagination) the between is not going to work.

Below Doesn’t work

 
 SELECT Column1, Column2, ......., Column N  
   FROM TableName
  WHERE ROWNUM Between 5 AND 10; 

The alternate for this is as Follows

 
SELECT * FROM
 (SELECT rows_to_page.*, ROWNUM rnum FROM
   (SELECT Column1, Column2, ......., Column N  
      FROM TableName
     WHERE ROWNUM Between 5 AND 10) rows_to_page
    WHERE ROWNUM < end_offset) 
 WHERE rnum > start_offset;
--16-12-2015 12:33:21
SELECT SYSDATE FROM dual;

--16-12-2015
SELECT trunc(SYSDATE) FROM dual;

--16-12-2015
SELECT to_date(SYSDATE) AS ToDate  FROM dual;

--16-12-2015
SELECT to_date(SYSDATE, 'DD-MM-YY') AS ToDate  FROM dual;

--16-12-2015
SELECT to_char(SYSDATE, 'DD-MM-YYYY') AS ToDate  FROM dual;

Table Function return a result set that mimics what we would normally expect from a traditional SQL SELECT statement

Table functions are a new feature in Oracle9i that allow you to define a set of PL/SQL statements that will, when queried, behave just as a regular query to table would. The added benefit to having a table function is that you can perform transformations to the data in question before it is returned in the result set.

CREATE OBJECT
Create our own object type called PERSON_DETAILS.Then we create a table of PERSON_DETAILS called PERSON_DETAILS_TABLE.

CREATE TYPE PERSON_DETAILS AS OBJECT
       (USER_NAME     VARCHAR2(50),
        ADDRESS       VARCHAR2(50),
        LOCATION      VARCHAR2(50));
/
CREATE TYPE PERSON_DETAILS_TABLE AS TABLE OF PERSON_DETAILS;
/

PIPELINED Clause

Within the CREATE FUNCTION clause, there is a new option called PIPELINED. This option tells Oracle to return the results of the function as they are processed, and not wait for a complete execution or completion of the result set. This pipelining of the result set to one row at a time has the immediate advantage of not requiring excessive memory or disk staging resources.
PIPE ROW(out_rec)

The PIPE ROW statement is the interface or mechanism to send a piped row through the PIPELINED option through to the caller of the function.

Working with a simple pipelined function requires 2 things

  • collection type
  • pipelined function
 CREATE OR REPLACE TYPE number_ntt AS TABLE OF NUMBER;
CREATE FUNCTION row_generator(rows_in IN P_INTEGER) RETURN number_ntt
  PIPELINED IS
BEGIN
  FOR i IN 1 .. rows_in LOOP
    PIPE ROW(i);
  END LOOP;
  RETURN;
END;
/

The CSV_TABLE is a collection which has rows of comma separated value.

CREATE OR REPLACE TYPE "CSV_TABLE" as table of varchar2(32767)
CSV_TABLE
FUNCTION CSV_TO_TABLE(p_delimted_string VARCHAR2,
                         p_delimter        VARCHAR2 := ',')
  RETURN CSV_TABLE
  PIPELINED IS
  indexCount PLS_INTEGER;
  csvString  VARCHAR2(32767) := p_delimted_string;
BEGIN
  LOOP
    indexCount := instr(csvString, p_delimter);
  
    IF indexCount > 0 THEN
      PIPE ROW(substr(csvString, 1, indexCount - 1));
      csvString := substr(csvString, indexCount + length(p_delimter));
    ELSE
      PIPE ROW(csvString);
      EXIT;
    END IF;
  
  END LOOP;
  RETURN;
END CSV_TO_TABLE;

Input

A,B,C,D

Output

A
B
C
D

The above output is a collection.

 SELECT PACKAGE_NAME.CSV_TO_TABLE('A,B,C,D') FROM DUAL;

When the Column value is NULL it would be replaced by ‘~’ in output

  SELECT NVL(FAX_NO, '~') FROM DUAL;
  SELECT NVL(NULL, '~') FROM DUAL;

The Below query can be used in scenarios where 3 drop downs are used and Location is mandatory and loaded first based on which Area and Pincode should be populated and selection can be made based on that later.

  SELECT Name, Age, PhoneNo
    FROM Person 
   WHERE Location =  p_location and 
         Area =  NVL(p_area, Area) and 
         PinCode =  NVL(p_pin_code, PinCode);

Having more than one input in where clause
i.e

 SELECT * 
  FROM Person 
 WHERE Location IN('North Chennai', 'South Chennai');

When the same query is used in search screen it may have Three possible Values

  • NULL
  • Single Value
  • Multiple Value

The above query works for Single and Multiple value but does not work for NULL.The Above query works for NULL and Single Value but not for Multiple Value.

The below is a simple query which works when PERSON_ID is NULL, Single Value, Multiple Value(CSV).

 SELECT PERSON_ID
  FROM PERSONS P
 WHERE ((CASE
         WHEN 'P101' IS NULL THEN
          NULL
         ELSE
          'P101'
       END) IS NULL OR P.WATERFALL_ID IN ('P101'));

For Multiple Value we need to do slight modification.We need to convert the CSV values into table and give it as input.

 SELECT DISTINCT PERSON_ID
   FROM tblPerson 
  WHERE nvl(PERSON_ID, '~') IN (SELECT column_value
                                  FROM TABLE(PACKAGE_NAME.CSV_TO_TABLE(P_CSV_PERSON_ID))
                                UNION ALL
                                SELECT '~'
                                  FROM dual);

The select query in the where clause will take the value of P_CSV_PERSON_ID (Single or Multiple value) else it will take ~ in case it is NULL

The Other workaround to this is as below

SELECT PERSON_ID
    FROM tblPerson 
   WHERE (((CASE
           WHEN P_CSV_PERSON_ID IS NULL THEN
            NULL
           ELSE
            P_CSV_PERSON_ID
         END)) IS NULL
      OR PERSON_ID IN
         (SELECT *
            FROM TABLE(PACKAGE_NAME.CSV_TO_TABLE(P_CSV_PERSON_ID))));

where

 P_CSV_PERSON_ID= 'P101,P102'

For more details on CSV_TO_TABLE refer Link

The data you are trying to insert already exists and its a primary key with unique value

 unique constraint (DATABASENAME.PK_COLUMN_NAME) violated

When you try to insert some value which are not allowed for the column this error is thrown.Similar to ENUM of Java.

In the below example CATEGORY Column may have MALE and FEMALE But when you try to insert BOTH this is going to throw error since it is not allowed value.

CATEGORY IN ('MALE','FEMALE')
check constraint (DATABASENAME.CHK_CATEGORY) violated

If the Parent table referred by Foreign Key does not have the value then this is thrown

integrity constraint (DATABASENAME.FK_CATEGORY) violated - parent key not found

Whenever a table is created in Oracle its shld be done by Four step process

  1. Table Creation
  2. Adding Constraints(Primary Key, Foreign Key)
  3. Adding Synonyms
  4. Giving Grants
  5. Alter Queries

Table Creation

create table TABLE_NAME_1
( COLUMN_NAME_1   varchar2(50),
  COLUMN_NAME_2   NUMBER(1),
  COLUMN_NAME_3   DATE not null,
  COLUMN_NAME_4   NUMBER(38)
 );

Constraint
Query 1

alter table TABLE_NAME_2 
  add constraint FK_COLUMN_NAME_1 foreign key (COLUMN_NAME_1) references TABLE_NAME_1 (COLUMN_NAME_1);

Query 2

alter table TABLE_NAME_3 add constraint fk_column_1 
foreign key (COLUMN_1, COLUMN_2) references TABLE_NAME_2 (COLUMN_1, COLUMN_2);

Synonym

 CREATE PUBLIC SYNONYM TABLE_NAME_1 FOR OWNER_NAME.TABLE_NAME_1;
 CREATE PUBLIC SYNONYM SEQUENCE_NAME_SEQ FOR OWNER_NAME.SEQUENCE_NAME_SEQ;
 GRANT ALL ON SEQUENCE_NAME_SEQ TO ADMIN,PART,ALL_DEVELOPERS;

Grants

 GRANT ALL ON TABLE_NAME_1 TO ADMIN,PART,ALL_DEVELOPERS;
 GRANT INSERT,UPDATE,DELETE,SELECT ON TABLE_NAME_1 TO ADMIN,PART,ALL_DEVELOPERS;

Other Queries

 ALTER TABLE TABLE_NAME_1 COLUMN_NAME_1 NOT NULL;
 ALTER TABLE TABLE_NAME_1 MODIFY COLUMN_NAME_1 varchar2(50);
 ALTER TABLE TABLE_NAME_1 DROP COLUMN COLUMN_NAME_1;
 ALTER TABLE TABLE_NAME_1 DROP CONSTRAINT COLUMN_NAME_1;

Things to Consider

While creating Primary key(No Null Values allowed like unique key) as a combination of two or more columns you should take in to consideration only the non null columns as part of unique key.

 ALTER TABLE TABLE_NAME
   ADD CONSTRAINT PK_TABLE_NAME PRIMARY KEY (COLUMN_NAME_1, COLUMN_NAME_2, COLUMN_NAME_3)
  USING INDEX TABLESPACE INDX;

COLUMN_NAME_1, COLUMN_NAME_2, COLUMN_NAME_3 should be a Non Null column.

When deleting a Table to run new script the following should be done.

DROP TABLE TABLE_NAME;
DROP SEQUENCE SEQUENCE_NAME;
DROP PUBLIC SYNONYM SYNONYM_NAME;

In case you are deleting multiple Table the same thing should be done with tables grouped together as one below.

DROP TABLE TABLE_NAME;
.
.
.

DROP SEQUENCE SEQUENCE_NAME;
.
.
.

DROP PUBLIC SYNONYM SYNONYM_NAME;
.
.
.
.

The Child Table referencing the Parent table should be deleted first.

 DROP TABLE CHILD_TABLE_NAME;
 DROP TABLE PARENT_TABLE_NAME;

Checking if the table column is referenced some where by Child Table in form of Primary Key

SELECT TABLE_NAME AS "CHILD_TABLE"
       ,CONSTRAINT_NAME
  FROM ALL_CONSTRAINTS T
 WHERE R_OWNER = 'OWNER_NAME'
   AND CONSTRAINT_TYPE = 'R'
   AND R_CONSTRAINT_NAME IN (SELECT CONSTRAINT_NAME
                               FROM ALL_CONSTRAINTS
                              WHERE CONSTRAINT_TYPE IN ('P', 'U')
                                AND TABLE_NAME = 'TABLE_NAME'
                                AND OWNER = 'OWNER_NAME')
 ORDER BY TABLE_NAME
         ,CONSTRAINT_NAME;

Sequence

CREATE SEQUENCE TEST_SEQ
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;

Audit Trigger

CREATE OR REPLACE TRIGGER TRIGGER_NAME
BEFORE INSERT OR UPDATE ON TABLE_NAME
REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW
BEGIN
  IF INSERTING THEN
    :NEW.CRE_USER_UID := USERUID;
    :NEW.CRE_TIMESTAMP := SYSDATE;
  ELSIF UPDATING THEN
    :NEW.UPD_USER_UID := USERUID;
    :NEW.UPD_TIMESTAMP := SYSDATE;
  END IF;
/
END TRIGGER_NAME;

Note the / in the End of Trigger

SELECT A.PinCode
      ,A.Location
      ,A.Year
      ,SUM(CASE
             WHEN A.Area IN ('North Chennai', 'South Chennai') THEN
              A.Amount
             ELSE
              0
           END) AS total_amount_north_south
      ,SUM(CASE
             WHEN A.Area IN ('East Chennai', 'West Chennai') THEN
              A.Amount
             ELSE
              0
           END) AS total_amount_east_west
      ,SUM(A.Amount) AS Total_amount_for_Chennai
  FROM Tamilnadu 
 GROUP BY A.PinCode
	 ,A.Location
         ,A.Year

For Procs

---for procs  
 CREATE PUBLIC SYNONYM  PROC_NAME
   FOR DATABASE_NAME.PROC_NAME;

   GRANT EXECUTE, DEBUG ON PROC_NAME TO ADMIN,ALL_DEVELOPERS;

For Tables

---for tables
CREATE PUBLIC SYNONYM  TABLE_NAME
   FOR TABLE_NAME_SYNONYM;

GRANT INSERT,UPDATE,DELETE,SELECT ON TABLE_NAME TO ADMIN,ALL_DEVELOPERS;