Final DBMS Lab Manual

19
Advances in Database Management Systems (10SCS13) Advances in Database Management Systems Lab Experiments 1. Develop a database application to demonstrate storing and retrieving of BLOB and CLOB objects. 2. Develop a database application to demonstrate the representation of multivalued attributes, and use of nested tables to represent complex objects. Write suitable queries to demonstrate it. 3. Design and develop a suitable Student Database application. One of the attributes to me maintained is the attendance of a student in each subject for which he/she has enrolled. Using TRIGGERS, we write active rules to do the following: A. Whenever attendance is updated, check if the attendance is less than 85%; if so notify the Head of Department concerned. B. Whenever the marks in the Internal Assessment Test are entered, check if the marks are less than 40%; if so, notify the Head of the Department concerned. I Sem. M.Tech. CSE (QIP) 2011-12 Page 1 of 19

Transcript of Final DBMS Lab Manual

Page 1: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

Advances in Database Management Systems

Lab Experiments

1. Develop a database application to demonstrate storing and retrieving

of BLOB and CLOB objects.

2. Develop a database application to demonstrate the representation of

multivalued attributes, and use of nested tables to represent complex

objects. Write suitable queries to demonstrate it.

3. Design and develop a suitable Student Database application. One of

the attributes to me maintained is the attendance of a student in each

subject for which he/she has enrolled. Using TRIGGERS, we write

active rules to do the following:

A. Whenever attendance is updated, check if the attendance is less

than 85%; if so notify the Head of Department concerned.

B. Whenever the marks in the Internal Assessment Test are entered,

check if the marks are less than 40%; if so, notify the Head of the

Department concerned.

4. Design, develop and execute a program in a language of your choice

to implement any one algorithm for mining association rules. Run the

program against any large database available in the public domain

and discuss the results.

I Sem. M.Tech. CSE (QIP) 2011-12 Page 1 of 15

Page 2: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

Experiment 1

1) Develop a database application to demonstrate storing and retrieving of BLOB and CLOB objects.

SOURCE CODEusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Data.SqlClient;using System.Windows.Forms;using System.IO;namespace BLOB{

public partial class Form1 : Form{Image curImage;String curFilename;String connectionString = "server=localhost;database=college;user id=sa;password=password";String savedImageName = "";public Form1(){InitializeComponent();}

private void button1_Click(object sender, EventArgs e){OpenFileDialog openDlg = new OpenFileDialog();if (openDlg.ShowDialog() == DialogResult.OK){curFilename = openDlg.FileName;textBox3.Text = curFilename;}}

private void button2_Click(object sender, EventArgs e){if (textBox3.Text != ""){

FileStream file = new FileStream(curFilename, FileMode.OpenOrCreate, FileAccess.Read);byte[] rawdata = new byte[file.Length];file.Read(rawdata, 0, System.Convert.ToInt32(file.Length));file.Close();String sql = "SELECT * FROM EMPLOYEES";SqlConnection con = new SqlConnection();con.ConnectionString = connectionString;con.Open();SqlDataAdapter adapter = new SqlDataAdapter(sql, con);SqlCommandBuilder cmb = new SqlCommandBuilder(adapter);DataSet ds = new DataSet("EMPLOYEES");adapter.Fill(ds, "EMPLOYEES");DataRow row = ds.Tables["EMPLOYEES"].NewRow();row["empcode"] = textBox1.Text;row["ename"] = textBox2.Text;row["photo"] = rawdata;

I Sem. M.Tech. CSE (QIP) 2011-12 Page 2 of 15

Page 3: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

ds.Tables["EMPLOYEES"].Rows.Add(row);adapter.Update(ds, "EMPLOYEES");con.Close();MessageBox.Show("Image Saved");}}

private void button3_Click(object sender, EventArgs e){if (textBox1.Text != ""){String sql = "Select * from employees where empcode=" + textBox1.Text;SqlConnection con = new SqlConnection();con.ConnectionString = connectionString;con.Open();FileStream file;BinaryWriter bw;int buffersize = 100;byte[] outbyte = new byte[buffersize];long retval;long startindex = 0;SqlCommand command = new SqlCommand(sql, con);SqlDataReader myReader = command.ExecuteReader(CommandBehavior.SequentialAccess);savedImageName = textBox3.Text;while (myReader.Read()){file = new FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write );bw = new BinaryWriter(file);startindex = 0;retval = myReader.GetBytes(2, startindex, outbyte, 0, buffersize);while (retval == buffersize){bw.Write(outbyte);bw.Flush();startindex += buffersize;retval = myReader.GetBytes(2, startindex, outbyte, 0, buffersize);}bw.Write(outbyte, 0, (int)retval - 1);bw.Flush();bw.Close();file.Close();}con.Close();curImage = Image.FromFile(savedImageName);pictureBox1.Image = curImage;pictureBox1.Invalidate();con.Close();}}}}

I Sem. M.Tech. CSE (QIP) 2011-12 Page 3 of 15

Page 4: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

OUTPUT

Experiment 2I Sem. M.Tech. CSE (QIP) 2011-12 Page 4 of 15

Page 5: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

2) Develop a database application to demonstrate the representation of multivalued attributes, and use of nested tables to represent complex objects. Write suitable queries to demonstrate it.

SQL> CREATE TYPE t_experience AS OBJECT 2 ( 3 CompanyName varchar2(20), 4 Position varchar2(20), 5 NoOfYears number(2) 6 ); Type created.

SQL> CREATE TYPE t_experience_tbl AS TABLE OF t_experience;

Type created.

SQL> CREATE TABLE employees 2 ( Name varchar2(20), 3 Experiences t_experience_tbl) 4 NESTED TABLE Experiences STORE AS Experiences_tab;

Table created.

SQL> insert into employees values 2 ( 'jag', t_experience_tbl 3 ( t_experience('abc company','Software Engineer',3), 4 t_experience('xyz company','System Analyst',2), 5 t_experience('mnp company','Research fellow',4) 6 ) 7 );

1 row created.

SQL> insert into employees values 2 ( 'gaj', t_experience_tbl 3 ( t_experience('git','ai',1), 4 t_experience('git','l',3) 5 ) 6 );

1 row created.

OUTPUT:I Sem. M.Tech. CSE (QIP) 2011-12 Page 5 of 15

Page 6: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

SQL> select * from employees;

NAME--------------------EXPERIENCES(COMPANYNAME, POSITION, NOOFYEARS)--------------------------------------------------------------------------------jagT_EXPERIENCE_TBL(T_EXPERIENCE('abc company', 'Software Engineer', 3), T_EXPERIENCE('xyz company', 'System Analyst', 2), T_EXPERIENCE('mnp company', 'Research fellow', 4))

gajT_EXPERIENCE_TBL(T_EXPERIENCE('git', 'ai', 1), T_EXPERIENCE('git', 'l', 3))

SQL> select a.CompanyName,a.NoOfYears from 2 table(select experiences from employees where name='jag') a;

COMPANYNAME NOOFYEARS-------------------- ----------abc company 3xyz company 2mnp company 4

SQL> select a.CompanyName,a.NoOfYears from 2 the(select experiences from employees where name='gaj') a;

COMPANYNAME NOOFYEARS-------------------- ----------git 1git 3

Experiment 3

I Sem. M.Tech. CSE (QIP) 2011-12 Page 6 of 15

Page 7: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

3) Design and develop a suitable Student Database application. One of the attributes to me maintained is the attendance of a student in each subject for which he/she has enrolled. Using TRIGGERS, we write active rules to do the following:

A. Whenever attendance is updated, check if the attendance is less than 85%; if so notify the Head of Department concerned.

B. Whenever the marks in the Internal Assessment Test are entered, check if the marks are less than 40%; if so, notify the Head of the Department concered.

PART A:SQL> create table marks( 2 usn varchar2(10) primary key, 3 m1 integer, 4 m2 integer, 5 m3 integer, 6 m4 integer, 7 m5 integer, 8 m6 integer);

Table created.

SQL> insert into marks values('2gi05cs012', 10,10,10,10,10,10);

1 row created.

SQL> insert into marks values('2gi05cs002', 11,11,11,11,11,11);

1 row created.

SQL> insert into marks values('2gi05cs018', 20,11,18,17,10,11);

1 row created.

SQL> select * from marks;

USN M1 M2 M3 M4 M5 M6---------- ---------- ---------- ---------- ---------- ---------- ----------2gi05cs012 10 10 10 10 10 102gi05cs002 11 11 11 11 11 112gi05cs018 20 11 18 17 10 11

SQL> create or replace trigger mtrigger 2 after insert or update on marks 3 for each row 4 declare 5 marks_exception1 exception; 6 marks_exception2 exception; 7 marks_exception3 exception; 8 marks_exception4 exception;

I Sem. M.Tech. CSE (QIP) 2011-12 Page 7 of 15

Page 8: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

9 marks_exception5 exception; 10 marks_exception6 exception; 11 maximum_marks integer; 12 begin 13 maximum_marks:=&maximum_marks; 14 if :new.m1/maximum_marks*100<40 then 15 raise marks_exception1; 16 elsif :new.m2/maximum_marks*100<40 then 17 raise marks_exception2; 18 elsif :new.m3/maximum_marks*100<40 then 19 raise marks_exception3; 20 elsif :new.m4/maximum_marks*100<40 then 21 raise marks_exception4; 22 elsif :new.m5/maximum_marks*100<40 then 23 raise marks_exception5; 24 elsif :new.m6/maximum_marks*100<40 then 25 raise marks_exception6; 26 end if; 27 exception 28 when marks_exception1 then 29 raise_application_error(-20001,'Marks less than 40% in M1'); 30 when marks_exception2 then 31 raise_application_error(-20002,'Marks less than 40% in M2'); 32 when marks_exception3 then 33 raise_application_error(-20003,'Marks less than 40% in M3'); 34 when marks_exception4 then 35 raise_application_error(-20004,'Marks less than 40% in M4'); 36 when marks_exception5 then 37 raise_application_error(-20005,'Marks less than 40% in M5'); 38 when marks_exception6 then 39 raise_application_error(-20006,'Marks less than 40% in M6'); 40 end; 41 /Enter value for maximum_marks: 100old 13: maximum_marks:=&maximum_marks;new 13: maximum_marks:=100;

Trigger created.

OUTPUT (PART A):

SQL> insert into marks values('2gi05cs020',20,60,55,77,88,99); insert into marks values('2gi05cs020',20,60,55,77,88,99) *ERROR at line 1:ORA-20001: Marks less than 40% in M1ORA-06512: at "MTECH.MTRIGGER", line 26ORA-04088: error during execution of trigger 'MTECH.MTRIGGER'

SQL> insert into marks values('2gi05cs020',40,60,55,77,88,99);

1 row created.I Sem. M.Tech. CSE (QIP) 2011-12 Page 8 of 15

Page 9: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

SQL> update marks 2 set m2=8 3 where usn='2gi05cs012';update marks *ERROR at line 1:ORA-20001: Marks less than 40% in M1ORA-06512: at "MTECH.MTRIGGER", line 26ORA-04088: error during execution of trigger 'MTECH.MTRIGGER'

PART B:

SQL> create table attendance( 2 usn varchar2(10) primary key, 3 att1 integer, 4 att2 integer, 5 att3 integer, 6 att4 integer, 7 att5 integer, 8 att6 integer);

Table created.

SQL> insert into attendance values('2gi05cs001', 10,10,10,10,10,10);

1 row created.

SQL> insert into attendance values('2gi05cs002', 11,11,11,11,11,11);

1 row created.

SQL> insert into attendance values('2gi05cs003', 10,11,8,7,10,11);

1 row created.

SQL> select * from attendance;

USN ATT1 ATT2 ATT3 ATT4 ATT5 ATT6---------- ---------- ---------- ---------- ---------- ---------- ----------2gi05cs001 10 10 10 10 10 102gi05cs002 11 11 11 11 11 112gi05cs003 10 11 8 7 10 11

SQL> create or replace trigger atrigger 2 after update on attendance 3 for each row 4 declare 5 attn_exception1 exception; 6 attn_exception2 exception; 7 attn_exception3 exception; 8 attn_exception4 exception;

I Sem. M.Tech. CSE (QIP) 2011-12 Page 9 of 15

Page 10: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

9 attn_exception5 exception; 10 attn_exception6 exception; 11 total_classes integer; 12 begin 13 total_classes:=&total_classes; 14 if :new.att1/total_classes*100<85 then 15 raise attn_exception1; 16 elsif :new.att2/total_classes*100<85 then 17 raise attn_exception2; 18 elsif :new.att3/total_classes*100<85 then 19 raise attn_exception3; 20 elsif :new.att4/total_classes*100<85 then 21 raise attn_exception4; 22 elsif :new.att5/total_classes*100<85 then 23 raise attn_exception5; 24 elsif :new.att6/total_classes*100<85 then 25 raise attn_exception6; 26 end if; 27 exception 28 when attn_exception1 then 29 raise_application_error(-20001,'Attendance less than 85% in Subject 1'); 30 when attn_exception2 then 31 raise_application_error(-20002,'Attendance less than 85% in Subject 2'); 32 when attn_exception3 then 33 raise_application_error(-20003,'Attendance less than 85% in Subject 3'); 34 when attn_exception4 then 35 raise_application_error(-20004,'Attendance less than 85% in Subject 4'); 36 when attn_exception5 then 37 raise_application_error(-20005,'Attendance less than 85% in Subject 5'); 38 when attn_exception6 then 39 raise_application_error(-20006,'Attendance less than 85% in Subject 6'); 40 end; Enter value for total_classes: 50old 13: total_classes:=&total_classes;new 13: total_classes:=50;

Trigger created.

OUTPUT (PART B):

SQL> UPDATE ATTENDANCE

I Sem. M.Tech. CSE (QIP) 2011-12 Page 10 of 15

Page 11: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

2 SET ATT1=5 3 WHERE USN='2tg04is051';UPDATE ATTENDANCE *ERROR at line 1:ORA-20001: Attendance less than 85% in Subject 1ORA-06512: at "MTECH.ATRIGGER", line 26ORA-04088: error during execution of trigger 'MTECH.ATRIGGER'

SQL> UPDATE ATTENDANCE 2 SET ATT1=46,ATT2=45,ATT3=47,ATT4=48,ATT5=49,ATT6=47 3 WHERE USN='2tg04is051';

1 row updated.

Experiment 4

I Sem. M.Tech. CSE (QIP) 2011-12 Page 11 of 15

Page 12: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

3) Design, develop and execute a program in a language of your choice to implement any one algorithm for mining association rules. Run the program against any large database available in the public domain and discuss the results.

Tricky Part 1: Generating Candidates

Collapseprivate Dictionary<string, double> GenerateCandidates(Dictionary<string, double> dic_FrequentItems){ Dictionary<string, double> dic_CandidatesReturn = new Dictionary<string, double>(); for (int i = 0; i < dic_FrequentItems.Count - 1; i++) { string strFirstItem = Alphabetize(dic_FrequentItems.Keys.ElementAt(i)); for (int j = i + 1; j < dic_FrequentItems.Count; j++) { string strSecondItem = Alphabetize(dic_FrequentItems.Keys.ElementAt(j)); string strGeneratedCandidate = GetCandidate(strFirstItem, strSecondItem); if (strGeneratedCandidate != string.Empty) { strGeneratedCandidate = Alphabetize(strGeneratedCandidate); double dSupport = GetSupport(strGeneratedCandidate); dic_CandidatesReturn.Add(strGeneratedCandidate, dSupport); } } } return dic_CandidatesReturn;}

private string GetCandidate(string strFirstItem, string strSecondItem){ int nLength = strFirstItem.Length; if (nLength == 1) { return strFirstItem + strSecondItem; } else { string strFirstSubString = strFirstItem.Substring(0, nLength - 1); string strSecondSubString = strSecondItem.Substring(0, nLength - 1); if (strFirstSubString == strSecondSubString) { return strFirstItem + strSecondItem[nLength - 1]; } else return string.Empty; }}

Tricky Part 2: Generating Rules

Collapse

I Sem. M.Tech. CSE (QIP) 2011-12 Page 12 of 15

Page 13: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

private List<clssRules> GenerateRules(){ List<clssRules> lstRulesReturn = new List<clssRules>(); foreach (string strItem in m_dicAllFrequentItems.Keys) { if (strItem.Length > 1) { int nMaxCombinationLength = strItem.Length / 2; GenerateCombination(strItem, nMaxCombinationLength, ref lstRulesReturn); } } return lstRulesReturn;}

private void GenerateCombination(string strItem, int nCombinationLength, ref List<clssRules> lstRulesReturn){ int nItemLength = strItem.Length; if (nItemLength == 2) { AddItem(strItem[0].ToString(), strItem, ref lstRulesReturn); return; } else if (nItemLength == 3) { for (int i = 0; i < nItemLength; i++) { AddItem(strItem[i].ToString(), strItem, ref lstRulesReturn); } return; } else { for (int i = 0; i < nItemLength; i++) { GetCombinationRecursive(strItem[i].ToString(), strItem, nCombinationLength, ref lstRulesReturn); } }}

private void Solve() { double dMinSupport = double.Parse(txt_Support.Text) / 100; double dMinConfidence = double.Parse(txt_Confidence.Text) / 100; ////Scan the transaction database to get the support S of each 1-itemset, Dictionary<string, double> dic_FrequentItemsL1 = GetL1FrequentItems(dMinSupport);

Dictionary<string, double> dic_FrequentItems = dic_FrequentItemsL1; Dictionary<string, double> dic_Candidates = new Dictionary<string, double>(); do { dic_Candidates = GenerateCandidates(dic_FrequentItems); dic_FrequentItems = GetFrequentItems(dic_Candidates, dMinSupport);I Sem. M.Tech. CSE (QIP) 2011-12 Page 13 of 15

Page 14: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

} while (dic_Candidates.Count != 0);

Dictionary<string, Dictionary<string, double>> dicClosedItemSets = GetClosedItemSets(); List<string> lstMaximalItemSets = GetMaximalItemSets(dicClosedItemSets); List<clssRules> lstRules = GenerateRules(); List<clssRules> lstStrongRules = GetStrongRules(dMinConfidence, lstRules); frmOutput objfrmOutput = new frmOutput(m_dicAllFrequentItems, dicClosedItemSets, lstMaximalItemSets, lstStrongRules); objfrmOutput.ShowDialog(); }

OUTPUT (PART B):

I Sem. M.Tech. CSE (QIP) 2011-12 Page 14 of 15

Page 15: Final DBMS Lab Manual

Advances in Database Management Systems (10SCS13)

I Sem. M.Tech. CSE (QIP) 2011-12 Page 15 of 15