Data and Relationships |
|
Introduction |
In the database world, a relationship is a link that exists between two objects, mainly tables, so that data can flow from one object to another. A relationship can do even more than that: it can be used to check the accuracy of information from one object to another, it can be used to update the information in one object when related information in another object has been changed. Remember that whenever you are dealing with a group of records, also called a set of records, or a set of data, or a data set, you need an object that can "translate" the values of your records into values that are data-oriented. The object used to take care of this aspect is implemented through the DataSet class. In this case, since we want to address relationships among tables, we will need a data set object to manage such links.
Imagine that you are creating a list of employees in a mid-size to large company and want to categorize them by their employment status. You can start by creating a list of the employees:
This is a classic table. There are two common ways you can create a table. You can generate a table from a database, or you can use the DataTable class that allows you to manually create a table. Here is an example: private: System::Void btnCreate_Click(System::Object * sender, System::EventArgs * e) { DataSet* dsEmployment = new DataSet; DataTable *dtEmployees = new DataTable(S"Employees"); DataColumn *dcEmployees[] = new DataColumn*[3]; dcEmployees[0] = new DataColumn(S"FirstName", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[0]); dcEmployees[1] = new DataColumn(S"LastName", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[1]); dcEmployees[2] = new DataColumn(S"Department", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[2]); DataRow *drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Peter"; drEmplRecord->Item[S"LastName"] = S"Larsen"; drEmplRecord->Item[S"Department"] = S"Accounting"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Paul"; drEmplRecord->Item[S"LastName"] = S"Banack"; drEmplRecord->Item[S"Department"] = S"IT/IM"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Helene"; drEmplRecord->Item[S"LastName"] = S"Cassavoy"; drEmplRecord->Item[S"Department"] = S"Accounting"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Anselme"; drEmplRecord->Item[S"LastName"] = S"Thomas"; drEmplRecord->Item[S"Department"] = S"Public Relations"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Bertha"; drEmplRecord->Item[S"LastName"] = S"Um"; drEmplRecord->Item[S"Department"] = S"Corporate"; dtEmployees->Rows->Add(drEmplRecord); dsEmployment->Tables->Add(dtEmployees); this->dataGrid1->DataSource = dsEmployment; this->dataGrid1->DataMember = S"Employees"; } To associate each employee with a department, you can first create a table for the departments. Here is an example: private: System::Void btnCreate_Click(System::Object * sender, System::EventArgs * e) { DataSet* dsEmployment = new DataSet; DataTable *dtEmployees = new DataTable(S"Employees"); DataColumn *dcEmployees[] = new DataColumn*[3]; dcEmployees[0] = new DataColumn(S"FirstName", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[0]); dcEmployees[1] = new DataColumn(S"LastName", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[1]); dcEmployees[2] = new DataColumn(S"Department", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[2]); DataRow *drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Peter"; drEmplRecord->Item[S"LastName"] = S"Larsen"; drEmplRecord->Item[S"Department"] = S"Accounting"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Paul"; drEmplRecord->Item[S"LastName"] = S"Banack"; drEmplRecord->Item[S"Department"] = S"IT/IM"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Helene"; drEmplRecord->Item[S"LastName"] = S"Cassavoy"; drEmplRecord->Item[S"Department"] = S"Accounting"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Anselme"; drEmplRecord->Item[S"LastName"] = S"Thomas"; drEmplRecord->Item[S"Department"] = S"Public Relations"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"FirstName"] = S"Bertha"; drEmplRecord->Item[S"LastName"] = S"Um"; drEmplRecord->Item[S"Department"] = S"Corporate"; dtEmployees->Rows->Add(drEmplRecord); DataTable *dtEmplStatus = new DataTable(S"EmploymentStatus"); DataColumn *dcEmployment = new DataColumn; dcEmployment = new DataColumn(S"EmplStatus", System::Type::GetType(S"System.String")); dtEmplStatus->Columns->Add(dcEmployment); DataRow *drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatus"] = S"Full Time"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatus"] = S"Part Time"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatus"] = S"Contractor"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatus"] = S"Intern"; dtEmplStatus->Rows->Add(drEmployment); dsEmployment->Tables->Add(dtEmployees); dsEmployment->Tables->Add(dtEmplStatus); this->dataGrid1->DataSource = dsEmployment; this->dataGrid2->DataSource = dsEmployment; this->dataGrid1->DataMember = S"Employees"; this->dataGrid2->DataMember = S"EmploymentStatus"; } |
Creating a Relationship |
As it should appear obviously, a relationship makes more sense in the presence of at least two tables, although a table can (also) have some type of relationship with itself. By the rules and suggestions of relational databases, for a table to participate in a relationship, the table should provide at least one column that would be used to uniquely represent or identify each record. Such a column is usually called an index. Although it can be positioned anywhere in the table, it is usually the first or most-left column. Also traditionally, the name of this column ends with a No, such as RecordNo, or an ID suffix, such as RecordID. In our first table, to uniquely identify each record, we can create a column called EmployeeID and add an incremental number to each record. In the same way, a column used to identify each department in the second table of our example can be called EmploStatusID. The tables would then be created as follows: |
private: System::Void btnCreate_Click(System::Object * sender, System::EventArgs * e) { DataSet* dsEmployment = new DataSet; DataTable *dtEmployees = new DataTable(S"Employees"); DataColumn *dcEmployees[] = new DataColumn*[4]; dcEmployees[0] = new DataColumn(S"EmployeeID", System::Type::GetType(S"System.Int32")); dtEmployees->Columns->Add(dcEmployees[0]); dcEmployees[1] = new DataColumn(S"FirstName", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[1]); dcEmployees[2] = new DataColumn(S"LastName", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[2]); dcEmployees[3] = new DataColumn(S"Department", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[3]); DataRow *drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"1"; drEmplRecord->Item[S"FirstName"] = S"Peter"; drEmplRecord->Item[S"LastName"] = S"Larsen"; drEmplRecord->Item[S"Department"] = S"Accounting"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"2"; drEmplRecord->Item[S"FirstName"] = S"Paul"; drEmplRecord->Item[S"LastName"] = S"Banack"; drEmplRecord->Item[S"Department"] = S"IT/IM"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"3"; drEmplRecord->Item[S"FirstName"] = S"Helene"; drEmplRecord->Item[S"LastName"] = S"Cassavoy"; drEmplRecord->Item[S"Department"] = S"Accounting"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"4"; drEmplRecord->Item[S"FirstName"] = S"Anselme"; drEmplRecord->Item[S"LastName"] = S"Thomas"; drEmplRecord->Item[S"Department"] = S"Public Relations"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"5"; drEmplRecord->Item[S"FirstName"] = S"Bertha"; drEmplRecord->Item[S"LastName"] = S"Um"; drEmplRecord->Item[S"Department"] = S"Corporate"; dtEmployees->Rows->Add(drEmplRecord); DataTable *dtEmplStatus = new DataTable(S"EmploymentStatus"); DataColumn *dcEmployment[] = new DataColumn*[2]; dcEmployment[0] = new DataColumn(S"EmplStatusID", System::Type::GetType(S"System.Int32")); dtEmplStatus->Columns->Add(dcEmployment[0]); dcEmployment[1] = new DataColumn(S"EmplStatus", System::Type::GetType(S"System.String")); dtEmplStatus->Columns->Add(dcEmployment[1]); DataRow *drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatusID"] = S"1"; drEmployment->Item[S"EmplStatus"] = S"Full Time"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatusID"] = S"2"; drEmployment->Item[S"EmplStatus"] = S"Part Time"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatusID"] = S"3"; drEmployment->Item[S"EmplStatus"] = S"Contractor"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatusID"] = S"4"; drEmployment->Item[S"EmplStatus"] = S"Intern"; dtEmplStatus->Rows->Add(drEmployment); dsEmployment->Tables->Add(dtEmployees); dsEmployment->Tables->Add(dtEmplStatus); this->dataGrid1->DataSource = dsEmployment; this->dataGrid2->DataSource = dsEmployment; this->dataGrid1->DataMember = S"Employees"; this->dataGrid2->DataMember = S"EmploymentStatus"; } private: System::Void btnClose_Click(System::Object * sender, System::EventArgs * e) { Close(); } |
As mentioned already, this type of field is used to uniquely identify each record of a table. Therefore, it is based on this field that a table can be related to another. To actually create the relationship, in our example, to associate each employee to a department, the table that holds the list of employees must have a field that represents the corresponding department from the Departments table. Therefore, you must create such a new column. The most important rule you must observe is that this new field must have the same data type as the column that uniquely identifies each department in the other table. Therefore, the field in the Departments table is referred to as the Primary Key. The new column created in the Employees table is referred to as the Foreign Key because this column acts only as an "ambassador". In the strict sense, it doesn't belong to the table in which it it created and in fact, its values should/must not be changed by its hosting table. After creating the foreign key column, the relationship is not automatically created between both tables, since neither the compiler nor the database engine (if you were working on a database) is aware of the role of this new field. To create a relationship between two DataTable objects, the Microsoft .NET Framework provides the DataRelation class. As this is a small class, its main role is to join two tables. In order to create a relationship, you can declare a pointer to DataRelation and use one of its 5 constructors to initialize the relationship. The first constructor has the following syntax: DataRelation(String* relationName, DataColumn* parentColumn, DataColumn* childColumn); The first argument allows you to specify a name for the relationship. The second argument must identify the primary key column of the table that would supply the values. In our example, this would be the primary key of the Departments table. The third argument is the column used as the foreign key in the table that would receive the values of the other table. In our example, this would be the foreign key of the Employees table. This indicates that you should first define and identify the columns that would be used in the relationship. Based on this description, the relationship can be created as follows: |
private: System::Void btnCreate_Click(System::Object * sender, System::EventArgs * e) { DataSet* dsEmployment = new DataSet; DataTable *dtEmployees = new DataTable(S"Employees"); DataColumn *dcEmployees[] = new DataColumn*[5]; dcEmployees[0] = new DataColumn(S"EmployeeID", System::Type::GetType(S"System.Int32")); dtEmployees->Columns->Add(dcEmployees[0]); dcEmployees[1] = new DataColumn(S"FirstName", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[1]); dcEmployees[2] = new DataColumn(S"LastName", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[2]); dcEmployees[3] = new DataColumn(S"Department", System::Type::GetType(S"System.String")); dtEmployees->Columns->Add(dcEmployees[3]); dcEmployees[4] = new DataColumn(S"EmplStatusID", System::Type::GetType(S"System.Int32")); dtEmployees->Columns->Add(dcEmployees[4]); DataRow *drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"1"; drEmplRecord->Item[S"FirstName"] = S"Peter"; drEmplRecord->Item[S"LastName"] = S"Larsen"; drEmplRecord->Item[S"Department"] = S"Accounting"; drEmplRecord->Item[S"EmplStatusID"] = S"1"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"2"; drEmplRecord->Item[S"FirstName"] = S"Paul"; drEmplRecord->Item[S"LastName"] = S"Banack"; drEmplRecord->Item[S"Department"] = S"IT/IM"; drEmplRecord->Item[S"EmplStatusID"] = S"3"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"3"; drEmplRecord->Item[S"FirstName"] = S"Helene"; drEmplRecord->Item[S"LastName"] = S"Cassavoy"; drEmplRecord->Item[S"Department"] = S"Accounting"; drEmplRecord->Item[S"EmplStatusID"] = S"2"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"4"; drEmplRecord->Item[S"FirstName"] = S"Anselme"; drEmplRecord->Item[S"LastName"] = S"Thomas"; drEmplRecord->Item[S"Department"] = S"Public Relations"; drEmplRecord->Item[S"EmplStatusID"] = S"1"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"5"; drEmplRecord->Item[S"FirstName"] = S"Bertha"; drEmplRecord->Item[S"LastName"] = S"Um"; drEmplRecord->Item[S"Department"] = S"Corporate"; drEmplRecord->Item[S"EmplStatusID"] = S"4"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"6"; drEmplRecord->Item[S"FirstName"] = S"Renée"; drEmplRecord->Item[S"LastName"] = S"Bright"; drEmplRecord->Item[S"Department"] = S"IT/IM"; drEmplRecord->Item[S"EmplStatusID"] = S"3"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"7"; drEmplRecord->Item[S"FirstName"] = S"Jeanne"; drEmplRecord->Item[S"LastName"] = S"Tristan"; drEmplRecord->Item[S"Department"] = S"Corporate"; drEmplRecord->Item[S"EmplStatusID"] = S"1"; dtEmployees->Rows->Add(drEmplRecord); drEmplRecord = dtEmployees->NewRow(); drEmplRecord->Item[S"EmployeeID"] = S"8"; drEmplRecord->Item[S"FirstName"] = S"Sandrine"; drEmplRecord->Item[S"LastName"] = S"Holland"; drEmplRecord->Item[S"Department"] = S"Public Relations"; drEmplRecord->Item[S"EmplStatusID"] = S"4"; dtEmployees->Rows->Add(drEmplRecord); DataTable *dtEmplStatus = new DataTable(S"EmploymentStatus"); DataColumn *dcEmployment[] = new DataColumn*[2]; dcEmployment[0] = new DataColumn(S"EmplStatusID", System::Type::GetType(S"System.Int32")); dtEmplStatus->Columns->Add(dcEmployment[0]); dcEmployment[1] = new DataColumn(S"EmplStatus", System::Type::GetType(S"System.String")); dtEmplStatus->Columns->Add(dcEmployment[1]); DataRow *drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatusID"] = S"1"; drEmployment->Item[S"EmplStatus"] = S"Full Time"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatusID"] = S"2"; drEmployment->Item[S"EmplStatus"] = S"Part Time"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatusID"] = S"3"; drEmployment->Item[S"EmplStatus"] = S"Contractor"; dtEmplStatus->Rows->Add(drEmployment); drEmployment = dtEmplStatus->NewRow(); drEmployment->Item[S"EmplStatusID"] = S"4"; drEmployment->Item[S"EmplStatus"] = S"Intern"; dtEmplStatus->Rows->Add(drEmployment); dsEmployment->Tables->Add(dtEmployees); dsEmployment->Tables->Add(dtEmplStatus); DataColumn *colParent = dsEmployment->Tables->Item[S"EmploymentStatus"]->Columns->Item[S"EmplStatusID"]; DataColumn *colChild = dsEmployment->Tables->Item[S"Employees"]->Columns->Item[S"EmplStatusID"]; DataRelation *drEmployeeStatus = new DataRelation(S"EmployeeStatus", colParent, colChild); dsEmployment->Relations->Add(drEmployeeStatus); this->dataGrid1->DataSource = dsEmployment; this->dataGrid1->DataMember = S"EmploymentStatus"; } private: System::Void btnClose_Click(System::Object * sender, System::EventArgs * e) { Close(); }
|
||
Previous | Copyright © 2004-2010 FunctionX, Inc. | |
|