You are on page 1of 9

Course Code : MCSL-045

Course Title : UNIX and DBMS Lab


Assignment Number : MCA(IV)/045/Assignment/2018-19
Maximum Marks : 100
Weightage : 25%
Last Date of Submission : 15th October, 2018 (for July session)
15th April, 2019 (for January session)

PART-I: MCS-041

Question 1:
Write the UNIX commands for the following:
(a) To wait for a specified number of seconds before exit.

Ans.
root@devx:/ # date; sleep 5; date

Wed Sep 28 13:15:43 DFT 2011

Wed Sep 28 13:15:48 DFT 2011

root@devx:/ #

Wait : waits until a process is terminated

root@devx:/ # date; sleep 10 &

Wed Sep 28 13:17:59 DFT 2011

[1] 745564

root@devx:/ # wait -p $( jobs -p); date

Wed Sep 28 13:18:09 DFT 2011

root@devx:/ #

(b) To arrange to print a file on the line printer without making you wait for the command to
finish.

Ans.

Command Line Printing


If you're logged into a unix server or linux terminal, you may print text files or postscript files using
the "lpr" command. This command prints to the default printer. Currently the default printer on
CETS computers is set to the Levine 164 queue. To print a text file called "hrsprn", enter
% lpr hrsprn
If you would like to print to a different printer in the system, follow the lpr command with "-P"
followed immediately by the name of the printer. For example, to print the "hrsprn" file to the
Towne M70 queue, enter
% lpr -PPay-to-print_M70 hrsprn
and, similarly, you may print to Levine 164 by entering

% lpr -PPay-to-print_169_Color hrsprn


The "lpr" command also accepts files in the Postscript format. Do not attempt to print non-text or
non-Postscript files, though, because this can cause the printer to malfunction. See below for some
printing solutions for other file types.

(c) What are the differences among the following three commands?
i. cat file | pr
ii. pr < file
iii. pr file
Ans

Print (type) contents of named files -- cat


$cat p1.c
$cat p1.c > /dev/lpt2 ..... redirect to printer
$cat inventory
Print files -- pr
Print contents with header (time, file name, page number, 66 lines per page)
$pr p1.c
Print file contents with line numbers
$pr -n mydate.c
(c) To change the command prompt from $ to ?.

Ans

Are you sick of seeing the same old prompt whenever you go to the "DOS/Command Prompt" --
"C:\>" for example?

Here's how to change it to something more exciting:

• If you're in Windows, go to the "DOS Prompt" or the "Command Prompt"


• Type

PROMPT

followed by the format characters for your new prompt (only a few format characters are
listed below to keep the compatibility. to find out all the codes available to you in your
operating system, type "PROMPT/?" without the quotes and press ENTER):

$d -- current date
$t -- current time
$g -- ">" character
$p -- current path/directory

for example:

PROMPT $t $p $g

• Press ENTER

E ) To grant the permissions of read, write and execute to the user and read only to the group and
others for any file using chmod.

Ans.

Changing Ownership and Group


For changing the ownership of a file/directory, you can use the following command:
chown user
In case you want to change the user as well as group for a file or directory use the
command
chown user:group filename
Let's see this in action

(f) To direct a standard output to any file.

Ans.
$ fortune | tee >(?stdout?) | pbcopy
"...Unix, MS-DOS, and Windows NT (also known as the Good, the Bad, and
the Ugly)."

(g) To print all the filenames in the current directory that doesn’t contain the temp.
Ans .

find ./ -name \*\~ | xargs /bin/rm

(h) To list all the filenames that others can read and write.
Ans.

ls [-AabCcdFfghikLlmnopqRrstux1] [-timeout seconds] [-X attr] [pathname...]

(i) To split a file test, which is containing 100 lines into 25 lines each.
Ans.

(head -100 > f1.txt; cat > f2.txt) < input.txt >

(j) To display those lines that are common to file1 and file2.

Ans.

$ comm /path/to/file1/ /path/to/file2

$ comm -1 /path/to/file1/ /path/to/file2

$ comm -2 /path/to/file1/ /path/to/file2

$ comm -3 /path/to/file1/ /path/to/file2

Question 2:
(a) Write a shell program to translate all the lower case letters in any text file to the upper case
letters.

Ans.

echo -n "Enter a text file name : "


read file
if [ ! -f $file ]
then
echo "$file not a file!"
exit 1
fi

cat $file | tr '[A-Z]' '[a-z]'


(b) Given the filename by the user as the input, write a shell script to display the first five lines of
the file.
Ans.

1 #!/bin/bash
2 # get filename
3 echo -n "Enter File Name : "
4 read fileName
5 # make sure file exits for reading
6 if [ ! -f $fileName ]; then
7 echo "Filename $fileName does not
8 exists"
9 exit 1
10 fi
11 # display last five lines of the file using
12 tail command
13 tail -5 $fileName

(c) Write a shell script to display the list of the files whose filename consists of 4 characters (with
any file extension) and filename starts with the alphabet f.

Ans.

find /tmp -type f -print| awk -F/ ' length($NF) == 4 '

What awk does:

• Using / as field separator,


• Finding filename $NF (last field)
• Computing length
• And check if value is 4, then print it.

PART-II: MCS-043
Question 1:
Design a database for maintaining inventory of a retail shop. You are required to perform the
following activities for the maintenance of the above:
(a) Create the database.
Ans.

The CREATE DATABASE statement is used to create a new SQL database.


Syntax
CREATE DATABASE databasename;
CREATE DATABASE retailInventory;
CREATE TABLE companysetup (
index int primary key,
companyname varchar(255),
location varchar(255),
contact varchar(10),
tinno varchar(10)
);
CREATE TABLE setpredictivedays (
numdays int );
CREATE TABLE tbladjustitemqty (
id int primary key,
productid int,
qty int,
userid varchar(10),
dateencoded date
);
CREATE TABLE tblaudittrail (
id int foreign key References tbladjustitemqty (id),
userid varchar(10) foreign key References tbladjustitemqty (userid),
action varchar(255),
productid int foreign key References tbladjustitemqty (productid),
daterecorded date );
CREATE TABLE tblcategory (

id int foreign key References tbladjustitemqty (id),

categoryname varchar(255),

description varchar(255),

userid varchar(10) ) foreign key References tbladjustitemqty (userid),


daterecorded date );
CREATE TABLE tblcategory (
id int,
categoryname varchar(255),
description varchar(255),
userid varchar(10),
daterecorded varchar(10)
);
CREATE TABLE tblsales (
id int foreign key References tbladjustitemqty (id),
invoiceid varchar(255),
itemid varchar(255),
saleqty int, total int
userid varchar(10) foreign key References tbladjustitemqty (userid),

daterecorded date

);

(b) Write the following queries using SQL:


(i) Find the details of the items whose sales have exceeded Rs. 2,00,000.
Ans.

Where tblsales.total > 2,00,000 && tbladjustmentempty.id = tblsales.id

(ii) Find the details of the six items in terms of numbers/quantity in alphabetical order that
have got the maximum sale.

Ans.
Select Max(tblsales.total) from tblsales, tbladjustitemqty
Where tbladjustmentempty.id = tblsales.id order by desc

(iii) Find the names of those items that have an overall sale of 40% of what have been procured.
Ans.

Select * from tblsales, tbladjustitemqty


Where tblsales.total > = 0.4 && tbladjustmentempty.id = tblsales.id

(iv) Create a view of the items for the manager showing overall performance of the week for
each item.

Ans.

CREATE VIEW weekperf AS SELECT * FROM tblsales,tblcatogory WHERE tblsales.id =


tblcatogory.id;

(c) Create the procedures for the queries (i) to (iii) above.

Ans.

CREATE PROCEDURE itemdetail


AS
Select * from tblsales, tbladjustitemqty
Where tblsales.total > 2,00,000 && tbladjustmentempty.id = tblsales.id;
CREATE PROCEDURE itemdetail
AS
Select Max(tblsales.total) from tblsales, tbladjustitemqty
Where tbladjustmentempty.id = tblsales.id order by desc;
CREATE PROCEDURE itemdetail
AS
Select * from tblsales, tbladjustitemqty
Where tblsales.total > = 0.4 && tbladjustmentempty.id = tblsales.id

(d) Perform the following activities:


(i) Create a trigger that prints the daily catalog on change of a price of an item.

Ans.
mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
Query OK, 0 rows affected (0.03 sec)
mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account

FOR EACH ROW SET @sum = @sum + NEW.amount;


Query OK, 0 rows affected (0.01 sec)

(ii) Create a trigger that increases the price of a specific item by a certain percentage on a
specific weekend.

Ans.

CREATE TRIGGER print_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON emp
FOR EACH ROW
WHEN (new.empno > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := new.sal - old.sal;
dbms_output.put('Old salary: ' || :old.sal);
dbms_output.put(' New salary: ' || :new.sal);
dbms_output.put_line(' Difference ' || sal_diff);
END;
/

(e) reate a transaction that finds the total items sold per week and prints the overall
revenue generated.

Ans.

SELECT
[Order Details].OrderID, [Order Details].ProductID, Products.ProductName, [Order
Details].UnitPrice, [Order Details].Quantity, [Order Details].Discount, CCur([Order
Details].UnitPrice*[Quantity]*(1-[Discount])/100)*100 AS ExtendedPrice

FROM Products

INNER JOIN [Order Details]

ON Products.ProductID=[Order Details].ProductID

ORDER BY [Order Details].OrderID;

(f) Create two different types of users: the first user – a manager who can see reports and change
the items and its price value and second user who sells these items.
Ans.

The ORDER BY clause of the SELECT query lets you specify the sequence of rows in the final result
set. As you'll learn in later chapters, you can actually embed a SELECT statement within another
SELECT statement or SELECT expression to answer very complex questions. However, the SELECT
query cannot be embedded at any level.

You might also like