PL I – ASSIGNMENT 4

ASSIGNMENT NO. 4

Title:

            Design at least 10 SQL queries for suitable database application using SQL DML statements: Insert, Select, Update, Delete Clauses using distinct, count, aggregation on Client-Data sever(three tier)

Requirements:

  1. Computer System with Windows/Linux/Open Source Operating System.
  2. Oracle Server
  3. JDK
  4. JDBC Connector

Theory:

            Three Tier Architecture Diagram

exp2-1

 Data Manipulation Language (DML) Statements : Data manipulation language (DML) statements access and manipulate data in existing schema objects. These statements do not implicitly commit the current transaction. The SELECT statement is a limited form of DML statement in that it can only access data in the database. It cannot manipulate data in the database, although it can operate on the accessed data before returning the results of the query. The data manipulation language statements are: INSERT, UPDATE, DELETE, SELECT

The SELECT statement is a limited form of DML statement in that it can only access data in the database. It cannot manipulate data in the database, although it can operate on the accessed data before returning the results of the query.

The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.

Syntax: There are two basic syntax of INSERT INTO statement as follows:

INSERT INTO TABLE_NAME (column1, column2, column3,…columnN)]  VALUES (value1, value2, value3,…valueN);

Here, column1, column2,…columnN are the names of the columns in the table into which want to insert data.

It’s not required to specify the column(s) name in the SQL query if values for all the columns of the table are provided. But make sure the order of the values is in the same order as the columns in the table. The SQL INSERT INTO syntax would be as follows:

INSERT INTO TABLE_NAME VALUES (value1,value2,value3,…valueN);

Following statements would create records in CUSTOMERS table:

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, ‘Ramesh’, 32, ‘Ahmedabad’, 2000.00 );

INSERT INTO CUSTOMERS VALUES (7, ‘Muffy’, 24, ‘Indore’, 10000.00 );

Populate one table using another table:

You can populate data into a table through select statement over another table provided another table has a set of fields, which are required to populate first table. Here is the syntax:

INSERT INTO first_table_name [(column1, column2, … columnN)]  SELECT column1, column2, …columnN FROM second_table_name   [WHERE condition];

SQL SELECT statement is used to fetch the data from a database table which returns data in the form of result table. These result tables are called result-sets.

Syntax: The basic syntax of SELECT statement is as follows:

SELECT column1, column2, columnN FROM table_name;

Here, column1, column2…are the fields of a table whose values you want to fetch. If you want to fetch all the fields available in the field, then you can use the following syntax:

SELECT * FROM table_name;

SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;

 

The SQL WHERE clause is used to specify a condition while fetching the data from single table or joining with multiple tables. If the given condition is satisfied then only it returns specific value from the table. You would use WHERE clause to filter the records and fetching only necessary records. The WHERE is also used in UPDATE, DELETE statement

Syntax: The basic syntax of SELECT statement with WHERE clause is as follows:

SELECT column1, column2, columnN  FROM table_name WHERE [condition]

You can specify a condition using comparison or logical operators like >, <, =, LIKE, NOT, etc.

SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000;

SQL> SELECT ID, NAME, SALARY  FROM CUSTOMERS WHERE NAME = ‘Hardik’;

The SQL AND and OR operators are used to combine multiple conditions to narrow data in an SQL statement. These two operators are called conjunctive operators. These operators provide a means to make multiple comparisons with different operators in the same SQL statement.

The AND Operator: The AND operator allows the existence of multiple conditions in an SQL statement’s WHERE clause.

Syntax: The basic syntax of AND operator with WHERE clause is as follows:

SELECT column1, column2, columnN  FROM table_name WHERE [condition1] AND [condition2]…AND [conditionN];

You can combine N number of conditions using AND operator. For an action to be taken by the SQL statement, whether it be a transaction or query, all conditions separated by the AND must be TRUE.

SQL> SELECT ID, NAME, SALARY  FROM CUSTOMERS WHERE SALARY > 2000 AND age < 25;

The OR Operator: The OR operator is used to combine multiple conditions in an SQL statement’s WHERE clause.

Syntax: The basic syntax of OR operator with WHERE clause is as follows:

SELECT column1, column2, columnN  FROM table_name WHERE [condition1] OR [condition2]…OR [conditionN]

You can combine N number of conditions using OR operator. For an action to be taken by the SQL statement, whether it be a transaction or query, only any ONE of the conditions separated by the OR must be TRUE.

SQL> SELECT ID, NAME, SALARY  FROM CUSTOMERS WHERE SALARY > 2000 OR age < 25;

The SQL UPDATE Query is used to modify the existing records in a table. You can use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected.

Syntax: The basic syntax of UPDATE query with WHERE clause is as follows:

UPDATE table_name SET column1 = value1, column2 = value2…., columnN = valueN WHERE [condition];

You can combine N number of conditions using AND or OR operators.

SQL> UPDATE CUSTOMERS SET ADDRESS = ‘Pune’ WHERE ID = 6;

SQL> UPDATE CUSTOMERS SET ADDRESS = ‘Pune’, SALARY = 1000.00;

The SQL DELETE Query is used to delete the existing records from a table. You can use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted.

Syntax: The basic syntax of DELETE query with WHERE clause is as follows:

DELETE FROM table_name WHERE [condition];

You can combine N number of conditions using AND or OR operators.

SQL> DELETE FROM CUSTOMERS WHERE ID = 6;

If you want to DELETE all the records from CUSTOMERS table, you do not need to use WHERE clause and DELETE query would be as follows:

SQL> DELETE FROM CUSTOMERS;

The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate records and fetching only unique records. There may be a situation when you have multiple duplicate records in a table. While fetching such records, it makes more sense to fetch only unique records instead of fetching duplicate records.

Syntax: The basic syntax of DISTINCT keyword to eliminate duplicate records is as follows:

SELECT DISTINCT column1, column2,…..columnN  FROM table_name WHERE [condition]

SELECT DISTINCT SALARY FROM CUSTOMERS ORDER BY SALARY;

The SQL GROUP BY  clause is used in collaboration with the SELECT statement to arrange identical data into groups. The GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause.

Syntax: The basic syntax of GROUP BY clause is given below. The GROUP BY clause must follow the conditions in the WHERE clause and must precede the ORDER BY clause if one is used.

SELECT column1, column2 FROM table_name WHERE [ conditions ] GROUP BY column1, column2

SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP BY NAME;

Following is an example, which would sort the result in descending order by NAME:

SQL> SELECT * FROM CUSTOMERS ORDER BY NAME DESC;

SQL has many built-in functions for performing processing on string or numeric data. Following is the list of all useful SQL built-in functions:

SQL SUM Function – The SQL SUM aggregate function allows selecting the total for a numeric column.

SQL COUNT Function – The SQL COUNT aggregate function is used to count the number of rows in a database table.

Syntax : SELECT COUNT(column_name) FROM   table_name WHERE  CONDITION;

SQL MAX Function – The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain column.

SQL MIN Function – The SQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column.

SQL AVG Function – The SQL AVG aggregate function selects the average value for certain table column.

Conclusion: Hence details about various DML commands and aggregate functions with GROUP BY clause are provided here.

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)

VALUES (2, ‘Khilan’, 25, ‘Delhi’, 1500.00 );

 

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)

VALUES (3, ‘kaushik’, 23, ‘Kota’, 2000.00 );

 

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)

VALUES (4, ‘Chaitali’, 25, ‘Mumbai’, 6500.00 );

 

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)

VALUES (5, ‘Hardik’, 27, ‘Bhopal’, 8500.00 );

 

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)

VALUES (6, ‘Komal’, 22, ‘MP’, 4500.00 );

********************** PROGRAM **************************

AIM: Design at least 10 SQL queries for suitable database application using SQL DML  statements: Insert, Select, Update, Delete Clauses using distinct, count, aggregation  on Client-Data sever(three tier)

 import java.io.*;
import java.sql.*;
import oracle.jdbc.*;
import oracle.sql.*;
public class Expt4 {
    public static void main(String args[])
    {
        try
            {
                System.out.println("***********MENU********");
                System.out.println("1. Insert\n2. Delete\n3. Update\n4. Select\n5. Aggregate Functions\n6. Exit\nEnter your Choice\n*********************\n");
               BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
               int choice=Integer.parseInt(br.readLine());
               Class.forName("oracle.jdbc.OracleDriver");
                Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
                Statement stmt=con.createStatement();
                do
                {
               switch(choice)
               {
                   case 1:
                       System.out.println("************INSERT***********");
                       System.out.println("Enter your First name");
                       String fname=br.readLine();
                       System.out.println("Enter your Last name");
                       String lname=br.readLine();
                       System.out.println("Enter your Roll no.");
                       int rno=Integer.parseInt(br.readLine());
                       System.out.println("Enter your Dept no.");
                       int dno=Integer.parseInt(br.readLine());
                       String query="insert into student_te_a(rno,fname,lname,deptno) values("+rno+",'"+fname+"','"+lname+"',"+dno+");";
                       int rows=stmt.executeUpdate(query);
                       System.out.println(rows + " rows inserted");
                       break;
                   case 2:
                            System.out.println("************DELETE***********");
                           System.out.println("Enter Roll no. you want to delete");
                           rno=Integer.parseInt(br.readLine());
                           query="delete from student_te_a where rno="+rno;
                           rows=stmt.executeUpdate(query);
                           System.out.println(rows + " row(s) deleted");
                           break;
                   case 3:
                            System.out.println("************UPDATE***********");
                            System.out.println("Enter Roll no. you want to update");
                            rno=Integer.parseInt(br.readLine());
                            System.out.println("Enter new Dept no.");
                            dno=Integer.parseInt(br.readLine());
                            query="update student_te_a set dno="+dno+" where rno="+rno;
                           rows=stmt.executeUpdate(query);
                           System.out.println(rows + " row(s) updated");
                           
                       break;
                   case 4:
                       System.out.println("************SELECT***********");
                            query="select * from student_te_a where rno between 1 to 10";
                           ResultSet rs=stmt.executeQuery(query);
                           while(rs.next())
                           {
                               System.out.println("******************");
                               System.out.println("Roll no"+rs.getInt(1));
                               System.out.println("First Name"+rs.getString(2));
                               System.out.println("Last Name"+rs.getInt(3));
                               System.out.println("Dept no"+rs.getInt(4));
                               System.out.println("******************");
                          }
                      
                       break;
                   case 5:
                       break;
                   case 6:
                       System.exit(0);
                       break;
                   default:
                       System.out.println("Please check your choice");
             
               }
                }while(true);
    }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
   
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *