Thursday 26 December 2013

Different types of SQL commands

Different types of SQL commands are
·        Data retrieval retrieves data from the database, for example, SELECT.
·        Data Manipulation Language (DML) inserts new rows, changes existing rows, and removes unwanted rows, for example, CREATE, ALTER, DROP, RENAME and TRUNCTATE.
·        Transaction Control manages and changes logical transactions. Transactions are changes made to the data by DML statements grouped together for example, COMMIT, SAVE POINT and ROLLBACK.
·        Data Control Language (DCL) gives and removes rights to database objects, for example GRANT and REVOKE.

Create Table:-
  Two types of creating table:
          a) Create table
          b) Create table ... as select

a) Create Table
  This command is used to create a new relation and the corresponding syntax is:
Syntax
CREATE TABLE relation_name (col1 datatype(size), col2 datatype(size),....,coln datatype(size));

Example:-
  The Mordern book house mostly supplies books to institutions which frequently buy books from them. Various relatoins used are Customer, Sales, Book Author, and publisher. Design database scheme for the same.

  1) The customer table definition is as follows:
Create table customer (cust_no varchar(4) primery key, cust_name    varchar(25),cust_add varchar(30), cust_ph varchar(15));

 b) Create table ... as select
      This type of create command is used to crate the structure of a new table from the structure of existing table.

Syntax
CREATE TABLE relation_name1 (col1,col2,...,coln) AS SELECT col1,col2,...,coln FROM relation_name2;

Example:
  Create the structure for special customer from the structure of CUSTOMER table.
   => create table special_customer
         (cust_no,cust_name,cust_add)
         as select cust_no,cust_name,cust_add
         from customer;


Alter Table:-
  Alter table command is divided into two parts they are
  a) ALTER TABLE --.ADD ...
  b) ALTER TABLE ...MODIFY ...

a) ALTER TABLE --.ADD ...
  This is used to add some extra columns into an existing table. The generalized fornat is given below.

Syntax
ALTER TABLE relation_name ADD (new_col1 datatype(size), col2_datatype(size),...,new_coln datatype(size));

Example:-
  Add customer phone number and fax number in the customer relation.
=> ALTER TABLE customer
      ADD(cust_ph_no varchar(15),
      Cust_fax_no varchar(15));

  b) ALTER TABLE ... MODIFY
     This form is used to change the width as well as data type of existing relations. The feneralized syntax of this form is shown below.

Syntax
ALTER TABLE relation_name MODIFY (col1 new_datatype(size), col2 new_datatype(size),....,coln new_datatype(size));
Example:-
  Modify the data type of the publication year as numeric data type
  => ALTER TABLE book MODIFY (pub_year number(4));

Restriction of the Alter Table
  Using the alter table clause you cannot perform the following tasks:
·        Change the name of the table
·        Change the name of the column
·        Drop a column
·        Decrease the size of a column if table data exists
Drop Table
  This command is used to delete a table. The generalized syntax of this form is given below:

Syntax
DROP TABLE relation_name

Example:-
  Write the command for deleting special-customer relation.
  => DROP TABLE special_customer;


Renaming a Table
   You can rename a table provided you are the owner of the table.

Syntax
RENAME old_table_name TO new_table_name;

Example:-
 => RENAME test To test_info;

Truncating a Table
  Truncating a table is removing all records from the table. The structure of the table stays intact. The SQL language has a DELETE statement which can be used to remove one or more (or all) rows from a table. Truncation releases storage space occupied by the table, but deletion does not.

Syntax
TRUNCATE TABLE table_name;

Example:-
  => TRUNCATE TABLE student;

SQL Operators

SQL Operators:-
Operators and conditions are used to perform operations such as addition  subtraction or comparison on the data items in as SQL statement

Different types of SQL Operators are:-
·        Arithmetic Operators
·        Comparison Operators
·        Logical Operators
·        Set Operators
·        Operator Precedence

1) Arithmetic Operators
  Arithmetic operators are used in SQL expressions to add, subtract, multiply, divide and negate data values, The result of this expression is a number value.

Unary operators (B)
+,-
Denotes a positive or negative expression
Binary operator (B)
*
Multiplication
/
Division
+
Addition
-
Subtraction
Example:-
   Update Emp_Salary
   Set salary=salary*1.05;


2) Comparison Operators
  These are used to compare one expression with another. The comparison operators are given below:

Operator
Definition
=
Equality
!=,<>
Inequality
> 
Greater than
< 
Less than
>=
Greater than or equal to
<=
Less than or equal to
IN
Equal to any member of set
NOT IN
Not equal to any member of set
Is NULL
Test for nulls
Is NOT NULL
Test for anything other than nulls
LIKE
Returns true when the first expression matches the pattern of the second expression
ALL
Compares a value to every value in a list
ANY, SOME
Compares a value each value in a list
EXISTS
True if subquery returns at least one row
BETWEEN x and y
>= x and <=y

Example:-
  1) Get the name of students who have secured first class
      => select student_names
          from studen_tb
          Where percentage >= 60 and percentage < 67
2) Get the out of state student name
   => select student_names
         from student_names
         where state<> ‘Maharashtra’;
3) Get the names of students living in ‘Pune’.
   => select student_names
         from student
         where city=’Pune’;
4) Display the names of students with no contact phone number
   => select student_names
         from student
         where ph_no is null;
5) Display the names of students who have secured second class in exam
   => select student_names
        from student
        where perecentage BETWEEN 50 AND 55;

3) Logical Operators
  A logical operator is used to produce a single result from combining the two separate conditions. Following figure shows logical operators and their definitions.

Operator
Definition
AND
Returns true if both component conditions are true; otherwise return false.
OR
Returns true if either component conditions is true; otherwise return false.
NOT
Returns true if condition is false; otherwise returns false.

Example:-
  1) Display the names of students living in Pune and Bombay
   => select student_names
         from student
         where city=’Pune’ or city=’Bombay’;

  2) Display the names of students who have secured higher second class in exam.
   => select student_names
         from student
         where percentage >= 55 and percentage < 60

4) Set Operators
  Set operators combine the results of two separate queries into a simple result.
Operator
Definition
UNION
Returns all distinct rows from both queries
INTERSECT
Returns common row selected by both queries
MINUS
Returns all distinct rows that are in the first query, but not in second one
Example:-
  Consider following two relations-
  Permanent_Emp {Emp_Code,Name,Salary}
  Temporary_emp={Emp_Code,Name,Daily_Wages}
 1) Display name of all employees.
  => select Name
       from Permanent_Emp
       UNION
       select Name
       from Temporary_Emp;

6) Operator Precedence
  Precedence defines the order that the DBMS uses when evaluating the different operators in the same expression. The DBMS evaluates operators with the highest precedence first before evaluating the operators of lower precedence. Operators of equal precedence are evaluated from the left to right


Operator
Definition
:
Prefix for host variable
,
Variable separator
()
Surrounds subqueries
Surrounds a literal
“  “
Surrounds a table or column alias or literal text
( )
Overrides the normal operator precedence
+, -
Unary operators
*, /
Multiplication and division
+ , -
Addition and subtraction
||
Character concatenation
NOT
Reverses the result of an expression
AND
True if both conditions are true
OR
True if either conditions are true
UNION
Returns all data from both queries
INTERSECT
Returns only rows that match both queries
MINUS
Returns only row that do not match both queries