Вы находитесь на странице: 1из 65

EXPERIMENT NO: 1

AIM: INTRODUCTION TO DBMS THEORY: DBMS


A DBMS (Database Management System) is a software program used to manage a

database. These programs enable users to access and modify database A DBMS is a complex set of software programs that controls the organization, storage, management, and retrieval of data in a database.
A DBMS includes four main components, which are: Modeling Language, Data

Structures, DB Query Language and Report Writer, and Transaction Mechanism. Each of these components can be further broken down into smaller and more specific pieces, but it is the sum of these parts which are combined to create the management system around the particular database to be utilized. A database management system, or DBMS, gives the user access to their data and helps them transform the data into information. Such database management systems include dBase, Paradox, IMS, and Oracle. These systems allow users to create, update, and extract information from their databases. Compared to a manual filing system, the biggest advantages to a computerized database system are speed, accuracy, and accessibility. A database is a structured collection of data. Data refers to the characteristics of people, things, and events. Oracle stores each data item in its own field.

THE MAIN FOUR COMPONENTS OF DBMS:


MODELING LANGUAGE
A modeling language to define the schema of each database hosted in the DBMS, according to the DBMS data model. The four most common types of organizations are the hierarchical, network, relational and object models.

DATA STRUCTURES

[DBMS]

Data structures (fields, records, files and objects) optimized to deal with very large amounts of data stored on a permanent data storage device (which implies relatively slow access compared to volatile main memory).

DB QUERY LANGUAGE AND REPORT WRITER


A database query language and report writer to allow users to interactively interrogate the database, analyze its data and update it according to the users privileges on data.

TRANSACTION MECHANISM
A transaction mechanism, that ideally would guarantee the ACID properties, in order to ensure data integrity, despite concurrent user accesses (concurrency control), and faults (fault tolerance).

DATA TYPES OF SQL


CHAR : This data type is used to store character strings values of fixed length. The size in

brackets determines the number of characters the cell can hold. The maximum number of characters (i.e. the size) this data type can hold is 255 characters. Syntax is CHAR(SIZE) Example is CHAR (20)

VARCHAR : This data type is used to store variable length alphanumeric data. The

maximum this data type can hold is 2000 characters. One difference between this data type and the CHAR data type is ORACLE compares VARCHAR values using non-padded comparison semantics i.e. the inserted values will not be padded with spaces. Syntax is VARCHAR(SIZE) Example is VARCHAR (20) OR VARCHAR2 (20)

NUMBER : The NUMBER data type is used to store numbers (fixed or floating point).

Numbers of virtually any magnitude maybe stored up to 38 digits of precision. Numbers as large as 9.99 * 10 to the power of 124, i.e. followed by 125 zeros can be stored. The precision, (P), determines the maximum length of the data, whereas the scale, (S), determines the number of places to the right of the decimal. If scale is omitted then the default is zero. If precision is omitted values are stored with their original precision up to the maximum of 38 digits. Syntax is NUMBER (P, S) Example is NUMBER (10, 2)

[DBMS]

LONG :

This data type is used to store variable length character strings containing up to 2GB. LONG data can be used to store arrays of binary data in ASCII format. LONG values cannot be indexed, and the normal character functions such as SUBSTR cannot be applied to LONG values. Syntax is LONG (SIZE) Example is LONG (20)

DATE : This data type is used to represent data and time. The standard format id DD-

MM-YY as in 13-JUL-85. To enter dates other than the standard format, use the appropriate functions. Date Time stores date in the 24-hour format. By default, the time in a date field is 12:00:00 am, if no time portion is specified. The default date for a date field is the first day of the current month. Syntax is DATE

LONG RAW : LONG RAW data types are used to store binary data, such as Digitized picture or image. Data loaded into columns of these data types are stored without any further conversion. LONG RAW data type can contain up to 2GB. Values stored in columns having LONG RAW data type cannot be indexed. Syntax is LONGRAW (SIZE)

RAW : It is used to hold strings of byte oriented data. Data type can have a maximum

length of 255 bytes. Syntax is RAW(SIZE)

[DBMS]

EXPERIMENT NO: 2
AIM:TO STUDY THE DDL(DATA DEFINATION COMMANDS ). THEORY:
DDL DATA DEFINATION LANGUAGE
The SQL sentences that are used to create these objects are called DDLs or Data Definition Language. The sql provides various commands for defining relation schemas, deleting relations, creating indexes and modify relation schemas. DDL is part of sql which helps a user in defining the data structures into the database. Following are the various DDL commands are

Create table Alter table drop table Rename table

CREATE TABLE: A table is basic unit of storage. It is composed of rows and columns. To
create a table we will name the table and the columns of the table. We follow the rules to name tables and columns: It must begin with a letter and can be up to 30 characters long. It must not be duplicate and not any reserved word.

Syntax to create a table is CREATE TABLE tablename (column_name1 datatype (size), column_name2 datatype (size) ); Example is [DBMS]

CREATE TABLE student (rollno number (4), name varchar2 (15));

ALTER TABLE : After creating a table one may have need to change the table either by add
new columns or by modify existing columns. One can do so by using alter table command. Syntax to add a column is ALTER TABLE tablename ADD(col1 datatype,col2 datatype); Syntax to modify a column is ALTER TABLE tablename MODIFY(col1 datatype,col2 datatype);

[DBMS]

DROP TABLE : To remove the definition of oracle table, the drop table statement is used.
Syntax to drop table is DROP TABLE tablename

RENAME : One can change the name of a table by rename command Syntax to rename table is
RENAME oldname TO newname

[DBMS]

EXPERIMENT NO: 3
AIM: TO STUDY THE DML (DATA MANIPULATION COMMANDS). THEORY:
DML DATA MANIPULATION LANGUAGE
The SQL sentences used to manipulate data within these objects are called DMLs or Data Manipulation Language. It is language that enables users to access or manipulate data as organized by appropriate data model. By data manipulation we have Retrieval of information stored in database. Insertion of new information into database. Deletion of information from database. Modification of data stored in database.

TWO TYPES OF DML ARE:

Procedural DML Non-procedural DML (Declarative)


[DBMS]

FOLLOWING ARE DML COMMANDS ARE:

Select Update Delete Insert

USE OF SELECT STATEMENT: SELECTING ALL COLUMNS OF THE TABLE:


A SELECT statement is used as a DATA RETRIVAL statement i.e. It retrieves information from the database.
SYNTAX: SQL> SELECT * FROM TABLE NAME;
SELECT identifies WHAT COLUMNS. FROM identifies WHICH TABLE.

[DBMS]

Simply, SELECT clause specify which column is to be displayed & FROM


clause specify the table containing the columns listed in the SELECT clause.

Here, * is used to select all columns.

SELECTING SPECIFIC COLUMNS OF THE TABLE:


SYNTAX: SQL> SELECT ENAME,JOB FROM EMP;

[DBMS]

We can use SELECT statement to display specific columns of the table by specifying the column names separated by commas. As shown above In SELECT clause We specify the column names, in the order in which we want them to appear as output.

USE OF INSERT STATEMENT:


SYNTAX: SQL> INSERT into CSE(student,rollno) VALUES ('MONIKA',651);
[DBMS]

INSERT statement is used to ADD NEW ROW TO A TABLE. Using INSERT We can only insert on row at a time. As shown in above example, In above example CSE is the name of the TABLE & STUDENT, ROLLNO are its two ATTRIBUTES. Enclose CHARACTER & DATE values within a SINGLE QUOTATION MARKS.

[DBMS]

CREATING A SCRIPT
i.e USE of & substitution in a SQL statement to Prompt For values. & is a PLACEHOLDER for the VARIABLE VALUE.

USE OF DELETE STATEMENT:


SYNTAX: SQL> DELETE from CSE where rollno BETWEEN 605 AND 630; i.e. DELETE FROM table [WHERE condition];

[DBMS]

If we OMIT WHERE CLAUSE then ALL ROWS OF THE COLUMN ARE DELETED. We can confirm the delete operation by displaying the deleted rows using SELECT statement as shown above

USE OF UPDATE STATEMENT:


SYNTAX: SQL> UPDATE cse SET rollno=21 WHERE student='ITIKA'; Here, If we do not use WHERE clause then ALL ROWS OF THE TABLE ARE UPDATED.
[DBMS]

SPCIFIED ROW or ROWS are modified if we specify the WHERE clause.

EXPERIMENT NO: 4
AIM: TO STUDY THE USE OF NOT NULL CONSTRAINT THEORY: CONSTRAINTS:
CONSTRAINTS enforces RULES at the table level
[DBMS]

Constraints prevent the deletion of the table if there is DEPENDENCIES from the other table. Basically, Constraints are used to PREVENT INVALID DATA ENTRIES INTO THE TABLES.

HOW TO DEFINE A CONSTRAINT: SYNTAX: SQL> CREATE TABLE [table name] (column datatype [Default expr] [column_constraint], .. [table_constraint][,.]);

THE NOT NULL CONSTRAINT This constraint ensures that the column contains no null values. As column without the NOT NULL constraint can contain NULL values BY DEFAULT.

Here, On table STUDENT NOT NULL constraint is added to the LAST_NAME i.e. now LAST_NAME cant be NULL If we try to add NULL value in this column then following message will be displayed as ERROR.

[DBMS]

Whereas we can add NULL value to the NAME column as there exist no NOT NULL constraint.

VIEWING CONSTRAINTS
The QUREY USER_CONSTRAINTS on table is used to view all the constraint definition & names.
[DBMS]

C stands for CHECK. (NOT NULL constraint is a CHECK constraint) P stands for PRIMARY KEY, R stands for REFRENTIAL INTEGRITY, U stands for UNIQUE

We can view NOT NULL constraint using DESCRIBE command also.

VIEWING the columns associated with constraints.

[DBMS]

We can view the columns associated with the constraint names in the USER_CONS_COLUMNS view. In above example: CONSTRAINT_NAME: SYS_C00590 COLUMN_NAME: LAST_NAME

[DBMS]

EXPERIMENT NO: 5
AIM: TO STUDY THE USE OF PRIMARY KEY CONSTRAINT THEORY: PRIMARY KEY:
A PRIMARY KEY constraint creates a primary key for a table. Only one primary key can be created for each table. A PRIMARY KEY constraint is a column or a set of columns that are uniquely identifies each row in the table. This constraint enforces UNIQUENESS of the column or column combination and ensures that no column that is part of the primary key can contain a NULL value. NULL VALUES are NOT ALLOWED & ALREADY existing values are not replaced.

In this Department_id is a PRIMARY KEY. i.e. It do not contain DUPLICATE entries & NO NULL values. If we try to add DUPICATE values than following ERROR is encountered.

[DBMS]

If we try to add NULL value than following ERROR is encountered.

PRIMARY KEY constraint can be defined at the COLUMN LEVEL or TABLE LEVEL. A composite PRIMARY KEY is created by using the TABLE-LEVEL definition. A table can have only one PRIMARY KEY constraint but can have several UNIQUE constraints. We can call them ALTERNATE KEYS.

EXPERIMENT NO: 6
AIM: TO STUDY THE USE OF FOREIGN KEY CONSTRAINT
[DBMS]

THEORY: FOREIGN KEY:


The FOREIGN KEY, or refrential integrity constraint, designates a column or combination of columns as a foreign key and establishes a relationship between a primary key or a unique key in the same table or a different table. A FOREIGN KEY value must match an existing value in the parent table or be NULL FOREIGN KEYS are based on data values and are purely logical, not physical, pointers.

SYNTAX: SQL> CREATE TABLE table2 ( department_id NUMBER(4) CONSTRAINT emp_deptid_fk REFERENCES table1(department_id), )

The FOREIGN KEY is defined in the child table, and the table containing the referenced column is the Parent table. The FOREIGN KEY is defined using the combination of the following keywords: FOREIGN KEY: is used to define the column in the child table at the table constraint level.
[DBMS]

REFERENCES: identifies the table and column in the parent table. ON DELETE CASCADE: indicates that when the row in the parent table is deleted, the dependent rows in the chile table will also be deleted. ON DELETE SET NULL: converts foreign key values to the NULL when parent value is REMOVED. Here, 5 NOT ALLOWED value as 5 doesnt EXIT

EXPERIMENT NO: 7
AIM: TO STUDY THE USE OF ORDER BY CLAUSE SORTING IN DESENDING ORDER SORTING IBY COLUMN ALIAS
[DBMS]

THEORY: THE ORDER BY CLAUSE:


This ORDER BY clause is used to SORT the rows. The order of rows returned in the query result is UNDEFINED. Generally ORDER BY clause is the LAST clause of the SQL statement. In this we can specify an expression, or an alias, or column position as a sort condition.

DEFAULT ORDER OF THE DATA:


The default order of the sorting is ASCENDING. i.e. NEUMERIC values are displayed with the Lowest values first. NULL values appear at the END. CHARACTERS are displayed in ALPHABETICAL ORDER. DATE values displayed with the earliest value first.

ORDER BY CLAUSE:
Sorting rows with ORDER BY clause use two keywords: ASC: Used for ascending order, by default DESC: Used for descending order.

EXAMPLE: SELECTING DEFAULT ORDER SYNTAX: SQL> SELECT ename, job, sal, hiredate from EMP ORDER BY sal;
[DBMS]

SORTING IN DESENDING ORDER: SYNTAX: SQL> SELECT student, rollno FROM CSE ORDER BY student DESC;

[DBMS]

SORTING BY COLUMN ALIAS:


SYNTAX: SQL> SELECT ename, hiredate, job, sal*12 AnnualSal FROM emp ORDER BY AnnualSal;

[DBMS]

SORTING BY MULTIPLE COLUMNS: SQL> SELECT ename,job,sal, hiredate FROM emp ORDER BY ename, sal DESC;

We can sort by a column that s NOT in the SELECT item.

EXPERIMENT NO: 8
[DBMS]

AIM: TO STUDY THE USE OF CASE MANIPULATION FUNCTIONS. THEORY: CASE MANIPULATION FUNCTION:
There are three case conversion functions: LOWER, UPPER, and INITCAP LOWER: It is used to convert MIXED CASE or UPPERCASE character string to LOWERCASE. UPPER: It is used to convert MIXED CASE or LOWERCASE character string to UPPERCASE. INITCAP: It converts the FIRST letter of each word to UPPERCASE and remaining letters to LOWERCASE. EXAMPLE: USE OF UPPER FUNCTION: SYNTAX: SQL> SELECT UPPER(ename)|| is a || job AS EMPLOYEE DETAILS FROM emp;

[DBMS]

USE OF LOWER FUNCTION: SYNTAX: SQL> SELECT LOWER(ename) NAME, UPPER(job) JOB FROM emp WHERE sal>2000;

[DBMS]

USE OF INITCAP FUNCTION: SQL> SELECT INITCAP(ename) NAME, UPPER(job) JOB FROM emp WHERE sal>2000;

EXPERIMENT NO: 9
[DBMS]

AIM: TO STUDY THE USE OF CHARACTER MANIPULATION FUNCTIONS. THEORY: CHARACTER MANIPULATION FUNCTIONS
The following are the CHARACTER MANIPULATION FUNCTIONS which are used to manipulate the character string: CONCAT: This function is used to join two columns together (We are limited to use two parameters with CONCAT). SUBSTR: This function will Extracts a string of determined length. LENGTH: This function shows the length of the string as a NEUMERIC VALUE. INSTR: This function Find the NEUMERIC POSITION F THE NAMED STRING. LPAD: This function Pads the character value RIGHT-JUSTIFIED. RPAD: This function Pads the character value LEFT-JUSTIFIED.

USE OF CONCAT FUNCTION: SYNTAX: SQL> SELECT CONCAT(ename,sal) NAME FROM emp;

[DBMS]

USE OF LENGTH FUNCTION: SYNTAX: SQL> SELECT LENGTH(ename), sal FROM emp;

[DBMS]

USE OF LPAD FUNCTION: SYNTAX: SQL> SELECT LPAD(sal,8,*) FROM EMP;

[DBMS]

USE OF RPAD FUNCTION: SYNTAX: SQL> SELECT RPAD(sal,8,$) FROM emp WHERE sal>3000;

USE OF SUBSTRING FUNCTION: SYNTAX: SQL> SELECT sal,job FROM emp WHERE SUBSTR(ename,-1,1)=n;
[DBMS]

USE OF INSTRING FUNCTION: SYNTAX: SQL> SELECT ename,INSTR(ename,A), job FROM emp WHERE sal>2000;

EXPERIMENT NO: 10
AIM: TO STUDY THE USE OF VARIOUS NUMBER FUNCTIONS. THEORY:
[DBMS]

NUMBER FUCTIONS:
Number Functions are those which accept NUMERIC input and return NUMERIC value. Some important Number Functions are explained below: ROUND: Rounds value, column or expression to specific decimal places. ROUND (expression| value, n): if n is omitted, no decimal places. If n is negative, numbers to left of the decimal point are rounded. TRUNC (expression| value, n): It truncates the column, expression, or value to the n decimal places, or, if n is omitted, then n is by default ZERO. MOD (m, n): It Returns the remainder of m divided by n.

USE OF ROUND FUNCTION: SYNTAX:


SQL> SELECT ROUND(45.923, 2), ROUND(45.923,0), ROUND(45.923,-1) FROM DUAL;

USE OF TRUNC FUNCTION: SYNTAX:


[DBMS]

SQL> SELECT TRUNC(45.923, 2), TRUNC(45.923,0), TRUNC(45.923,-1) FROM DUAL;

USE OF MOD FUNCTION: SYNTAX:


SQL> SELECT MOD(300,10), MOD(23,9) FROM DUAL;

SQL> SELECT ename, MOD(sal,200) FROM emp;

[DBMS]

EXPERIMENT NO: 11
AIM: TO STUDY THE USE OF VARIOUS DATE FUNCTIONS. THEORY: DATE FUNCTIONS: Single-row date functions operate on DATE DATA TYPE. These are called Single-row date functions because they are operated on single row. ADD_MONTHS FUNCTION:
[DBMS]

SYNTAX: ADD_MONTHS(<d>,<i>) Here d is the date and i is any integer value. This Function returns the date d Plus i months. If i is a decimal value then database will implicitly covert it to an integer by truncating the decimal portion. EXAMPLE: SQL> SELECT SYSDATE, ADD_MONTHS(SYSDATE,4) ADDITION, ADD_MONTHS(SYSDATE,-4) SUBTRACTION FROM DUAL;

[DBMS]

LAST_DAY FUNCTION: SYNTAX: LAST_DAY(<d>) This function will return the last day of the month for the date d. EXAMPLE: SQL> SELECT SYSDATE, LAST_DAY(SYSDATE) from DUAL;

MONTHS_BETWEEN FUNCTION: SYNTAX:


[DBMS]

MONTHS_BETWEEN(<d1>,<d2>) Here d1,d2 both are dates. This function will return the number of months that d2 is later than d1. A whole number is returned, if d1 d2 are same day of the month. Also if both dates are the last day of a month. EXAMPLE: SQL> SELECT MONTHS_BETWEEN(9-SEP-1988,9-NOV-1988) FROM DUAL;

NEXT_DAY FUNCTION:
[DBMS]

SYNTAX: NEXT_DAY(<d>,<day>) Here d is the date and day is the text string containing the full or abbreviated day of the week in the sessions language. This function return the next day following d. The time portion of the return date is the same as the time period of d. EXAMPLE: SQL> SELECT NEXT_DAY(01-SEP-95,FRIDAY) FROM DUAL;

ROUND FUNCTION: SYNTAX: ROUND(<d>,<fmt>)


[DBMS]

Here d gives the date and fmt is a character string containing a date-format string. EXAMPLE: SQL> SELECT SYSDATE, ROUND(SYSDATE,MONTH) FROM DUAL;

TRUNC FUNCTION: SYNTAX: TRUNC(<d>,<fmt>)

[DBMS]

Here d gives the date and fmt is a character string containing a date-format string. This will return starting date of the month. EXAMPLE: SQL> SELECT SYSDATE, TRUNC(SYSDATE,MONTH) FROM DUAL;

[DBMS]

EXPERIMENT NO: 12
AIM:TO STUDY THE DCL(DATA CONTROL COMMANDS ). THEORY:

GRANTING AND REVOKING PERMISSIONS

GRANT : The Grant statement provides various types of access to

database objects such as tables, views and sequences. A privilege is consent to execute an action or to access another user object. These consents can be given by grant statement.

SYNTAX :
GRANT {object privileges} TO username ON object name

[WITH GRANT OPTION];

The WITH GRANT OPTION allows the grantee to in turn grant object privileges to other users.

OBJECT PRIVILEGES
Each object privileges that is granted authorizes the grantee to Perform some operation on the object. The user can grant all the privileges or grant only specific object privileges.

ALTER:

allows the grantee to change the table definition with the. ALTER TABLE command.

[DBMS]

DELETE:

allows the grantee to remove the records from the table with the DELETE command.

INDEX:

allows the grantee to create an index on the table with the CREATE INDEX command.

INSERT: allows the grantee to add records to the table with the INSERT
Command.

SELECT: allows the grantee to query the table with the SELECT
Command.

UPDATE: allows the grantee to modify the records in the tables with the
UPDATE command.

EXAMPLE TO GRANT SELECT & INSERT PRIVILEGES ON TABLE ITEM TO USER NAMED AS Nikita.
SQL>GRANT SELECT, INSERT ON item TO Nikita;

EXAMPLE TO GRANT ALL PRIVILEGES ON TABLE EMP TO USER NAMED AS Neha.


SQL>GRANT ALL ON EMP TO Neha.;

[DBMS]

EXAMPLE TO GRANT ALL PRIVILEGES ON TABLE DEPT TO USER NAMED AS Pearl.


SQL>GRANT ALL ON DEPT TO Pearl.;

REVOKE

: Privileges once given can be denied to a user using the

REVOKE command. The object owner can revoke privileges granted to another user. A user of an object who is not the owner, but has been granted the GRANT privilege, has the power to REVOKE the privileges from a grantee. The REVOKE statement is used to deny the grant given on an object.

SYNTAX:
REVOKE {object privileges} ON object name FROM username;

EXAMPLE TO REVOKE SELECT & INSERT PRIVILEGES ON TABLE ITEM TO USER NAMED AS Sajan.
SQL>REVOKE SELECT, INSERT ON item TO Sajan;

EXAMPLE TO REVOKE ALL PRIVILEGES ON TABLE EMP TO USER NAMED AS Arav.


SQL>REVOKE ALL ON EMP TO Arav;

EXPERIMENT NO: 13
[DBMS]

AIM:TO STUDY THE PATTERN MATCHING OPERATORS i.e LIKE

THEORY: PATTERN MATCHING


SQL includes string matching operator LIKE for comparison on character string using patterns. The LIKE predicate allows for a comparison of one string with another string value, which is not identical. This is achieved by using wildcard characters. The patterns are case sensitive and that is uppercase letters do not match lower case characters. The keyword NOT LIKE is used to select those rows that do not match the specified pattern of characters. Two WILDCARD CHARACTERS that are available are: FOR CHARACTER DATA TYPES: The percent sign (%) matches any string The Underscore (_) matches any single character EXAMPLE IS: SQL>SELECT * FROM EMP WHERE ename LIKE _O%;

[DBMS]

SQL>SELECT * FROM EMP WHERE ename LIKE JON__;

EXPERIMENT NO: 14
AIM:TO CREATE JOINS WITH USING CLAUSE AND RETREVING RECORDS. THEORY: JOINS
A JOIN can be recognized in sql select statement if its has more than one table after from keyword. This join condition is based on primary keys and foreign keys. There must be n-1 join conditions for n joins to tables. If join condition is omitted then the result is Cartesian product.
[DBMS]

SYNTAX IS
SQL>SELECT list of columns FROM table1, table2 WHERE condition;

TYPES OF JOINS ARE

EQUI JOIN : It returns all rows from tables where there is a match.
Tables are joined on columns that have the same datatype & size in table. It is also known as equality join or simple join or inner join. Syntax is SELECTfield1,field2 FROM table1,table2 WHERE table1.field=table2.field; EXAMPLE IS: SQL>SELECT ename, emp.deptno=dept.deptno; dname FROM emp, dept WHERE

[DBMS]

CARTESION JOIN : When the join condition is omitted the result is


Cartesian join of two or more tables in which all the combinations of rows will be displayed. All the rows are joined to all rows of the second table. SYNTAX IS SQL>SELECT field1, field2 FROM table1, table2; EXAMPLE IS SQL>SELECT ename, dname FROM emp, dept;

[DBMS]

OUTER JOIN : While using equi join we see that if there exists certain rows in one table which dont have corresponding values in the second table thn those rows will not be selected. We can forcefully select those rows by outer join. The rows for those columns will have NULL values. Syntax is

[DBMS]

SELECT table1.col, table2.col FROM table1, table2 WHERE table1.col (+) = table2.col; EXAMPLE IS SQL>SELECT empno, ename, emp.deptno, dname FROM emp, dept WHERE emp.deptno (+) = dept.deptno;

SELF JOIN : The self join can be seen as join of two copies of the same table. The table is not actually copied but sql performs the command as though it were. EXAMPLE IS SQL>SELECT e.ename, m.ename FROM emp e, emp m WHERE e.mgr=e.empno;

[DBMS]

EXPERIMENT NO: 15
AIM: TO STUDY VARIOUS SET OPERATORS. THEORY:
[DBMS]

SET OPERATORS
Set keywords are used to combine information of similar type from one or more than one table. Set operations and the operators are based on set theory. It consumes two or more queries into one result. The types of set operators are:

UNION : The union clause merges the outputs of multiple queries

into a single set of rows and columns. It combines rows returned by two select statements by eliminating duplicate rows. SYNTAX IS SQL>SELECT <statement> UNION SELECT <statements>; EXAMPLE IS SQL>SELECT designation FROM emp_info WHERE deptt=comp UNION SELECT designation FROM emp_info WHERE deptt=eco;

INTERSECT : The intersect operator combines two select

statements and return only those rows that are returned by both queries. SYNTAX IS SQL>SELECT <statement> INTERSECT SELECT <statements>; EXAMPLE IS SQL>SELECT designation FROM emp_info WHERE deptt=comp INTERSECT SELECT designation FROM emp_info WHERE deptt=eco;

MINUS : It combines the result of two queries and returns only

those values that are selected by first query but not in second query. SYNTAX IS
[DBMS]

SQL>SELECT <statement> MINUS SELECT <statements>; EXAMPLE IS SQL>SELECT desgination FROM emp_info WHERE deptt=comp MINUS SELECT desgination FROM emp_info WHERE deptt=eco;

EXPERIMENT NO: 16
AIM: TO MAKE USE OF SUBQUERIES. THEORY: SUBQUERIES
[DBMS]

A sub query is a form of an SQL statement that appears inside another SQL statement. It is also termed as nested query. The statement containing a sub query is called a parent statement. The parent statement uses the rows returned by the sub query. It can be used by the following commands:

To insert records in a target table. To create tables and insert records in the table created. To update records in a target table. To create views. To provide values for conditions in WHERE, HAVING, IN etc. used with SELECT, UPDATE, and DELETE statements.

TYPES OF SUB QUERIES ARE

SINGLE ROW : It returns one row from inner nested query.

EXAMPLE IS: SQL>SELECT deptno FROM emp WHERE ename =MILLER;

MULTIPLE ROW : Subqueries that return more than one row called multiple row queries. Operators like IN,ALL,ANY are used.

EXAMPLE IS: SQL>SELECT ename,sal,deptno FROM emp WHERE sal IN


[DBMS]

(SELECT min(sal) FROM emp GROUP BY deptno);

MULTIPLE COLUMN : It returns more than one column .

EXAMPLE IS: SQL>SELECT ordid , prodid , qty FROM item WHERE (qty,prodid) IN (SELECT prodid,qty FROM item WHERE ordid = 605 AND ordid <> 605);

EXPERIMENT NO: 17
AIM: TO MAKE USE OF HAVING CLAUSE. THEORY:

HAVING CLAUSE :

[DBMS]

The having clause filters the group values created by group by clause. This clause can precede the group by clause but it is more logical if we place group by first. SYNTAX IS: SQL>SELECT col1,col2 FROM tablename GROUP BY <col> HAVING <condition>; EXAMPLE IS: SQL>SELECT dept,max(salary) FROM emp_info GROUP BY dept HAVING max(salary) > 12000;

EXPERIMENT NO: 18
AIM: TO MAKE USE OF DISTINCT CLAUSE. THEORY:

DISTINCT CLAUSE : The distinct keyword duplicates all rows from


results of a select statement.

[DBMS]

SYNTAX TO REMOVE DISTINCT ROWS IS: SQL>SELECT DISTINCT <col1>,<cols2> FROM tablename; EXAMPLE IS: SQL>SELECT DISTINCT job FROM emp;

EXPERIMENT NO: 19
AIM: TO STUDY HOW TO CREATE A VIEW. THEORY: VIEWS :
A view is very commonly used database object that is derived at runtime.
[DBMS]

A view contains data of its own. Its contents are derived from another table. The command for creating view is CREATE VIEW command. Editing in the tables are automatically reflected in the views. It is virtual table & does not have any data of its own. SYNTAX TO CREATE A VIEW IS: SQL>CREATE [OR REPLACE] VIEW view name AS sub query [WITH CHECK OPTION] [WITH READ ONLY]; EXAMPLE IS: SQL>CREATE VIEW monika AS SELECT empno, ename, sal, comm FROM emp;

TYPES OF VIEWS ARE AS FOLLOWS:

JOIN VIEW:- It is defined as view that has more than one table specified in from clause and does not contain following clauses i.e. distinct, aggregation, group by. This type of view allows update, insert and delete command to change data in table. SYNTAX IS: SQL>CREATE OR REPLACE VIEW monika AS SELECT ename, empno, sal FROM

[DBMS]

emp, dept WHERE emp.deptno = dept.deptno;

The views to be updateable must not include the following are Set operators , aggregate functions Distinct operator , rownum pseudo columns Group by clause , having clause

INLINE VIEW: - Oracle also offers an inline view that is very handy and inline view is part of SQL statements. It allows you in body of SQL statement to define SQL for view that SQL statement will use to resolve its query. MATERIALIZED VIEW: - Snapshot also called materialized view. It is defined as copy of part of table or entire table. It reflects the current status of table that is being copied. The original status table is also called master table. Two types are Read only and update. Read-only does not allow changes to be made in view. It simply publishes and subscribes the replications. It allows changes in local copy which periodically updates master table.

[DBMS]

EXPERIMENT NO: 20
AIM: TO STUDY MULTIPLE ROW FUNCTIONS. THEORY: GROUP FUNCTIONS

COUNT : This function returns the number of rows or non-null values for column x. SYNTAX IS : COUNT([DISTINCT|ALL]COLUMN NAME) EXAMPLE IS:

[DBMS]

SQL>SELECT COUNT(EMPNO)FROM EMP;

SUM : This function ireturns the sum of values for the column x. This function is applied on columns having numeric datatype and it returns the numeric value. SYNTAX IS: SUM([DISTINCT|ALL]COLUMN NAME) EXAMPLE IS : SQL>SELECT SUM(SAL) FROM EMP;

AVG : Ths function returns the average of values for the column x. It ignores the null values in the column x.

[DBMS]

SYNTAX IS : AVG([DISTINCT|ALL]COLUMN NAME) EXAMPLE IS: SQL>SELECT AVG(SAL),COUNT(SAL) FROM EMP;

MIN : This function returns the minimum of values for the column x for all the rows. SYNTAX IS: MIN([DISTINCT|ALL]COLUMN NAME) EXAMPLE IS: SQL>SELECT MIN(SAL) FROM EMP;

MAX : This function returns the maximum of values for the column x for all the rows.

[DBMS]

SYNTAX IS : MAX([DISTINCT|ALL]COLUMN NAME) EXAMPLE IS: SQL>SELECT MIN(SAL),MAX(SAL) FROM EMP;

[DBMS]

Вам также может понравиться