Beginning SQL

Beginning SQL Beginning SQL

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

Chapter 4 152 SELECT * FROM MemberDetails; DROP TABLE MemberDetails; CREATE TABLE MemberDetails ( MemberId integer NOT NULL, FirstName vargraphic(50) NOT NULL, LastName vargraphic(50) NOT NULL, DateOfBirth date, Street varchar(100), City varchar(75), State varchar(75), ZipCode varchar(12), Email varchar(200), DateOfJoining date ); INSERT INTO MemberDetails SELECT * FROM TempMemberDetails; DROP TABLE TempMemberDetails; Once the NOT NULL constraints are in place, you can add the index again: CREATE UNIQUE INDEX member_name_indx ON MemberDetails (LastName DESC, FirstName); Finally, define the relationships between tables by adding FOREIGN KEY constraints. Figure 4-1 shows the foreign key links, and the following list reiterates them. ❑ The CategoryId column in the Films table is a foreign key linking to the CategoryId column in the Category table. ❑ The CategoryId column in the FavCategory table is a foreign key linking to the CategoryId column in the Category table. ❑ The MemberId column in the FavCategory table is a foreign key linking to the MemberId column in the MemberDetails table. ❑ The LocationId column in the Attendance table is a foreign key linking to the LocationId column in the Location table. ❑ The MemberId column in the Attendance table is a foreign key linking to the MemberId column in the MemberDetails table. In an earlier Try It Out, you defined the relationship between the Attendance and Location tables. The Location table is the primary table to which the Attendance table links via the LocationId column. The Attendance table also links to the MemberDetails table, so you need to define that relationship as well. If you’ve already added the constraint in the earlier Try It Out, then don’t execute the code again or you’ll receive an error. If you didn’t execute the code, then the SQL used in an earlier Try It Out to add the constraint was as follows:

Chapter 4<br />

152<br />

SELECT * FROM MemberDetails;<br />

DROP TABLE MemberDetails;<br />

CREATE TABLE MemberDetails<br />

(<br />

MemberId integer NOT NULL,<br />

FirstName vargraphic(50) NOT NULL,<br />

LastName vargraphic(50) NOT NULL,<br />

DateOfBirth date,<br />

Street varchar(100),<br />

City varchar(75),<br />

State varchar(75),<br />

ZipCode varchar(12),<br />

Email varchar(200),<br />

DateOfJoining date<br />

);<br />

INSERT INTO MemberDetails<br />

SELECT * FROM TempMemberDetails;<br />

DROP TABLE TempMemberDetails;<br />

Once the NOT NULL constraints are in place, you can add the index again:<br />

CREATE UNIQUE INDEX member_name_indx<br />

ON MemberDetails (LastName DESC, FirstName);<br />

Finally, define the relationships between tables by adding FOREIGN KEY constraints. Figure 4-1 shows<br />

the foreign key links, and the following list reiterates them.<br />

❑ The CategoryId column in the Films table is a foreign key linking to the CategoryId column in<br />

the Category table.<br />

❑ The CategoryId column in the FavCategory table is a foreign key linking to the CategoryId column<br />

in the Category table.<br />

❑ The MemberId column in the FavCategory table is a foreign key linking to the MemberId column<br />

in the MemberDetails table.<br />

❑ The LocationId column in the Attendance table is a foreign key linking to the LocationId column<br />

in the Location table.<br />

❑ The MemberId column in the Attendance table is a foreign key linking to the MemberId column<br />

in the MemberDetails table.<br />

In an earlier Try It Out, you defined the relationship between the Attendance and Location tables. The<br />

Location table is the primary table to which the Attendance table links via the LocationId column.<br />

The Attendance table also links to the MemberDetails table, so you need to define that relationship<br />

as well.<br />

If you’ve already added the constraint in the earlier Try It Out, then don’t execute the code again or<br />

you’ll receive an error. If you didn’t execute the code, then the <strong>SQL</strong> used in an earlier Try It Out to add<br />

the constraint was as follows:

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

Saved successfully!

Ooh no, something went wrong!