Sunteți pe pagina 1din 18

DBMS PRACTICAL FILE

SUBMITTED TO: Mr. Shamandeep Singh

SUBMITTED BY: Bharti 10901124(C4) 5th SEMESTER

INDEX Sr. No.


1.

Experiments
Introduction to DBMS. Introduction to ORACLE. To study different constraints. Explanation of DDL, DML and DCL.

Remark

2.

3.

4.

5.

To use oracle functions viz aggregate, numeric, converison, string functions.

6.

To make views of a table.

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.

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 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).

Advantages of database systems:

Database advantages include the following: shared data; centralized control; disadvantages of redundancy control; improved data integrity; improved data security, and database systems; and, flexible conceptual design.

Disadvantages of database system Disadvantages are as follows: a complex conceptual design process; the need for multiple external databases; the need to hire database-related employees; high DBMS acquisition costs; a more complex programmer environment; potentially catastrophic program failures; a longer running time for individual applications; and, highly dependent DBMS operations.

DATA TYPES: 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) 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-MMYY 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)

EXPERIMENT NO: 2 AIM: INTRODUCTION TO ORACLE THEORY: ORACLE


Oracle is one of the powerful RDBMS product that provide efficient solutions for database applications. Oracle is the product of Oracle Corporation which was founded by LAWRENCE ELLISION in 1977. The first commercial product of oracle was delivered in 1970. The first version of oracle 2.0 was written in assembly language. Nowadays commonly used versions of oracle are ORACLE 8, 8i & 9i Oracle 8 and onwards provide tremendous increase in performance, features and functionality.

FEATURES OF ORACLE:

Client/Server Architecture Large database and Space Management Concurrent Processing High transaction processing performance High Availability Many concurrent database users Controlled availability Openness industry standards Manageable security Database enforced integrity Distributed systems Portability Compatibility

ORACLE SERVER TOOL :


Oracle is a company that produces most widely used server based multi-user RDBMS. Oracle server is a program installed on server hard-disk drive. This program must be loaded in RAM to that it can process the user requests. Oracle server takes care of following functions. Oracle server tools are also called as back end. Functions of server tool:

Updates the data Retrieves the data sharing Manages the data sharing Accepts the query statements PL/SQL and SQL Enforce the transaction consistency

EXPERIMENT NO: 3 AIM: TO STUDY DIFFERENT CONSTRAINTS THEORY: CONSTRAINTS Defination:


Constraints enforce rules at the table level. they 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][,.]); NOT

NULL: 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. Whereas we can add NULL value to the NAME column as there exist no NOT NULL constraint.

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. 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.

FOREIGN KEY:
The FOREIGN KEY, or referential 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. 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

CHECK CONSTRAINT
The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a single column it allows only certain values for this column.

If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
SQL CHECK CONSTRAINT ON CREATE TABLE

The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons" table is created. The CHECK constraint specifies that the column "P_Id" must only include integers greater than 0. CREATE TABLE Persons ( P_Id int NOT NULL , LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CHECK (P_Id>0) )
SQL CHECK CONSTRAINT ON ALTER TABLE

To create a CHECK constraint on the "P_Id" column when the table is already created, use the following SQL: MySQL / SQL Server / Oracle / MS Access: ALTER TABLE Persons ADD CHECK (P_Id>0) To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
TO DROP A CHECK CONSTRAINT

To drop a CHECK constraint, use the following SQL: SQL Server / Oracle / MS Access: ALTER TABLE Persons DROP CONSTRAINT chk_Person

SQL UNIQUE CONSTRAINT


The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it. Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.
SQL UNIQUE CONSTRAINT ON CREATE TABLE

The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created:

CREATE TABLE Persons (P_Id int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255)) To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax: CREATE TABLE Persons (P_Id intNOTNULL,LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT uc_Person ID UNIQUE (P_Id,LastName))
SQL UNIQUE CONSTRAINT ON ALTER TABLE

To create a UNIQUE constraint on the "P_Id" column when the table is already created, use the following SQL: ALTER TABLE Persons ADD UNIQUE (P_Id) To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
TO DROP A UNIQUE CONSTRAINT

To drop a UNIQUE constraint, use the following SQL: ALTER TABLE Persons DROP CONSTRAINT uc_PersonID

EXAMPLE:
The Persons table:
P_Id 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger

The Orders table:


O_Id 1 2 3 OrderNo 77895 44678 22456 P_Id 3 3

"P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons" table. The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table. The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. The FOREIGN KEY constraint also prevents that invalid data form being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.

EXPERIMENT NO: 4 AIM: EXPLANATION OF DDL, DML AND DCL

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:

Alter table & Create table & drop table Create index & drop index Create view & drop view

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 )

FOLLOWING ARE DML COMMANDS ARE:


Select Update Delete Insert

DCL DATA CONTROL LANGUAGE


The SQL sentences, which are used to control the behavior of these objects, are called DCLs or Data Control Language. It is language used to control data and access to the database. Following are some DCL commands are

Commit Rollback Save point Set transaction

A Data Control Language (DCL) is a computer language and a subset of SQL, used to control access to data in a database. Examples of DCL commands include:

GRANT to allow specified users to perform specified tasks. REVOKE to cancel previously granted or denied permissions.

The following privileges can be GRANTED TO or REVOKED FROM a user or role:


CONNECT SELECT INSERT UPDATE DELETE EXECUTE USAGE

EXPERIMENT NO: 5 AIM: TO USE ORACLE FUNCTIONS VIZ AGGREGATE, NUMERIC ,CONVERISON, STRING FUNCTIONS. THEORY: Aggregate Functions COUNT : This function returns the number of rows or non-null values for column x.
SYNTAX: COUNT([DISTINCT|ALL]COLUMN NAME) EXAMPLE:

The following "Orders" table:


O_Id 1 2 3 OrderDate 2008/11/12 2008/10/23 2008/09/02 OrderPrice 1000 1600 Customer Hansen Nilsen

700

Hansen

Now we want to count the number of orders from "Customer Nilsen".We use the following SQL statement:

SELECT COUNT(Customer) Customer='Nilsen'

AS

CustomerNilsen

FROM

Orders

WHERE

The result of the SQL statement above will be 1, because the customer Nilsen has made 1 order in total: CustomerNilsen 1

SUM : This
SYNTAX

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.

SUM([DISTINCT|ALL]COLUMN NAME)

EXAMPLE:
The following "Orders" table: O_Id 1 2 3 4 OrderDATE 2008/11/12 2008/10/23 2008/09/02 2008/09/03 OrderPrice 1000 1600 700 300 Customer Hansen Nilsen Hansen Hansen

5 6

2008/08/30 2008/10/04

2000 100

Jensen Nilsen

Now we want to find the sum of all "OrderPrice" fields". We use the following SQL statement:

Now to find the sum of all "OrderPrice" fields", we use the following SQL statement:

SELECT SUM(OrderPrice) AS OrderTotal FROM Orders The result will be:


OrderTotal 5700

AVG : Ths function returns the average of values for the column x. It ignores the null
values in the column x. SYNTAX: AVG([DISTINCT|ALL]COLUMN NAME) EXAMPLE:
The following "Orders" table: O_Id 1 2 3 4 5 6 OrderDATE 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

Now to find the average value of the "OrderPrice" fields, we use the following SQL statement:

SELECT AVG(OrderPrice) AS OrderAverage FROM Orders


The result will be: OrderAverage 950

MIN : This function returns the minimum of values for the column x for all the rows.
SYNTAX: MIN([DISTINCT|ALL]COLUMN NAME)

EXAMPLE:
The following "Orders" table:

O_Id 1 2 3 4 5 6

OrderDATE 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04

OrderPrice 1000 1600 700 300 2000 100

Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

Now to find the minimum value of the "OrderPrice" column, we use the following SQL statement:

SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders


The result will be: SmallestOrderPrice 100

MAX : This function returns the maximum of values for the column x for all the rows.
SYNTAX: MAX([DISTINCT|ALL]COLUMN NAME) EXAMPLE:
The following "Orders" table: O_Id 1 2 3 4 5 6 OrderDATE 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen

Now to find the maximum value of the "OrderPrice" column, we use the following SQL statement:

SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders


The result will be: LargestOrderPrice 2000

NUMERIC FUNCTIONS:
Numeric functions are used to perform operations on numbers. They accept numeric values as input and return numeric values as output. Few of the Numeric functions are: Function Name Return Value ABS (x) CEIL (x) FLOOR (x) TRUNC (x, y) ROUND (x, y) Absolute value of the number 'x' Integer value that is Greater than or equal to the number 'x' Integer value that is Less than or equal to the number 'x' Truncates value of number 'x' up to 'y' decimal places Rounded off value of the number 'x' up to the number 'y' decimal places

The following examples explains the usage of the above numeric functions Function Name Examples ABS (1) ABS (x) ABS (-1) CEIL (2.83) CEIL (x) CEIL (2.49) CEIL (-1.6) FLOOR (2.83) FLOOR (x) FLOOR (2.49) FLOOR (-1.6) ROUND (125.456, 1) TRUNC (x, y) ROUND (125.456, 0) ROUND (124.456, -1) TRUNC (140.234, 2) TRUNC (-54, 1) ROUND (x, y) TRUNC (5.7) TRUNC (142, -1) These functions can be used on database columns. Return Value 1 -1 3 3 -1 2 2 -2 125.4 125 120 140.23 54 5 140

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

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

MOD FUNCTION:
SYNTAX: SQL> SELECT MOD(300,10), MOD(23,9) FROM DUAL; SQL> SELECT ename, MOD(sal,200) FROM emp;

EXPERIMENT NO: 6 AIM: TO MAKE VIEWS OF A TABLE THEORY: VIEWS


A view is very commonly used database object that is derived at runtime.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: SQL>CREATE [OR REPLACE] VIEW view name AS sub query [WITH CHECK OPTION] [WITH READ ONLY]; EXAMPLE: SQL>CREATE VIEW monika AS SELECT empno, ename, sal, comm FROM emp; TYPES OF VIEWS 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: SQL>CREATE OR REPLACE VIEW monika AS SELECT ename, empno, sal FROM 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.

S-ar putea să vă placă și