Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Removing Rows From a Table You use the DELETE statement to remove rows from a table. When removing a row, you specify the name of the table and the rows to delete using a WHERE clause | Removing Rows From a Table You use the DELETE statement to remove rows from a table. When removing a row you specify the name of the table and the rows to delete using a WHERE clause. Warning If you omit the WHERE clause in a DELETE statement all rows from the table will be deleted. Make sure you provide a WHERE clause if you don t want to remove all the rows from the table. Typically you ll specify the value for the primary key in your WHERE clause. The following DELETE statement removes the row from the Customers table where the CustomerID is CRCOM DELETE FROM Customers WHERE CustomerID CRCOM Figure 3.24 shows this DELETE statement along with a SELECT statement that demonstrates that the row has been removed. Figure 3.24 Using an UPDATE statement to remove a row from the Customers table In the next section you ll learn how the database software maintains the integrity of the information stored in the database. Maintaining Database Integrity The database software ensures that the information stored in the tables is consistent. In technical terms it maintains the integrity of the information. Two examples of this are the following The primary key of a row always contains a unique value. The foreign key of a row in the child table always references a value that exists in the parent table. Let s take a look at what happens when you try to insert a row into a table with a primary key that already exists. The following INSERT statement attempts to add a row to the Customers table with a CustomerlD of ALFKI a row with this primary key already exists in the Customers table INSERT INTO Customers CustomerlD CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax VALUES ALFKI Jason Price Company Jason Price Owner 1 Main Street New York NULL 12345 USA 800 -555-1212 NULL If you attempt to run this INSERT statement you ll get the following error message from the database Violation of PRIMARY KEY constraint PK_Customers . Cannot insert duplicate .