Databases and Data Representation 11/8/05 CS360 Windows Programming 1 Relationships Relation: one-to-many LectID Name Course 1 Shereen CS360-01 Students 12 2 Doug CS300-01 11 3 Chris CS445-01 15 4 Josh CS120-01 23 5 Mike CS120-02 19 OfficeID LectID Primary key 1 1 Building Strain Room 203C 2 4 Marsh 324 3 4 Strain 202 4 2 Strain 201 Foreign key 11/8/05 CS360 Windows Programming 2 Database Views CREATE VIEW StrainOffices AS SELECT OfficeID, LectID, Room FROM Offices WHERE Building = ‘Strain’ OfficeID LectID 1 1 Building Strain Room 203C 2 4 Marsh 324 3 4 Strain 202 4 2 Strain 201 11/8/05 OfficeID LectID 1 1 Room 203C 3 4 202 4 2 201 CS360 Windows Programming 3 Database Views Views do not store data – they are “virtual” tables If we query a view, tuples are obtained from the base table so that the query can be answered OfficeID LectID Room SELECT OfficeID, Room FROM StrainOffices WHERE LectID = 1 1 1 203C 3 4 202 4 2 201 OfficeID Room 1 203C 11/8/05 CS360 Windows Programming 4 Database Views We can rename the columns in the view if we want CREATE VIEW StrainOffices(OId, Lid, RoomNum) AS SELECT OfficeID, LectID, Room FROM Offices WHERE Building = ‘Strain’ 11/8/05 OId 1 LId 3 4 202 4 2 201 CS360 Windows Programming 1 RoomNum 203C 5 Database Joins LectID Name Course 1 Shereen CS360-01 Students 12 2 Doug CS300-01 11 3 Chris CS445-01 15 4 Josh CS120-01 23 5 Mike CS120-02 19 OfficeID LectID Primary key 1 1 Building Strain Room 203C 2 4 Marsh 324 3 4 Strain 202 4 2 Strain 201 Foreign key 11/8/05 CS360 Windows Programming 6 Joins SELECT * FROM Lectures INNER JOIN Offices ON Lecturers.LectID = Offices.LectID ORDER BY Offices.LectID LId 1 Name Course Students OId LId Bld Room Shereen CS360-01 12 1 1 Strain 203C 2 3 4 4 Doug Chris Josh Josh CS300-01 CS445-01 CS120-01 CS120-01 5 Mike CS120-02 19 11/8/05 11 15 23 23 4 2 3 CS360 Windows Programming 2 3 4 4 Strain 201 Marsh 324 Strain 202 5 7 Your Turn SELECT Name FROM Lecturers INNER JOIN Offices ON Lecturers.LectID = Office.LectID INNER JOIN Advisees ON Lecturers.LectID = Advisees.AdvID WHERE Building = ‘Strain’ AND Name = ‘Harry’ LectID Name Course Students 1 Shereen CS360-01 12 2 Doug CS300-01 11 3 Chris CS445-01 15 4 Josh CS120-01 23 5 Mike CS120-02 19 11/8/05 AdvID 1 LectID Name 1 John 2 1 Mike 3 1 Harry 4 2 Holly 5 2 Ron OfficeID LectID Building Room 1 1 Strain 203C 2 4 Marsh 324 3 4 Strain 202 4 2 Strain 201 CS360 Windows Programming 8 Connecting to MySQL C# can connect to MySQL Need to download a .NET connector http://dev.mysql.com/downloads/connector/n et/1.0.html Need the MySql.Data.dll o 11/8/05 I’ve placed it in CS360 Pub under MySQL Connector\bin\.NET 1.1 CS360 Windows Programming 9 Connecting to MySQL 11/8/05 CS360 Windows Programming 10 Connecting to MySQL using MySql.Data.MySqlClient; string connStr = "server=cs445.cs.pacificu.edu; user id=shereen; password=abc123; database=shereen;"; MySqlConnection conn = null; MySqlDataAdapter da = null; MySqlDataReader reader = null; MySqlCommand cmd = null; 11/8/05 CS360 Windows Programming 11 Connecting to MySQL try { conn = new MySqlConnection(connStr); conn.Open(); cmd = new MySqlCommand("SELECT * FROM pet", conn); reader = cmd.ExecuteReader(); lb2.Text = ""; while (reader.Read()) { lb2.Text = lb2.Text + reader.GetString(0) + "\n"; } } 11/8/05 CS360 Windows Programming 12 Connecting to MySQL catch (MySqlException ex) { lb2.Text = "Exception accessing MySQL server: " + ex.Message; } catch (Exception ex) { lb2.Text = "Exception accessing database list: " + ex.Message; } finally { if (reader != null) reader.Close(); if (conn != null) conn.Close(); 11/8/05 } CS360 Windows Programming 13 11/8/05 CS360 Windows Programming 14