Experiment No.1: Computer Software and Applications - II
Experiment No.1: Computer Software and Applications - II
EXPERIMENT NO.1
DDL- Data Definition Language (DDL) statements are used to define the database structure or schema. Data
Definition Language understanding with database schemas and describes how the data should consist in the
database, therefore language statements like CREATE TABLE or ALTER TABLE belongs to the DDL. DDL is
about "metadata".
DDL includes commands such as CREATE, ALTER and DROP statements.DDL is used to CREATE, ALTER
OR DROP the database objects (Table, Views, Users).
CREATE TABLE
Syntax: Create table table name( fieldname1 datatype(),fieldname2 datatype()...);
ALTER TABLE
1. ADD
2.MODIFY
ADD
Syntax:alter table table name ADD (fieldname datatype()...);
modify
syntax: Alter table table name modify (fieldname datatype()...);
DESCRIBE TABLE
Syntax: DESCRIBE TABLE NAME;
DROP TABLE
Syntax: DROP Table name;
EXPERIMENT NO.2
DML- Data Manipulation Language (DML) statements are used for managing data within schema objects DML
deals with data manipulation, and therefore includes most common SQL statements such SELECT, INSERT,
etc. DML allows to add / modify / delete data itself.
DML is used to manipulate with the existing data in the database objects (insert, select, update, delete).
DML Commands:
1.INSERT
2.SELECT
3.UPDATE
4.DELETE
*INSERT:
Syntax: INSERT INTO Table name values();
*SELECT:
Syntax: Select*from <table name>
*UPDATE:
Syntax: Update<table name> set to(calculation);
*DELETE:
Syntax: Delete form<table name>
Example : In the below figure shows the update and delete command on the given table.
EXPERIMENT NO.3
NEW CONCEPTS : The most commonly used SQL command is SELECT statement.
The SQL SELECT statement is used to query or retrieve data from a table in the database. A
query may retrieve information from specified columns or from all of the columns in the
table. To create a simple SQL SELECT Statement, you must specify the column(s) name and
the table name. The whole query is called SQL SELECT Statement.
THEORY :-
The SQL SELECT statement queries data from tables in the database. The statement begins
with the SELECT keyword. The basic SELECT statement has 3 clauses:
SELECT
FROM
WHERE
The SELECT clause specifies the table columns that are retrieved. The FROM clause
specifies the tables accessed. The WHERE clause specifies which table rows are used. The
WHERE clause is optional; if missing, all table rows are used.
For example,
This query accesses rows from the table - s. It then filters those rows where the city column
contains Rome. Finally, the query retrieves the name column from each filtered row. Using
the example s table, this query produces:
name
Mario
The WHERE clause filters the rows of the FROM table to use those whose city
column contains Rome. This chooses a single row from s:
The SELECT clause retrieves the name column from the rows filtered by the WHERE
clause:
name
Mario
The remainder of this subsection examines the 3 major clauses of the SELECT statement,
detailing their syntax and semantics:
SELECT Clause
The SELECT clause is mandatory. It specifies a list of columns to be retrieved from the
tables in the FROM clause. It has the following general format:
SELECT [ALL|DISTINCT] select-list
select-list is a list of column names separated by commas. The ALL and DISTINCT
specifiers are optional. DISTINCT specifies that duplicate rows are discarded. A duplicate
row is when each corresponding select-list column has the same value. The default is ALL,
which retains duplicate rows.
For example,
A special select list consisting of a single '*' requests all columns in all tables in the FROM
clause. For example,
SELECT * FROM sp
sno pno qty
S1 P1 NULL
S2 P1 200
S3 P1 1000
S3 P2 200
The * delimiter will retrieve just the columns of a single table when qualified by the table
name. For example:
SELECT sp.* FROM sp
This produces the same result as the previous example.
An unqualified * cannot be combined with other elements in the select list; it must be stand
alone. However, a qualified * can be combined with other elements. For example,
S1 P1 NULL Paris
S2 P1 200 London
S3 P1 1000 Rome
S3 P2 200 Rome
FROM Clause
The FROM clause always follows the SELECT clause. It lists the tables accessed by the
query.
For example,
SELECT * FROM s
When the From List contains multiple tables, commas separate the table names. For example,
SELECT sp.*, city
FROM sp, s
WHERE sp.sno=s.sno
When the From List has multiple tables, they must be joined together. See Joining Tables.
Correlation Names
Like columns in the select list, tables in the from list can be renamed by following the table
name with the new name. For example,
SELECT supplier.name FROM s supplier
The new name is known as the correlation (or range) name for the table. Self joins require
correlation names.
WHERE Clause
The WHERE clause is optional. When specified, it always follows the FROM clause. The
WHERE clause filters rows from the FROM clause tables. Omitting the WHERE clause
specifies that all rows are used.
The predicate evaluates to a SQL logical value -- true, false or unknown. The most basic
predicate is a comparison:
color = 'Red'
This predicate returns:
The = (equals) comparison operator compares two values for equality. Additional comparison
operators are:
For example,
SELECT * FROM sp WHERE qty >= 200
sno pno qty
S2 P1 200
S3 P1 1000
S3 P2 200
Note: In the sp table, the qty column for one of the rows contains null. The comparison - qty
>= 200, evaluates to unknown for this row. In the final result of a query, rows with a WHERE
clause evaluating to unknown (or false) are eliminated (filtered out).
Both operands of a comparison should be the same data type, however automatic conversions
are performed between numeric, datetime and interval types. The CAST expression provides
explicit type conversions;
What do we use SQL for? Well, we use it to select data from the tables located in adatabase.
Immediately, we see two keywords: we need to SELECT information FROM a table. We
have the most basic SQL structure:
To illustrate the above example, assume that we have the following table:
We shall use this table as an example throughout the tutorial. To select all the stores in this
table, we key in,
SELECT Store_name FROM Store_ Information
RESULT:
Store name
Los Angeles
San Diego
Los Angeles
Boston
2) SQL DISTINCT
The SELECT keyboard allows us to grab all information from a column (or columns)
On a table. This, of course, necessarily mean that there will be redundancies. What if we only
want to select each DISTINCT elements? This is easy to accomplish in SQL. All we need to
do is to add DISTINCT after SELECT.
we key in,
RESULT:
Store name
Los Angeles
San Diego
Los Angeles
Boston
Retrieving data from a tables
The SELECT statement can be used to retrieve data from any or all columns from a table.
Say that you have a table called product, which consists product name, description, price,
and etc. This is the statement to retrieve all the columns from the table product.
SELECT * FROM product
SELECT – specifies retrieval command plus what to select.
* - stands for everything.
How about if we want search product by specific chrematistics? That is when WHERE clause
becomes handy. Where clause is used when only a subset of all the rows in a table Is
required. Here is how you would list the name the price of the products has the pricevalueless
than $100.
CONCLUSION:
___________________________________________________________________________
___________________________________________________________________________
EXPERIMENT NO.4
TITLE : Consider the employee database. Where the primaries are underlined & write the
queries using following clauses & also retrieve the data from the given database.
Employee (employee_name, street, city)
Works (employee_name, company_name, salary)
Company (company_name, city)
THEORY:
GROUP BY clause:-
The GROUP BY clause will gather all the rows together that contains data in the
specified column(s) and will allow aggregate functions to be performed on the one or more
columns.
HAVING clause:-
The HAVING clause allows you to specify conditions on the rows for each group – in other
words, which rows should be selected is based on the conditions you specify. The having clause should
follow the GROUP BY clause if you are going to use it.
ORDER BY clause:-
ORDER BY clause is an optional clause which will allow you to display the results of your
query in a sorted order (either ascending order or descending order ) based on the columns that you
specify to order by.
[ ]=optional
PROCEDURE:
Creation of database:
mysql>create table employee(empname varchar(30) primary key, street varchar(30), city varchar(30));
1) Display the employee name, salary from database and display the result in ascending order
based on their salary.
mysql >select empname, salary from works order by salary;
EMPNAME SALARY
--------------------------------------
Smith 3000
brown 5000
jones 10000
calis 17000
peter 22000
ATNT 22000
Samsung 10000
3)Display employees with salary and companyname having salary in between 4000 and
20000.
mysql >SELECT EMPNAME, SALARY, COMPANYNAME
FROM WORKS
WHERE SALARY BETWEEN 4000 AND 20000;
4)Display employees with salary and company name having salary greater than 10000.
SELECT AVG(SALARY)
FROM WORKS
HAVING AVG(SALARY)>10000
GROUP BY COMPANYNAME;
mysql >DESCRIBE EMPLOYEE;
CONCLUSION:
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
EXPERIMENT NO: 5
Title: Consider the previous database & perform the different Join operations which are as
follows: I) Inner Join II) Outer Join III) Right Outer Join IV) Full Outer Join
PRIOR CONCEPTS:
Joins:
A join is a query that combines rows form two or more tables, views. or materialized views.
Oracle performs a join whenever multiple tables appear in the queries FROM clause. The
query's select list can select any columns for many of these tables. If any two of these tables
have a column name in common, then you must qualify all references o these columns
throughout the query with table names to avoid ambiguity.
Join Conditions:
Most join queries contain WHERE clause conditions that compare two columns, each from a
different table. Such a condition is called a join condition. To execute a join, Oracle combines
pair of rows, each containing one row from each table , for which the join condition evaluates
to TRUE . The columns in the join conditions need not also appear in the select list.
Outer Joins:
An outer join extends the result of simple join. An outer join returns all rows that satisfy the
join condition and also returns some or all of those rows from one table for which no rows
from the other satisfy the join condition.
NEW CONCEPTS:
Prototype:
To write a query that performs an outer join of tables A and B and returns all rows from A (a
left outer join), use the LEFT [OUTER] JOIN syntax in the FROM clause, or apply the outer
join operator (+) to all columns of B in the WHERE clause. For all rows in A that have no
matching rows in B, Oracle returns null for any select list expressions containing columns of
B.
To write a query that performs an outer join of tables A & B and returns all rows from B(a
right outer join), use the RIGHT [OUTER] JOIN syntax in the FROM clause , or apply the
outer join operator(+) to all columns of A in the join condition in the WHERE clause. For all
rows in B that have no matching rows in A, Oracle returns null for any select list expressions
containing columns of A.
To write a query that performs an outer join and returns all rows from A and B, extended with
nulls if they do not satisfy the join conditions (a full outer join), use the FULL [OUTER] JOIN
syntax in the FROM clause.
PROCEDURE:
2) Write a query using a LEFT OUTER join to display all information about employee.
mysql > SELECT
EMPLOYEE.EMPNAME, WORKS.COMPANYNAME, WORKS.SALARY
2 FROM EMPLOYEE, WORKS
3 WHERE EMPLOYEE.EMPNAME=WORKS.EMPNAME (+);
EMPNAME COMPANYNAME SALARY
------------------------- --------------------------------- -----------------
Jones Samsung 10000
Smith ACL 3000
Brown ATNT 5000
Peter ATNT 22000
Calis ATNT 17000
3)
Write a query using RIGHT OUTER JOIN display all information about employee.
mysql >SELECT
EMPLOYEE.EMPNAME, WORKS.COPMANYNAME, WORKS.SALARY
FROM EMPLOYEE, WORKS
WHERE EMPLOYEE.EMPNAME (+) = WORKS.EMPNAME;
EMPNAME COMPANYNAME SALARY
-----------------------------------------------------------------------------------------------------
Brown Krit
CONCLUSION:
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________
EXPERIMENT NO. :6
PRIOR CONCEPTS:
To study and understand the concept of different functions in Oracle and SQL plus
INTRODUCTION:
Aggregate functions are functions that take a collection of values as input return single value
SQL. Offers files that built in a aggregate function are used to compute against returned
column of numeric data from select statement. They basically summaries return of particular
column selected data.
Aggregate functions are as follows:
1. min 2.max 3.sum 4.count 5.avg
NEW CONCEPTS:
1. Min (column):- Return the smallest value in the given data column.
2. Max (column):- Return the largest value in the given data column.
3. Sum (column):-Return the given column the sum of numeric value in given column.
The input sum must be a collective of numbers.
4. Avg (column):- Return the avg_value in given column
PROCEDURE:
AVG:
AVG (<DISTINCT>|<ALL>] <n>)
Select avg(expr) from table-name, where condition;
MIN:
MIN (<DISTINCT>|<ALL>] <expr>)
Select min (expr) from table-name, where condition;
MAX:
MAX (<DISTINCT>|<ALL>] <expr>)
Select max (expr) from table-name, where condition;
SUM:
SUM (<DISTINCT>|<ALL>] <expr>)
Select sum (expr) from table-name, where condition;
MIN (SALARY)
100000
MAX (SALARY)
300000
Sum (SALARY)
600000
Count (SALARY)
1
Observation:-
mysql >update Works set salary = 10000
2 where empname =’PETER’;
0 rows updated
Empname managername
Calis lance
Peter mack
Smith sam
Jones hary
Brown krit
CONCLUSION :-
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
EXPERIMENT NO-7
TITLE: Write a query to perform the different Set operations which are as fallows:
I) Union II) Intersect III) Except IV) Minus
PRIOR CONCEPT:
Consider the above & perform the different set operation which is as follows
I) Union II) Intersect III ) Except IV) Minus
NEW CONCEPTS:
PROTOTYPE:-In oracle 8i (and above) the UNION command has been largely replaced by
the new.
Syntax:
SELECT Command {UNION | UNION ALL | INTERSECT | MINUS}
SELECT Command
Procedure:
Queries using different set operations
1> write a query to retrieve information about city of the employee using
UNION ALL
2>write a query to retrieve information about the city of the employee using UNION
(Display the cities of the employees same as that of company and companies specified in
work table)
mysql >Select Emplpyee.city
From Emplopyee,Company
where employee.city=Company.city
UNION
Select Company.city
From Company, Works
where Company.companyname=Works.companyname;
CITY:-
Columbia
Losangeles
Vegas
3>write a query to retrieve information about the city of the employee using INTERSECT
mysql >Select Employee.city
From Emoployee,Company
where Employee.city=Company.city
intersect
Select Company.city
From Company, Works
where Company.companyname=Works.companyname;
CITY:-
Losangeles
4>write a query to retrieve information about the city of the employee using MINUS
mysql >Select Employee.city
From Employee, Company
where Employee.city=Company.city
MINUS
Select Company.city
From Company, Works
OBSERVATION:
mysql >Describe z2
NAME NULL? TYPE
EMPNAME NOT NULL varchar 2(30)
COMPANYNAME Varchar 2(30)
SALARY Number (30)
SQL>Select*from z2
EMPNAME COMPANYNAME SALARY
Smith ACL 3000
Brown ATNT 5000
Jones SAMSUNG 10000
Peter ATNT 22000
Calis ATNT 17000
CONCLUSION :-
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________