mtech dbms lab programs1 (1)

17
Advances in Database Management Laboratory 2012- 2013 Program No. 1 Develop a database application to demonstrate storing and retrieving of BLOB and CLOB object using 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; } } Department of Computer Science & Engineering, Page 1 V.T.U. PG Centre, Gulbarga.

Upload: smile990

Post on 27-Oct-2015

563 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

Program No. 1

Develop a database application to demonstrate storing and retrieving of BLOB and CLOB object

using 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";

Department of Computer Science & Engineering, Page 1 V.T.U. PG Centre, Gulbarga.

Page 2: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

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;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);

Department of Computer Science & Engineering, Page 2 V.T.U. PG Centre, Gulbarga.

Page 3: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

}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();}}}}

Output:

Department of Computer Science & Engineering, Page 3 V.T.U. PG Centre, Gulbarga.

Page 4: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

Program No. 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 (CompanyName varchar2(20),Position varchar2(20),NoOfYears number(2));

Type created.

SQL> CREATE TYPE t_experience_tbl AS TABLE OF t_experience;

Type created.

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

Table created.

SQL> insert into employees values( 'jag', t_experience_tbl

( t_experience('abc company','Software Engineer',3), t_experience('xyz company','System Analyst',2), t_experience('mnp company','Research fellow',4) )

);

1 row created.

SQL> insert into employees values ( 'gaj', t_experience_tbl

( t_experience('git','ai',1), t_experience('git','l',3) )

);

1 row created.

Department of Computer Science & Engineering, Page 4 V.T.U. PG Centre, Gulbarga.

Page 5: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

Program No. 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.

PART A:

SQL> create table attendance(usn varchar2(10) primary key,att1 integer,att2 integer,att3 integer,att4 integer,att5 integer,att6 integer);

Table created.

SQL> insert into attendance values('2AE05cs001', 10,10,10,10,10,10);1 row created.

SQL> insert into attendance values('2AE05cs002', 11,11,11,11,11,11);1 row created.

SQL> insert into attendance values('2AE05cs003', 10,11,8,7,10,11);1 row created.

SQL> select * from attendance;

USN ATT1 ATT2 ATT3 ATT4 ATT5 ATT6---------- ---------- ---------- ---------- ---------- ---------- ----------2AE05cs001 10 10 10 10 10 102AE05cs002 11 11 11 11 11 112AE05cs003 10 11 8 7 10 11

Department of Computer Science & Engineering, Page 5 V.T.U. PG Centre, Gulbarga.

Page 6: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

SQL> create or replace trigger atriggerafter update on attendancefor each rowdeclareattn_exception1 exception;attn_exception2 exception;attn_exception3 exception;attn_exception4 exception;attn_exception5 exception;attn_exception6 exception;total_classes integer;begin total_classes:=&total_classes;if :new.att1/total_classes*100<85 thenraise attn_exception1;elsif :new.att2/total_classes*100<85 thenraise attn_exception2;elsif :new.att3/total_classes*100<85 thenraise attn_exception3;elsif :new.att4/total_classes*100<85 thenraise attn_exception4;elsif :new.att5/total_classes*100<85 thenraise attn_exception5;elsif :new.att6/total_classes*100<85 thenraise attn_exception6;end if;exception when attn_exception1 thenraise_application_error(-20001,'Attendance less than 85% in Subject 1');when attn_exception2 thenraise_application_error(-20002,'Attendance less than 85% in Subject 2');when attn_exception3 thenraise_application_error(-20003,'Attendance less than 85% in Subject 3');when attn_exception4 thenraise_application_error(-20004,'Attendance less than 85% in Subject 4');when attn_exception5 thenraise_application_error(-20005,'Attendance less than 85% in Subject 5');when attn_exception6 thenraise_application_error(-20006,'Attendance less than 85% in Subject 6');end; Enter value for total_classes: 50old 13: total_classes:=&total_classes;new 13: total_classes:=50;

Trigger created.

Department of Computer Science & Engineering, Page 6 V.T.U. PG Centre, Gulbarga.

Page 7: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

OUTPUT (PART A):

SQL> UPDATE ATTENDANCE 2 SET ATT1=5 3 WHERE USN='2AE04is051';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 ATTENDANCESET ATT1=46,ATT2=45,ATT3=47,ATT4=48,ATT5=49,ATT6=47WHERE USN='2AE04is051';

1 row updated.

PART B:

SQL> create table marks(usn varchar2(10) primary key,m1 integer,m2 integer,m3 integer,m4 integer,m5 integer,m6 integer);

Table created.

SQL> insert into marks values('2AE05cs012', 10,10,10,10,10,10);1 row created.

SQL> insert into marks values('2AE05cs002', 11,11,11,11,11,11);1 row created.

SQL> insert into marks values('2AE05cs018', 20,11,18,17,10,11);1 row created.

Department of Computer Science & Engineering, Page 7 V.T.U. PG Centre, Gulbarga.

Page 8: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

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 mtriggerafter insert or update on marksfor each rowdeclaremarks_exception1 exception;marks_exception2 exception;marks_exception3 exception;marks_exception4 exception;marks_exception5 exception;marks_exception6 exception;maximum_marks integer;begin maximum_marks:=&maximum_marks;if :new.m1/maximum_marks*100<40 thenraise marks_exception1;elsif :new.m2/maximum_marks*100<40 then raise marks_exception2;elsif :new.m3/maximum_marks*100<40 thenraise marks_exception3;elsif :new.m4/maximum_marks*100<40 thenraise marks_exception4;elsif :new.m5/maximum_marks*100<40 thenraise marks_exception5;elsif :new.m6/maximum_marks*100<40 thenraise marks_exception6;end if;exception when marks_exception1 thenraise_application_error(-20001,'Marks less than 40% in M1');when marks_exception2 thenraise_application_error(-20002,'Marks less than 40% in M2');when marks_exception3 thenraise_application_error(-20003,'Marks less than 40% in M3');when marks_exception4 thenraise_application_error(-20004,'Marks less than 40% in M4');when marks_exception5 thenraise_application_error(-20005,'Marks less than 40% in M5');

Department of Computer Science & Engineering, Page 8 V.T.U. PG Centre, Gulbarga.

Page 9: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

when marks_exception6 thenraise_application_error(-20006,'Marks less than 40% in M6');end; /Enter value for maximum_marks: 100old 13: maximum_marks:=&maximum_marks;new 13: maximum_marks:=100;

Trigger created.

OUTPUT (PART B):

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

insert into marks values('2AE05cs020',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('2AE05cs020',40,60,55,77,88,99);

1 row created.

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'

Department of Computer Science & Engineering, Page 9 V.T.U. PG Centre, Gulbarga.

Page 10: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

Program No. 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.

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;

Department of Computer Science & Engineering, Page 10 V.T.U. PG Centre, Gulbarga.

Page 11: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

}}

Tricky Part 2: Generating Rules

Collapseprivate 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,

Department of Computer Science & Engineering, Page 11 V.T.U. PG Centre, Gulbarga.

Page 12: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

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); } 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(); }

Department of Computer Science & Engineering, Page 12 V.T.U. PG Centre, Gulbarga.

Page 13: MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

OUTPUT (PART B):

Department of Computer Science & Engineering, Page 13 V.T.U. PG Centre, Gulbarga.