May 7, 2013

Oracle Collection - Varrays


Varrays hold a fixed number of elements, although the number of elements can be changed at runtime. Like nested tables, varrays use sequential numbers as the index or key to the elements. You can define equivalent SQL types, allowing varrays to be stored in database tables. Varrays are a good choice when the number of elements is known in advance, and when the elements are likely to be accessed in sequence.


      A VARRAY is an array of varying size. It has an ordered set of data elements, and all the elements are of the same data type. The number of elements in a VARRAY is the "size" of the VARRAY. You must specify a maximum size (but not a minimum size) when you declare the VARRAY type.

         In general, the VARRAY type should be used when the number of items to be stored is small; it is not suitable for large numbers of items or elements. Note that you cannot index or constrain VARRAY values. Varray is available in PL/SQL as well as in SQL

Arrays must be dense (have consecutive subscripts). So, you cannot delete individual elements from an array


  • Use to preserve ordered list
  • Use when working with a fixed set, with a known number of entries
  • Use when you need to store in the database and operate on the Collection as a whole

  •   Varrays in SQL

    CREATE OR REPLACE TYPE varray_type AS VARRAY(SIZE) OF element_type;

     Varrays in PLSQL

    TYPE varray_type IS VARRAY(SIZE) OF element_type (datatype) ;

    varray_name varray_type; (variable type in PL/SQL)

    varray_name := varray_type();(create it using the constructor)


    CREATE TABLE table_name(field1 [VARCHAR2 NUMBER DATE],field2 varray_type);

    Example :
    CREATE TYPE Project AS OBJECT ( project_no NUMBER(2), title  VARCHAR2(35),cost  NUMBER(7,2));

    CREATE TYPE ProjectList AS VARRAY(50) OF Project;

    CREATE TABLE department ( dept_id  NUMBER(2), name  VARCHAR2(15), budget NUMBER(11,2), projects ProjectList);


    BEGIN
       INSERT INTO department
          VALUES(30, 'Accounting', 1205700,
             ProjectList(Project(1, 'Design New Expense Report', 3250),
                         Project(2, 'Outsource Payroll', 12350),
                         Project(3, 'Evaluate Merger Proposal', 2750),
                         Project(4, 'Audit Accounts Payable', 1425)));
    END;

    DECLARE
       new_projects ProjectList :=
          ProjectList(Project(1, 'Issue New Employee Badges', 13500),
                      Project(2, 'Develop New Patrol Plan', 1250),
                      Project(3, 'Inspect Emergency Exits', 1900),
                      Project(4, 'Upgrade Alarm System', 3350),
                      Project(5, 'Analyze Local Crime Stats', 825));
    BEGIN
       UPDATE department
          SET projects = new_projects WHERE dept_id = 60;
    END;

    ---

    May 3, 2013

    Oracle Collection - Nested tables

    Nested tables


             Nested tables hold an arbitrary number of elements and use sequential numbers as the index or key to the elements. Nested tables can be stored in database tables and manipulated through SQL. They are appropriate for data relationships that must be stored persistently. Nested tables are flexible in that arbitrary elements can be deleted, rather than just removing an element from the end. Note that the order and subscripts (keys) of nested tables are not preserved as the table is stored and retrieved in the database

       Nested tables can be stored in a database column.Nested tables are initially dense, but they can become sparse (Data does not have to be stored in consecutive rows) when elements are deleted. Nested Table is available in PL/SQL as well as in SQL.

    Nested tables are dense, but they can become sparse (have nonconsecutive subscripts). So, you can delete elements from a nested table using the built-in procedure DELETE. That might leave gaps in the index, but the built-in function NEXT lets you iterate over any series of subscripts

    CREATE OR REPLACE TYPE nestedtable_type AS TABLE OF element_type; (PLSQL Type in SQL)

    CREATE TABLE nested_table (id NUMBER, col1 nestedtable_type) NESTED TABLE col1 STORE AS col1_tab;   (Nested Table)

    TYPE nestedtable_type IS TABLE OF element_type; (PLSQL Type in PLSQL)


    Oracle Collection - PLSQL Table

    Collections


        A collection is an ordered group of elements ,of the same type.Collection have only one dimension,we can nodel multi-dimensional arrays by creating collections whose elements are also collections.

    The three type of collections are following

    1) Associative Array
    2) Nested tables
    3) Varrays

    Associative Array
    TYPE array_type IS TABLE OF element_type INDEX BY key_type;
    array_name array_type;
    array_name(index) := value;


    The “Associative Arrays” are also known as “Index-By” tables in PL/SQL.Associative arrays are single-dimensional, unbounded, sparse collections of homogeneous elements. Associative arrays can be sparse, which means not all elements between two elements need to be defined.Similar to hash table in other language
    For instance you can have an element at index -29 and one at index 12 with nothing in between. Homogeneous elements mean every element must be of the same type.
    Associative arrays are useful for small to medium sized lookup tables where the array can be constructed in memory ( only , which is destroyed after the session ends) each time a procedure is called or a package is initialized. There is no fixed limit on their size and their index values are more flexible- associative array keys can be negative and/or nonsequential, and associative arrays can use string values instead of numbers .The Associative Array is only available in PL/SQL


    type my_tab_t is table of number index by pls_integer/ binary_integer;
    type my_tab_t is table of number index by varchar2(4000);
    type my_tab_t is table of tab.value%TYPE index by tab.id%TYPE;
    Example :
    Declare
      cursor c_customer is select cust_name,cust_no,rownum from customers;
      type type_cname is table of customers% rowtype index by binary_integer;
      tab_cname type_cname;
      v_counter number:=0;


    begin

    for r_customer in c_customer loop
       tab_cname(v_counter) := r_customer.cust_name ;
       v_counter:=v_counter+1;
    end loop;


    forall i in tab_cname.first .. tab_cname.last
      dbms_output.put_line(tab_cname(i));
    end;


    PLSQL Table attributes

    DELETE - Delete rows in a table.
    EXISTS - Return TRUE if the specified entry exists in the table.
    COUNT - Returns the number of rows in the table .
    FIRST - Returns the index of the first in the table.
    LAST - Returns the index of last row in the table.
    NEXT - Returns the index of the last row in the table .
    PRIOR - Returns the index of previous row in the table before the specified row.

    Use NOCOPY hint to reduce overhead of passing collections in and out of program units






    -----------

    Feb 12, 2013

    NOSQL


              What is necessary for top-tier Web sites, according to proponents of NoSQL, is massive scalability, low latency, the ability to grow the capacity of your database on demand and an easier programming model. These, and others, are things which, according to them, SQL RDBMSes just don't provide in a cost-effective manner.

    NoSQL database systems are developed to manage large volumes of data that do not necessarily follow a fixed schema. Data is partitioned among different machines (for performance reasons and size limitations) so JOIN operations are not usable.Documents are addressed in the database via a unique key that represents that document.No SQl Suuports CAP Property (Consistency, Availability, Partition tolerance)

    C - Consistency
    ----------------
    A - Availability
    ----------------
    P - Partition tolerance
    --------------------
    Partial partition tolerance for these databases is obtained by mirroring database clusters between multiple data centers. The advantage these databases have over a traditional RDBMS is that with the work spread over all of those machines, you can achieve ultra-low latency even when there are extremely high numbers of reads and writes, and with all those machines, you can analyze massive amounts of data quickly.This is meaningless when the database is on a single server

    NoSQL implementations can be categorized by their manner of implementation:

    1) Document store

    Compared to relational databases, for example, collections could be considered as tables as well as documents could be considered as records.But they are different: every record in a table has the same sequence of fields, while documents in a collection could have fields that are completely different.Documents are addressed in the database via a unique key that represents that document. One of the other defining characteristics of a document-oriented database is that, beyond the simple key-document (or key-value) lookup that you can use to retrieve a document, the database will offer an API or query language that will allow you to retrieve documents based on their contents.
    Having keys and values are the so-called document databases. A document, in this case, is a collection of various fields of information. Each individual document can have a different number of fields of varying lengths. These databases are useful if you have a lot of semi-structured data, and they are a good fit for object-oriented programming models

    Eg :CouchDB, MongoDB

    2) Graph

    This kind of database is designed for data whose relations are well represented as a graph (elements interconnected with an undetermined number of relations between them). The kind of data could be social relations, public transport links, road maps or network topologies

    Eg: Neo4j, InfoGrid and HyperGraphDB

    3) Key-value store

    Key-value stores allow the application to store its data in a schema-less way. The data could be stored in a datatype of a programming language or an object.Each piece of data that goes into the database is given a key, and when you want the data back, you use the key to get

    4) XML databases

    It does not use SQL as its query language.
    It may not give full ACID guarantees.
    It has a distributed, fault-tolerant architecture

    5) Wide-column databases

    It tend to draw inspiration from Google's BigTable model.

    Eg: Cassandra, HBase

    Advantages of NoSQL
    -------------------------
    Elastic scaling

    Storage Type: Column Based NoSQL Databases, Document Based NoSQL Databases, Graph Based NoSQL Databases, Key-Value Based NoSQL Databases

    License Type: AGPL NoSQL Databases, Apache NoSQL Databases, BSD NoSQL Databases, GPL NoSQL Databases, Open Source NoSQL Databases, Proprietary NoSQL Databases

    Implementation Language: C NoSQL Databases, C++ NoSQL Databases, Erlang NoSQL Databases, Java NoSQL Databases, Python NoSQL Databases

    Data Storage: BDB NoSQL Databases, Disk NoSQL Databases, GFS NoSQL Databases, Hadoop NoSQL Databases, Plug-in NoSQL Databases, RAM NoSQL Databases, S3 NoSQL Databases

    Big Data


    Big Data is all about finding a needle of value in a haystack of unstructured information.Big data refers to large datasets that are challenging to store, search, share, visualize, and analyze.

    BigTable is a compressed, high performance, and proprietary data storage system built on Google File System .Big Table is Googles Database used for Google Reader,Google Maps,Google Book Search,My Search History,Google Earth, Blogger.com,Google Code hosting, Orkut,YouTube and Gmail.

    BigTable maps two arbitrary string values (row key and column key) and timestamp (hence three dimensional mapping) into an associated arbitrary byte array. It is not a relational database

    Relational databases (such as Oracle, MySQL, and SQL Server) versus newer non-relational databases (such as MongoDB, CouchDB, BigTable, and others).

    Big Data is not just about volume, the approach to analysis contends with data content and structure that cannot be anticipated or predicted.