Friday, March 8, 2013

SQL Basics- Part 3


SQL Functions
Char Functions: Few basic functions
1.     Concat (empanme, address): only 2 parameters
a.     Select concat(empname, address) from emp;
2.     UPPER(empname) : 1 parameter
a.     Select UPPER(empname) from emp;
3.     LOWER(empname): 1 parameter
a.     Select LOWER(empname) from emp;
4.     initcap(empname) : First letter of each word capital
a.     select initcap(empname) from emp;
b.    Result: “Test Result” for value “test result”
5.     REPLACE(empname, str1, str2): str1 in empname is replaced by str2

NUMBER Functions:
1.     ROUND(num): float to integer
2.     ROUND(num, +/- n): nth digit after/before decimal
a.     Select round(121.57, -2) from  sysibm.sysdummy1;
b.    Result: 100 (if n = -1 –> 120, n=1 -> 121.60)
3.     CEIL(num): add 1 if value has any decimal value
4.     FLOOR(num): cuts 0ff decimal part FLOOR(2.1) = 2, FLOOR(-2.1) = 3
5.     SQRT(num): square root of number
6.     Sin, cos, tan etc – Takes 1 parameter
NULL value:
·         Always use is [not ] null  ( [!]= null is not correct)
·         Any arithmetic operation with null returns null.
List functions:
Greatest (x1 .. x255)      :
Select greatest (salary, 10000) from emp; returns salary if >10000 else return 10000
Least(x1 .. X255)
Select least(salary, 5000) from emp; returns salary if <5000 5000="" else="" o:p="" return="">
GROUP Functions/ Aggregate Functions:
1.     Sum(column)
2.     Avg(column)
3.     Min(column)
4.     max (column)
5.     count(sal)
6.     count(*)
7.     stddev (column)
8.     variance (column)
Rules for group functions:
·         cannot use in where clause
o    Select * from emp where sal> avg(sal) (Not Valid)
o    Solution: Select * from emp where sal> (Select avg(sal) from emp );
·         Cannot specify column in select having group function unless column is specified in group by clause
o    Select dept, sum(salary) from emp; (Not valid)
o    Select dept, sum(salary from emp group by dept; Valid
·         Whichever Column is in group by Clause need not be present in select statement
o    Select sum(salary) from emp group by dept;
·         where clause can come before group by
·         sequence of columns in group by affects speed of processing
·         Specify condition on group by output use having   
o    Select dept, sum(salary) from emp group by dept having sum(salary) > 1000;
·         For sorted o/p use order by after group by
·         Group function can be used in having clause without using group by statement



Sequence of statements in query
·         Select

·         Where

·         Group by

·         Having

·         Order by

SQL Basics- Part 2


TABLE:
Table is Collection of relative data. Columns represent fields for data and rows represent data.
In example given below: ID, NAME, ADDRESS represents columns which is used to store specific information for every record.
Table: EMP
ID
NAME
ADDRESS
COUNTRY
DOB
1
XX
Pune
IN
01.01.1960
2
YY
Helsinki
FI
31.12.2000

So while creating any table, we need to specify fields for that table and what type of value that field is going to store for records.
Record (row of data) is each individual entry that exists in that table. In above example there are 2 records.

·         In Oracle, there can be maximum 1000 columns per table. (750 for DB2)
·         (Refer http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.sqlref%2Fsrc%2Ftpc%2Fdb2z_limits.htm for maximum limits for data types, table name, column name etc.)


How to Create Table
Consider above table EMP.
Columns in table are:
Column             Type                             SQL Data Type
ID                     Number                         BIGINT (INT range is 32767 so BIGINT preferred)
EMPNAME       String                           VARCHAR(50)
ADDRESS        String                           VARCHAR(300)
COUNTRY         String                           CHAR(2)
DOB                 Date                             DATE

We used EMPNAME instead of NAME because NAME is DB2 keyword.
Why name and address are VARCHAR?
EMPNAME, ADDRESS normally vary considerably in length, the choice of a varying-length data type seems appropriate. If you choose a data type of CHAR(50) for EMPNAME, for example, the result is a lot of wasted, unused space. 50 bytes space is always used irrespective of length of name if data type is CHAR.

Note: Using VARCHAR saves disk space, but it incurs a 2-byte overhead cost for each value. Using VARCHAR also requires additional processing for varying-length rows. Therefore, using CHAR is preferable to VARCHAR, unless the space that you save by using VARCHAR is significant. So do not use  VARCHAR(n) unless n is less than 18.





Syntax:
CREATE TABLE table-name
(
Columnname1   data-type ,
Columnname1   data-type
);

CREATE TABLE EMP
(
ID                     BIGINT,
EMPNAME       VARCHAR(50),
ADDRESS        VARCHAR(300),
COUNTRY         CHAR(2),
DOB                 DATE
);

How to Insert records in table
Syntax for Insert
INSERT INTO table-name (column-names) VALUES (row values separated by comma)
(column-names) is optional
Row values: As per column sequence in table or column names specified in (column-names)

1.     INSERT INTO EMP VALUES ( 1, 'XX', 'Pune', 'FI', '1960-06-01' );
2.     INSERT INTO EMP (ID,EMPNAME,ADDRESS,COUNTRY,DOB)
VALUES (2,'YY',           'Helsinki',          'FI','2000-12-31');
3.     INSERT INTO EMP (ID, EMPNAME) VALUES (3, ‘ZZ’);
4.     INSERT INTO EMP VALUES(4, null, null, null, null);

NOTE:
·         Null values same for “char varchar date int”
·         Null value in middle takes 1 byte (in case of char also 1 bytes) (For Oracle DB)
·         If ending columns have null then 0 bytes for that field (For Oracle DB)
QUERY:
Select * from emp;
·         * is meta character [All columns]

Relational Operator as precedence
1.     GREATER THAN >
2.     GREATER THAN EQUAL TO >=
3.     LESS THAN <
4.     LESS THAN EQUAL TO <=
5.     NOT EQUAL TO <> / ^= /! =
6.     EQUAL TO =

Other Predicates in where clause:
1.     BETWEEN A AND B : ONLY 2 VALUES mutually inclusive /faster than < = >
2.     NOT BETWEEN A AND B : ONLY 2 VALUES mutually exclusive
3.     IN (SET) : LIST OF VALUES discrete
4.     LIKE : A CHARACTER PATTERN
5.     IS NULL

LOGICAL CONDITION:
1.             AND
2.             OR
3.             NOT

PRECEDENCE OF OPERATORS:
1.     Arithmetic [BY PRECEDENCE ( ) ** * / + -]() ** /*+-   ** =  Exponentiation
2.     Concatenation
3.     Comparison Cond.
4.     Is [not] null, like, [not] in
5.     [Not] between
6.     NOT
7.     AND
8.     OR






Alias:
Select salary*12 from emp;     salary*12 is computed/virtual column
Select salary * 12 “ANNUAL” from emp;              ANNUAL is column alias
You can make alias case sensitive as in “Annual” or long character “Annual Salary”.

DISTINCT | UNIQUE:
Select DISTINCT empname from emp;
Select UNIQUE empname from emp;
Combination of columns can also be used for Distinct, unique.
Select DISTINCT empname, country from emp;

Sorting:
Order by
Select * from emp order by empname;
Select * from emp order by empname desc;

Wild Cards:
Wild cards are used for search. If you want to search name starting with A, name ending with A, name contains A, you can use wild card search option in where clause.
% Denotes 0 or many characters
_  Denotes 1 character (Underscore)

Select * from emp where empname like ‘A%’ (Select all employees with name starting with A)
Select * from emp where empname like ‘%A’ (Name ending with A)
Select * from emp where empname like ‘%A%’ (Name contains A)
Select * from EMP where empname like ‘t_’ (Name: ta, tb etc. t and any 1 character)

Delete
delete from emp;
ANSI: delete emp;


DDL
All DDL’s are auto commit by default.
CREATE, ALTER, DROP, TRUNCATE (added as SQL 2008 standards)

Transaction Control:
Savepoint:
It is used to save savepoint in long transactions.
Oracle Syntax:
Savepoint savepoint-name;
DB2 Syntax:
Savepoint savepoint-name on rollback retain cursors|locks;
On rollback retain cursors|locks: behaviour upon rollback to this savepoint.( cursors are unaffected by a rollback to savepoint operation/ Locks acquired since the savepoint are not tracked, and are not rolled back (released) upon rollback to the savepoint).

UPDATE new_emp set name = UPPER(name);
Savepoint s2 on rollback retain cursors;
update emp set name = lower(name) where employee_id =180;
Rollback to s2;

LOCKS:
1.     Locks are used for avoiding destructive intervention between concurrent transactions.
2.     Record locking is automatic.
3.     Lock is held for only user modified data.
4.     Default locking is row level locking (Cell level locking in Oracle 10G) unless
a.     Isolation level is uncommitted read/repeatable read
b.    The table locksize attribute is TABLE.
c.     Explicit table lock acquired.

SQL Basics- Part 1


Before starting SQL, let’s see the basic terms
Data: Computer Information
Persistence: Preserving data for future use.
 Large amount of Inter related Data – database
 DBMS: Database management System ANSI defn. of DBMS - Readymade s/w allows you to insert update delete and process
            E.g. Ms Excel, dBASE, foxbase, Foxpro, Clipper, Database Dataflex  ,Dataease  etc.
RDBMS: Relational Database Management System (Relational means it maintains relation between tables)
RDBMS examples:
11.     Informix (fastest),
22. Oracle (most popular around 50% market share of world RDBMS market Best s/w tools, mix programming is easy) 
33.  Sybase
44.    MS SQL server (upcoming Limitation Windows Domain)
55.  Ingres, Progress (character based only on DOS and UNIX)
66.     CICS, TELON, IDMS, IBM DB2 (Mainframe like ATM machine)
77.     MS Access Vatcom SQL Paradox (PC based not for n/w)
88.     MySQL 

Difference between DBMS and RDBMS
DBMS
RDBMS
Nomenclature
Field                            
Record                        
File                              

Column / Attribute
Row / Tuple / Entity
table/relation/entity class
Relationship between two files maintained programmatically
Relationship between two tables maintained at the time of table creation
High network traffic
Low network traffic
file level locking
row level locking
[Oracle 10g automatic row level locking]New version of Oracle cell level locking
Distributed databases not supported
Most RDBMS support Distributed database
[Largest Distributed databases in Oracle Yahoo! 20000 Oracle Linux Server in Silicon Valley and in Australia]
No Security
Multiple level of Security [Oracle Server Database crated in another Partition of HDD]
logging in [username/password] /
command [DBA restriction on using Commands (134command in Oracle 10g)]
/object [Grant Permissions on Owner Object]Oracle Tools/Oracle Products



SQL (Structured Query Language)

·         Structured Query Language
·         Standard Query Language
·         Common to all database
·         Conforms to ANSI standards. And ISO standard
·         Initially created by IBM 1978-79. Now controlled by ANSI

SQL Commands (12 commands + TRUNCATE added as SQL 2008 Standards)
1.     DDL (data definition language): Create, drop, alter, truncate
2.     DML (data manipulation language): Insert, update, delete, merge (added in SQL 2003 std/expanded in SQL 2008 std)
3.     DCL (data control language): Grant, Revoke
4.     DQL (data query language): select
5.     DTL (data transactional language): Rollback, commit, savepoint


Rules of table names/ column names:
·         Must be 1-30 characters long in Oracle 9i. (127 characters for DB2) Object names or Qualifier are case insensitive (EMP or emp same)
·         Should not be an Database Server reserved word
·         Must contain only A-Z, a-z, 0-9, _, $, and #. [avoid $ and # may not work in other RDBMS]
·         Must begin with a letter/Alphabet (use descriptive name)
·         Must not a duplicate name of another object owned by same user.

Data Types
            A data type is a type of data.
Range of values that a column can have. (Data type is a data storage format that can contain a specific type or range of values.)
String Data Types:
1.     CHARACTER (CHAR): Fixed-length character strings with a length 1-255.
2.     VARCHAR:  The maximum length is 32704.
Numeric Data Types:
1.     INTEGER (INT): The range is -32768 to +32767.
2.     DECIMAL (NUMERIC): The range is -2147483648 to +2147483647. (If decimal(7,2) and input as 50.605 then rounding takes place as 50.61 while storing)
3.     DOUBLE: The maximum range is 1 - 10³¹ to 10³¹ - 1
4.     SMALLINT, BIGINT, REAL etc.: The range of big integers is -9223372036854775808 to +9223372036854775807.
Date-time Data Types:
1.     DATE: A date is a three-part value representing a year, month, and day in the range of 0001-01-01 to 9999-12-31.
2.     TIME: A time is a three-part value representing a time of day in hours, minutes, and seconds, in the range of 00.00.00 to 24.00.00.
3.     TIMESTAMP: timestamp is a seven-part value representing a date and time by year, month, day, hour, minute, second, and microsecond, in the range of 0001-01-01-00.00.00.000000000 to 9999-12-31-24.00.00.000000000 with nanosecond precision.
XML data type: it is used to define columns of a table that store XML values.