0% found this document useful (1 vote)
362 views

MCS 045

The document describes a database schema and provides questions to perform queries and create procedures on the schema. It includes creating the database, performing queries to find dependent details, manager name, employees under 18, date of birth of an employee's son, and departments an employee has worked in. Procedures must also be created for the queries.

Uploaded by

kavithakiran
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
362 views

MCS 045

The document describes a database schema and provides questions to perform queries and create procedures on the schema. It includes creating the database, performing queries to find dependent details, manager name, employees under 18, date of birth of an employee's son, and departments an employee has worked in. Procedures must also be created for the queries.

Uploaded by

kavithakiran
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Course Code : MCSL-045

Course Title : UNIX and DBMS Lab


Assignment Number : MCA(4)/045/Assign/08
Maximum Marks : 100
Weightage : 25%
Last Dates for Submission : 31st October, 2008 (For July, 2008 session)
30th April, 2009 (For Jan, 2009 session)

The assignment has two parts A and B. Answer all the questions. Each part is for 20
marks. UNIX and DBMS lab record carries 40 Marks. Rest 20 marks are for viva voce. You
may use illustrations and diagrams to enhance the explanations. Please go through the
guidelines regarding assignments given in the MCA Programme Guide for the format of
presentation. If any assumptions made, please state them.

PART-I: MCS-041

Question 1:

Write the UNIX commands for the following:

(a) To kill a particular process with a PID.


Ans: Unix assign a unique number to every process running in the memory. This number
is called process ID or simply PID. The PIDs start with 0 and run upto a maximum of
32767.

To kill a process we must first note the PID of the process to be killed using the ps
cpmmand. e.g.
$ cat employee.dat | grep ‘Nagpur’ | sort > output.dat &
6173
$
If the above background process is to be killed we must execute the following command

$ kill 6173
6173 Terminated.

(b) To append the contents of file2 after the contents of the file1 and redirect them to a new
file3.
Ans: $ cat file1 file2 > file3

(c) To print the first difference between any two given files.
Ans:
cmp :- Compares two files, and if they differ, tells the first byte and line number where
they differ.
We can use the `cmp' command to show the offsets and line numbers where two files
differ. `cmp' can also show all the characters that differ between the two files, side by
side.

SYNTAX
cmp options... FromFile [ToFile]
cmp file1 file2

(d) To change the command prompt from $ to *.


Ans:
$ PS1= “*”
echo $PS1

(e) To grant the permissions of read, write and execute to the user and read only to the
group and others for all the files in a current directory.
Ans: The permissions signify who all can access the file, and for what purpose. This is
decided by the file permissions. A set of nine characters denote these permissions. There
are three types of permissions to a file:
r read
w write
x execute
There are three entities to which any combination of these permissions are assigned.
These entities are the owner, the group, and the rest. Of the nine characters, the first
three characters decide the permissions held by the owner of the file. The next set of
three characters specify the permissions for the other users in the group to which the file
owner belongs, while the last set decides the permissions for the users outside the group.
Out of the three characters belonging to each set the first character is for indicating the
‘read’ permission, the second character is for ‘write’ and the last is for ‘execute’
permission.

We know that directories too are treated by Unix as files. A directory, as Unix perceives, is
a file which contains the names of the files present in the directory. So, if a owner has to
be granted read, write and execute and group and others can only read the directory, the
permission will be

r w x r- - r - -
(f) To direct a standard output to any of the line printer.
Ans: The lp command is used to send the user’s print job to the print queue. When we
submit the job for printing using the lp command it returns a ‘request id’. This id can be
used to keep track of our job or to cancel it if required. When we cancel the print request
our job gets removed from the print queue
The standard output can be redirected to a file or a printer. Unix provides redirection
symbol for the purpose. The symbol > implies redirection of output. The > operator tells
Unix, “Don’y display this output on screen instead, put it somewhere else”.
$ cat file1 > lp
This will send the content of file1 to lp

(g) To list all the files in the current directory whose file names starts with a.
Ans:
$ ls a*

(h)
(i) To split a file splittest, which is containing 20 lines into 5 lines each which are directed to
four various files.
Ans:
$ split -25 < splittest

(j) To display the last 20 lines in a given file.


Ans:
tail -20 <filename>
e.g. tail -20 test

Question 2:

(a) Write a shell program to count no. of characters, no. of blank spaces, no. of
words and no. of lines in a given file by the user.

#!/bin/bash
# to count no. of chars, no. of blank spaces, no. of words and no. of lines in a given file by the
user

str=" "
i=0
temp=' '
len=0
cchar=0
cbspace=0
cwords=0
clines=0

echo "Ente file name"


read str

len=`echo $str | wc -c`


len=`expr $len - 1`
cwords=’ $len
while [ $i -le $len ]
do
temp=`echo $str | cut -c $i`
if[$temp –ge 65 & $temp –le 89]
then
cchar=’ expr $ccount + 1’
fi
if [ $temp -eq ‘ ‘ ]
then
cbspace=`expr $cbspace + 1`
fi

i=`expr $i + 1`
done

echo "Number of characters in the file are: $cchar"


echo "Number of blank spaces in the file are: $cbspace"
echo "Number of words in the file are: $cwords"
echo "Number of lines in the file are: $clines"
(b) Write a shell script to find the day when a date is given.

#!/bin/bash
# to find the day when a date is given

set -A arr

if [[ `cal $2 2007 | head -3 | tail -1 | awk '{print NF}'` -eq 1 ]];


then
count=0
for i in Saturday Sunday Monday Tuesday Wednesday Thursday Friday
do
arr[$count]="$i"
count=$(( $count + 1 ))
if [[ $count -ge 6 ]]; then break; fi
done
fi

if [[ `cal $2 2007 | head -3 | tail -1 | awk '{print NF}'` -eq 2 ]];


then
count=0
for i in Friday Saturday Sunday Monday Tuesday Wednesday Thursday
do
arr[$count]="$i"
count=$(( $count + 1 ))
if [[ $count -ge 6 ]]; then break; fi
done
fi

if [[ `cal $2 2007 | head -3 | tail -1 | awk '{print NF}'` -eq 3 ]];


then
count=0
for i in Thursday Friday Saturday Sunday Monday Tuesday Wednesday
do
arr[$count]="$i"
count=$(( $count + 1 ))
if [[ $count -ge 6 ]]; then break; fi
done
fi

if [[ `cal $2 2007 | head -3 | tail -1 | awk '{print NF}'` -eq 4 ]];


then
count=0
for i in Wednesday Thursday Friday Saturday Sunday Monday Tuesday
do
arr[$count]="$i"
count=$(( $count + 1 ))
if [[ $count -ge 6 ]]; then break; fi
done
fi

if [[ `cal $2 2007 | head -3 | tail -1 | awk '{print NF}'` -eq 5 ]];


then
count=0
for i in Tuesday Wednesday Thursday Friday Saturday Sunday Monday
do
arr[$count]="$i"
count=$(( $count + 1 ))
if [[ $count -ge 6 ]]; then break; fi
done
fi

if [[ `cal $2 2007 | head -3 | tail -1 | awk '{print NF}'` -eq 6 ]];


then
count=0
for i in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
do
arr[$count]="$i"
count=$(( $count + 1 ))
if [[ $count -ge 6 ]]; then break; fi
done
fi

if [[ `cal $2 2007 | head -3 | tail -1 | awk '{print NF}'` -eq 7 ]];


then
count=0
for i in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
do
arr[$count]="$i"
count=$(( $count + 1 ))
if [[ $count -ge 6 ]]; then break; fi
done
fi

if [[ $1 -ge 8 ]]; then


num=$(( ($1 % 7) - 1 ))
echo ${arr[$num]}
else
num=$(( $1 - 1 ))
echo ${arr[$num]}
fi
(c) Write a shell script to display the largest element in a given 4X4 matrix.

#!/bin/bash
# display the largest element in a given 4X4 matrix

Max=0
outer=1 # Set outer loop counter.

# Beginning of outer loop.


for a in 1 2 3 4
do
echo "Pass $outer in outer loop."
echo "---------------------"
inner=1 # Reset inner loop counter.

# ===============================================
# Beginning of inner loop.
for b in 1 2 3 4
do
echo "Pass $inner in inner loop."
let "inner+=1" # Increment inner loop counter.
echo "Enter a number"
read num
if [$num –ge $max]
then
max=`expr $num`
fi

done
# End of inner loop.

let "outer+=1" # Increment outer loop counter.


echo # Space between output blocks in pass of outer loop.
done
# End of outer loop.
echo "Maximum number is : $max"
exit 0
PART-II: MCS-043

Question 1:

Consider the following employee database schema:

EMPLOYEE (ESSN, ENAME, DEPT_NO, SALARY)


DEPENDENT (ESSN, DEPEND_NAME, RELATION, DOB)
DEPARTMENT (DEPT_NO, DEPT_NAME, MANAGER)

(a) Create the appropriate database using Oracle. (2 Marks)

(b) Perform following queries using SQL: (5 Marks)

(i) Find details of dependents for employee having name AJAY.


(ii) Find the name of the manager of the department in which employee with ESSN
Code 5078 works.
(iii) Find the name of all employees whose age is less than 18 years.
(iv) Find the DOB of the son of the employee having employee code ESSN 5078.
(v) Find the details of the departments in which the employee having employee code
ESSN 5078 has worked.

(c) Create the procedures for the above queries. (5 Marks)

(d) Write appropriate triggers, exceptions and functions for the above employee
database schema.

Ans:

(a) SQL> CREATE TABLE RIKS_DEPARTMENT


2 (DEPT_NO NUMBER(3) PRIMARY KEY,
3 DEPT_NAME VARCHAR2(15) NOT NULL,
4 MANAGER VARCHAR2(40) NOT NULL);

Table created.

SQL> CREATE TABLE RIKS_EMPLOYEE


2 (ESSN NUMBER(6) PRIMARY KEY,
3 ENAME VARCHAR2(40) NOT NULL,
4 DEPT_NO NUMBER(3) REFERENCES RIKS_DEPARTMENT,
5 SALARY NUMBER(8,2));

Table created.

SQL> CREATE TABLE RIKS_DEPENDENT


2 (ESSN NOT NULL REFERENCES RIKS_EMPLOYEE,
3 DEPEND_NAME VARCHAR2(40) NOT NULL,
4 RELATION VARCHAR2(15) NOT NULL,
5 DOB DATE);

Table created.
SQL> INSERT INTO RIKS_DEPARTMENT
2 (DEPT_NO,DEPT_NAME,MANAGER)
3 VALUES(101,'ACCOUNT','Mr. RAJESH KUMAR');

1 row created.

SQL> INSERT INTO RIKS_DEPARTMENT


2 (DEPT_NO,DEPT_NAME,MANAGER)
3 VALUES(102,'FINANCE','Mr. SUNIL JOSHI');

1 row created.

SQL> INSERT INTO RIKS_DEPARTMENT


2 (DEPT_NO,DEPT_NAME,MANAGER)
3 VALUES(103,'IT','Mr. RAJ SHEKHAR');

1 row created.

SQL> INSERT INTO RIKS_EMPLOYEE


2 VALUES(5078,'Mr. RAJESH KUMAR', 101, 55000.65);

1 row created.

SQL> INSERT INTO RIKS_EMPLOYEE


2 VALUES(5079,'Mr. RAJ SHEKHAR', 103, 48000.36);

1 row created.

SQL> INSERT INTO RIKS_EMPLOYEE


2 VALUES(5076, 'Mr. AJAY KUMAR',102, 12677.89);

1 row created.

SQL> INSERT INTO RIKS_EMPLOYEE


2 VALUES(5077, 'Mr. KETAN PARIKH', 103, 36758.15);

1 row created.

SQL> INSERT INTO RIKS_EMPLOYEE


2 VALUES(5072, 'Mr. SURESH PATEL', 101, 25483.68);

1 row created.

SQL> INSERT INTO RIKS_EMPLOYEE


2 VALUES(5075, 'Mr. SUNIL JOSHI', 102,70000.21);

1 row created.
SQL> SELECT * FROM RIKS_EMPLOYEE;

ESSN ENAME DEPT_NO SALARY


---------- ---------------------------------------- ---------- ----------
5078 Mr. RAJESH KUMAR 101 55000.65
5079 Mr. RAJ SHEKHAR 103 48000.36
5076 Mr. AJAY KUMAR 102 12677.89
5077 Mr. KETAN PARIKH 103 36758.15
5072 Mr. SURESH PATEL 101 25483.68
5075 Mr. SUNIL JOSHI 102 70000.21

6 rows selected.

SQL> INSERT INTO RIKS_DEPENDENT


2 VALUES(5078,'MRS. KUSUM KUMAR', 'WIFE', '21-FEB-84');

1 row created.

SQL> INSERT INTO RIKS_DEPENDENT


2 VALUES(5079,'MRS. PUSPA SHEKHAR', 'MOTHER', '21-FEB-55');

1 row created.

SQL> INSERT INTO RIKS_DEPENDENT


2 VALUES(5076,'MRS. SHALINI KUMAR', 'WIFE', '25-JAN-82');

1 row created.

SQL> INSERT INTO RIKS_DEPENDENT


2 VALUES(5078,'RATAN KUMAR', 'SON', '21-FEB-99');

1 row created.

Ans: (b)

SQL> SELECT E.ENAME,E.ESSN,D.DEPEND_NAME,D.RELATION,D.DOB


2 FROM RIKS_EMPLOYEE E, RIKS_DEPENDENT D
3 WHERE E.ESSN=D.ESSN AND
4 E.ENAME LIKE('%AJAY%');

ENAME ESSN
---------------------------------------- ----------
DEPEND_NAME RELATION DOB
---------------------------------------- --------------- ---------
Mr. AJAY KUMAR 5076
MRS. SHALINI KUMAR WIFE 25-JAN-82

SQL> SELECT * FROM RIKS_DEPENDENT;

ESSN DEPEND_NAME RELATION DOB


---------- ---------------------------------------- --------------- ---------
5078 MRS. KUSUM KUMAR WIFE 21-FEB-84
5079 MRS. PUSPA SHEKHAR MOTHER 21-FEB-55
5076 MRS. SHALINI KUMAR WIFE 25-JAN-82
5078 RATAN KUMAR SON 21-FEB-99

SQL> SELECT D.MANAGER


2 FROM RIKS_DEPARTMENT D, RIKS_EMPLOYEE E
3 WHERE D.DEPT_NO=E.DEPT_NO AND
4 E.ESSN=5078;

MANAGER
----------------------------------------
Mr. RAJESH KUMAR

SQL> SELECT D.DOB


2 FROM RIKS_DEPENDENT D, RIKS_EMPLOYEE E
3 WHERE D.ESSN=E.ESSN AND
4 D.RELATION='SON' AND
5 E.ESSN=5078;

DOB
---------
21-FEB-99

SQL> SELECT D.* FROM RIKS_DEPARTMENT D, RIKS_EMPLOYEE E


2 WHERE D.DEPT_NO=E.DEPT_NO
3 AND E.ESSN=5078;

DEPT_NO DEPT_NAME MANAGER


---------- --------------- ----------------------------------------
101 ACCOUNT Mr. RAJESH KUMAR

SQL> SPOOL OFF;

Ans©

Procedures:

CREATE OR REPLACE PROCEDURE create_department AS


BEGIN
CREATE TABLE RIKS_DEPARTMENT
DEPT_NO NUMBER(3) PRIMARY KEY,
DEPT_NAME VARCHAR2(15) NOT NULL,
MANAGER VARCHAR2(40) NOT NULL);
END;

CREATE OR REPLACE PROCEDURE create_employee AS


BEGIN
CREATE TABLE RIKS_EMPLOYEE
ESSN NUMBER(6) PRIMARY KEY,
ENAME VARCHAR2(40) NOT NULL,
DEPT_NO NUMBER(3) REFERENCES RIKS_DEPARTMENT,
SALARY NUMBER(8,2));
END;

CREATE OR REPLACE PROCEDURE create_dependent AS


BEGIN
CREATE TABLE RIKS_DEPENDENT
ESSN NOT NULL REFERENCES RIKS_EMPLOYEE,
DEPEND_NAME VARCHAR2(40) NOT NULL,
RELATION VARCHAR2(15) NOT NULL,
DOB DATE);
END;

CREATE OR REPLACE PROCEDURE insert_values_department AS


BEGIN
INSERT INTO RIKS_DEPARTMENT
(DEPT_NO,DEPT_NAME,MANAGER)
VALUES(101,'ACCOUNT','Mr. RAJESH KUMAR');

INSERT INTO RIKS_DEPARTMENT


(DEPT_NO,DEPT_NAME,MANAGER)
VALUES(102,'FINANCE','Mr. SUNIL JOSHI');

INSERT INTO RIKS_DEPARTMENT


DEPT_NO,DEPT_NAME,MANAGER)
VALUES(103,'IT','Mr. RAJ SHEKHAR');

END;

CREATE OR REPLACE PROCEDURE insert_values_employee AS


BEGIN
INSERT INTO RIKS_EMPLOYEE
VALUES (5078,'Mr. RAJESH KUMAR', 101, 55000.65);

INSERT INTO RIKS_EMPLOYEE


VALUES (5079,'Mr. RAJ SHEKHAR', 103, 48000.36);

INSERT INTO RIKS_EMPLOYEE


VALUES (5076, 'Mr. AJAY KUMAR', 102, 12677.89);

INSERT INTO RIKS_EMPLOYEE


VALUES (5077, 'Mr. KETAN PARIKH', 103, 36758.15);

INSERT INTO RIKS_EMPLOYEE


VALUES(5072, 'Mr. SURESH PATEL', 101, 25483.68);

INSERT INTO RIKS_EMPLOYEE


VALUES(5075, 'Mr. SUNIL JOSHI', 102,70000.21);
SELECT * FROM RIKS_EMPLOYEE;

END;

CREATE OR REPLACE PROCEDURE insert_values_dependent AS


BEGIN
INSERT INTO RIKS_DEPENDENT
VALUES(5078,'MRS. KUSUM KUMAR', 'WIFE', '21-FEB-84');

INSERT INTO RIKS_DEPENDENT


VALUES(5079,'MRS. PUSPA SHEKHAR', 'MOTHER', '21-FEB-55');

INSERT INTO RIKS_DEPENDENT


VALUES(5076,'MRS. SHALINI KUMAR', 'WIFE', '25-JAN-82');

INSERT INTO RIKS_DEPENDENT


VALUES(5078,'RATAN KUMAR', 'SON', '21-FEB-99');

END;

CREATE OR REPLACE PROCEDURE question_queries AS


BEGIN
SELECT E.ENAME,E.ESSN,D.DEPEND_NAME,D.RELATION,D.DOB
FROM RIKS_EMPLOYEE E, RIKS_DEPENDENT D
WHERE E.ESSN=D.ESSN AND
E.ENAME LIKE('%AJAY%');

SELECT * FROM RIKS_DEPENDENT;

SELECT D.MANAGER
FROM RIKS_DEPARTMENT D, RIKS_EMPLOYEE E
WHERE D.DEPT_NO=E.DEPT_NO AND
E.ESSN=5078;

SELECT D.DOB
FROM RIKS_DEPENDENT D, RIKS_EMPLOYEE E
WHERE D.ESSN=E.ESSN AND
D.RELATION='SON' AND
E.ESSN=5078;

SELECT D.* FROM RIKS_DEPARTMENT D, RIKS_EMPLOYEE E


WHERE D.DEPT_NO=E.DEPT_NO
AND E.ESSN=5078;

END;

You might also like