Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

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.

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.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!