Sunday 25 December 2016

Array in Linux/Unix Shell Scripting

In Linux shell an Array is the collection of same or different kind of elements which are accessible using zero-based index. In shells array there is no specific data type that means while declaring a shells array no need to define the data type for array. You can store integer, float, string etc in a same array.

This post is part of the shell scripts posts. You can read more basic shell script examples on this page ( http://coolstuffsite.blogspot.in/p/shells-script.html )

In this post i will make shells array easy for you with proper examples.

Declaring a Shell Arrays
To declare a shell array use the following syntax.
declare -a array_name

where array_name is the name of array
Note: In bash, declaring array is not necessary. We can insert element individually with array index.

Declaring and Assigning values to an Array:
Method 01:
Use the following syntax to declare and assigning or initializing the values.
array_name[index]=value

where array_name is the name of array.
      index is position of element which start from zero.
      value is the array value at particular index.
     
Method 02:
You can use another method to declare bash array as following syntax.
array_name=(value1 value2 ... valuen)

This method is useful when you need to take dynamic elements . In this method you can declare space separated static array elements as well as you can place any Linux command, wild cards or SQL query .
For example if we use '*' then it will create a array of all directories and files available in current directory. I will make you understand these concept in following examples .

Accessing Bash Array Elements:
To read the array element use the following syntax.
echo ${array_name[index]}

To access all the array elements use the following syntax.
echo ${array_name[@]}
        #or
echo ${array_name[*]}

To print the number of elements of an array use the following syntax.
echo ${#array_name[@]}

Now lets start some practice with examples.

Example01 : Declaring array with method 01 and accessing array elements.
#!/bin/bash


# Declaring an array demoArray.
declare -a demoArray # It is optional


# Assigning diffrent data type elements to array demoArray
demoArray[0]='Unix'
demoArray[1]='Linux'
demoArray[2]=100
demoArray[4]=50.0
demoArray[5]='"This is demoArray script"'


# Printing third element which is an integer
echo "3rd elemets of array demoArray is : ${demoArray[2]}"
# Printing last element which is a string
echo "6th or last element of array is : ${demoArray[5]}"
# To print all the elements of the array
echo "List of all elemets : ${demoArray[@]}"


# To print the number of elements in the array
echo "Number of elements in array is : ${#demoArray[*]}"


Output:
./bashArray01.sh

3rd elemets of array demoArray is : 100
6th or last element of array is : "This is demoArray script"
List of all elemets : Unix Linux 100 50.0 "This is demoArray script"
Number of elements in array is : 5


Example02: Declaring array with method 02 and accessing array elements.
In the below example 'declare -a demoArray=(Unix Linux 100 50.0 "This is demoArray script")' and 'demoArray=(Unix Linux 100 50.0 "This is demoArray script")' represents the same array.

#!/bin/bash


# Declaring array and assigning static elements
declare -a demoArray=(Unix Linux 100 50.0 "This is demoArray script")


# Printing third element which is an integer
echo "3rd elements of array demoArray is : ${demoArray[2]}"
# Printing last element which is a string
echo "6th or last element of array is : ${demoArray[5]}"
# To print all the elements of the array
echo "List of all elements : ${demoArray[@]}"


# Declaring and initializing array current directory contents.
#declare -a currDirContent=(*)
currDirContent=(*)


# Printing all elements of current directory.
echo "-------------------------------"
echo "Current dir files are : ${currDirContent[@]}"


# Declaring lower case alphabets array
lowercase=({a..z})
# Printing a to z
echo "-------------------------------"
echo "${lowercase[@]}"
Output:
./bashArray02.sh

3rd elements of array demoArray is : 100
6th or last element of array is :
List of all elements : Unix Linux 100 50.0 This is demoArray script
-------------------------------
Current dir files are : bashArray01.sh bashArray02.sh
-------------------------------
a b c d e f g h i j k l m n o p q r s t u v w x y z



Example02: In this example we are creating an array of all emailIds which are in the customer table of a mysql database. To understand this example consider a table "customer" which having the following details.
mysql> select * from customer;
+------------+--------------+----------------------+-----------+
| customerid | customername | emailid              | accountno |
+------------+--------------+----------------------+-----------+
| 1001       | Neeraj singh | neeraj@gmail.com     |     30004 |
| 1002       | Tanu         | tanu@gmail.com       |     30005 |
| 1003       | Shivam Kumar | shivam@yahoo.com     |     30006 |
| 1004       | vinay        | vinay.s@gmail.com    |     30007 |
| 1005       | Ajay singh   | ajay.singh@gmail.com |     30008 |
+------------+--------------+----------------------+-----------+


Once we get array of above emailIds we will access the emails using a for loop.

#!/bin/bash


# Creating array of emails from database. 
declare -a EMAILIDS=(`mysql -umukesh -p*** -hlocalhost -e "SELECT emailid FROM mydb.customer"`)
# The above command will be equivalent to :
# EMAILIDS=(neeraj@gmail.com tanu@gmail.com shivam@yahoo.com vinay.s@gmail.com ajay.singh@gmail.com )


# Accessing emails using for loop
for email in "${EMAILIDS[@]}"
do
    echo "$email"
done


# Print all array of emails
echo "-----------------------------"
echo ${EMAILIDS[@]}


# Printing number of emails
echo "-----------------------------"
echo "Total emails are : ${#EMAILIDS[@]}"

Output:
./bashArray03.sh

neeraj@gmail.com
tanu@gmail.com
shivam@yahoo.com
vinay.s@gmail.com
ajay.singh@gmail.com
-----------------------------
neeraj@gmail.com tanu@gmail.com shivam@yahoo.com vinay.s@gmail.com ajay.singh@gmail.com
-----------------------------
Total emails are : 5

Saturday 10 December 2016

Apache-Tomcat Interview Questions And Answers

Q. Difference between apache and apache-tomcat server ?
Apache:
  •     Apache mostly serves static content by itself, but there are many add-on modules (some of     which come with Apache itself) that let it modify the content and also serve dynamic content written in Perl, PHP, Python, Ruby, or other languages.
  •     Basically Apache is an HTTP Server, serving HTTP.

Apache-Tomcat:
  •     Tomcat is primarily a servlet/JSP container. Its written in Java. It can serve static content too, but its main purpose is to host servlets and JSPs.
  •     JSP files (which are similar to PHP, and older ASP files) are generated into Java code (HttpServlet), which is then compiled to .class files by the server and executed by the Java virtual machine.
  •     Apache Tomcat is used to deploy your Java Servlets and JSPs. So in your Java project you can build your WAR (short for Web ARchive) file, and just drop it in the deploy directory in Tomcat.
  •     Although it is possible to get Tomcat to run Perl scripts and the like, you wouldn't use Tomcat unless most of your content was Java.
  •     Tomcat is a Servlet and JSP Server serving Java technologies

Note:
    The two technologies can be used together through a connector module called mod_jk. This will allow you to use the Apache HTTP server to serve regular static webpages, and the Tomcat Servlet engine to execute servlets.
   
Q. Difference between web server and application server ?
    1. Application Server supports distributed transaction and EJB.
        While Web Server only supports Servlets and JSP.
    2. Application Server can contain web server in them. most of App server e.g. JBoss or WAS has
        Servlet and JSP container.
    3. Though its not limited to Application Server but they used to provide services like Connection
        pooling, Transaction management, messaging, clustering, load balancing and persistence. Now
        Apache tomcat also provides connection pooling.
    4. In terms of logical difference between web server and application server. web server is supposed
        to provide http protocol level service
        while application server provides support to web service and expose business level service
        e.g. EJB.
    5. Application server are more heavy than web server in terms of resource utilization.

Q. How to start and shutdown to tomcat server
   There are two .sh file in cd $CATALINA_HOME/bin  dir or in /usr/share/tomcat7/bin/
    ./startup.sh
    ./shutdown.sh

Q. After startup what is the url for default web-application?
      http://localhost:8080/

Q. What are the directories under the apache-tomcat installation dir ?
  •     conf - Server configuration files (including server.xml)
  •     logs - Log and output files
  •     shared - For classes and resources that must be shared across all web applications
  •     webapps - Automatically loaded web applications
  •     work - Temporary working directories for web applications
  •     temp - Directory used by the JVM for temporary files (java.io.tmpdir)

Q. How to change the default(8080) port number?
  •     Go to tomcat>conf folder.
  •     Edit server.xml. (/usr/share/tomcat/conf/server.xml)
  •     Search "Connector port"
  •     Replace "8080" by your port number.
  •     Restart tomcat server.
  •    
    For example, if you change the port to 1977, you would request the URL http://localhost:1977/
   
    Note: While changing the port number make sure that port is not already in use and port no should be greater than 1024, as ports less than or equal to 1024 require superuser access to bind to.
  
Q. How to know your Apache tomcat version ?
    root@ubuntu:~# /usr/share/tomcat7/bin/version.sh
    Using CATALINA_BASE:   /usr/share/tomcat7
    Using CATALINA_HOME:   /usr/share/tomcat7
    Using CATALINA_TMPDIR: /usr/share/tomcat7/temp
    Using JRE_HOME:        /usr/lib/jvm/java-1.7.0-openjdk-i386
    Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
    Server version: Apache Tomcat/7.0.26
    Server built:   Apr 1 2013 08:32:04
    Server number:  7.0.26.0
    OS Name:        Linux
    OS Version:     3.2.0-105-generic-pae
    Architecture:   i386
    JVM Version:    1.7.0_101-b00
    JVM Vendor:     Oracle Corporation

Q. what are the configuratiopn file in tomcat server?
    Tomacat XML Configuration Files:
  1.     server.xml(TOMCAT-HOME/conf/server.xml)
  2.     web.xml   (TOMCAT-HOME/conf/web.xml)    
  3.     tomcat-users.xml (TOMCAT-HOME/conf/tomcat-users.xml)
Q. How to use tomcat server as a HTTP server ?
    Tomcat also contains a HTTP connector which can be used to serve static HTML pages. The standard directory which will be served is below the Tomcat webapps/ROOT installation directory. Place static content into this directory.

    To allow directory browsing via Apache Tomcat change the listings parameter in the file conf/web.xml from false to true.
    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
   
Q. Explain what is Jasper?
    Jasper is a Tomcat’s JSP engine
    It parses JSP files to compile them into JAVA code as servlets
    At run-time, Jasper allows to automatically detect JSP file changes and recompile them
   
Q. Where can be set roles,username and password ?
   /conf/tomcat-users.xml
  
   <tomcat-users>
        <role rolename="manager-gui" />
        <user username="tomcat" password="s3cret" roles="manager-gui" />
   </tomcat-users>

Saturday 3 December 2016

Python Mysql Database Access

There is a python module called MySQLdb through which we can access many databases like Mysql,Oracle,PostgreSQl,Sybase,GadFly etc.
Here i am going with Mysql on Linux operating system.
Other than MySQLdb module there is one more module called mysql-connector which also provide database access from python script.

Diffrence between MySQLdb and mysql-connector.
MySQLdb : MySQLdb is a C module that links against the MySQL protocol implementation in the libmysqlclient library. It is faster, but requires the library in order to work.

mysql-connector : mysql-connector is a Python module that reimplements the MySQL protocol in Python. It is slower, but does not require the C library and so is more portable.

MySQLdb:
MySQLdb is an interface or python module for connecting to a MySQL database server from Python.

Verify if MySQLdb  have installed in your machine. Try to run the following script.
    #!/usr/bin/python

    import MySQLdb


if it is throwing following error that mean MySQLdb module need to install in your machine.
    Traceback (most recent call last):
      File "test.py", line 3, in <module>
        import MySQLdb
    ImportError: No module named MySQLdb


To install MySQLdb module in your Unix/Linux OS, download zip file from MySQLdb Download page(http://pkgs.fedoraproject.org/repo/pkgs/MySQL-python/MySQL-python-1.2.2.tar.gz/532268f02870bea18c1d465e88afff30/MySQL-python-1.2.2.tar.gz) and run the following command.

You can also try wget command to download zip frile in your unix machine.

    $ wget http://pkgs.fedoraproject.org/repo/pkgs/MySQL-python/MySQL-python-1.2.2.tar.gz/532268f02870bea18c1d465e88afff30/MySQL-python-1.2.2.tar.gz
    $ gunzip MySQL-python-1.2.2.tar.gz
    $ tar -xvf MySQL-python-1.2.2.tar
    $ cd MySQL-python-1.2.2
    $ python setup.py build
    $ python setup.py install



Before starting create a test databases called mydb and table as customer with following table structure and values.
    mysql> select * from mydb.customer;

    +------------+--------------+----------------------+-----------+
    | customerid | customername | emailid              | accountno |
    +------------+--------------+----------------------+-----------+
    | 1001       | Neeraj singh | neeraj@gmail.com     |     30004 |
    | 1002       | Tanu         | tanu@gmail.com       |     30005 |
    | 1003       | Shivam Kumar | shivam@yahoo.com     |     30006 |
    | 1004       | vinay        | vinay.s@gmail.com    |     30007 |
    | 1005       | Ajay singh   | ajay.singh@gmail.com |     30008 |
    +------------+--------------+----------------------+-----------+


Example 01 : Read/Select operation

In read operation we need to use the following useful method to fetch or read the information from database.
fetchone() : It fetches the next row of a query result set. A result set is an object that is returned when a cursor object is used to query a table.
fetchall(): It fetches all the rows in a result set. If some rows have already been extracted from the result set, then it retrieves the remaining rows from the result set.
rowcount: This is a read-only attribute and returns the number of rows that were affected by an execute() method.

#!/usr/bin/python

import MySQLdb

# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# Prepare SQL query to INSERT a record into the database.
sql = "SELECT * FROM customer \
       WHERE customerid > '%d'" % (1003)
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Fetch all the rows in a list of lists.
   print "Total count :"+str(cursor.rowcount)
   results = cursor.fetchall()
   for row in results:
      customerid = row[0]
      customername = row[1]
      emailid = row[2]
      accountno = row[3]
      # Now print fetched result
      print "customerid=%s,customername=%s,emailid=%s,accountno=%d" % \
             (customerid, customername, emailid, accountno )
except :
   print "Error: unable to fecth data"


# Close the cursor and disconnect from server
finally:
        cursor.close()
        conn.close()

Read operation with fetchone() method
#!/usr/bin/python

import MySQLdb


# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")


# Fetch a single row using fetchone() method.
data = cursor.fetchone()


print "Database version : %s " % data


# Close the cursor and disconnect from server
finally:
        cursor.close()
        conn.close()

        

Ouptput:
Database version : 5.5.49-0ubuntu0.12.04.1

Example 02 : Insert Operation
#!/usr/bin/python


import MySQLdb


# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# Prepare SQL query to INSERT a record into the database.
#sql = """INSERT INTO customer(customerid,
#         customername, emailid, accountno)
 #        VALUES ('1006', 'Mohan', 'mohan@reddif.com', '30009')"""


# Prepare dynamic SQL query to INSERT a record into the database.
sql = "INSERT INTO customer(customerid,\
         customername, emailid, accountno) \
                 VALUES('%s','%s','%s','%d')" \
         %('1006', 'Mohan', 'mohan@reddif.com', 30009)
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   conn.commit()
except:
   # Rollback in case there is any error
   conn.rollback()


# Close cursor and disconnect from server
conn.close()
cursor.close()


Example 03 : Update Operation
Following procedure will update all records which having customername 'Mohan'.
#!/usr/bin/python


import MySQLdb


# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# Prepare SQL query to UPDATE required records
sql = "UPDATE customer SET emailid = 'mohan.kumar@reddif.com'\

                          WHERE customername = '%s'" % ('Mohan')
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   conn.commit()
except:
   # Rollback in case there is any error
   conn.rollback()


# disconnect from server
conn.close()


Example 04 : Delete Operation
#!/usr/bin/python


import MySQLdb


# Open database connection
conn = MySQLdb.connect("localhost","mukesh","mks123","mydb" )


# prepare a cursor object using cursor() method
cursor = conn.cursor()


# Prepare SQL query to DELETE required records
sql = "DELETE FROM customer WHERE customerid = '%s'" % ('1006')
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   conn.commit()
except:
   # Rollback in case there is any error
   conn.rollback()


# disconnect from server
conn.close()


After performing this delete operation through python script. Connect mysql db and verify the operation successfully proceeded or not.



************************END*******************************


Tuesday 18 October 2016

SQL & PL/SQL Interview Question Answers Set - 01

Q.What is a stored procedure ?

A stored procedure is a sequence of statements that perform specific function.
A stored procedure is a named pl/sql block which performs an action.It is stored in the database as a schema object and can be repeatedly executed.It
can be invoked, parameterised and nested.

Q.What is SQL*Loader?

SQL*Loader is a product for moving data in external files into tables in an Oracle database. To load data from external files into an Oracle database,
two types of input must be provided to SQL*Loader :
the data itself and the control file.
    The control file describes the data to be loaded. It describes the Names and format of the data files, Specifications for loading data and the Data
    to be loaded (optional). Invoking the loader sqlload username/password controlfilename <options>.

Q.What are the two parts of a procedure ?

The above person have answered wrong. Or rather the question may be wrong. It should be packages instead of procedures. What are the 2 parts of
package ? But the question is what are the 2 parts for procedure!!!
Procedure Specification and Procedure Body.

Q.What is Date Functions?
 
Date Functions are ADD_MONTHS, LAST_DAY, NEXT_DAY, MONTHS_BETWEEN & SYSDATE.
select sysdate from dual;

Q.What is NVL?

NVL: Null value function converts a null value to a non-null value
for the purpose of evaluating an expression. Numeric Functions accept numeric I/P & return numeric values. They are MOD, SQRT, ROUND, TRUNC & POWER.
    select sqrt(8) from dual;
    select round(2.34) from dual;
    select mod(8,2) from dual;
    select power(2,3) from dual;

    TRUNC(TO_DATE('22-AUG-03'), 'YEAR')    would return '01-JAN-03'
    TRUNC(TO_DATE('22-AUG-03'), 'Q')    would return '01-JUL-03'
    TRUNC(TO_DATE('22-AUG-03'), 'MONTH')    would return '01-AUG-03'
    TRUNC(TO_DATE('22-AUG-03'), 'DDD')    would return '22-AUG-03'
    TRUNC(TO_DATE('22-AUG-03'), 'DAY')    would return '17-AUG-03'

select trunc(to_date('22-AUG-03'), 'YEAR') FROM DUAL;

Q.What is Intersect?
 
Intersect is the product of two tables listing only the matching rows.

Q.What is Minus?
Minus is the product of two tables listing only the non-matching rows.

Q.How to know the last executed procedure?
Execute procedure name (parameter1,parameter2)
Select timestamps, owner, obj_name, action_name from dba_audit_trail;

this statement gives last executed time for procedure , function & package.

Q.What are the Restrictions on Cursor Variables?
 
Currently, cursor variables are subject to the following restrictions:You cannot declare cursor variables in a package spec. For example, the
following declaration is not allowed:
CREATE PACKAGE emp_stuff AS TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE; emp_cv EmpCurTyp;  not allowedEND emp_stuff;You cannot pass cursor variables to a procedure that is called through a database link.If you pass a host cursor variable to PL/SQL, you cannot fetch from it on the server side unless you also open it there on the same server call.You cannot use comparison operators to test cursor variables for equality, inequality, or nullity.You cannot assign nulls to a cursor variable.Database columns cannot store the values of cursor variables. There is no equivalent type to use in a CREATE TABLE statement.You cannot store cursor variables in an associative array, nested table, or varray.Cursors and cursor variables are not interoperable; that is, you cannot use one where the other is expected. For example, you cannot
reference a cursor variable in a cursor FOR loop

Q.What are the cursor attributes used in PL/SQL?
    %ISOPEN     - To check whether cursor is open or not
    %ROWCOUNT   - Number of rows fetched/updated/deleted.
    %FOUND      - To check whether cursor has fetched any row. True if rows are fetched.
    %NOT FOUND  - To check whether cursor has fetched any row. True if no rows are fetched.
These attributes are proceeded with SQL for Implicit Cursors and with Cursor name for Explicit Cursors.

Q.What is Consistency?
    consistency: states that until commit the data will not be reflected to other users,in order to maintain proper consistency.
    consider a example: if user A transfer money to user B. The changes are updates in A account (debit) but until it will be updated in B (credit) too
    till then others will not be able to see the debit of A. when debit to A and credit to B happen then one can see the updates hence maintain
    consistency

Q.What are % TYPE and % ROWTYPE? What are the advantages of using these over data types?
    % TYPE provides the data type of a variable or a database column to that variable.
    % ROWTYPE provides the record type that represents a entire row of a table or view or columns selected in the cursor.
    The advantages are :
    I.  Need not know about variables data type
    ii. If the database definition of a column in a table changes, the data type of a variable changes accordingly.

Q.What will the Output for this Coding
Declare
    Cursor c1 is select * from emp FORUPDATE;
    Z c1%rowtype;
Begin
    Open C1;
    Fetch c1 into Z;
    Commit;
    Fetch c1 into Z;
end;

By declaring this cursor we can update the table emp through z,means we not need to write table name for updation, it may be only by "z".
By issuing the TCL like commit or rollback, the cursor will be closed automatically, you cannot fetch again. You will get an error if you

Q.What is Commit?
    Commit is an event that attempts to make data in the database identical to the data in the form. It involves writing or posting data to the
    database and committing data to the database. Forms check the validity of the data in fields and records during a commit. Validity check are
    uniqueness, consistency and db restrictions.

Q.Give the structure of the function?
    FUNCTION funName (argument list .....) Return datatype is
        local variable declarations
    Begin
        executable statements
    Exception
        execution handlers
    End;

Q.What is a cursor ? Why Cursor is required?
    Cursor is a named private SQL area from where information can be accessed. Cursors are required to process rows individually for queries returning
    multiple rows.

Q.If the application is running very slow? At what points you need to go about the database in order to improve the performance?

    For improving performance, we need to check the sql statement blocks , because for every sql statement execution transfer to sql engine and come
    back to plsq engine that process takes more time to process the plsql block.

Q.What are advantages of Stored Procedures?
    Extensibility,Modularity, Reusability, Maintainability and one time compilation.

Q.What is difference b/w stored procedures and application procedures, stored function and application function?
    Stored procedures are sub programmes stored in the database and can be called & execute multiple times wherein an application procedure is the one
    being used for a particular application same is the way for function.

    Both can be executed any number of times. Only difference is that stored procedures/ functions are stored in database in complied format while the
    application procedures/functions are not in pre complied format and at run time has to be compiled.

Q.State the difference between implicit and explicit cursors.
    Implicit Cursor are declared and used by the oracle internally.
    whereas the explicit cursors are declared and used by the user.

    more over implicitly cursors are no need to declare oracle creates and process and closes
    automatically. the explicit cursor should be declared and closed by the user.

Q.How to avoid using cursors? What to use instead of cursor and in what cases to do so?
    just use subquery in for clause
    ex:
        for emprec in (select * from emp)
        loop
            dbms_output.put_line(emprec.empno);
        end loop;

    no exit statement needed
    implicit open,fetch,close occurs

Q.What is pl/sql?what are the advantages of pl/sql?
    PL/SQL(a product of Oracle) is the 'programming language' extension of sql.
    It is a full-fledged language although it is specially designed for database centric activities.

Q.How to disable multiple triggers of a table at at a time?
    ALTER TABLE<TABLE NAME> DISABLE ALL TRIGGER;

Q.How many types of database triggers can be specified on a table ? What are they ?
                        Insert Update Delete
    Before Row             o.k.     o.k.     o.k.
    After Row             o.k.     o.k.     o.k.
    Before Statement     o.k.     o.k.     o.k.
    After Statement     o.k.     o.k.     o.k.

    If FOR EACH ROW clause is specified, then the trigger for each Row affected by the statement.
    If WHEN clause is specified, the trigger fires according to the returned Boolean value.

Q.Explain how procedures and functions are called in a PL/SQL block ?
    Function can be called from SQL query + explicitly as well
    e.g
    1)select empno,salary,fn_comm(salary)from employee;
    2)commision=fn_comm(salary);

    Procedure can be called from begin-end clause.
    e.g.
    Begin
    (
        proc_comm(salary);
    )
    end

    Function is called as part of an expression.
    sal := calculate_sal ('a822');

    procedure is called as a PL/SQL statement
    calculate_bonus ('A822');

Q.What will happen after commit statement ?

    Cursor C1 is Select empno,ename from emp;
    Begin
        open C1;
        loop
             Fetch C1 into eno.ename;
            Exit When
            C1 %notfound;-----
            commit;
        end loop;
    end;
The cursor having query as SELECT .... FOR UPDATE gets closed after COMMIT/ROLLBACK.
The cursor having query as SELECT.... does not get closed even after COMMIT/ROLLBACK.

Q.What happens if a procedure that updates a column of table X is called in a database trigger of the same table ?
    Trigger will be called... Based on the event what trigger as to do. if trigger is also doing the same update statement then Mutating Table occurs.
    if trigger is not doing any DML statement nothing happens just Trigger will be called..

Q.Can we declare a column having number data type and its scale is larger than precision
ex: column_name NUMBER(10,100),
column_name NUMBER(10,-84)
Yes,
we can declare a column with above condition.
table created successfully.

Q.The IN operator may be used if you know the exact value you want to return for at least one of the columns.
SELECT column_name FROM table_name WHERE column_name IN (value1,value2,..)

Q.What is SQL Deadlock?
Deadlock is a unique situation in a multi user system that causes two or more users to wait indefinitely for a locked resource. First user needs a
resource locked by the second user and the second user needs a resource locked by the first user. To avoid dead locks, avoid using exclusive table
lock and if using, use it in the same sequence and use Commit frequently to release locks.

Q.Where the Pre_defined_exceptions are stored ?
In the standard package.
Procedures, Functions & Packages ;

Q. Below is the table
city     gender     name
delhi     male     a
delhi     female     b
mumbai     male     c
mumbai     female     d
delhi     male     e
I want the o/p as follows:
male female
delhi 2 1
mumbai 1 1
Please help me in writing the query that can yield the o/p mentioned above?
select city, sum(decode(gender,'male',1,0)) Male_cnt, sum(gender,'female',1,0) female_cnt
from table_name
group by city
select a.city,a.male,b.female from
(select city,count(gender) male from city where gender='m'group by city,gender)a
join
(select city,count(gender) female from city where gender='f' group by city,gender)b
on a.city=b.city

Q.What is Sequences?
Sequences are used for generating sequence numbers without any overhead of locking. Drawback is that after generating a sequence number if the
transaction is rolled back, then that sequence number is lost

Q.What is Mutating SQL Table?
Mutating Table is a table that is currently being modified by an Insert, Update or Delete statement.Constraining Table is a table that a triggering
statement might need to read either directly for a SQL statement or indirectly for a declarative Referential Integrity constraints. Pseudo Columns
behaves like a column in a table but are not actually stored in the table. E.g. Currval, Nextval, Rowid, Rownum, Level etc.

Q.In a Distributed Database System Can we execute two queries simultaneously? Justify?
Yes, Distributed database system based on 2 phase commit,one query is independent of 2 nd query so of course we can run.

Q.What are the modes of parameters that can be passed to a procedure ?
IN parameter is the default mode which acts as a constant inside calling environment.value passed in this parameter can not be changed.
OUT parameter mode is used to pass value from calling environment into main block,here we can change the value.
It acts as a variable inside calling environment.
INOUT parameter mode which pass value into calling environment and will get the value back in main block.
IN parameter mode uses call by reference method to pass value from formal parameter to actual parameter.
OUT & INOUT parameter mode uses call by value method to pass values.
IN,OUT,IN-OUT parameters.

Q.Name the tables where characteristics of Package, procedure and functions are stored?
User_objects, User_Source and User_error.

Q.How can a function return more than one value in oracle with proper example?
Basically as per property of function it has to return one value. So the other values can be returned from the out parameter of the function.
But its advised if you want more that one return value go for procedure however function will also yield the same result.

Q.Explain If the entire disk is corrupted how will you and what are the steps to recover the database?
if the entire disk is corrupted and no backup is there don nothing sit and relax their is no possibility of recovery ...a backup is required for
restoration and for recovery redo log and archive logs.
Once if you have theses than think of recovering ..a dba should always plan for the recovery scenario depending upon the critical of the database.
oracle provides 0% data loss facility through data guard and online backup .its dba who has to decide.

Q.What is Rollback?
Rollback causes work in the current transaction to be undone.

Q.What is a cursor for loop ?
Cursor for loop implicitly declares %ROWTYPE as loop index,opens a cursor, fetches rows of values from active set into fields in the record and closes
when all the records have been processed.
eg.
FOR emp_rec IN C1 LOOP
salary_total := salary_total +emp_rec sal;
END LOOP;

Q.How to sort the rows in SQL?

Sort the Rows:
SELECT column1, column2, ... FROM table_name ORDER BY columnX, columnY, ..
SELECT column1, column2, ... FROM table_name ORDER BY columnX DESC
SELECT column1, column2, ... FROM table_name ORDER BY columnX DESC, columnY ASC1.
select column1,column2,column3 from table1 where column1>10 order by 1,3,2;
2.select * from emp order by eno desc;
You can sort your results based on any column in the tables because you are select all the columns.
3.select eno,ename from emp order by deptno;
This is wrong because the column deptno is not present in the select clause.

Q.What is Pragma EXECPTION_INIT? Explain the usage?
The PRAGMA EXECPTION_INIT tells the compiler to associate an exception with an oracle error. To get an error message of a specific oracle error.
e.g. PRAGMA EXCEPTION_INIT (exception name, oracle error number)

Q.Whats the use of dynamic sql in oracle?
Dynamic SQL enables you to write programs that reference SQL statements whose full text is not known until runtime.

Q.How we can create a table in PL/SQL block. insert records into it? is it possible by some procedure or function? please give example?

CREATE OR REPLACE PROCEDURE ddl_create_proc (p_table_name IN VARCHAR2)
AS
l_stmt VARCHAR2(200);
BEGIN
DBMS_OUTPUT.put_line('STARTING ');
l_stmt := 'create table '|| p_table_name || ' as (select * from emp )';
execute IMMEDIATE l_stmt;
DBMS_OUTPUT.put_line('end ');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('exception '||SQLERRM || 'message'||sqlcode);
END;

Q.What is a JOIN?
JOIN is the form of SELECT command that combines info from two or more tables.
Types of Joins are Simple (Equijoin & Non-Equijoin), Outer & Self join.
Equijoin returns rows from two or more tables joined together based upon a equality condition in the WHERE clause.
Non-Equijoin returns rows from two or more tables based upon a relationship other than the equality condition in the WHERE clause.
Outer Join combines two or more tables returning those rows from one table that have no direct match in the other table.
Self Join joins a table to itself as though it were two separate tables.

Q.what is the starting oracle error number?
what is meant by forward declaration in functions?
One must declare an identifier before referencing it. Once it is declared it can be referred even before defining it in the PL/SQL. This rule applies to function and procedures also.

Q.Can we declare a column having number data type and its scale is larger than precision
ex: column_name NUMBER(10,100),
column_name NUMBAER(10,-84)
No, you cant. May be your table might be created successfully but if you try to insert values it will shows you an error.
Numeric or Value Error
NUMBER (p,s)
p- precision, which is the total size of digits
s- scale , which represents the total number of digits that are present to the right side of the decimal.
s should be strictly less than p. If I am missing something. Please fill me in.
Yes,we can declare a column with above condition.table created successfully.
yes, 100 is the total size and 10 is included in 100

Q.Is it possible to use Transaction control Statements such a ROLLBACK or COMMIT in Database Trigger? Why?
It is not possible. As triggers are defined for each table, if you use COMMIT of ROLLBACK in a trigger, it affects logical transaction processing.
yes WE can use COMMIT and ROLLBACK triggers, but by using PRAGAMA AUTONAMOUS_TRANSATIONS. Now the transaction treated as a autonomous transaction.

Q.What is COLUMN?
COLUMN command define column headings & format data values.

Q.SELECT statements in SQL?
SELECT column_name(s) FROM table_name
SELECT DISTINCT column_name(s) FROM table_name
SELECT column FROM table WHERE column operator value
SELECT column FROM table WHERE column LIKE pattern
SELECT column,SUM(column) FROM table GROUP BY column
SELECT column,SUM(column) FROM table GROUP BY column HAVING SUM(column) condition value
Note that single quotes around text values and numeric values should not be enclosed in quotes. Double quotes may be acceptable in some databases.

Q.What is the Delete Statements in SQL?
Delete statement in SQL is used to delete partial/all data.
Especially delete statement is useful in case of partial delete depending upon our criteria otherwise use TRUNCATE to delete whole data from table.
When delete command fires then:
1) Triggers will fire (If created on that table)
2) This will not auto commit changes made So there is one chance to rollback.
3) If u delete whole data then HWM (Highest Water Mark) will not change which gets changed in case of Truncate.

Q.What is Posting?
Posting is an event that writes Inserts, Updates & Deletes in the forms to the database but not committing these transactions to the database.

Q.Where the Pre_defined_exceptions are stored?
In the standard package.
Procedures, Functions & Packages ;

Q.What are two parts of package?
The two parts of package are PACKAGE SPECIFICATION & PACKAGE BODY. Package Specification contains declarations that are global to the packages and local to the schema.
Package Body contains actual procedures and local declaration of the procedures and cursor declarations.

Q.What are the datatypes a available in PL/SQL?
Some scalar data types such as
NUMBER,
VARCHAR2,
DATE,
CHAR,
LONG,
BOOLEAN.
Some composite data types such as RECORD & TABLE.

Q.What is an Exception? What are types of Exception?
Exception is the error handling part of PL/SQL block. The types are Predefined and user defined. Some of Predefined exceptions are.
CURSOR_ALREADY_OPEN
DUP_VAL_ON_INDEX
NO_DATA_FOUND
TOO_MANY_ROWS
INVALID_CURSOR
INVALID_NUMBER
LOGON_DENIED
NOT_LOGGED_ON
PROGRAM-ERROR
STORAGE_ERROR
TIMEOUT_ON_RESOURCE
VALUE_ERROR
ZERO_DIVIDE
OTHERS.
exception is an identifier and error handling part of pl/sql types := 1)predefined 2) user defined.

Q.Explain rowid, rownum?what are the psoducolumns we have?
ROWID - Hexa decimal number each and every row having unique.Used in searching.
ROWNUM - It is a integer number also unique for sorting Normally TOP N Analysis.
Other Psudo Column are
NEXTVAL,CURRVAL Of sequence are some examples
pseudo columns are default columns provided by oracle

Q.How to avoid using cursors? What to use instead of cursor and in what cases to do so?
Just use sub-query in for clause
ex:For emprec in (select * from emp)
loop
dbms_output.put_line(emprec.empno);
end loop;
no exit statement needed
implicit open,fetch,close occurs

Q.what is the starting oracle error number?
what is meant by forward declaration in functions?
One must declare an identifier before referencing it. Once it is declared it can be referred even before defining it in the PL/SQL. This rule applies to function and procedures also
ORA-20000

Q.What are the components of a PL/SQL block?
A set of related declarations and procedural statements is called block.
DECLARE -- declaration section BEGIN -- executable statements -- main section EXCEPTION -- handling possible exceptions -- occurring in the main section END;

Q.What is difference between stored procedures and application procedures,stored function and application function?
Stored procedures are sub programs stored in the database and can be called & execute multiple times where in an application procedure is the one being used for a particular application same is the way for function
Procedure:-
Execute as a PL/SQL
statement,No RETURN clause in
the header,Can return none, one,
or many values,Can contain a RETURN
statement
Function:-Invoke as part of an
expression,Must contain a RETURN
clause in the header,Must return a single value,Must contain at least one RETURN statement

Q.What is Raise_application_error ?
Raise_application_error is a procedure of package DBMS_STANDARD which allows to issue an user_defined error messages from stored sub-program or database
trigger.

Q.What is trigger,cursor,functions in pl-sql and we need sample programs about it?
Trigger is an event driven PL/SQL block. Event may be any DML transaction.
Cursor is a stored select statement for that current session. It will not be stored in the database, it is a logical component.
Function is a set of PL/SQL statements or a PL/SQL block, which performs an operation and must return a value.

Q.How we can create a table in PL/SQL block. insert records into it??? is it possible by some procedure or function?? please give example...
CREATE OR REPLACE PROCEDURE ddl_create_proc (p_table_name IN VARCHAR2)
AS
l_stmt VARCHAR2(200);
BEGIN
DBMS_OUTPUT.put_line('STARTING ');
l_stmt := 'create table '|| p_table_name || ' as (select * from emp )';
execute IMMEDIATE l_stmt;
DBMS_OUTPUT.put_line('end ');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('exception '||SQLERRM || 'message'||sqlcode);
END;

Q.In pl/sql functions what is use of out parameter even though we have return statement.
With out parameters you can get the more than one out values in the calling program. It is recommended not to use out parameters in functions. If you
need more than one out values then use procedures instead of functions.

Q.How to debug the procedure ?
You can use DBMS_OUTPUT oracle supplied package or DBMS_DEBUG package.

Q.What is ref cursor?
In PL/SQL ,pointer has a datatype REF X where
REF-Reference
X-class of objects
Cursor Variables has a datatype REF-CURSOR
where Cursor Variables are like pointers which hold the memory location of some item instead of the item itself.

Q.State the advantage and disadvantage of Cursor?
Advantage :
In pl/sql if you want perform some actions more than one records you should user these cursors only. bye using these cursors you process the query
records. you can easily move the records and you can exit from procedure when you required by using cursor attributes.
disadvantage:
using implicit/explicit cursors are depended by situation. if the result set is less than 50 or 100 records it is better to go for implicit cursors.
if the result set is large then you should use explicit cursors. other wise it will put burden on cpu.

Q.What is a database trigger ? Name some usages of database trigger ?
A database triggers is stored PL/SQL program unit associated with a specific database table or view. The code in the trigger defines the action the
database needs to perform whenever some database manipulation (INSERT, UPDATE, DELETE) takes place.
Unlike the stored procedure and functions, which have to be called explicitly, the database triggers are fires (executed) or called implicitly
whenever the table is affected by any of the above said DML operations.
Till oracle 7.0 only 12 triggers could be associated with a given table, but in higher versions of Oracle there is no such limitation. A database
trigger fires with the privileges of owner not that of user
A database trigger has three parts
1. A triggering event
2. A trigger constraint (Optional)
3. Trigger action
A triggering event can be an insert, update, or delete statement or a instance shutdown or startup etc. The trigger fires automatically when any of
these events occur A trigger constraint specifies a Boolean expression that must be true for the trigger to fire. This condition is specified using
the WHEN clause. The trigger action is a procedure that contains the code to be executed when the trigger fires.
Database trigger is stored PL/SQL program unit associated with a specific database table. Usages are Audit data modifications, Log events
transparently, Enforce complex business rules Derive column values automatically, Implement complex security authorizations. Maintain replicate tables.

Q.What are the return values of functions SQLCODE and SQLERRM ?
SQLCODE returns the latest code of the error that has occurred.
SQLERRM returns the relevant error message of the SQLCODE.

Q.How packaged procedures and functions are called from the following?
a. Stored procedure or anonymous block
b. an application program such a PRC *C, PRO* COBOL
c. SQL *PLUS
a.PACKAGE NAME.PROCEDURE NAME (parameters);
variable := PACKAGE NAME.FUNCTION NAME (arguments);
EXEC SQL EXECUTE
b.
BEGIN
PACKAGE NAME.PROCEDURE NAME (parameters)
variable := PACKAGE NAME.FUNCTION NAME (arguments);
END;
END EXEC;
c. EXECUTE PACKAGE NAME.PROCEDURE if the procedures does not have any out/in-out parameters. A function can not be called.

Q.Operators used in SELECT statements are?
 = Equal
 <> or != Not equal
 > Greater than
 < Less than
 >= Greater than or equal
 <= Less than or equal
 BETWEEN Between an inclusive range
 LIKE Search for a pattern

Q.Give the structure of the procedure ?
basically procedure has three
parts
1.variable declaration(optional)
2.body(mandatory)
3.Exception(optional)
suppose ex
CREATE OR REPLACEPROCEDURE emp_pro( p_id IN employees.employee_id%TYPE)
IS
v_name employees.last_name%TYPE;
v_mail employees.email%TYPE;
BEGIN
SELECT last_name,email INTO v_name,v_mail FROM employees
WHERE employee_id:=p_id;
DBMS_OUTPUT.PUT_LINE('NAME:'||v_name ||'MAILID:'||v_mail);
END;
/
PROCEDURE name (parameter list.....)
is
local variable declarations
BEGIN
Executable statements.
Exception.
exception handlers
end;

Q.What is SPOOL?
spool command used for printing the out put of the sql statements in a file. Eg.
spool /tmp/sql_out.txt
select emp_name, emp_id from emp where dept='sales';
spool off;
we can see the out on /tmp/sql_out.txt file.
SPOOL command creates a print file of the report.

Q.IS Stored Function Is Pre-Compiled as Stored Procedure ? If No Why
stored procedure means the pre-compiled procedure ,which is used for a specific action to be executed more than one times whenever you called from
coding in the program.
stored procedure is an important concept in the application development in oracle.

Q.Explain the two type of Cursors ?
There are two types of cursors, Implicit Cursor and Explicit Cursor.
PL/SQL uses Implicit Cursors for queries. User defined cursors are called Explicit Cursors. They can be declared and used.

Q.What is Overloading of procedures ?
Overloading of procedure
name of the procedure is same but the number of parameters should be different.In that case,procedure will be overloaded.
2. if the number of parameters are same in that case,data type should be different.
if the two rules are satisfied in that case procedure will be overloaded.

Q.What are the PL/SQL Statements used in cursor processing ?

DECLARE CURSOR cursor name,
OPEN cursor name,
FETCH cursor name INTO or Record types,
CLOSE cursor name.

Q.What is Set Transaction?
Set Transaction is to establish properties for the current transaction.

Q.What is Character Functions?
Character Functions are INITCAP, UPPER, LOWER, SUBSTR & LENGTH. Additional functions are GREATEST & LEAST.
Group Functions returns results based upon groups of rows rather than one result per row, use group functions.
They are AVG, COUNT, MAX, MIN & SUM.

Q.What is an Exception ? What are types of Exception?
Exception is the error handling part of PL/SQL block. The types are Predefined and user defined. Some of Predefined exceptions are.
CURSOR_ALREADY_OPEN
DUP_VAL_ON_INDEX
NO_DATA_FOUND
TOO_MANY_ROWS
INVALID_CURSOR
INVALID_NUMBER
LOGON_DENIED
NOT_LOGGED_ON
PROGRAM-ERROR
STORAGE_ERROR
TIMEOUT_ON_RESOURCE
VALUE_ERROR
ZERO_DIVIDE
OTHERS.

Q.Is it possible to use Transaction control Statements such a ROLLBACK or COMMIT in Database Trigger? Why?
 It is not possible. As triggers are defined for each table, if you use COMMIT of ROLLBACK in a trigger, it affects logical transaction processing.
yes WE can use COMMIT and ROLLBACK triggers, but by using PRAGAMA AUTONAMOUS_TRANSATIONS. Now the transaction treated as a autonomous transaction.

Q.What is Multiple columns?
Multiple columns can be returned from a Nested subquery.

Q.What is Savepoint?
Savepoint is a point within a particular transaction to which you may rollback without rolling back the entire transaction.

Wednesday 28 September 2016

Top Daily Use Linux/Unix Commands

pwd
cat
less
cd
mkdir
ls
cp
mv
head
tail
wc
grep
kill
nohup
df
du
zip
tar
find
date

Top Useful Network Monitoring Commands

Useful commands for network monitoring

Ping  (Unix/Windows)
Traceroute  (Unix/Windows)
Arp (Unix/Windows)
Curl and wget (Unix/ Windows)
Netstat (Unix/Windows)
Whois (Unix/ Windows)
SSH (Unix/Linux/Windows)
TCPDump (Unix/Linux/Windows)
Ngrep (Unix/Linux/Windows)
NMAP (Unix/Windows)
Netcat (Windows/Unix)
Lsof (Unix/Windows)
IPtraf (Linux)

Ping (Unix/Window):
Ping is very basic and important command. Ping sends an ICMP ECHO_REQUEST packet to the specified host. If the host responds, you get an ICMP packet back. You can “ping” an IP address to see if a machine is alive. If there is no response, you know something is wrong. It’s also used to check the “speed” or latency time for said network connection. It’s a command that exists on all OS’s that support TCP/IP and it’s one of those basics you should know. Following is the ping examle.

$ ping HostName/IP Address
$ ping google.com
$ ping 192.168.182.132

You can specify the count of ECHO_REQUEST packets to be sent while ping a host. For this we can use parameter '-c' as following.

$ ping -c 4 192.168.182.132
PING 192.168.182.132 (192.168.182.132) 56(84) bytes of data.
64 bytes from 192.168.182.132: icmp_req=1 ttl=64 time=0.281 ms
64 bytes from 192.168.182.132: icmp_req=2 ttl=64 time=0.495 ms
64 bytes from 192.168.182.132: icmp_req=3 ttl=64 time=0.459 ms
64 bytes from 192.168.182.132: icmp_req=4 ttl=64 time=0.250 ms

--- 192.168.182.132 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.250/0.371/0.495/0.107 ms

Traceroute  (Unix/Windows):
Traceroute command is a very useful network diagnostic tool. Traceroute displays each host that a packet travels through(devices, switches, routers, computers) as it tries to reach its destination. In case of trouble it can give us an idea at which point problem is there while moving packets to its destination.

Traceroute (Unix):
~$ traceroute 192.168.182.132
traceroute to 192.168.182.132 (192.168.182.132), 30 hops max, 60 byte packets
 1  ubuntu.local (192.168.182.132)  0.302 ms  0.316 ms  0.304 ms

tracert( Window ):
C:\Users\Mukesh.Kumar>tracert google.com

Tracing route to google.com [216.58.220.46]
over a maximum of 30 hops:
  1     *        *        *     Request timed out.
  2    48 ms    56 ms    49 ms  10.210.0.82
  3    55 ms    23 ms    41 ms  10.210.0.86
  4    25 ms    26 ms    29 ms  125.17.150.37
  5    62 ms    42 ms    38 ms  182.79.234.221
  6    49 ms     *       59 ms  72.14.242.178
  7    56 ms    46 ms    34 ms  66.249.94.73
  8    44 ms    36 ms     *     209.85.255.43
  9    57 ms    50 ms    29 ms  maa03s18-in-f14.1e100.net [216.58.220.46]

Trace complete.

Note : The Unix "traceroute" uses UDP datagrams rather than ICMP to perform a similar function link ping.

Arp (Unix/Windows) :
Using the arp command allows you to display and modify the Address Resolution Protocol (ARP) cache. An ARP cache is a simple mapping of IP addresses to MAC addresses.
Example:
C:\Users\Mukesh.Kumar>arp -a 192.168.182.132

Interface: 192.168.182.1 --- 0x1e
  Internet Address      Physical Address      Type
  192.168.182.132       00-0c-29-08-b7-93     dynamic
 
Wget and curl (Unix/ Windows):
    This command allow to download files or entire webpage.both are command line tools that can download contents from FTP, HTTP and HTTPS
    both can send HTTP POST requests
    both support HTTP cookies
    both are designed to work without user interaction, like from within scripts
    both are fully open source and free software
    both support metalink

    Note : curl supports FTP, FTPS, Gopher, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT,
      LDAP, LDAPS, FILE, POP3, IMAP, SMB/CIFS, SMTP, RTMP and RTSP.    
      Wget only supports HTTP, HTTPS and FTP

Netstat (Unix/Windows):
Netstat prints information about the Linux networking subsystem. By default, netstat displays a list of open sockets.  If you don't specify any address families, then the active sockets of  all  configured address families will be printed.  The type of information printed is controlled by the first argument,as follows:
Netstat( Unix ):
   --route , -r
       Display the kernel routing tables. See the description in route(8) for details.  netstat -r and route -e produce the same output.

   --groups , -g
       Display multicast group membership information for IPv4 and IPv6.

   --interfaces, -i
       Display a table of all network interfaces.

   --masquerade , -M
       Display a list of masqueraded connections.

   --statistics , -s
       Display summary statistics for each protocol.
'

Whois (Unix/ Windows) :
Network command used to consult domain data. Mainly data like the domain owner, it’s expire time, configured registries, contact data, etc. are consulted. It’s very recommendable to use it to contact domain administrators or for service migration instances, such as email or webpage migrations.

In order to use ‘whois’ on Windows, you should download the software linked on the following URL: https://technet.microsoft.com/en-us/sysinternals/whois.aspx

You can also consult them using services such as https://www.whois.net/ on your browser.

SSH (Unix/Linux/Windows):
SSH, or Secure Shell, is a protocol used to securely log onto remote systems. It is the most common way to access remote Linux and Unix-like servers.

$ ssh remote-host

If remote user name is diffrence then you can use username also
$ ssh user-name@remote-host

If you wish to execute a command on remote system, you can specify it after the host-name.
$ ssh remote_host command_to_run
$ ssh mukesh@ubuntu-server.com ls

Note : To use SSH on Windows we recommend using Putty. http://www.putty.org/

TCPDump (Unix/Linux/Windows):
tcpdump is a most powerful and widely used command-line packets sniffer or package analyzer tool which is used to capture or filter TCP/IP packets that received or transferred over a network on a specific interface. It is available under most of the Linux/Unix based operating systems. tcpdump also gives us a option to save captured packets in a file for future analysis. It saves the file in a pcap format, that can be viewed by tcpdump command or a open source GUI based tool called Wireshark (Network Protocol Analyzier) that reads tcpdump pcap format files.

Ngrep (Unix/Linux/Windows):
This takes the potency of the ‘grep’ command to the Net. It’s basically a tcpdump with text subchain filters in real time. It’s an HTTP, SMTP, DNS and other protocol communication packets filter. It has a very powerful filtering system over regular expressions and it’s usually used to process files generated by tcpdump, Wireshark, etc.

NMAP (Unix/Windows):
The Nmap aka Network Mapper is an open source and a very versatile tool for Linux system/network administrators. Nmap is used for exploring networks, perform security scans, network audit and finding open ports on remote machine. It scans for Live hosts, Operating systems, packet filters and open ports running on remote hosts.

Netcat (Windows/Unix):
Netcat or nc is a networking utility for debugging and investigating the network.

This utility can be used for creating TCP/UDP connections and investigating them. The biggest use of this utility is in the scripts where we need to deal with TCP/UDP sockets.

Lsof (Unix/Windows):
It’s a tool that’s useful for identifying which files a process is using or keeping open. In the case of Unix environments, a file is also a network connection, so it’s useful to know which ports are open during a specific running process, something which can prove to be extremely useful in some cases.
It can also be used to know how many files a single process has open. It doesn’t have anything to do with the network, but we’re sure it’ll be useful for you anyway. Lsof is one of those tools you should know about.

IPtraf (Linux):
A specialized network command which obtains traffic statistics. It has an ncurses interface (text) to analyze the traffic that goes through an interface in real time. Very useful if you see anomalies on your device and you need to see and inspect the traffic coming through it.

Sunday 25 September 2016

How To Install NRPE on Ubuntu 16.04, 14.04, 12.04 and LinuxMint

NRPE is stands for Nagios Remote Plugin Executor. In previous post i had described about installation of Nagios Server on Ubuntu operating system(link). This article will help you to install NRPE on Ubuntu 15.04, 14.04, 12.04 & LinuxMint systems.

The NRPE addon is designed to allow you to execute Nagios plugins on remote Linux/Unix machines. The main reason for doing this is to allow Nagios to monitor "local" resources (like CPU load, memory usage, etc.) on remote machines. Since these public resources are not usually exposed to external machines, an agent like NRPE must be installed on the remote Linux/Unix machines.
Need personal assistance on Nagios? Please contact me at

immukesh72@gmail.com

at very nominal charges

Note: It is possible to execute Nagios plugins on remote Linux/Unix machines through SSH. There is a check_by_ssh plugin that allows you to do this. Using SSH is more secure than the NRPE addon, but it also imposes a larger (CPU) overhead on both the monitoring and remote machines. This can become an issue when you start monitoring hundreds or thousands of machines. It is useful to use NRPE to lower load on monitoring and remote host as well.












 As showing in above image the NRPE addon consists of two pieces:

– The check_nrpe plugin, which resides on the local monitoring machine.
– The NRPE daemon, which runs on the remote Linux/Unix machine.

Step 1. Install NRPE and Nagios Plugins

NRPE is available under default apt repositories of Ubuntu systems. Execute the following command to install it

$ sudo apt-get install nagios-nrpe-server nagios-plugins nagios-plugins-basic nagios-plugins-standard

Step 2. Configure NRPE

Edit the /etc/nagios/nrpe.cfg file and in allowed_hosts give the ip address of Nagios monitoring Server from which monitoring will be done. For example nagios monitoring host server ip is 192.168.182.130, then add this IP address to allowed_hosts list.

allowed_hosts=127.0.0.1,192.168.182.130

Step 3. Restart the NRPE service

Restart the nrpe service in client machine with following command.

$ sudo /etc/init.d/nagios-nrpe-server restart
Step 4. Install nagios-nrpe-plugin package in your nagios monitoring server. 
Run the following command to install the check_nrpe plugin on your Nagios server. It will install check_nrpe plugin under /usr/lib/nagios/plugins/.

$ sudo apt-get install nagios-nrpe-plugin

Step 5. Verification 

Now login to the nagios server and sudo nagios user. To verify if nagios are able to communicate with nrpe agent in client machine run check_nrpe plugin by passing client machine IP address ( In my case client machine IP is 192.168.182.131 )as following. If you are not able to find check_nrpe plugin please follow the setp 4 to install check_nrpe plugin.

nagios@ubuntu:~$ /usr/lib/nagios/plugins/check_nrpe -H 192.168.182.131
NRPE v2.12

The output "NRPE v2.12" shows that the nagios server was successfully communicating with NRPE agent.

Step 6. Add nagios check command in NRPE

By defaults all nagios installed in /usr/lib/nagios/plugins/ directory. You can add new service check as per your requirements in /etc/nagios/nrpe.cfg. Folloing are the default check commans in nrpe.cfg file.

command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 150 -c 200

Step 7. Start/Stop/Restart NRPE service

Each time when we make change into the configuration file which are in client machine that time we need to restart the nrpe srevice.

$ sudo /etc/init.d/nagios-nrpe-server stop
$ sudo /etc/init.d/nagios-nrpe-server start
$ sudo /etc/init.d/nagios-nrpe-server restart


   
Need personal assistance on Nagios? Please contact me at

immukesh72@gmail.com

at very nominal charges

Tuesday 30 August 2016

SQL Important Queries

>I have a table with duplicate items in it. Write me a query which returns only duplicate rows with number of times they are repeating.

    select item,count(*) from tab group by item having count(*)>1;

>How to find the 2nd max sal from the table.

  To get 2nd highest salary.
  select max(salary) from employee where salary < (select max(salary) from employee);
  select max(salary) from employee where salary not in(select max(salary) from employee);

  To get 5th highest salary.
  select * from tab a where 5 =(select count(distinct salary) from tab b where a.salary<=b.salary);
 
> What command is used to create a table by copying the structure of another table?
   without data:
   CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1=2;

   with data:
   CREATE TABLE new_table AS SELECT * FROM old_table ;

> How to copy all data from tab1 to tab2 ,where tab1 and tab2 having same table structure.

   INSERT INTO emp_details1 SELECT * from emp_details where emp_id >1000;

> What operator tests column for the absence of data?
    NULL operator is used to check the absence of data in a column.
    Example:
    SELECT * FROM emp WHERE sal is NULL;
  
> How can we delete records in a table using update table?
  update tab set name=null where id=2;
 
>Identify error in following SQL:
    SELECT EMPNO, SUM(SAL)
    FROM EMP
    GROUP BY EMPNO
    HAVING ENAME= ?KENT?
a) Syntax Error
b) SUM() is not an aggregate function
c) HAVING can not be used with GROUP BY
d) The Having condition has to be based on some column that appears in the select list.
 
>How to remove the duplicate records from table which havting same id? Consider following table which having 2 rows with id=100.
  #select * from tab;
     id  |    name    |
    -----+------------+
     100 | mukesh     |
     101 | shivam     |
     102 | smrati     |
     100 | mukesh     |
     --in above table need to remove mukesh
--step1
  add one column num
  --ALTER TABLE tab2 ADD column num character varying;
--Step2
  Create sequence num_seq;
--Step3
  UPDATE TABLE tab set num=nextval('num_seq');
--Step4
  DELETE FROM tab where num in(SELECT MAX(num) from tab group by name having count(*)>1;
--Step5
  Check if still duplicate is there.
  Select count(*),name from tab group by name having count(*)>1;
  if above query returns any records ,repete step4.
 
 --Method 2 :

      acc_no |  name  | joint_account
    --------+--------+---------------
     100    | mukesh | 101
     200    | shivam | 102
     300    | smrati | 200
     400    | rity   | 100
     100    | mukesh | 122
     401    | rity   | 122
     402    | rity   | 122
  
--delete from emp_details1 where acc_no not in(select min(acc_no)from emp_details1 group by name);

    mydb=# select * from emp_details1;
     acc_no |  name  | joint_account
    --------+--------+---------------
     100    | mukesh | 101
     200    | shivam | 102
     300    | smrati | 200
     400    | rity   | 100
     100    | mukesh | 122

SQL Theory Questions & Answers

> How many kinds of statement SQL have?

SQL have 3 kind of statements as following.
DDL (Data Definition Language)
    CREATE – To create objects in the database
    ALTER  – To alter the structure of the database
    DROP   – To delete objects from the database
    TRUNCATE – To remove all records from a table. The  space allocated for the records is also removed

DML (Data Manipulation Language):
    SELECT – To retrieve data from the database
    INSERT – To insert data into a table
    UPDATE – To update existing data within a table
    DELETE – To delete all records from a table. Delete statement does not release space.

DCL (Data Control Language ):
    GRANT     – To give user access privileges to database objects
    REVOKE  – To withdraw access privileges given with the GRANT command
    COMMIT  – To save the work done
    ROLLBACK – To restore database to original since the last COMMIT

> How many types of constraints are in SQL ?

Constraints are the rule that restricts the values in a database.
  1.   PRIMARY KEY
  2.   NOT NULL
  3.   UNIQUE
  4.   CHECK
  5.   FOREIGN KEY
> What is the difference between primary key and unique key?
 
Primary Key:
i  ) Can be only one in a table.
ii ) It never allows null values.
iii) Primary Key is unique key identifier and can not be null and must be unique.

Unique Key:
i  ) Can be more than one unique key in one table.
ii ) Unique key can have one or multiple null but value can not duplicate.
iii) It can’t be candidate key
iv ) Unique key can be null and may not be unique.
 
> What is the difference between TRUNCATE and DELETE commands?
  •   DELETE is a DML command whereas TRUNCATE is a DDL command.Hence DELETE operation can be rolled back but TRUNCATE can not.
  •   DELETE does not release the memory occupied by the records of the table .TRUNCATE releases the memory occupied by the records of the table
  •   WHERE clause can be used with DELETE and not with TRUNCATE.
> Explain UNION, MINUS, UNION ALL and INTERSECT?

    INTERSECT - Return all distinct rows but conman records exist in both queries.
    Union     - Return all distinct rows from both queries.
    Union All - Returns all rows of both queries.
    MINUS     - Returns all distinct rows selected by the first query but not by the second

Example:
SELECT new_id from tab1 INTERSECT SELECT new_id FROM TAB2;

> How many types of join explain with example.
1) Cartesian join
2) Inner join
3) Outer join
    -Left-outer join
    -Right-outer join
4) Self join

1) Cartesian join:
    A Cartesian join occurs when data is selected from two or more tables and there is no common relation specified in the WHERE clause.
    If the first table has 3 rows and the second table has 4 rows, the result will have 12 rows.
    Example:
    SELECT * FROM Table1,Table2;
  
2) Inner join:
    Inner join is the cartesian product between 2 or more tables which satisfies the join condition in the WHERE clause .
    Example:
    SELECT Table1.Emp_ID, Table1.City, Table2.Cust_ID, Table2.City
    FROM Table1, Table2
        WHERE Table1.City = Table2.City;

3) Outer join
    -Left-outer join :
    A left outer join will return all the rows that an inner join returns and also return those rows from left table which does not have match in right side table.

    -Right-outer join
    A right outer join will return all the rows that an inner join returns and also return those rows from right table which does not have match in lest side table.

4) Self join:
    When you wish to join a table with itself based on some criteria then treat the table as two different tables by giving synonyms.
    Example:
    Consider a table Employee_Manager with column Emp_id,Emp_name and Mgr_id
    SELECT     Emp.Employee_ID , Emp.Employee_Name ,
    Emp.Mgr_id , Manager.Employee_Name
     FROM employee_Manager  Emp, employee_Manager  Manager
        WHERE Emp.Mgr_id  =  Manager.Employee_ID;
      
> What is difference between Stored Procedure and Trigger?
  • For executing SP(Stored Procedure) we have to call Stored Procedure. But in case of Trigger it is call whenever there is an action takenon table or column(Insert, Update ,Delete).
  • Stored procedure can take input parameters, but we can not pass parameters as input to a trigger.
  • Stored procedure returns value but trigger cannot returns value.
  • Stored procedures can be scheduled through a job to execute on a predefined time, but we can not schedule a trigger.
  • We can use Print commands inside a stored procedure for debugging purposes but we can not use print commands inside a trigger.
  • Stored procedures are used for performing tasks while Triggers used for auditing work.
> What is difference between procedure and function?
  • Function must return a value but in Stored Procedure it is optional( Procedure can return zero or n values).
  • Functions can have only input parameters for it whereas Procedures can have input/output parameters .
  • Functions can be called from Procedure whereas Procedures cannot be called from Function.
> How to add constraints while creating table
  CREATE TABLE Customer1(
    customerid varchar2(4) CONSTRAINT cust1_pkey PRIMARY KEY CHECK (customerid like 'C%'),
    customername varchar2(20) CONSTRAINT custname_nnull NOT NULL,-- DEFAULT 'XXX',
    emailid varchar2(20) CONSTRAINT email_unique UNIQUE,
    accountno NUMBER CONSTRAINT acc_fkey REFERENCES accounts(accno)
);

--MySQL DB
CREATE TABLE `staging`.`DailyCheck` (
   `date` date NOT NULL DEFAULT '0000-00-00',
   `campaignid` varchar(255) NOT NULL DEFAULT '',
   `campaign` varchar(255) NOT NULL,
   `imps` int(11) NOT NULL DEFAULT '0',
   `clicks` int(11) NOT NULL DEFAULT '0',
   `cost` double NOT NULL DEFAULT '0',
   PRIMARY KEY (`campaignid`,`date`),
   KEY `datidx` (`date`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 CREATE TABLE CUSTOMER(
    customerid varchar(4) NOT NULL,
    customername varchar(20) NOT NULL,-- DEFAULT 'XXX',
    emailid varchar(20)  UNIQUE,
    accountno int(11)
    );
  
> How to use AAD,DROP and MODIFY on column.
    DROP TABLE TAB1;

    ALTER TABLE tab ADD phone_no char(10);
    ALTER TABLE tab MODIFY phone_no char(12); --not working in postgres.
    ALTER TABLE tab1 MODIFY phone_no CONSTRAINT ph_nnull NOT NULL;
    ALTER TABLE tab ADD CONSTRAINT tab_uniqe UNIQUE(phone_no);

--modifying column in postgresql
    ALTER TABLE "MY_TABLE" ALTER COLUMN "MyColumn" TYPE varchar(8) USING substr("MyColumn", 1, 8)

    ALTER TABLE tab DROP CONSTRAINT ph_unique;
--droping a column
    ALTER TABLE TAB DROP column phone_no;
--rename a column
    ALTER TABLE TAB1 RENAME COLUMN id to new_id;

    ALTER TABLE TAB ADD CONSTRAINT tab_pkey PRIMARY KEY (ID);
    alter TABLE tab add CONSTRAINT tab_pkey PRIMARY KEY (ID,NAME);
    ALTER TABLE TAB ADD CONSTRAINTS c_check CHECK(ID BETWEEN 101 AND 120);

    ALTER TABLE tab DROP constraint tab_pkey;
    ALTER TABLE TAB DROP PRIMARY KEY; --Not supporting in postgres

    ALTER TABLE employee ADD FOREIGN KEY (dno) REFERENCES department(dnumber)

> In subqueries, which is efficient ,the IN clause or EXISTS clause? Does they produce the same result?

  EXISTS is efficient because,
  1.Exists is faster than IN clause.
  2.IN check returns values to main query where as EXISTS returns Boolean (T or F).
 
> Explain normalization with examples.

Normalization is a process of eleminating the redundancy and increasing the integrity.
Normal Forms: Review : -
Unnormalized ? There are multivalued attributes or repeating groups
    1 NF ? No multivalued attributes or repeating groups.
    2 NF ? 1 NF plus no partial dependencies
    3 NF ? 2 NF plus no transitive dependencies

Option 1: Make a determinant of the repeating group (or the multivalued attribute) a part of the primary key.

Option 2: Remove the entire repeating group from the relation. Create another relation which would contain all the attributes of the repeating group, plus the primary key from the first relation. In this new relation, the primary key from the original relation and the determinant of the repeating group will comprise a primary key.

Remove the attributes, which are dependent on a non-key attribute, from the original relation. For each transitive dependency, create a new relation with the non-key attribute which is a determinant in the transitive dependency as a primary key, and the dependent non-key attribute as a dependent.
        
> What is De-normalization and difference between normalization and De-normalization?
  • De-normalization is the process of attempting to optimize the performance of a database by adding redundant data or by grouping data.
  • Normalization is the process of De-composing a relation with anomalies into a well structured relation.
> What is difference between Co-related sub query and nested sub query?
  •   Correlated sub query runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query.
  •   Nested sub query runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.
  For example,
  Correlated Subquery:
    select e1.empname, e1.basicsal, e1.deptno from emp e1 where e1.basicsal = (select max(basicsal) from emp e2 where e2.deptno = e1.deptno);

  Nested Subquery:
    select empname, basicsal, deptno from emp where (deptno, basicsal) in (select deptno, max(basicsal) from emp group by deptno);
  
> What is the main difference between the IN and EXISTS clause in sub queries?
The main difference between the IN and EXISTS predicate in subquery is the way in which the query gets executed.

IN : The inner query is executed first and the list of values obtained as its result is used by the outer query.The inner query is executed for only once.

EXISTS : The first row from the outer query is selected ,then the inner query is executed and , the outer query output uses this result for checking.This process of inner query execution repeats as many no.of times as there are outer query rows. That is, if there are ten rows that can result from outer query, the inner query is executed that many no.of times.

> what is the ACID property in database. 
ATOMICITY-- Atomicity requires that each transaction is "all or nothing": if one part of the transaction fails, the entire transaction fails, and the database state is left unchanged
CONSISTENCY -- The consistency property ensures that any transaction will bring the database from one valid state to another
ISOLATION -- Isolation refers to the requirement that other operations cannot access or see the data in an intermediate state during a transaction
DURABILITY --Durability refers to the guarantee that once the user has been notified of success, the transaction will persist, and not be undone.

> How to find out the database name(schema name) and tables from SQL*PLUS command prompt?
  select * from information_schema.tables
 
> What is output of following query? "Select 2 from employee";
   It depends upon number of rows in table. This query will print 2 as many times as rows in table.
 
> What is the use of CASCADE CONSTRAINTS?
1)-Cascade constraint is used with drop column clause.
2)-Cascade constraint drops all referential integrity constraint primary key and unique key from a multi column table.
3)-Cascade constraint also drop multi column constraint.

    Alter table Emp Drop (PK) cascade constraint;

> What is a default TCP/IP socket assigned for SQL Server?
   1433 is default tcp/ip socket sql server.
   5432 in case of postgresql.
   3306 in case of mysql

> How can i hide a particular table name of our schema.
    We can create in two ways:
    1) using synonym
    2) by using views.

synonym : an alternative name for a table, view, sequence, procedure, stored function, package, materialized view, Java class schema object, user-defined object type, or another synonym.

> What are two methods of retrieving SQL?
   1-select
   2-using cursor
   stored procedures
Related Posts Plugin for WordPress, Blogger...