Anda di halaman 1dari 62

1

Task-1
Question 1) Define and explain the terms a) RDBMS
Definition RDBMS is the Short form for Relational Database Management System. RDBMS refers to a relational database plus supporting software for managing users and processing SQL queries, performing backups/restores and associated tasks. Explanation RDBMS is Relational Data Base Management System. RDBMS is a collection of related data in a database. In RDBMS we have relations of tables say primary key ,foreign keys relation ,that involves referential integrity and normalization like concepts where as Server Side Processing Row Level Locking Network traffic is Low Distributed Network Supported Nomenclature Concept of Table. But in RDBMS we can share data server to client. But RDBMS allows simultaneous access of users to the database.

b) Data types supported in Oracle


The following is a list of data types available in Oracle. Character Data types
Data Type Syntax char(size) nchar(size) nvarchar2(size) varchar2(size) long raw long raw Oracle 9i Maximum size of 2000 bytes. Maximum size of 2000 bytes. Maximum size of 4000 bytes. Maximum size of 4000 bytes. Maximum size of 2GB. Maximum size of 2000 bytes. Maximum size of 2GB. Oracle 10g Maximum size of 2000 bytes. Maximum size of 2000 bytes. Maximum size of 4000 bytes. Maximum size of 4000 bytes. Maximum size of 2GB. Maximum size of 2000 bytes. Maximum size of 2GB. Oracle 11g Maximum size of 2000 bytes. Maximum size of 2000 bytes. Maximum size of 4000 bytes. Maximum size of 4000 bytes. Maximum size of 2GB. Maximum size of 2000 bytes. Maximum size of 2GB. Explanation (if applicable) Where size is the number of characters to store. Fixed-length strings. Space padded. Where size is the number of characters to store. Fixed-length NLS string Space padded. Where size is the number of characters to store. Variable-length NLS string. Where size is the number of characters to store. Variable-length string. Variable-length strings. (backward compatible) Variable-length binary strings Variable-length binary strings. (backward compatible) MODULE: Relational Database

Mohamed Hassan , DSE 552 Management System

Numeric Data types


Data Type Syntax Oracle 9i Precision can range from 1 to 38. Scale can range from -84 to 127. Oracle 10g Precision can range from 1 to 38. Scale can range from -84 to 127. Oracle 11g Precision can range from 1 to 38. Scale can range from -84 to 127. Explanation (if applicable) Where p is the precision and s is the scale. For example, number(7,2) is a number that has 5 digits before the decimal and 2 digits after the decimal.

number(p,s)

Where p is the precision and s is the scale. Precision can Precision can Precision can For example, numeric(7,2) is a number that numeric(p,s) range from 1 to range from 1 to range from 1 to has 5 digits before the decimal and 2 digits 38. 38. 38. after the decimal. float Where p is the precision and s is the scale. Precision can Precision can Precision can For example, dec(3,1) is a number that has 2 range from 1 to range from 1 to range from 1 to digits before the decimal and 1 digit after the 38. 38. 38. decimal.

dec(p,s)

decimal(p,s)

Where p is the precision and s is the scale. Precision can Precision can Precision can For example, decimal (3,1) is a number that range from 1 to range from 1 to range from 1 to has 2 digits before the decimal and 1 digit 38. 38. 38. after the decimal.

integer int smallint real double precision

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

Date/Time Data types


Data Type Syntax date Oracle 9i Oracle 10g Oracle 11g Explanation (if applicable)

A date between Jan A date between Jan A date between Jan 1, 4712 BC and 1, 4712 BC and 1, 4712 BC and Dec 31, 9999 AD. Dec 31, 9999 AD. Dec 31, 9999 AD. fractional seconds precision must be a number between 0 and 9. (default is 6) fractional seconds precision must be a number between 0 and 9. (default is 6) Includes year, month, day, fractional seconds hour, minute, and precision must be a seconds. number between 0 and 9. (default is 6) For example: timestamp(6)

timestamp (fractional seconds precision)

timestamp (fractional seconds precision) with time zone

Includes year, month, day, hour, minute, and fractional seconds fractional seconds fractional seconds seconds; with a time zone precision must be a precision must be a precision must be a displacement value. number between 0 number between 0 number between 0 and 9. (default is 6) and 9. (default is 6) and 9. (default is 6) For example: timestamp(5) with time zone Includes year, month, day, hour, minute, and fractional seconds fractional seconds fractional seconds seconds; with a time zone precision must be a precision must be a precision must be a expressed as the session number between 0 number between 0 number between 0 time zone. and 9. (default is 6) and 9. (default is 6) and 9. (default is 6) For example: timestamp(4) with local time zone year precision is the number of digits in the year. (default is 2) day precision must be a number between 0 and 9. (default is 2) fractional seconds precision must be a number between 0 and 9. (default is 6) year precision is the number of digits in the year. (default is 2) day precision must be a number between 0 and 9. (default is 2) fractional seconds precision must be a number between 0 and 9. (default is 6) year precision is the number of digits in the year. (default is 2) day precision must be a number between 0 and 9. (default is 2) fractional seconds precision must be a number between 0 and 9. (default is 6) Time period stored in years and months. For example: interval year(4) to month Time period stored in days, hours, minutes, and seconds. For example: interval day(2) to second(6)

timestamp (fractional seconds precision) with local time zone

interval year (year precision) to month interval day (day precision) to second (fractional seconds precision)

Large Object (LOB) Datatypes

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

4 Data Type Syntax bfile Oracle 9i Oracle 10g Oracle 11g Explanation (if applicable)

Maximum file size of 4GB.

File locators that point to Maximum file size of 232-1 Maximum file size of 264-1 a binary file on the server bytes. bytes. file system (outside the database). Store up to (4 gigabytes -1) * (the value of the CHUNK Stores unstructured parameter of LOB binary large objects. storage). Store up to (4 gigabytes -1) * (the value of the CHUNK Stores single-byte and parameter of LOB storage) multi-byte character data. of character data. Store up to (4 gigabytes -1) * (the value of the CHUNK Stores unicode data. parameter of LOB storage) of character text data.

blob

Store up to (4 gigabytes -1) Store up to * (the value of the CHUNK 4GB of binary parameter of LOB data. storage). Store up to 4GB of character data. Store up to 4GB of character text data. Store up to (4 gigabytes -1) * (the value of the CHUNK parameter of LOB storage) of character data. Store up to (4 gigabytes -1) * (the value of the CHUNK parameter of LOB storage) of character text data.

clob

nclob

Rowid Datatypes Data Type Syntax Oracle 9i Oracle 10g Oracle 11g The format of the rowid is: BBBBBBB.RRR R.FFFFF Where BBBBBBB is the block in the database file; RRRR is the row in the block; FFFFF is the database file. Explanation (if applicable)

rowid

The format of the The format of the rowid rowid is: is: BBBBBBB.RRRR. BBBBBBB.RRRR.FFFFF FFFFF Where BBBBBBB is the Where BBBBBBB block in the database file; is the block in the RRRR is the row in the database file; block; RRRR is the row in FFFFF is the database the block; file. FFFFF is the database file.

Fixed-length binary data. Every record in the database has a physical address or rowid.

urowid(size)

Universal rowid. Where size is optional.

c. SQL,DML,DCL
definition for SQL: Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

5 SQL is short form of Structured Query Language. Explanation fro SQL : SQL is used in oracle to insert, update, delete, and to show the records in the oracle database. SQL is a powerful language which uses simple English to delete, update, insert and fetch records in a database. Definition for DML. DML is the short form Data Manipulation Language. Explanation for DML DML is concerned with manipulating the data in a database as opposed to the structure of a database. The core verbs for DML are SELECT, INSERT, DELETE, UPDATE, COMMIT and ROLLBACK. Definition for DCL DCL stands for Data control Language. Explanation for DCL It is used to create roles, permissions, and referential integrity as well it is used to control access to database. Eg. GRANT,REVOKE statements.

d. Database Models
There are 4 main types of database models. They are 1) Flat 2) Hierarchical 3) Network 3) Relational 4) Object-oriented 1) Flat Operating system files or flat text files. There is no logical overlay structure.

2)Hierarchical A branch-leaf tree structure as shown below such that child tables can only have single parent tables. A child table is completely dependent on the existence of its parent table. As a result, one-to-many relationships are supported but not many-tomany relationships. The primary disadvantage of a hierarchical structure is that everything must be accessed from the root node of the tree. In Figure 1.2, accessing a Song would require retrieval of an Artist and all of that artists songs. Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

3) Network Model Refinement of the hierarchical model where many-tomany relationships are permitted because child tables can have more than a single parent table. This creates a networked structure of tables as shown in Figure 1.3. A network structure is an improved hierarchical or branch-leaf tree structure where many-to-many entities can be accessed, but access to a node still requires access to all parent nodes from the root node.

4) Relational Any two tables can be linked irrespective of hierarchical placement. Therefore, any table can be accessed directly without having to access child tables through a hierarchy or network of parent tables. Relatively complex and efficient data structures can be created with the relational data model. The operative phrase for use of relational tables is rapid selection of groups of data rather than single items. Relational databases

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

7 are most effective for reporting. An example relational structure is shown below where any table can be retrieved from based on key values. Tables or entities are built from those keys.

5) Object Oriented Directly addressed hierarchies of collections to any data item within a structure. This assumes that the direct address or pointer is a known value. The relational data model is most efficient for accessing groups of data at once, such as in reporting. On the contrary, the object data model is excellent for access to unique data items within large, highly complex data sets or groups of interlinked objects. In other words, the object data model is much more effective than the relational model with respect to extremely complex data structures.

e. Normalization & Normal forms.


Normalization Normalization is a series of steps followed to obtain a database design that allows for efficient access and storage of data. These steps reduce data redundancy and the chances of data becoming inconsistent. The normalization forms are 1) First Normal form 2) Second Normal form 3) Third Normal Form 4) Fourth Normal Form 5) Fifth Normal Form. First Normal Form First Normal Form eliminates repeating groups by putting each into a separate table and connecting them with a one-to-many relationship. Two rules follow this definition:

Each table has a primary key made of one or several fields and uniquely identifying each record Each field is atomic, it does not contain more than one value
MODULE: Relational Database

Mohamed Hassan , DSE 552 Management System

For instance, assuming a table WAGON to follow each wagon in every station. wagon_id integer (ex. 101) description string (ex. 'wagon_type, empty_weight, capacity, designer, design_date') state string (ex. 'under repair') timestamp datetime (ex. '22/12/2008 17:37') station string (ex. 'New York Grand Central') The primary key is (wagon_id, timestamp). This table is not in 1NF because "description" is not atomic. To move it in 1NF we have to split "description" field in its components: wagon_id integer wagon_type string empty_weight number capacity number designer string design_date datetime state string timestamp datetime station string

Second Normal Form Second Normal Form eliminates functional dependencies on a partial key by putting the fields in a separate table from those that are dependent on the whole key. In our example, "wagon_type", "empty_weight", "capacity"... only depends on "wagon_id" but not on "timestamp" field of the primary key, so this table is not in 2NF. In order to reach 2NF, we have to split the table in two in the way that each field of each table depends on all the fields of each primary key: wagon_id integer wagon_type string empty_weight number capacity number designer string design_date datetime wagon_id integer timestamp datetime state string station string Third Normal Form
Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

Third Normal Form eliminates functional dependencies on non-key fields by putting them in a separate table. At this stage, all non-key fields are dependent on the key, the whole key and nothing but the key. In our example, in the first table it is most likely that "empty_weight", "capacity", "designer" and "design_date" depend on "wagon_type", so we have to split this table in two: wagon_id integer wagon_type string wagon_type string empty_weight number capacity number designer string design_date datetime Now our example with its 3 tables is in 3NF. Fourth Normal Form Fourth Normal Form separates independent multi-valued facts stored in one table into separate tables. In the last table of our example, it is clear that "empty_weight" and "capacity" are interesting for every day wagon management whereas "designer" and "design_date" for historical purpose. So we can think they will never be used at the same time and have to split the table in two to fit the 4NF. wagon_type string empty_weight number capacity number wagon_type string designer string design_date datetime Fifth Normal Form Fifth Normal Form breaks out data redundancy that is not covered by any of the previous normal forms.

f. Operators in SQL*plus

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

10

SQL*Plus Operators There are two type of Operators, namely Comparison Operators and Logical Operators. These operators are used mainly in the WHERE clause, HAVING clause to filter the data to be selected. Comparison Operators: Comparison operators are used to compare the column data with specific values in a condition. Comparison Operators are also used along with the SELECT statement to filter data based on specific conditions. The below table describes each comparison operator. Comparison Operators = <>, != < > >= <= Logical Operators: There are three Logical Operators namely AND, OR and NOT.

Description equal to is not equal to less than greater than greater than or equal to less than or equal to

Question 2) Create a table Client_Master with the following fields and insert the records.
Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

11 Field_name C_no C_name City Pincode State Bal_Due Data Type Varchar2(6) Varchar2(30) Varchar2(15) Number(10,0) Varchar2(20) Number(10,2)

Table creation codes. Create table client_master(c_no varchar2(6),c_name varchar2(30), City varchar2(15),pincode number(10,0),state varchar2(20), Bal_due number(10,2));

Codes to insert the records to the client_master table insert into client_master values('C00001','Afoo','Male',400054,'Heinveeru',15000); insert into client_master values('C00002','Simam','Adu',758001,'Maafanu',1000); insert into client_master values('C00003','Azi','Thinadu',400057,'Heinveeru',5000); insert into client_master values('C00004','Shimbai','Himafushi',400056,'Maafanu',2500);

Exercise on Retrieving records from the Table


a) Find the names of all the clients Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

12

b. Retrieve the entire contents of the Client_Master Table.

c. Retrieve the list of Names and the cities of all the clients.

d) List out all the Clients who are located in Male. Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

13

Exercise on Updating records in a Table a) Change the City of Client_no C00005 to Mafushi Records in the Client_Master Table before updating.

Records in the Client_Master Table After updating.

b. Change the bal_due of client_no C00001 to RF 1000. Table before updating Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

14

Table after updating

Execise on Deleting records in a Table a) Delete from Client_Master where the filed state=Heinveeru Table before deleting

Table After deleting Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

15

Exercise on Altering the table Structure a) Add a column called Telephone of data type number and size as 10 to the client_Master Table.

Exercise on renaming the table a) Change the name of the Client_Master table to C_M

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

16

Task -2
1) Create the table described below Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

17 Table Name : Client_master Description: Used to Store Client information Column name Client_no Name City Pin code State Bal_due Input the following data to the table Client no C00001 C00002 C00003 C00004 C00005 C00006 Name Ivan Bayross Vandama Saitwal Pramada Jagust Basu Navindgi Ravi Sreedharan Rukmini City Bombay Madras Bombay Bombay Delhi Bombay Pin code 400054 780001 400057 400056 100001 400050 State Maharashtra Tamil Nadu Maharashtra Maharashtra Delhi Maharashtra Bal_due 15000 0 5000 0 2000 0 Data type Varchar2 Varchar2 Varchar2 Number Varchar2 Number Size 6 20 15 8 15 10,2

In order to insert the Values in the above given table, Write the given code below to create the client_master table and to insert the values to the table.

create table client_master(Client_no varchar2(6),name varchar2(20),city varchar2(15),pin_code number(8),State varchar2(15),Bal_due number(10,2)); insert into client_master values('C00001','Ivan Bayross','Bombay',400054,'Maharastra',15000); insert into client_master values('C00002','Vandana Saitwal','Madras',780001,'Tamil Nadu',0); insert into client_master values('C00003','Pramada jagust','Bombay',400057,'Maharastra',5000); insert into client_master values('C00004','Basu Navindgi','Bombay',400056,'Maharastra',0); insert into client_master values('C00005','Ravi Sreedharan','Delhi',100001,'Delhi',2000); insert into client_master values('C00006','Rukmini','Madras',400050,'Maharastra',0);

Screenshot of the client_master table with the values inserted.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

18

2) Exercise on computations on table data; a) find the name of all the clients having a as the second letter in their name.

b) Find out the clients who stay in Maharastra whose second letter is a.

c) Find the list of all the clients who stay in Bombay or Delhi.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

19

d) Print the list of all the clients whose bal_due is greater than value 10,000.

e) Display the information for the client_no C00001 and C00002.

f ) List the names, City and States of Client who are not in the state of Maharashtra.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

20

g) Count the total number of employees.

h) count the total number of employee who stay in Maharashtra

3) Create the described below Table Name: product_master Column name Data type Size MODULE: Relational Database

Mohamed Hassan , DSE 552 Management System

21 Product_no Description Profit_percent Unit_measure Qty_on_hand Reorder_1v1 Sell_price Cost_price Varchar2 Varchar2 Number Varchar2 Number Number Number Number 15 15 4,2 10 8 8 8,2 8,2

Insert the following data into the table


Product_no Description Profit_percent Uni_measure Qty_on_hand Reorder level 20 3 5 20 3 3 3 3 3 Sell_Price Cost_price

P00001 P03453 P06734 P07865 P07868 P07885 P07965 P07975 P08865

1.44 Floppies Monitor Mouse 1.22 Floppies Keyboard CD Drive 540 HDD 1.44 Drive 1.22 Drive

5 6 5 5 2 2.5 4 5 5

Piece Piece Piece Piece Piece Piece Piece Piece Piece

100 10 20 100 10 10 10 10 2

525 12000 1050 525 3150 5250 8400 1050 1050

500 11280 1000 500 3050 5100 8000 1000 1000

Table creation and value insertion codes


Create table product_master(product_no varchar2(6),description varchar2(15),profit_percent number(4,2),unit_measure varchar2(10),qty_on_hand number(8),reorder_lvl number(8),sell_price number(8,2),cost_price number(8,2)); insert into product_master values('P00001','1.44 Floppies',5,'Piece',100,20,525,500); insert into product_master values('P03453','Monitors',6,'Piece',10,3,12000,11280); insert into product_master values('P06734','Mouse',5,'Piece',20,5,1050,1000); insert into product_master values('P07865','1.22 Floppies',5,'Piece',100,20,525,500); insert into product_master values('P07868','Keyboard',2,'Piece',10,3,3150,3050); insert into product_master values('P07885','CD Drive',2.5,'Piece',10,3,5250,5100); Mohamed Hassan , DSE 552 HDD',4,'Piece',10,3,8400,8000); MODULE: insert into product_master values('P07965','540 insert into product_master Management System values('P07975','1.44 Drive',5,'Piece',10,3,1050,1000); insert into product_master values('P08865','1.22 Drive',5,'Piece',2,3,1050,1000);

Relational Database

22

a) Find the Product Whose selling Price is greater than 2000 and less than or equal to 5000.

b) Find the Product Whose selling price is more than 1500.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

23

c) Calculate a new selling price as, Original selling price*0.5. Rename the new column in the above query as new _price.

d) Calculate the average Price of all the products.

e) Calculate the maximum product price. Rename the Output as max_price

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

24

f) Determine the minimum product price. Rename the output as min_price

g) count the number of the products having price greater than or equal to 1500.

h) Find the all the products whose qty_no_hand is less than reorder level

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

25

TASK -3
Sub Queries and Date manipulation a) Table name: Client_master Create the Tables described below description: used to store client information
Data type Varchar2 Varchar2 Varchar 2 Address 2 Varchar2 Mohamed Hassan City Varchar2 Management System Pincode Number State Varchar2 Bal_due Number Column_na me Client_no Name Address 1 Size 6 20 30

Table client_master creation codes

, DSE 552

30 15 8 15 10,2

create table client_master(client_no varchar2(6),name varchar2(20),address1 varchar2(30),address2 varchar2(30),city varchar2(15),pincode number(8),state varchar2(15),Bal_due number(10,2)); MODULE: Relational Database

26

Screen Shot Table Structure.

b) Table name: product_master description: used to store product information


Column_na Data me type Product_no Varchar2 Description Varchar2 Profit_perc Number ent Unit_measu Varchar2 Mohamed Hassan , DSE re Management System qty_on_han Number d Reorder_lvl Number Siz e 6 15 3,1

TABLE CREATION CODES create table product_master(product_no varchar2(6), description varchar2(15), profit_percent number(8,2), unit_measure varchar2(6), qty_on_hand number(8), reorder_lvl number(8),sell_price number(8),cost_price number(8));

552
3 3

MODULE: Relational Database

27

Created Table Structure Screenshot

c) Table name: salesman_master description: used to store salesman woking for company
Data type Varchar 2 Salesman_n Varchar ame 2 Address 1 Varchar 2 Address 2 Varchar Mohamed Hassan 2 Management System City Varchar 2 Pincode Number Column_nam e salesman_no Siz e 6 20 30

Table creation codes create table salesman_master(salesman_no varchar2(6), salesman_name varchar2(20), address1 varchar2(30), address2 varchar2(30), city varchar2(20), pincode varchar2(8),state varchar2(20), sal_amt number(8,2), tgt_to_get number(6,2), ytd_sales number(6,2),remarks Structure of salesman_master table

, DSE 552
20 8

30

MODULE: Relational Database

28

Tablce creation codes d) Table name: sales_order description: used to store client information
Column_na me Order_no Order_date Client_no dely_addr Salesman_ no dely_type Billed_yn delay_date Order_stat us Data type Varchar2 Date Varchar 2 Varchar2 Varchar2 Char Char Date Varchar2 Siz e 6 6 25 6 1 1 10

create table sales_order(order_no varchar2(6),order_date date,client_no varchar2(6),dely_addr varchar2(25),salesman_no varchar(6),dely_type char(1),billed_yn char(1),delay_date date,order_status varchar2(10)); Structure of sales_order table

e) Table name: sales_order_details description: used to store clients orders with details of each product ordered
Column_na me Order_no Product_no Qty_ordere d Qty_disp Product_ra Data type Varchar2 Varchar2 Number Number Number Siz e 6 6 8 8 10,2

Table creation codes create table sales_order_details(order_no varchar2(6),product_no varchar2(6),qty_ordered number(8),qty_disp number(8),product_rate number(10,2)); Screen shot of table structure.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

29

2) Insert the following data into their respective tables.

a) Data for client_master table Client_n o C00001 C00002 C00003 C00004 C00005 C00006 Name Ivan Bayross Vandana Saitwal Pramada Jaguste Basu Navindgi Ravi Sreedharan Rukmini City Bombay Madras Bombay Bombay Delhi Bombay Pincode 400054 780001 400057 400056 100001 400050 State Maharashtra Tamil Nadu Maharashtra Maharashtra Delhi Maharashtra Bal_due 15000 0 5000 0 2000 0

insert into client_master values('C00001','Ivan Bayross','Bombay',400054,'Maharashtra',15000); insert into client_master Codes to enter the above table values to the client_master table values('C00002','Vandana Saitwal','Madras',780001,'Tamil Nadu',0); insert into client_master values('C00003','Pramada jaguste','Bombay',400057,'Maharashtra',5000); insert into client_master values('C00004','Basu Navindgi','Bombay',400056,'Maharashtra',0); insert into client_master values('C00005','Ravi Sreedharan','Delhi',100001,'Delhi',2000); Mohamed Hassan , DSE 552 MODULE: Relational Database insert into client_master Management System values('C00006','Rukmini','Bombay',400050,'Mah arashtra',0);

30

Screenshot of created Table

b) Data for poruduct_master table product_no P00001 P03453 P06734 P07865 P07868 P07885 P07965 P07975 P08865 description 1.44 Floppies Monitors Mouse 1.22 Floppies Keyboards CD Drive 540 HDD 1.44 Drive 1.22 Drive profit_ percent 5 6 5 5 5 2.5 4 5 5 uom Piece Piece Piece Piece Piece Piece Piece Piece Piece qty_on_hand 100 10 20 100 10 10 10 10 2 reorder_lvl 20 3 5 20 3 3 3 3 3 sell_price 525 12000 1050 525 3150 5250 8400 1050 1050 cost_price 500 11280 1000 500 3050 5100 8000 1000 1000

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

31

The above table values inserted into the product_master codes insert into product_master values('P00001','1.44 Floppies',5,'Piece',100,20,525,500); insert into product_master values('P03453','Monitors',6,'Piece',10,3,12000,11280); insert into product_master values('P06734','Mouse',5,'Piece',20,5,1050,1000); insert into product_master values('P07865','1.22 Floppies',5,'Piece',100,20,525,500); insert into product_master values('P07868','Keyboards',2,'Piece',10,3,3150,3050);

Screenshot of the created table product_master

c) Data for sales_order table order_no 019001 019002 046865 019003 046866 019008 order_date 12-jan-96 25-jan-96 18-Feb-96 03-Apr-96 20-May-96 24-May-96 client_n o C00001 C00002 C00003 C00001 C00004 C00005 delay_type F P F F P F bill_yn N N N N N N salesman_no S00001 S00002 S00003 S00001 S00002 S00004 delay_date 20-jan-96 27-jan-96 20-Feb-96 07-Apr-96 22-May-96 26-May-96 order_status In Process Cancelled Fulfilled Fulfilled Cancelled In Process

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

32

insert into sales_order values('019001',to_date('12-jan-96','DD-MM-YYYY'), 'C00001','F','N','S00001', to_date('20-jan-96','DD-MM-YYYY'),'In Process'); insert into sales_order values('019002',to_date('25-jan-96','DD-MM-YYYY'), 'C00002','P','N','S00002', to_date('27-jan-96','DD-MM-YYYY'),'Cancelled'); insert into sales_order values('046865',to_date('18-Feb-96','DD-MM-YYYY'), 'C00003','F','N','S00003', to_date('20-Feb-96','DD-MM-YYYY'),'Fulfilled'); insert into sales_order values('019003',to_date('03-Apr-96','DD-MMYYYY'),'C00001','F','N','S00001', to_date('07-Apr-96','DD-MM-YYYY'),'Fulfilled'); insert into sales_order values('046866',to_date('20-May-96','DD-MMYYYY'),'C00004','P','N','S00002', to_date('22-May-96','DD-MM-YYYY'),'Cancelled'); insert into sales_order values('019008',to_date('24-May-96','DD-MMYYYY'),'C00005','F','N','S00004', to_date('26-May-96','DD-MM-YYYY'),'In Process');

d) Data for sales_order_details table order_no 190001 019001 019001 019002 046865 046865 046865 product_no P00001 P07965 P07885 P00001 P07868 P07885 P00001 qty_ordered 4 2 2 10 3 3 10 qty_disp 4 1 1 0 3 1 10 product_rate 525 8400 5250 525 3150 5250 525

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

33 046865 019003 019003 046866 046866 019008 019008 P03453 P03453 P06734 P07965 P07975 P00001 P07975 4 2 1 1 1 10 5 4 2 1 0 0 5 3 1050 1050 12000 8400 1050 525 1050

Codes to enter the above given values to the sales_order_details table insert into sales_order_details values('019001','P00001',4,4,525); insert into sales_order_details values('019001','P07965',2,1,8400); insert into sales_order_details values('019001','P07885',2,1,5250); insert into sales_order_details values('019002','P00001',10,0,525); insert into sales_order_details values('046865','P07868',3,3,3150); insert into sales_order_details values('046865','P07885',3,1,5250); insert into sales_order_details values('046865','P00001',10,10,525); insert into sales_order_details values('046865','P03453',4,4,1050); insert into sales_order_details values('019003','P03453',2,2,1050); insert into sales_order_details values('019003','P06734',1,1,12000); insert into sales_order_details values('046866','P07965',1,0,8400); insert into sales_order_details values('046866','P07975',1,0,1050); insert into sales_order_details values('019008','P00001',10,5,525); insert into sales_order_details values('019008','P07975',5,3,1050); Screenshot of the sales_order_details with the inserted values

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

34

e) Data for salesman_master table


Sales_ Man_no S00001 S00002 S00003 S00004 Sales_man -name Kiran Manish Ravi Ashish Addre ss 1 A/14 65 P-7 A/5 Addres_ 2 Worli Nariman Bandra Juhu City Pin_ Code 400002 400001 400032 400044 State Sal_ Amt 3000 3000 3000 3500 Tgt_t o Get 100 200 200 200 Ytd_ Sales 50 100 100 150 remarks

Bombay Bombay Bombay Bombay

Maharastra Maharastra Maharastra Maharastra

Good Good Good Good

3) Exercise on Computations on table data a) Find the names of all the clients having a as the second letter in their names

b) Find out the clients who stay in a city whose second letter is a
Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

35

Second letter starts with a

c) Find the list of all clients who stay in Bombay or Delhi

d) Print the list of clients whose bal_due is greater than value 10000

e) print the information from sales_order table for orders placed in the month of January.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

36

f) Display the order information for client_no C00001 and C00002

g) Find the products whose selling price is greater than 2000 and less than or equal to 5000

h) Find the products whose selling price is more than 1500. Calculate a new selling price as, original selling price*0.15. Rename the new column in the above query as new_price.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

37

i) List the names,city and stat of clients who are not in the state of Maharashtra

j) Count the total number of orders

k) Calculate the average price of all the products.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

38

l) Determine the maximum and minimum product prices, rename the output as max_price and min_price.

m) Count the number of products having price greater than or equal to 1500.

n) Find all the products whose qty_on_hand is less than reorder level

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

39

4) Exercise on data manipulation a) Display the order number and day on which clients place their order.

4) b) Display the month(in alphabets) and date when the order must be delivered.

4)c) Display the order_Date in the format DD-month-YY. Eg. 12-February-96


Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

40

4)d) find the date,15 days after todays date.

4) e) Find the number of days elapsed between todays date and the delivery date of the order placed by the clients.

5) Exercise on using having and group by clauses. a) Print the description and total qty sold for each product
MODULE: Relational Database

Mohamed Hassan , DSE 552 Management System

41

b)

Find the values of each product Sold

c) Calculate the average qty sold for each client that has maximum order values of 15000.00

d) Find out the sum total of all the billed orders for the month of January.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

42

6) Exercise on joins and correlation: a) Find out the products, which have been sold to Ivan Bayross

b) Find out the products and their products and their quantities that will have to be delivered in the current month.

c) Find the product_no and description of constantly sold ie. Rapidly moving products

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

43

d) find the names of clients who have purchased CD Drive

e) List the product_no and order_no of customers having qty_ordered less than 5 from the sales_order_details table for the product 1.44 Floppies.

f) Find the products and their quantities for the orders placed by Ivan Bayross and Vandana Saitwal

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

44

g) Find the products and their quantities for the orders placed by client_no C00001 and C00002

7) Exercise on Sub-quries. a) find the product_no and description of non-moving products i.e. products not being sold.

b) Find the customer name,adress1,address2,city and pin code for the client who has place order_no 019001.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

45

c) Find the client names, who have placed orders before the month of may 96.

d) Find out if the product 1.44 Drive has been ordered by a client and print the client_no, name to whom it was sold.

e) Find the names of clients who have placed orders worth Rs.10000 or more.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

46

8) Exercise on construction sentences with data; a) print information from product_master,sales_order_details table in the following format for all the records. {description} worth Rs.{Total sales for the product} was sold

b) Print the information from product_master, Sales_order_details in the following format for all the records: {description} worth rs.{total sales for the product} was ordered in the months of {order_date in month format}

c) Print information from client_master, product_master,sales_order tables in the following format. All the records:{cust_name} has places order{ order_no} n {order_date}
Mohamed Hassan , DSE 552 Management System MODULE: Relational Database

47

TASK -4 PL/SQL programming 1) Write a PL/SQL program to accept a number, starting limit and ending limit from the user and print the multiplication table using a loop?

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

48

2) Create a table as shown below and insert the following records.


Table name: Employee Field name Emp_id Emp_name Designation Basic _salary Data type Varchar2(8) Varchar2(20) Varchar2(20) number Emp_i d S001 S002 S003 S004 S005 Emp_Na me Hidha Niyaz Shimhan Shifaana z Shiba Designation Accountant Secretary CEO Housemaid Representat ive Basic_sal ary 3000 4000 5000 6000 7000

Write a PL/SQL which accepts the Emp_id and the new Designation from the user and update the employee with the new designation. Display the newly updated table values.
Table Creation and value insertion codes create table employee(emp_id varchar2(8),emp_name varchar2(20),designation varchar2(20), basic_salary number); insert into employee values('S001','Hidha','Accountant',3000); insert into employee values('S002','Niyaz','Secretary',4000); insert into employee values('S003','Shimhan','CEO',5000); insert into employee values('S004','Shifaanaz','Housemaid',6000); insert into employee values('S005','Shiba','Representative',7000);

Screenshot of the created table with values before updating the values

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

49

Pl/SQL program to update the table designation using user inputted designation and user inputted emp_id
set serveroutput on; declare userinput_empid varchar2(8); userinput_designation varchar2(20); begin userinput_empid:=&userinput_empid; userinput_designation:=&userinput_designation; update employee set designation=userinput_designation where emp_id=userinput_empid; end; /

Emp_id S001 is updated with the new Designation Engineer Mohamed Hassan , DSE 552 MODULE: Relational Database Management System

50

3) Create a table called student_mark details with the fields Field name std_id Std_name English_mark Science_mark Maths_mark Dhivehi_mark Islam_mark Data type Number(8,0) Varchar2(20) Number(8,2) Number(8,2) Number(8,2) Number(8,2) Number(8,2)

Write a PL/SQL program to accept std_id,std_name,English_mark,Science_mark, maths_mark, dhivehi_mark and Islam_mark from the user, calculate the total, average,grade,and insert the records into the table.
create table student_mark(std_id number(8,0),std_name varchar2(20),english_mark number(8,2), science_mark number(8,2),maths_mark number(8,2),dhivehi_mark number(8,2),islam_mark number(8,2));

Screenshot of Student_mark structure

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

51

set serveroutput on; declare sid number(8,0); sname varchar2(20); english number(8,2); science number(8,2); maths number(8,2); dhivehi number(8,2); islam number(8,2); total_mark number(8,2); average_mark number(8,2); grade char(1); begin sid:=&sid; sname:=&sname; english:=&english; science:=&science; maths:=&maths; dhivehi:=&dhivehi; islam:=&islam; insert into student_mark values(sid,sname,english,science,maths,dhivehi,islam); total_mark:=english+science+maths+dhivehi+islam; average_mark:=total_mark/5; if (average_mark > 80) then grade:='A'; elsif (average_mark > 70) then grade:='B'; elsif (average_mark > 55) then grade:='C'; elsif (average_mark >= 40) then grade:='D'; else grade:='E'; end if; dbms_output.put_line('Total Mark='||total_mark); dbms_output.put_line('Average Mark='||Average_mark); dbms_output.put_line('Garde ='||grade); end; Mohamed Hassan , DSE 552 MODULE: Relational Database / Management System

52

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

53

Screenshot of student_mark table with inserted values

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

54

4) Create a table called employee_details with the fields and insert the records
Field name Emp_id Emp_nam e Designati on Data type Number(8, 0) Varchar2(2 0) Varchar2(2 0) Field_na me 1001 1002 1003 1004 Emp_na me Niyaz Shifaa Hidha Ishau Designati on Clerk Cashier Manager Supervis or Basic_sal ary 3000 2000 5000 3500 Mobile_n o 9856452 9547895 9632145 9514865

Table creating and inserting the values into the table codes:
create table employee_details(emp_id number(8,0),emp_name varchar2(20), designation varchar2(20),basic_salary number(10,2),mobile_no number(8,0)); insert into employee_details values(1001,'Niyaz','Clerk',3000,9856452); insert into employee_details

Screenshot of the table before updating basic_salary column

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

55

Code PL/SQL to update basic salary of all employees whose basic salary < 4000
declare bsal number(10,2); i number(4); begin for i in 1001..1004 loop select basic_salary into bsal from employee_details where emp_id=i; if bsal<4000 then update employee_details set basic_salary=(bsal+(bsal*0.1)) where emp_id=i; end if; end loop; end; /

Employee_details table after updating.

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

56

Basic salary values <4000

5) Write a PL/SQL block to accept from user a number and print the factors of the given number?

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

57

Task 5 Views and Cursors 1) Explain the term Cursors? List the different types of cursors and explain each? For every SQL statement executed by the oracle a work area is used for its the internal processing of the SQL statement. This work area is private to SQLs operation and is called a cursor. Cursors can be thought as a pointer into a table in the database. There are mainly 2 types of cursors : 1) Implicit Cursor. 2) Explicit Cursor.

Implicit cursor: Oracle will implicitly create an area for the DML operations. Programmer will
not have control on implicit cursors. The only useful attribute on this implicit cursor is SQL ROWCOUNT it will give the number of rows affected by the recent DML operation.

Explicit Cursor:
Explicit cursors are created by the programmer and programmer has control on it. Programmers can 1) Open 2) Close 3) Fetch And do some manipulations on the values

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

58

TASK5) 2) Write a PL/SQL code block that determines the top three highest paid employees from the employee table. Use appropriated cursor attributes for the same. Display the name and the salary.
Emp_no E001 E002 E003 E004 E005 E006 Emp_name Mazhar Asif Irufa Kudamiyaa Zaid Ashafaq salary 10000 1000 3500 4000 2500 2000

Table creation and table data entry codes


create table employee(emp_no varchar2(6),emp_name varchar2(10),salary number(6)); insert insert insert insert insert insert into into into into into into employee employee employee employee employee employee values('E001','Mazhar',10000); values('E002','Asif',1000); values('E003','Irufa',3500); values('E004','Kudamiyaa',4000); values('E005','Zaid',2500); values('E006','Ashafaq',2000);

Table creation codes to hold the fetched highest 3 salary


create table highest_salary(emp_name varchar2(10),salary number(6));

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

59

PL\SQL Codes
set serveroutput on; declare sal number(6); name varchar2(10); i int; cursor d is select emp_name,salary from employee order by salary desc; begin open d; i:=1; while i<=3 loop fetch d into name,sal; insert into highest_salary values(name,sal); i:=i+1; end loop; close d; end; /

Task 5) 3) Create a sequence inv_seq, which will generate the number from 1 to 999 in ascending order

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

60

TASK 5 4) use the sequence inv_seq to generate values for inv_no column in the invoice_hdr table note that the invoice number must start with l
Column name Inv_no Inv_dt Client_name Data type Number Date Varchar2 Size 4 20 Remarks Primary key

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

61

Task 5 Give the syntax to create a view vw_prod on the product_master table. Give examples for inserting, updating and deleting record/s using the view vw_prod Table name product_master Column name Product_no Description Qty_on_hand Reorder_level Data type Varchar2 Varchar2 Number Number Size 6 5 8 8 Attributes Primary key/ First letter must start with P Not null Not null

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

62

Cost_price Sell_price

Number Number

8,2 8,2

Not null

Mohamed Hassan , DSE 552 Management System

MODULE: Relational Database

Anda mungkin juga menyukai