The DDL command to update a record is UPDATE. The basic formula to use is: UPDATE TableName SET ColumnName Operator Expression You must specify the name of the involved table as the TableName factor of our formula. The SET statement allows you to specify a new value, Expression, for the field under the ColumnName column. With this formula, you must specify the name of the involved table as the TableName factor of our formula. The SET statement allows you to specify a new value, Expression, for the field under the ColumnName column. Imagine that, at one time, on a particular table, all records need to receive a new value under one particular column or certain columns. To update a record, the SQL provides the UPDATE keyword that is used to specify the table on which you want to maintain the record(s). The basic formula to use is: UPDATE TableName SET ColumnName = Expression Here is an example:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
Button btnShowRecords;
Button btnUpdateVideos;
DataGridView dgvVideos;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnShowRecords = new Button();
btnShowRecords.Text = "Show Videos";
btnShowRecords.Width = 120;
btnShowRecords.Location = new Point(12, 12);
btnShowRecords.Click += new EventHandler(btnShowRecordsClick);
btnUpdateVideos = new Button();
btnUpdateVideos.Text = "Update Videos";
btnUpdateVideos.Location = new Point(150, 12);
btnUpdateVideos.Width = 100;
btnUpdateVideos.Click += new EventHandler(btnUpdateVideosClick);
dgvVideos = new DataGridView();
dgvVideos.Location = new Point(12, 46);
Text = "Video Collection";
Controls.Add(btnUpdateVideos);
Controls.Add(btnShowRecords);
Controls.Add(dgvVideos);
StartPosition = FormStartPosition.CenterScreen;
dgvVideos.Width = this.Width - 30;
dgvVideos.Height = this.Height - 80;
dgvVideos.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
}
private void btnShowRecordsClick(object sender, EventArgs e)
{
using (SqlConnection cntVideos = new SqlConnection("Data Source=(local);" +
"Database='VideoCollection1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdVideos =
new SqlCommand("SELECT ALL * FROM Collection.Videos;",
cntVideos);
cntVideos.Open();
cmdVideos.ExecuteNonQuery();
SqlDataAdapter sdaVideos = new SqlDataAdapter(cmdVideos);
BindingSource bsVideos = new BindingSource();
DataSet dsVideos = new DataSet("VideosSet");
sdaVideos.Fill(dsVideos);
bsVideos.DataSource = dsVideos.Tables[0];
dgvVideos.DataSource = bsVideos;
}
}
void btnUpdateVideosClick(object sender, EventArgs e)
{
using (SqlConnection cntVideos =
new SqlConnection("Data Source=(local);" +
"Database='VideoCollection1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdVideos =
new SqlCommand("UPDATE Collection.Videos SET Rating = N'R';",
cntVideos);
cntVideos.Open();
cmdVideos.ExecuteNonQuery();
MessageBox.Show("All video records have been rated R.",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
public class Program
{
[STAThread]
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
With this code, all records of the Videos table will have their Rating fields set to a value of R:
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The SQL allows you change all records of a table with a single line of code. The formula to follow is: UPDATE TableName SET ColumnName = Expression The WHERE operator allows you to specify how the particular record involved would be identified. It is very important, in most cases, that the criterion used be able to uniquely identify the record. In the above table, imagine that you ask the interpreter to change the released year to 1996 where the director of the video is Rob Reiner. The UPDATE statement would be written as follows: UPDATE Videos SET YearReleased = 1996 WHERE Director = 'Rob Reiner';
Editing a record consists of changing a value in a field. It could be that the field is empty, such as the © Year of the the 'The Lady Killers' video of the following table. It could be that the value is wrong, such as the Director of the the 'The Distinguished Gentleman' video of this table:
To visually edit a record, first open the table to view its records. Locate the record, the column on which you want to work, and locate the value you want to change, then change it. In SQL, you must provide a way for the interpreter to locate the record. To do this, you would associate the WHERE operator in an UPDATE statement using the following formula: UPDATE TableName SET ColumnName = Expression WHERE Condition(s) The WHERE operator allows you to specify how the particular record involved would be identified. It is very important, in most cases, that the criterion used be able to uniquely identify the record. In the above table, imagine that you ask the interpreter to change the released year to 1996 where the director of the video is Rob Reiner. The UPDATE statement would be written as follows: UPDATE Videos SET YearReleased = 1996 WHERE Director = 'Rob Reiner'; In the above table, there are at least two videos directed by Rob Reiner. When this statement is executed, all video records whose director is Rob Reiner would be changed, which would compromise existing records that did not need this change. Therefore, make sure your WHERE statement would isolate one particular record or only those that need to be updated. Here is an example used to change the name of the director of a particular video: void btnChangeVideoDirectorClick(object sender, EventArgs e)
{
using (SqlConnection cntVideos =
new SqlConnection("Data Source=(local);" +
"Database='VideoCollection1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdVideos =
new SqlCommand("UPDATE Videos " +
"SET Director = 'Jonathan Lynn' " +
"WHERE [Video Title] = N'The Distinguished Gentleman';",
cntVideos);
cntVideos.Open();
cmdVideos.ExecuteNonQuery();
MessageBox.Show("The director of 'The Distinguished Gentleman' " +
"video has been updated.",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
If you think all records of a particular table are, or have become, useless, you can clear the whole table, which would still keep its structure. To visually delete all records from a table, open it in design view, first select all of them, and press Delete. You would receive a warning. If you still want to delete the records, click Yes. If you change your mind, click No. Using SQL, to clear a table of all records, use the DELETE operator with the following formula: DELETE TableName; When this statement is executed, all records from the TableName factor would be removed from the table. Be careful when doing this because once the records have been deleted, you cannot get them back.
Instead of removing all records, to delete only the first n of a table, use the following formula: DELETE TOP (Number) TableName;
In the parentheses, enter the desired number of records. When the statement executes, the first n records of the table would be deleted. Here is an example: DELETE TOP (2) Employees; GO This statement asks the database engine to delete the first two records of the Employees table.
If you find out that a record is not necessary, not anymore, or is misplaced, you can remove it from a table. To visually remove a record from a table, open the table in Table view, right-click the gray box of the record and click Delete. You can also first select the record and press Delete. You would receive a warning to confirm your intention. To programmatically delete a record:
In SQL, to delete a record, use the DELETE FROM statement associated with the WHERE operator. The formula to follow is: DELETE FROM TableName WHERE Condition(s) The TableName factor is used to identify a table whose record(s) would be removed. The Condition(s) factor allows you to identify a record or a group of records that carries a criterion. Once again, make sure you are precise in your criteria so you would not delete the wrong record(s). Here is an example used to remove a particular record from the table: using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
Button btnShowRecords;
Button btnDeleteVideo;
DataGridView dgvVideos;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnShowRecords = new Button();
btnShowRecords.Text = "Show Videos";
btnShowRecords.Width = 120;
btnShowRecords.Location = new Point(12, 12);
btnShowRecords.Click += new EventHandler(btnShowRecordsClick);
btnDeleteVideo = new Button();
btnDeleteVideo.Text = "Delete Video";
btnDeleteVideo.Location = new Point(150, 12);
btnDeleteVideo.Width = 100;
btnDeleteVideo.Click += new EventHandler(btnDeleteVideoClick);
dgvVideos = new DataGridView();
dgvVideos.Location = new Point(12, 46);
Text = "Video Collection";
Controls.Add(btnDeleteVideo);
Controls.Add(btnShowRecords);
Controls.Add(dgvVideos);
StartPosition = FormStartPosition.CenterScreen;
dgvVideos.Width = this.Width - 30;
dgvVideos.Height = this.Height - 80;
dgvVideos.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
}
private void btnShowRecordsClick(object sender, EventArgs e)
{
using (SqlConnection cntVideos = new SqlConnection("Data Source=(local);" +
"Database='VideoCollection1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdVideos =
new SqlCommand("SELECT ALL * FROM Collection.Videos;",
cntVideos);
cntVideos.Open();
cmdVideos.ExecuteNonQuery();
SqlDataAdapter sdaVideos = new SqlDataAdapter(cmdVideos);
BindingSource bsVideos = new BindingSource();
DataSet dsVideos = new DataSet("VideosSet");
sdaVideos.Fill(dsVideos);
bsVideos.DataSource = dsVideos.Tables[0];
dgvVideos.DataSource = bsVideos;
}
}
void btnDeleteVideoClick(object sender, EventArgs e)
{
using (SqlConnection cntVideos =
new SqlConnection("Data Source=(local);" +
"Database='VideoCollection1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdVideos =
new SqlCommand("DELETE FROM Collection.Videos " +
"WHERE Title = N'The Lady Killers';",
cntVideos);
cntVideos.Open();
cmdVideos.ExecuteNonQuery();
MessageBox.Show("The video title The Lady Killers has been deleted.",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
public class Program
{
[STAThread]
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
Consider the following table: CREATE TABLE Employees
(
EmployeeNumber int,
FirstName nvarchar(20),
LastName nvarchar(20),
HourlySalary money
);
GO
INSERT INTO Employees
VALUES(283007, N'Megan', N'Wright', 8.50),
(480295, N'Horace', N'Taylor', 20.25),
(804805, N'Marc', N'Knights', 10.85),
(294117, N'Bryan', N'Tenant', 30.25),
(837479, N'Paul', N'Motto', 18.02),
(280304, N'Joyce', N'Holliday', 11.66),
(924802, N'Peter', N'Mukoko', 8.68),
(725381, N'Marianne', N'Brooks', 22.64);
GO
By default, the DELETE expression acts on all records of a table. As an alternative, you can ask the database engine to consider only the first n records of a table. The formula to do this is: DELETE TOP (Number) FROM TableName
WHERE Condition(s)
In the parentheses after TOP, enter the desired number of records. When the statement executes, the WHERE condition would be applied on only the first Number of records. Any record that falls in that condition would be deleted. Here is an example: DELETE TOP (4) FROM Employees WHERE HourlySalary < 12.50; GO This code asks the database engine to delete any record in the first four records of the Employees table if that hourly salary of the employee in less than 12.50.
If you don't want to specify a fixed number of records, you can use a percentage instead. The formula to follow is: DELETE TOP (Number) PERCENT FROM TableName
WHERE Condition(s)
In the parentheses, enter a number between 0.00 and 100.00 included. The number of records to consider is based on the total number of the records using a percentage. Here is an example: DELETE TOP (40) PERCENT FROM Employees WHERE HourlySalary < 12.50; GO This code delete any record whose salary is less than 12.50 but the record must be among the first 40% of the records. When some record(s) has(have) been deleted, the operation is performed behind the scenes and you don't see the result. If you want to see a list of the records that were deleted, you can use the OUTPUT operator to display the result. To show the list of the records from a table that was completely emptied, you can use the following formula: DELETE FROM TableName OUTPUT DELETED.Columns The OUTPUT INSERTED expression follows the description we have seen for the record update. Here is an example: USE VideoCollection6; GO DELETE FROM Videos OUTPUT deleted.* GO To show the list of the records that were deleted based on a condition, use the following formula: DELETE FROM TableName OUTPUT DELETED.Columns WHERE Condition(s) Here is an example: USE VideoCollection6; GO DELETE FROM Videos OUTPUT deleted.* WHERE YearReleased IS NULL; GO
Instead of one, you can delete more than one record at a time. To programmatically delete a group or records, apply the DELETE FROM table formula and use a WHERE condition that can identify each one of the records. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|