Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CONCEPT OF RDBMS:
He applied the principles of relationships in statistics to data management and came up with twelve
laws. Using mathematics, he proved that if all these 12 laws were incorporated into database core
technology, there would be a revolution in the speed of incorporated into database core technology,
there would be a revolution in the speed of any DBMS system while managing data, even when used
on network operating systems. All of a sudden new life was injected into software houses who had
devoted large sums of money in developing DBMS system that programmers could use to manage
data.
These software houses took up the challenge and many products came into existence which
propounded that they followed Codds laws;
However, none of these products have been successful implementing all 12 laws into their DMBS core
technology. The lesser the number of Codds laws implemented in a products core technology, greater
are the overheads the product will place on the hardware used for running it.
The relational data management model eliminated all parent-child relationships and instead
represented all data in the database as simple row/column tables of data values.
A relation is similar to a table with rows/columns of data values. The rows of a table are
referred to as Tuples and the columns are referred to as Attributes. Several tuples of equal
length .
Each table is an independent entity and there is no physical relationship between tables.
Most data management system based on the relational model have a built-in support for
query language like ANSI SQl or QBE(Query by Example). These queries are simple English
constructs that allow adhoc data manipulation from a table.
Relational model of data management is based on set theory. Built in query language is
designed in the RDBMS, so that it can manipulate sets of data.
The user interface used with relational models is non-procedural because only what needs to
be done is specified and not how it has to be done. Using any of the other methods, you have
not only to specify what needs to be done but how it has to be done as well.
E. F. TED CODDS LAWS FOR A FULLY FUNCTIONAL RELATIONAL DATABASE MANAGEMENT SYSTEM
Relational Database Management:-A relational database management system uses only its
relational capabilities to manage the information stored in its database.
Logical Accessibility: - Every data item value stored in a relational database is accessible by
stating the name of the table it is stored in, the name of the column under which it is stored
and the values of the primary key that defines the row in which it is stored.
Representation of null values:- The database management system has a consistent method
for representing null values. For example, null values for numeric data must be distinct from
zero or any other numeric value and for character data it must be different from a string of
blanks or any other character value.
Catalog Facilities:- The logical description of al relational database is represented in the same
manner as ordinary data. This is done so that the facilities of the relational database
management system itself can be used to maintain database description.
Data Language:- A relational database management system may support many types of
languages for describing data and accessing the database. However, there must be at least one
language that uses ordinary character strings to support the definition of data, the definition
of views, the manipulation of data, constraints on data integrity, information concerning
authorization and the boundaries for recovery of unuts.
View Updatability: - Any view that can be defined using combinations of base tables, that are
theoretically updatable, is capable of being updated by the relational database management
system.
Insert, Update and Delete: - Any operation that describes the results of a single retrieval
operation is capable of being applied to an insert, update or delete operation as well.
Logical Data Independence: - Changes made to tables, that do not modify any data stored in
that table, do not require changes to be made to application program.
Integrity Constraints: - Constraints that apply to entity integrity and referential integrity are
specifiable by the data language implemented by the database management system and not
by the statements coded into the applications program.
DATA TYPES
CHAR: Values of this data type are fixed length character strings of maximum length 255
characters.
VARCHAR/VARCHAR2:- Values of this data type are variable length character strings of
maximum length 200 characters.
NUMBER : - The number data type is used to store numbers. Numbers of virtually any
magnitude maybe stored up to 38 digits of precision. Numbers as large as 9.99*10 to the
power 124, i.e. 1 followed by 125 zeros can be stored.
DATE:- The standard format is DD- MM-YY as in 12-MAR-91. To enter dates other than the
standard format, use the appropriate functions. Data Time stores date in the 24-hour format.
By default, the time in a data fields is 12:00:00 am, if no time portion is specified. The default
data for a data field is the first day of the current month.
LONG:- Cell defined as LONG can be store variable length character strings containing upto
65,535 characters. Long data can be used to store arrays of binary data in ASCII format.
Data Type
Varchar2
Varchar2
Varchar2
Varchar2
Varchar2
Varchar2
Number
Varchar2
Number
Size
6
20
30
30
15
15
6
60
10,2
Attributes
Data Type
Varchar2
Varchar2
Varchar2
Varchar2
Number
Number
Number
Number
Size
6
5
2,2
10
8
8
8,2
8,2
Attributes
DATA CONSTRAINTS
Beside the cell name, cell length and cell type, there are other parameters i.e. other data
constraints, that can be passed to the DBA at cell creation time.
These data constraints will be connected to a cell by the DBA as flags. Whenever a user attempts to
load a cell with data, the DBA will check the data being loaded into the cell against the data constraints
defined at the time the cell was created. If the data being loaded fails any of the data constraint checks
fired by the DBA, the DBA will not load the data into the cell, reject the entered record, and will flash
an error message to the user.
The constraints can either be placed at the column level or at the table level;
Column Level Constraints:
If the constraints are defined along with the column definition, it is called as column level
constraint. Column level constraint can be applied to any one column at a time i.e. they are local to
specific column. If the constraint spans across multiple columns, the user will have to use table level
constraints.
Data Type
Varchar2
Varchar2
Varchar2
Varchar2
Varchar2
Varchar2
Varchar2
Number
Number
Number
Size
6
20
30
30
20
6
20
8,2
6,2
6,2
Attributes
Primary Key
Not Null
Not Null
Not Null
Not Null
Not Null
Not Null
Remarks
Varcha2
60
Data Type
Varchar2
Date
Varchar2
Size
6
Attributes
Primary Key
Dely_addr
Salesman_no
Varchar2
Varchar2
25
6
Dely_type
Billed_yn
Dely-date
Order_status
Char
Char
Date
Varchar2
1
1
10
Rejects an Insert or update of a value, if a corresponding value does not currently exit in the
primary key table;
Rejects a DELETE, if it would invalidate a REFERENCES constraint;
Must reference a PRIMARY KEY or UNIQUE column(s) in primary key table;
Will reference the PRIMARY KEY of the primary key table if no column or group of columns is
specified in the constraint;
Must reference a table, not a view or cluster;
Requires that you own the primary key table, have REFERENCE privilege on it, or have columnlevel REFERENCE privilege on the reference columns in the primary key table;
A CHECK constraint on the client_no column of the client_master so that no client_no value
starts with C.
A CHECH constraint on name column of client_master so that the name is entered in upper
case.
A CHECH constraint on the city column of the client_master so that only the cities BOMBAY,
NEW DELHI, MADRAS and CALCUTTA as allowed.
Add Foreign key constraint on column s_order_no in table sales_order_details referencing table
sales_order, modify column qty_ordered to include NOT NULL constant;
1st step
create table sales_order1
(s_order_no varchar2(6)PRIMARY KEY,s_order_date date, client_no varchar2(6));
2nd step
create table sales_order_details1
(s_order_no varchar2(6), product_no varchar2(6),qty_order number(8),
PRIMARY KEY(s_order_no, product_no));
3rd step
ALTER TABLE sales_order_details1
ADD CONSTRAINT order_fkey
b.
Arithmetic Operator
SELECT product_no, description, sell_price*0.05, sell_price*1.05
FROM product_master;
Renaming Columns
SELECT product_no, description, sell_price*.0.05 Increase, sell_price*1.05 New_Price FROM
product_master;
LOGICAL OPERATOR
The logical operator that can be used in SQL sentences are
and all of must be include
or- any of may be included
not none of would be included
SELECT client_no, name, address1, city, pincode
From client_master WHERE city = BOMBAY or city = DELHI;
SELECT client_no, name, address1, city, pincode
FROM client_master WHERE city = BOMBAY AND (pincode=40054 OR pincode=40057);
SELECT product-no, description, profit_percent, sell_price
FROM product_master
WHERE profit_percent>=10 AND profit_percent<=20;
Range Searching
1. Select product_no, description, profit_percent, sell_price where profit_percent is between 10
and 20 both inclusive;
Select product_no, description , profit_percent, sell_price from product_master
Where profit_percent BETWEEN 10 AND 20;
2. Select product_no, description, profit_percent, sell_price where profit_percent is not
between 10 and 20;
SELECT product_no, description, profit_percent, sell_price FROM product_master
WHERE profit_percent NOT BETWEEN 10 AND 15;
Pattern Matching
ORACLE FUNCTION
Function are used to manipulate data item and return a result. Function follow the format of:
function_name(argument1, argument2,....). An argument is a user-supplied variable or constant. The
structure of function is such that it accepts zero or more argument.
Examples:
AVG
Syntax
Purpose
Example
MIN
Syntax
Purpose
Example
COUNT(expr)
Syntax
Purpose
Example
COUNT(*)
Syntax
Purpose
MAX
Example
Syntax
Purpose
Example
SUM
Syntax
Purpose
Example
ABS
Syntax
Purpose
Example
Syntax
Purpose
POWER
ROUND
Example
Syntax
Purpose
SQRT
Example
Syntax
Purpose
LOWER
INITCAP
Example
Syntax
Purpose
Example
Syntax
Purpose
AVG([DISTINCT |ALL]n)
Return average value of n, ignoring null values.
SELECT AVG(sell_price)Average FROM productmaster
MIN([DISTINCT|ALL] expr)
Returns minimum value of expr.
SELECT MIN(s_order_date)Minimum Date from
sales_order;
COUNT([DISTINCT |ALL]expr)
Returns the number of rows where expr is not null
SELECT COUNT(order_no) No of Orders FROM
sales_order;
COUNT(*)
Returns the number of rows in the table, including
duplicates and those with nulls.
SELECT COUNT(*) Total FROM client master;
MAX([DISTINCT |ALL expr])
Returns maximum value of expr.
SELECT MAX(qty_ordered)Maximum form
sales_order_details
SUM([DISTINCT |ALL]n)
Returns sum of Values of N
SELECT SUM(qty_ordered)Total Qty from
sales_order_details WHERE product_no = P0001;
ABS(n)
Returns the absolute value of N
SELECT ABS(-15) Absolute FROM dual;
POWER(m,n)
Returns m raised to nth power n must be an
integer; else an error is returned .
SELECT POWER(3,2)Raised FROM dual;
ROUND(n[,m])
Returns n rounded to m places right of the
decimal point : if m is omitted, to 0 places. M can
be negative to round off digits left of the decimal
point. M must be an integer.
SELECT ROUND(15.19,1)Round FROM dual;
SQRT(n)
Returns square root of n; if n<0, NULL SQRT
returns a real result
SELECT SQRT(25) Square Root From dual
LOWER(char)
Returns char, with all letter in lowercase
SELECT LOWER (IVAN BAYROSS)Lower from
dual
INITCAP(char)
Returns string with the first letter in upper case
Example
UPPER
SUBSTR
Syntax
Purpose
Example
Syntax
Purpose
Example
LENGTH
LTRIM
Syntax
Purpose
Example
Syntax
Purpose
Example
RTRIM
Syntax
Purpose
Example
LPAD
RPAD
Syntax
Purpose
Example
Syntax
Purpose
Example
TO_NUMBER
Syntax
Purpose
Example
TO_DATE
Syntax
Purpose
Example
TO_CHAR
Syntax
Purpose
Example
1.
Product_no
P00001
P03453
P06734
P06734
P03453
P00001
P03453
P00001
P06734
Qty_ordered
10
3
3
4
10
2
2
1
1
Qty_Disp
10
3
3
4
10
2
2
1
1
2.
Client_no
C00001
C00341
C23001
C00002
C00871
C00003
S_order_Date
12-OCT-95
25-NOV-95
03-JUL-96
18-FEB-96
20-MAR-96
12-JAN-95
JOINS
Joining Multiple Tables: Sometime we require to treat more than one table as though it were a single
entity. Then a single SQL sentence can manipulate data from all the tables as though the tables were
not separate objects, but one single entity.
To achieve this, we have to join tables are joined on columns that have the same data type and data
width in the tables;
Table Name Client_master
S_order_no
C00001
C00341
C23001
C00002
C00871
C00003
Client_no
RAJ
SUMIT
AMIT
ANIL
AJIT
BABITA
Example 1:
SELECT s_order_no, name, to_char(s_oder_date,DD/MM/YY)
FROM sales_order, client_master
WHERE client_master.client_no = sales_order.client_no
ORDER BY to_char(s_order_date,DD/MM/YY);
Example 2:
Select product_no, product_description and total qty_ordered for each product.
Table name : Product_master
Product_no
P00001
P03453
P06734
P07865
P07868
P07885
P07965
P07975
P08865
Description
1.44 Floppies
Monitors
Mouse
1.22 Floppies
Keyboards
CD Drive
HDD
1.44 Drive
1.22 Drive
Self Joins:
In some situations, you may find it necessary to join a table to itself, as though you were joining two
separate tables. This is referred to as a self join, the combined results consists of two rows from the
same table.
Table Name: employee
Employee_No
E00001
E00002
E00003
E00004
E00005
Name
Raj Kumar Singh
Babita Sharma
Anita Sukla
Chanda Kochar
Anil Kochar
Manager_no
E00002
E00003
E00004
-
SUBQUERIES
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 subquery is called a parent statement. The rows returned
by the subquery are used by the parent statement.
It can be used by the following commands
Examples:
1. Creating client_master table from the oldclient_master table:
CREATE TABLE client_master
AS SELECT *from oldclientmaster;
2. Select orders placed by Rahul Desai
SELECT *from sales_order
Where client_no = (Select client_no from client_master Where name = Rahul Desai);
3. To determine the non moving products in the product_master table:
SELECT product_no, description FROM product_master
WHERE product-no NOT IN (Select product_no FROM sales_order_details);
4. To select the name of persons who are in Mr. Pradeep department and who have also worked
on an inventory control system.
SELECT ename, deptno from emp
Where deptno IN(SELECT deptno from emp where ename = Pradeep) AND ename IN(SELECT
ename from inv_sys);
USING THE UNION
Union Clause:
The user can put together multiple queries and combine their output the union clause. The union
clause merges the output of two or more queries into a single set of rows and columns.
Example:
Select all the clients and the salesman in the city of Bombay;
SELECT salesman_no ID, name FROM salesman_master
WHERE city = Bombay
UNION
SELECT client_no ID, name FROM client_master
WHERE city = Bombay;
Intersect Clause:
The user can put together multiple queries and combine their output using the intersect clause. The
Intersect clause outputs only rows produced by both the queries intersected i.e. the output in an
Intersect clause will include only those rows that are retrieved by both the queries.
SELECT name FROM salesman_master WHERE city = Bombay
INTERSECT
SELECT name FROM salesman_master
WHERE Bombay IN (SELECt city FROM client_master WHERE client_master.client_no IN (SELECT
client_no FROM sales_order WHERE salesman_no = salesman_master.salesman_no));
INDEXES
An Index is an ordered list of contents of a column or group of columns in a table. An index created on
the single column of the table is called Simple Index. When multiple table columns are included in the
index it is called composite Index.
Simple Index:
CREATE INDEX indexfilename
ON tablename(columnname);
Example: Create an index on the table client_master, field client_no
CREATE INDEX client_ndx
ON client_master(client_no);
Composite Index:
CREATE INDEX indexfilename
VIEWS
Logical data is how we want to see the current data in our database. Physical data is how this data is
actually placed in our database.
View are masks placed upon tables. This allows the programmer to develop a method via which we
can display predetermined data to user according to our desire.
View may be created for following reason:
The DBA stores the view as a definition only. Hence, there is no duplication of data.
Simplifies queries.
Can be queried as a base table itself.
Provides data security
Avoids data redundancy.
Creation of views:
Syntax:
CREATE VIEW viewname AS
SELECT columnname, columnname
FROM tablename
WHERE columnname = expression list.
Example:
Create view on client_master for the admin department
CREATE VIEW vw_clientadmin AS
SELECT name, address1, address2, city, pincode, state
FROM client_master;
DATABASE TRIGGERS
Introduction:
Oracle allows the user to define procedures that are implicitly executed, when an insert, update or
delete is issued against a table form SQL * plus or oracle through an application. These procedures are
called triggers.
Use of Database Triggers
Database trigger support Oracle to provide a highly customized database management system. Some
of the uses to which the database triggers can be put to customize management information in Oracle
are as follows
A trigger can permit DML statements against a table only if they are issued, during
regular business hours or on predetermined weekdays.
A trigger can also be used to keep an audit trail of a table (i.e. to store the modifier
and deleted records of the table) along with the operation performed and the time on
which the operation was performed.
It can be used to prevent invalid transaction.
Enforce complex security authorizations.
2.
3.
TYPE OF TRIGGERS
When you define a trigger, you can specify the number of times the trigger action is to be executed,
once for every row affected by the triggering statement or once for the triggering statements, no
matter how many row it affects.
Row Triggers:
A row trigger is fired each time the table is affected by the triggering statement. For example if an
UPDATE statement updates multiple rows of a table, a row trigger is fired once for each row affected
by the UPDATE statement. If the triggering statement affects no rows, the trigger is not executed at all.
Row trigger should be used when the trigger action code depends on the data provided by the
triggering statement or rows that are affected.
Statement Triggers:
A row trigger is fired once on behalf of the triggering statement, independent of the number of rows
the triggering statement affects. Statement triggers are useful if the code in the trigger action does
not depend on the data provided by the triggering statement or the rows affected.
Before VS after Trigger
When defining a trigger you can specify the trigger timing, i.e. you can specify when the trigger action
is to be executed in relation to the triggering statement. BEFORE and AFETR apply to both row and the
statement triggers.
Before Trigger:
Before triggers execute the trigger action before the triggering statement. These types of triggers are
commonly used in the following situation.
* Before trigger are used when the trigger action should determine whether or not the triggering
statement should be allowed to complete. By using a before trigger, you can eliminate unnecessary
processing of the triggering statement.
*Before trigger are used to derive specific column values before completing a triggering INSERT or
UPDATE statement.
After Trigger:
After trigger execute the trigger action after the trigger statement is executed. These type of trigger
are commonly used in the following situation:
* After trigger are used when you want the triggering statement to complete before executing the
trigger action.
* If a before trigger is already present, an after trigger can perform different actions on the same
triggering statement.
: Recreates the trigger if it already exists. You can use this option to change the
definition of an existing trigger without first dropping it.
SCHEMA
: is the schema to contain the trigger. If you omit the schema, Oracle creates the
trigger in your own schema.
: indicates that Oracle fires the trigger before executing the triggering statement.
AFTER
: indicates that Oracle fires the trigger after executing the triggering statement.
DELETE
: indicates that Oracle fires the trigger whenever a delete statement removes a row
from the table.
INSERT
: indicates that Oracles fires the trigger whenever an INSERT statement adds a row
from the table.
UPDATE
: indicates that Oracle fires the trigger whenever an UPDATE statement changes a
value in one of the columns specified in the of clause. If you omit the of clause, oracle
fires the tiger whenever an update statement a value in any column of the table.
Data Type
Varchar2
Size
6
Name
Address1
Address2
City
State
Pincode
Bal_due
Varchar2
Varchar2
Varchar2
Varchar2
Varchar2
Number
Number
20
30
30
15
15
6
10,2
Attributes
Primary Key/ first letter must start
with C.
Not Null
Data Type
Varchar2
Varchar2
Number
Varchar2
Date
Size
6
20
10,2
8
Attributes
Primary Key
The creation of the database trigger audit_trail results in the auditing system for the table client. The
owner of the table can keep track of time of modification or deletion of a record that was modified or
deleted in client by querying the table auditclient.