Home

Record Maintenance

 

Introduction

When a table contains records, you can view and manipulate them as you see fit. While examining a table, you may come to a record whose values are not appropriate. Record maintenance consists of editing or deleting values of records of a table.

Editing a Record

To edit a value using SQL, you start with the UPDATE keyword and follow this formula:

UPDATE TableName
SET ColumnName = NewValue

In our formula, the TableName factor must be the name of a table in the selected or current database. The ColumnName must be a valid column in the TableName table. The NewValue factor of our formula is the new value that will replace the old one. If you use this formula, all records of the ColumnName would be given the NewValue. Here is an example:

System::Void btnMaintenance_Click(System::Object^  sender, System::EventArgs^  e)
{
	 ADODB::ConnectionClass ^ conVideos = gcnew  ADODB::ConnectionClass;
            Object ^ obj = gcnew Object;

            conVideos->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;"
                            L"Data Source='C:\\Programs\\VideoCollection.mdb'", 
                            L"", "", 0);
            conVideos->Execute(L"UPDATE Videos SET Rating = 'R';", obj, 0);
	MessageBox::Show(L"The rating of each record has been set to R.");
        conVideos->Close();
}

In some cases, you may want to change only one or more particular values. To do this, you must specify a condition that would be used to identify a record. The formula to use is:

UPDATE TableName
SET ColumnName = NewValue
WHERE Condition

The Condition factor specifies how the value will be located. Consider the following table of records:

Video ID Shelf # Title Director © Year Rating
1 CM-8842 Naked Gun 33 1/3 The Final Insult Peter Segal 1994 PG-13
2 DA-4093 Indecent Proposal Adryan Line 1993 R
3 DG-9048 The Bone Collector Phillip Noyce 1999  
4 DS-3808 Just Cause   1999 R
5 ML-4793 Live Baby Live Arne Glimcher 1991  
6 DP-3838 Fatal Attraction Adrian Lynn   R
7 DA-4092 Clear And Present Danger Philippe Noise 1994 PG-13
8 DG-9048 The Fisher King   1991  
9 DL-7480 The Firm   1993  

From this table, imagine that you want to replace Adryan Line with Adrian Lynn. The TableName is Videos. The column that holds the value is named Director. The NewValue is Peter Segal. The Condition can be that the shelf number is DA-4093. Based on this, to edit the value, you can use the following code:

UPDATE 	Videos
SET 	Director = "Adrian Lynn"
WHERE 	ShelfNumber = "DA-4093"

This can be done in code as follows:

System::Void btnMaintenance_Click(System::Object^  sender, System::EventArgs^  e)
{
	 ADODB::ConnectionClass ^ conVideos = gcnew  ADODB::ConnectionClass;
            Object ^ obj = gcnew Object;

            conVideos->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;"
                            L"Data Source='C:\\Programs\\VideoCollection.mdb'", 
                            L"", "", 0);
            conVideos->Execute(L"UPDATE Videos "
                               L"SET Director = 'Adrian Lynn' " 
                               L"WHERE ShelfNumber = 'DA-4093';", obj, 0);
        conVideos->Close();
}

In the same way, in the list of videos, notice that the videos titled "The Bone Collector" and "Clear And Present Danger" have a name of director that looks the same. After checking the videos, you find out that they were actually directed by the same person. So you decide to programmatically edit it. You can write the SQL statement as follows:

UPDATE 	Videos
SET 	Director    = "Phillip Noyce"
WHERE 	ShelfNumber = "DG-9048"

Before executing the statement, you check the table again but find out that, either by mistake or something else, another video is using the same shelf number. This means that if you execute the statement, any video that uses the same shelf number would have its director replaced, including those videos that don't have the same error. The solution to this is to make sure that you always have a way to distinguish each video from another. This is why a column with a COUNTER data type can be highly helpful because each record would have a unique value in this column. Using this feature, the problem would be solved as follows:

UPDATE 	Videos
SET 	Director = "Phillip Noyce"
WHERE 	VideoID  = 3

This would be done programmatically as follows:

System::Void btnMaintenance_Click(System::Object^  sender, System::EventArgs^  e)
{
	 ADODB::ConnectionClass ^ conVideos = gcnew  ADODB::ConnectionClass;
            Object ^ obj = gcnew Object;

            conVideos->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;"
                            L"Data Source='C:\\Programs\\VideoCollection.mdb'", 
                            L"", "", 0);
            conVideos->Execute(L"UPDATE 	Videos " 
                               L"SET Director = 'Phillip Noyce' "
                               L"WHERE 	VideoID  = 3;", obj, 0);
        conVideos->Close();
}

This time, because we know that only one video has a VideoID equal to 3, only its record will be changed.

 

Deleting a Record

Deleting a record consists of removing it from a table (or a form). To delete a record, you combine the DELETE operator in the following primary formula:

DELETE FROM TableName

When this statement is executed, all records from the TableName table would be removed. Here is an example:

System::Void btnMaintenance_Click(System::Object^  sender, System::EventArgs^  e)
{
	 ADODB::ConnectionClass ^ conVideos = gcnew  ADODB::ConnectionClass;
            Object ^ obj = gcnew Object;

            conVideos->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;"
                            L"Data Source='C:\\Programs\\VideoCollection.mdb'", 
                            L"", "", 0);
            conVideos->Execute(L"DELETE FROM Videos;", obj, 0);
            conVideos->Close();
}

In this case, all records from a table named Videos in the current database would be deleted. An alternative to the above formula is:

DELETE *
FROM TableName

In this formula, you use the * operator as the column placeholder. You can replace it with one or more names of columns but it doesn't matter because the DELETE operator signifies that the whole record will be deleted, regardless of the column name.

The TableName must be a valid name of a table in the specified or the current database. Here is an example:

System::Void btnMaintenance_Click(System::Object^  sender, System::EventArgs^  e)
{
	 ADODB::ConnectionClass ^ conVideos = gcnew  ADODB::ConnectionClass;
            Object ^ obj = gcnew Object;

            conVideos->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;"
                            L"Data Source='C:\\Programs\\VideoCollection.mdb'", 
                            L"", "", 0);
            conVideos->Execute(L"DELETE * FROM Videos;", obj, 0);
            conVideos->Close();
}

If you execute this type of statement, all records from the table would be deleted. You can specify what record to remove from a table. To do this, use the following formula of the DELETE operator:

DELETE *
FROM TableName
WHERE Condition

This time, the Condition factor allows you to set the condition that would be applied to locate the record. Here is an example of specifying a condition to delete a record:

System::Void btnMaintenance_Click(System::Object^  sender, System::EventArgs^  e)
{
	 ADODB::ConnectionClass ^ conVideos = gcnew  ADODB::ConnectionClass;
            Object ^ obj = gcnew Object;

            conVideos->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;"
                            L"Data Source='C:\\Programs\\VideoCollection.mdb'", 
                            L"", "", 0);
            conVideos->Execute(L"DELETE * FROM Videos "
                               L"WHERE Director = 'Phillip Noyce';", obj, 0);
            conVideos->Close();
}

When this code runs, all videos directed by Phillip Noyce would be deleted from the table. Instead of deleting all records like this, you may want to remove only one particular video. To do this, you must set a condition that sets that record apart. Once again, the condition can be easily handled by the COUNTER column.

 

Home Copyright © 2005-2016, FunctionX, Inc.