Beginning SQL
Beginning SQL Beginning SQL
Chapter 2 If you want to delete all the records from a table, you simply leave out the WHERE clause, as shown in the following statement: DELETE FROM MemberDetails; The preceding SQL fragment deletes all the records in the MemberDetails table. Don’t execute it or else you’ll have to enter the records again. If you want to delete some of the records, you use the WHERE clause to specify which ones: DELETE FROM MemberDetails WHERE MemberId = 3; Enter and execute this SQL. This SQL deletes all records from the MemberDetails table where the MemberId column has a value of 3. Because it holds a unique value, only the details of Sandra Tell are deleted — she is the only person whose MemberId is 3. Now that Sandra is gone from the membership, you also need to delete her details from the FavCategory and Attendance tables. Use the following statements to do so: DELETE FROM Attendance WHERE MemberId = 3; DELETE FROM FavCategory WHERE MemberId = 3; Everything that applies to the WHERE clause when used with the UPDATE statement also applies to the DELETE statement. Summary 50 For now, that completes your introduction to adding, updating, and deleting data. This chapter covered the following: ❑ How to add new records to a database using the INSERT INTO statement. To add a new record, you must specify which table the record should go into, which fields you assign values to, and finally the values to be assigned. ❑ Next you looked at how to update data already in the database using the UPDATE statement, which specifies which table’s records receive updates, as well as the fields and new values assigned to each record. Finally, you learned that the WHERE clause specifies which records in the table receive updates. If you don’t use a WHERE clause, all records in the table receive updates. ❑ The final statement covered in this chapter was the DELETE statement, which allows you to delete records from a table. You can either delete all records or use a WHERE clause to specify which records you want to delete. This chapter only touched on the basics; Chapter 3 shows you how to take data from one table and use it to insert data from one table into a second table. Chapter 3 also examines the WHERE clause in greater depth.
- Page 90: Most RDBMSs handle the most common
- Page 94: To add a new column, use the basic
- Page 98: Now that you know what you want to
- Page 102: ❑ Meeting date 2 ❑ Location 2
- Page 106: Generally speaking, picking a data
- Page 110: example, the MemberId field links t
- Page 114: Depending on which RDBMS you’re u
- Page 118: The FilmCategory table is very smal
- Page 122: 2 Entering Information The last cha
- Page 126: like this: INSERT INTO Category VAL
- Page 130: Inserting Data into the Case Study
- Page 134: The WHERE Clause So far you’ve se
- Page 138: Try It Out Using Logical Operators
- Page 146: 3 Extracting Information So far you
- Page 150: Returning Only Distinct Rows If you
- Page 154: The good news is that everything yo
- Page 158: MemberDetails (MemberId, FirstName,
- Page 162: MemberDetails table, the columns Fi
- Page 166: State DateOfJoining New State 2005-
- Page 170: It simply tells the database system
- Page 174: The BETWEEN operator functions exac
- Page 178: The final condition required is a c
- Page 182: In some database systems, the LIKE
- Page 186: The second query is a negative, in
Chapter 2<br />
If you want to delete all the records from a table, you simply leave out the WHERE clause, as shown in the<br />
following statement:<br />
DELETE FROM MemberDetails;<br />
The preceding <strong>SQL</strong> fragment deletes all the records in the MemberDetails table. Don’t execute it or else<br />
you’ll have to enter the records again. If you want to delete some of the records, you use the WHERE<br />
clause to specify which ones:<br />
DELETE FROM MemberDetails WHERE MemberId = 3;<br />
Enter and execute this <strong>SQL</strong>. This <strong>SQL</strong> deletes all records from the MemberDetails table where the<br />
MemberId column has a value of 3. Because it holds a unique value, only the details of Sandra Tell are<br />
deleted — she is the only person whose MemberId is 3. Now that Sandra is gone from the membership,<br />
you also need to delete her details from the FavCategory and Attendance tables. Use the following statements<br />
to do so:<br />
DELETE FROM Attendance WHERE MemberId = 3;<br />
DELETE FROM FavCategory WHERE MemberId = 3;<br />
Everything that applies to the WHERE clause when used with the UPDATE statement also applies to the<br />
DELETE statement.<br />
Summary<br />
50<br />
For now, that completes your introduction to adding, updating, and deleting data. This chapter covered<br />
the following:<br />
❑ How to add new records to a database using the INSERT INTO statement. To add a new record,<br />
you must specify which table the record should go into, which fields you assign values to, and<br />
finally the values to be assigned.<br />
❑ Next you looked at how to update data already in the database using the UPDATE statement,<br />
which specifies which table’s records receive updates, as well as the fields and new values<br />
assigned to each record. Finally, you learned that the WHERE clause specifies which records in the<br />
table receive updates. If you don’t use a WHERE clause, all records in the table receive updates.<br />
❑ The final statement covered in this chapter was the DELETE statement, which allows you to<br />
delete records from a table. You can either delete all records or use a WHERE clause to specify<br />
which records you want to delete.<br />
This chapter only touched on the basics; Chapter 3 shows you how to take data from one table and use it<br />
to insert data from one table into a second table. Chapter 3 also examines the WHERE clause in greater<br />
depth.