Beginning SQL

Beginning SQL Beginning SQL

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

Chapter 4 144 The results of the query are shown in the following table: MeetingDate MemberAttended MemberId LocationId 2004-01-01 Y 1 2 2004-01-01 N 4 2 2004-01-01 Y 5 2 2004-01-01 Y 6 2 2004-03-01 Y 1 1 2004-03-01 Y 4 1 2004-03-01 N 5 1 2004-03-01 N 6 1 For each meeting, the Attendance table stores each film club member and their attendance status. Rather than store a record regardless of whether a member attended or not, you can save space by storing a record only if a member actually attends a meeting. Getting a list of members who attended a meeting is simple enough. The SQL to find out who didn’t attend, however, is a little more involved and is covered in greater detail in Chapter 7. To make the changes, delete all the records where the member didn’t attend, that is, where MemberAttended equals N. Then delete the MemberAttended column. Use this SQL to delete all the records where MemberAttended equals N: DELETE FROM Attendance WHERE MemberAttended = ‘N’; Once you delete those records, you can drop the whole column: ALTER TABLE Attendance DROP COLUMN MemberAttended; Dropping the column deletes all the data stored in that column for all rows. Unfortunately, IBM’s DB2 doesn’t support dropping a column. The only option, as you saw earlier, is to delete the whole table and re-create it from scratch, having first saved the data: CREATE TABLE TempAttendance ( MeetingDate date, LocationId integer, MemberId integer ); INSERT INTO TempAttendance (MeetingDate, LocationId, MemberId) SELECT MeetingDate, LocationId, MemberId FROM Attendance

Chapter 4<br />

144<br />

The results of the query are shown in the following table:<br />

MeetingDate MemberAttended MemberId LocationId<br />

2004-01-01 Y 1 2<br />

2004-01-01 N 4 2<br />

2004-01-01 Y 5 2<br />

2004-01-01 Y 6 2<br />

2004-03-01 Y 1 1<br />

2004-03-01 Y 4 1<br />

2004-03-01 N 5 1<br />

2004-03-01 N 6 1<br />

For each meeting, the Attendance table stores each film club member and their attendance status. Rather<br />

than store a record regardless of whether a member attended or not, you can save space by storing a<br />

record only if a member actually attends a meeting. Getting a list of members who attended a meeting is<br />

simple enough. The <strong>SQL</strong> to find out who didn’t attend, however, is a little more involved and is covered<br />

in greater detail in Chapter 7.<br />

To make the changes, delete all the records where the member didn’t attend, that is, where<br />

MemberAttended equals N. Then delete the MemberAttended column.<br />

Use this <strong>SQL</strong> to delete all the records where MemberAttended equals N:<br />

DELETE FROM Attendance<br />

WHERE MemberAttended = ‘N’;<br />

Once you delete those records, you can drop the whole column:<br />

ALTER TABLE Attendance<br />

DROP COLUMN MemberAttended;<br />

Dropping the column deletes all the data stored in that column for all rows. Unfortunately, IBM’s DB2<br />

doesn’t support dropping a column. The only option, as you saw earlier, is to delete the whole table and<br />

re-create it from scratch, having first saved the data:<br />

CREATE TABLE TempAttendance<br />

(<br />

MeetingDate date,<br />

LocationId integer,<br />

MemberId integer<br />

);<br />

INSERT INTO TempAttendance (MeetingDate, LocationId, MemberId)<br />

SELECT MeetingDate, LocationId, MemberId FROM Attendance

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

Saved successfully!

Ooh no, something went wrong!