Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

SL NO Program Description Page No

SECTION A – UNIX LAB


1. Exercise 1 2
2. Exercise 2 3
3. Exercise 3 4
4. Exercise 4 5
5. Exercise 5 6
6. Exercise 6 7
7. Exercise 7 9
SECTION B – DBMS LAB

1. Sub queries and joins 12

2. Perform the relation using cursors 14

3. Write a function that gets the teacher id as parameter and returns the 15
class number associated with that teacher. If the teacher is not a class
teacher then give suitable message.
4. Identify the use of large object types in the teacher’s table. Do some 16
queries using these objects.

5. Create a user account class and give privileges related to table/view 16


creation, deletion, updating and dropping.

1
Exercise 1

1)Explore all the UNIX commands given in this manual.

Answer- Information:-
1. Date- show date and time.
2. History- list of previously executed commands.
3. Man- show on-line documentation by program name.
4. Who- who is on the system and what are they doing.
5. Who am i- who is logged onto this system.

File Management:-
1. Cat- combine files.
2. Cp- copy file.
3. Ls- list of file in a directory and their attributes.
4. Mv- change file name or directory location.
5. Chmod- set file permission.

Display contents of files:-


1. Cat- copy file to display device.
2. Vi- Screen editor for modify text files.
3. More- show text files on display terminal with paging control.
4. Head- show first few lines of a file.
5. Tail- show last few lines of file or reverse line order.
6. Grep- display lines that match a pattern.

Directories:-
1. Cd- change to new directory.
2. Mkdir- create new directory.
3. Rmdir-remove empty directory.
4. Mv- change name or directory location.
5. Pwd- show present working directory.

2) Create a directory.
Answer- mkdir
For ex:- mkdir abc

3) Create a subdirectory in the directory created.


Answer- step-1: first create a directory
For ex:- mkdir rootdirectory
Step-2: then enter in rootdirectory
For ex: cd rootdirectory
Step-3: then create another directory
For ex: mkdir subdirectory.

4) Change your current directory to the subdirectory.


Answer -Mv

2
5) Display the calendar for the current month.
Answer -Cal
For ex: cal 2010

6) Get a directory listing of the parent directory.


Answer -Ls

7) How many users were logged onto your system?


Answer -Who

8) Display your name in the form of a banner.


Answer -Banner Abhi

9) Display the name of device name of your terminal.


Answer -Tty

10) Move to the root directory.


Answer -Cd\

Exercise 2

11) Change your directory to the directory exercises. Create a file called example1 using the cat
command containing the following text:

water, water everywhere


and all the boards did shrink;
water, water everywhere,
No drop to drink.
Answer -Mv mydirectory exercise
Step-1: cat example
Step-2: type the text
Step-3: Press ctrl+D for save

12) Use the man command to obtain further information on the finger command.
Answer -Man finger

13) List all the processes that are presently running.


Answer -Ps

14) List the text files in your current directory.


Answer -Find/ usr –type –name *.txt print

15) Make a copy of any text file.


Answer -Cp

16) Rename one of your text files in the current directory.


Answer -Mv

17) Delete an unneeded copy of a file.


Answer -Rm

3
18) Print out any file on paper.
Answer -lp

19) Send a message to another user on your UNIX system, and get them to reply.
Answer -Write user

20) Create a small text file and send it to another user.


Answer -Mail

Exercise 3

21) When you receive a message, save it to a file other than your mailbox.
Answer -We can open all received message by
$mail
Then we save a mail message to a file by
$sx

22) Send a message to a user on a different computer system.


Answer -Mail mca11 ttyp5
Subject: Hello
How are you?
Then press ctrl+D .

23) Try to move to the home directory of someone else in your group. There are several ways to do
this, and you may find that you are not permitted to enter certain directories. See what files they
have, and what the file permissions are.
Answer -Cd user/mca11

24) Try to copy a file from another user’s directory to your own.
Answer -@pwd
User/mca11
$cd abhishek
$ cp xyz.txt/user2/mca12

25) Set permissions on all of your files and directories to those that you want. You may want to give
read permission on some of your files and directories to members of your group.
Answer -$chmod –R 777 abc.txt

26) Create a number of hierarchically related directories and navigate through them using a
combination of absolute pathnames (starting with "/") and relative pathnames.
Answer -$mkdir/user/mca11/helpme
$cd user/mca11/helpme

27) Try using wildcards (“*” and possibly “?”).


Answer -$ls *.txt
This command will display the listing of all files having extension .txt.

28) Put a listing of the files in your directory into a file called filelist. (Then delete it!)
Answer -$ls *.txt

4
$find filename.txt –printr> filename.txt
$vi as.txt
$rm file list

29) Create a text file containing a short story, and then use the spell program to check the spelling
of the words in the file.
Answer -$cat>story.txt
Type the text
Press ctrl+D to save.
$spell story.txt

30) Redirect the output of the spell program to a file called errors.
Answer -$spell story.txt>error

Exercise 4

31) Type the command ls -l and examine the format of the output. Pipe the output of the command
ls -l to the word count program wc to obtain a count of the number of files in your directory.
Answer -$ls –l |wc -c

32) Use cut to strip away the reference material and leave just the text field.
Answer -$cat –F 1-3-d

33) Use tr to strip away any tags that are actually in the text (e.g., attached to the words), so that you are
left with just the words.
Answer -$tr –d –c setting1.

34) Set a file to be read-only with the chmod (from change mode) command. Interpret the file
permissions displayed by the ls -l command.
Answer -$chmod 444
$ls –l

35) Delete one or more directories with the rmdir (from remove directory) command. See what
happens if the directory is not empty. Experiment (carefully!) with the rm -r command to delete a
directory and its content.
Answer -$rmdir
$rm –r
$rm –r abc

36) Experiment with redirecting command output (e.g., ls -l >file1). Try ">> " instead of " >" with
an existing text file as the output.
Answer -$ls –l>> file
This will append the text in existing file.

37) See whether upper-case versions of any of these commands work as well as the lower-case
versions.
Answer -$ls –l>file name
Not found.
38) Use the who command to see users logged into the system.

5
$who
Mca1 ttyp7 mar20 15:14
Mca2 ttyp5 mar19 11:11
Mca2 ttyp6 mar16 11:10
Mca3 ttyp7 mar11 13:11

39) Pipe the output of the who command to the sort command
Answer -$who|sort -4

40) Search for your login name in whofile using the grep command.
Answer -$who >who file
$grep mca22 whofile
Or
$who |grep mca22

Exercise 5

41) Compare two text files with the diff command.


Answer -$diff file1 file2

42) Count lines, words, and characters in a file with the wc command.
Answer -$wc –c for characters
$wc –l for lines
$wc –w for wordss

43) Display your current environment variables with the following command: set or env.
Answer -$set or $env both are same
$set or $env
Output:-
HOME=/user1/mca22
HUSHLOGIN= FALSE
HZ=100
TFS=
LOG NAME= mca22
MAIL= /user/spool/mail/mca22
MAILCHECK= 600
MF_ADM=odm.cat@unix
Msg-Mail=1
Ms_PROFILE=1
OPTLND=1
PATH=/bin:/usrer1/mca22/bin:
PS1=$
PS2=>
SHELL=/bin/sh
TERM=ansi
TZ=ESTSEDT

44) Concatenate all files in a directory redirected to /dev/null and redirecting standard error to

6
“errorFile”?
Answer -$cat *>>file name
Show error
Cat: input error: is a directory
Total 68
Dr wxh –xh –x2bca22 9-bca siz Dec 30 12:50
$cat file name

45) Display information on yourself or another user with the finger command.
Answer -$finger
Login name tty idle login time Where
Mca1 *P20 15:49 22:11 192.168.22.3
Mca2 *P21 12:22 21:11 192.168.2.3
Mca3 *P23 14:49 12:11 192.168.2.5
Mca4 *P26 11:49 22:11 192.168.2.2

47) Delete all the files in the current directory whose name ends in “.bak”.
Answer -$cat>a.back
Sss
Ctrl+D
$ls –l *.back
$rm *.back

48) Display lines 10 to 14 of any file which contains 25 lines.


Answer -$tail +10 file name|head -5
$head -10 file1| tail -14 file

49) Count how many lines contain the word science in a word file science.txt.
Answer -Cat> science.txt
- - - - - - - - - - - - - --
- - - - science- - - - - -
predd ctrl+D to save
$grep –c “science” science.txt |wc -l

50) List the statistics of the largest file (and only the largest file) in the current directory.
Answer -$ls –l| grep ‘n’ |sort>m1
$cat –d –f 4|sort m1;tail –l

Exercise 6

51) Kill any process with the help of the PID and run any process at the background.
Answer -$pwd
$kill 22556
$vi abc.txt
$date &

52) Select a text file and double space the lines.


Answer -$cat>a11.txt
Abc efg hig

7
Ctrl+d to save.
$ps -d>plog1.txt [second file]
Output:
Abc efg hig

53) List all the users from /etc/passwd in the alphabetically sorted order.
Answer -$cat /etc/password|sort

54) Create a file with duplicate records and delete duplicate records for that file.
Answer -$cat b11.txt
Abhishek abhishek abhishek
Ctrl+D to save.
$uniq –c b11.txt
Abhishek
$uniq abc.txt>ab1.txt
$Cp ab.txt abc.txt
$cat abc.txt

55) Use the grep command to search the file example1 for occurrences of the string “water”.
Answer -$cat>prog.sh
For I in cat example1
Do
Echo $i>>ex1
Done
Ctrl+D
$sh prog.sh
$cat ex1|grep –c “water”
$grep water example1

56) Write grep commands to do the following activities:

• To select the lines from a file that have exactly two characters.
$grep ‘^.$’ n1
• To select the lines from a file that start with the upper case letter.
$grep ‘[A-Z] ^1
• To select the lines from a file that end with a period.
$grep $n1
• To select the lines in a file that has one or more blank spaces.
$grep ‘ ‘n1
• To select the lines in a file and direct them to another file which has digits
as one of the characters in that line.
$grep ‘[0-9]’ n1>n2

57) Make a sorted wordlist from the file.


Answer -$tr ‘A-Z’ ‘a-z’ < file1|tr –cs ‘a-z’ ‘\02’|sort |uniq –c>file2

58) Try to execute the example shell scripts given in this manual.
Answer -$vi nv
Echo “Enter any no.”
Read a
Echo “Number is 4a”

8
59) Write a shell script that searches for a single word pattern recursively in the current directory
and displays the no. of times it occurred.
Answer -$grep –r ‘ABC’

Exercise 7

61) Write a shell script that accepts a string from the terminal and echo a suitable message if it
doesn’t have at least 5 characters including the other symbols.
Answer –
$vi abc
Echo “Enter the string”
Read s
C=`expr $s |wc –c`
C=`expr $c -1`
If Test $c –ge 5
Then
Echo “String is valid”
Else
Echo “String is invalid”
Fi
:wq

62) Write a shell script to echo the string length of the given string as argument.
Answer –
Vi xyz
Echo “enter the String”
Read a
C=’echo $a |wc –c’
C=`expr $c -1`
Echo $c
:wq

63) Write a shell script that accepts two directory names as arguments and deletes those files in the
first directory which are similarly named in the second directly. Note: Contents should also match
inside the files.
Answer –
Vi abc
Echo “enter the fist Dir”
Read a
Echo “Enter the second Dir”
Read b
Ls $a >> aa
Ls $b >> bb
For ‘in comm. -1-2 aabb’
Do
Q=’emp –s/usrer/bca/hour/s1/user1/bca1/abhi/$i’
Echo $q
If Test $q –eq 0
Sun/user1/bca1/hour/$ls

9
Sun/user1/bca1/abhi/&i
Else
fi
Done
:wq

64) Write a shell script to display the processes running on the system for every 30 seconds, but
only for 3 times.
Answer –
Vi process
I=”0”
While Test $i –le 3
Do sleep 5
Ps
i=`expr $i +1`
done

65) Write a shell script that displays the last modification time of any file.
Ls –l put|cat –c -45 -48

66) Write a shell script to check the spellings of any text document given as an argument.
Echo “enter the file”
Read a
Spell $a> error
Cut error

69) Write a shell script which reads the contents in a text file and removes all the blank spaces in
them and redirects the output to a file.
Answer –
W=`wc –w Test`
Echo “No of word $w”
C=1
Set ‘cat jay’
While test $c –le $w
Do
Echo –n $i>>jay
C=`expr $c +1`
Done
Cut jay

Or

Vi xyz
For I in ‘cat jay’
Do
Echo –n 4i
done

70) Write a shell script that changes the name of the files passed as arguments to lowercase.
Echo $i>temp
Tr “[:uppr:]” “[:lowe:]” [temp]

10
Mv /user/mca2/$i/user/mca2/$a
Session 8

71) Write a shell script to translate all the characters to lower case in a given text file.
Answer –
Echo enter a text file
Read file
If [1 $file]
Then
Echo 4file not a file
Exit
Fi
Cat $file |tr ‘[A-Z]’ ‘[a-z]’

72) Write a shell script to combine any three text files into a single file (append them in the order as
they appear in the arguments) and display the word count.
Answer –
# //bin/bash
File1=$1
File2=$2
File3=$3 out- ‘output $1’
Count=0
If [$# -ne 3]
Then
Echo “$(base name $o) file1 file 2file3”
Exit1
Fi
If [!=$file]
Then
Echo ‘$file! Not a file’
Exit2
Fi
If[! –f $file2]
Then
Echo “$file2 not file!”
Exit 3
Fi
If [1- f $file]
Then
Echo $file3 not a file!”
Exit2
Fi
$file1 $file 2$file3?? $out
Count=4(cat $out|wc –w)
Echo “count words written to out!”

11
Exercise 1

Table Creation:

-- Create table
create table TEACHER
(
T_NO VARCHAR2(3) not null,
F_NAME VARCHAR2(50),
L_NAME VARCHAR2(25),
SALARY NUMBER,
SUPERVISIOR VARCHAR2(3),
JOININGDATE DATE,
BIRTHDATE DATE,
TITLE VARCHAR2(3),
INC NUMBER
)

Tablespace SYSTEM pctfree 10 pctused 40 initrans 1 maxtents 1 maxextents unlimited);

-- Create/Recreate primary, unique and foreign key constraints


alter table TEACHER
add constraint TEACHER_PK primary key (T_NO)
using index

Tablespace SYSTEM pctfree 10 pctused 40 initrans 1 maxtents 255


storage(initial 64k minextents 1 maxextents unlimited);

-- Create table
create table CLA
(
CLASS_NO VARCHAR2(3) not null,
T_NO VARCHAR2(3),
ROOM_NO VARCHAR2(3)
)

Tablespace SYSTEM pctfree 10 pctused 40 initrans 1 maxtents 255


storage(initial 64k minextents 1 maxextents unlimited);

-- Create/Recreate primary, unique and foreign key constraints


alter table CLASS
add constraint CLASS_PK primary key (CLASS_NO)

Tablespace SYSTEM pctfree 10 initrans 2 maxtrans 255


storage(initial 64k minextents 1 maxextents unlimited);

12
Alter table CLASS
add constraint CLASS_FK foreign key (T_NO)
references TEACHER (T_NO);

-- Create table
create table PAYSCALE
(
MIN_LIMIT NUMBER,
MAX_LIMIT NUMBER,
GRADE VARCHAR2(5) not null
)
tablespace SYSTEM pctfree 10 pctused 40 initrans 1 maxtrans 255
storage (initial 64K minextents 1 maxextents unlimited );

1)Display the name of the teacher(s) who is (are) the youngest among all the teachers.

Select * from teacher


where birthdate = (select max (birthdate) from teacher);

2)Display details of all the teachers who have the same job title as that of ‘Jaideep’

Select * from teacher

where title = (select title from teacher where upper(f_name)=’JAIDEEP’;

3)Display the list of all the teachers who have joined after ‘10-Jul-95’ and whose salary is equal to
that of any of the teachers who joined before ‘10-Jul-95’.

Select * from teacher


where join_date > ’10-jul-1995’
and salary in (select salary from teacher where joining date < ’10-jul-1995’;

4)Identify all those teachers who are in grade ‘B’

Select t.* from teacher t, payscale s


where t.salary between s.min_limit
and s.max_limit
and s.grade = ’B’;

5)Display the names and numbers of all teachers who are class teachers and are in grade ‘C’

Select t.*
from teacher t , payscale s
where exists (select * from class c where t.t_no = c.t_no)
and (t.salary between s.min_limit and s.max_limit) and t.grade=C’;

13
Exercise 2

Perform the following using the following relations:

Teacher(t_no, f_name, l_name, salary, supervisor, joiningdate, birthdate, title) Class(class_no, t_no,
room_no)

Payscale(Min_limit, Max_limit, grade)

1)Write a host language block to delete all the rows from the ‘teacher’ table where the salary is less
than Rs.5000.

DECLARE
c_t_no teacher.t_no%TYPE;
c_f_name teacher.f_name%TYPE;
c_l_name teacher.l_name%TYPE;
c_salary teacher.salary%TYPE;
CURSOR c1 IS
SELECT t_no,f_name, l_name, salary
FROM teacher;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO c_t_no, c_f_name, c_l_name, c_salary ;
EXIT WHEN NOT c1%FOUND;
UPDATE teacher SET salary = salary * 1.10 WHERE salary < 5000;
END LOOP;
CLOSE c1;
END;

2) Write a host language code to insert the supervisor information from ‘teacher’ table to another
table called ‘supervisor’. The new table should have only those records where the job title is
‘supervisor’.

DECLARE
CURSOR c2 IS
SELECT t_no,f_name, l_name, salary
FROM teacher ;
teacher_rec c2%ROWTYPE;
BEGIN
OPEN c2;
FOR teacher_rec IN c2
LOOP
IF teacher_rec.salary > 20000
Teacher_rec.title = “SUPERVISOR”;
ENDIF;
END LOOP;
CLOSE c2;
END;

14
Exercise 3

1) Write a function that gets the teacher id as parameter and returns the class number associated
with that teacher. If the teacher is not a class teacher then give suitable message.

DECLARE
C_id teacher.t_no%TYPE;
C_f_name teacher.f_name%TYPE;
want_id NUMBER := 110;
BEGIN
SELECT t_no, f_name INTO c_t_no, c_f_name from teacher
WHERE t_no = want_id;
DBMS_OUTPUT.PUTLINE ( “teacher : “|| c_t_no ||’ ‘||c_f_name)
EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUTLINE(want_id || ‘ not a valid teacher id’);
END;
CREATE OR REPLACE TRIGGER new_teacher _id
AFTER INSERT ON teacher
FOR EACH ROW
DECLARE
o_t_no teacher.t_no%TYPE;
o_joiningdate teacher.joiningdate%TYPE;
BEGIN
SELECT t_no_sequence.nextval
INTO o_t_no
FROM dual;
:NEW.t_no := o_t_no;
:NEW.joiningdate := SYSDATE;
END;

Q2) Create at least two nested tables for both the University and Bank database systems. Use these
tables and enter some data into these relations. Query these databases.

CREATE TYPE address_t AS OBJECT (


street VARCHAR2(30),
city VARCHAR2(20),
state CHAR(2),
zip CHAR(5) );
/
CREATE TYPE address_tab IS TABLE OF address_t;
/
CREATE TABLE customers (
custid NUMBER,
address address_tab )
NESTED TABLE address STORE AS customer_addresses;

INSERT INTO customers VALUES (1,


address_tab(
address_t('101 First', 'Redwood Shores', 'CA', '94065'),

15
address_t('123 Maple', 'Mill Valley', 'CA', '90952')
) );

Exercise 4

Q1) Identify the use of large object types in the teacher’s table. Do some queries using these objects.

CREATE TABLE message (


msg_id NUMBER(8) NOT NULL PRIMARY KEY,
email_add VARCHAR(200),
name VARCHAR (200),
message CLOB,
posting_time DATE,
sort_key VARCHAR (600));
DECLARE
Image10 BLOB;
image_number INTEGER := 101;
BEGIN
SELECT item_blob INTO image10 FROM lob_table10
WHERE key_value = image_number;
DBMS_OUTPUT.PUT_LINE('Image size
is:'||DBMS_LOB.GETLENGTH(image10));
------
------
------
END;

Exercise 5

Q1) Create a user account “class” and give privileges related to table/view creation, deletion,
updating and dropping.

CREATE USER class


IDENTIFIED BY pass;
GRANT CREATE TABLE, DROP TABLE, CREATE VIEW, DROP VIEW
TO class;
Q. 2) Create a student account and give permission to this account for only viewing
the information on the relation Class (class_no, t_no, room_no.
Ans
DENY UPDATE, DELETE, INSERT ON employee TO student
GO
CREATE USER student
@Eclass_no int,
@St_no money,
@room_no int
GRANT EXECUTE ON student TO Class
GO

16

You might also like