Beginning SQL

Beginning SQL Beginning SQL

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

Chapter 4 If you’re using IBM DB2, you need to use the following code: CREATE TABLE TempLocation ( LocationId integer NOT NULL, Street varchar(100), City varchar(75), State varchar(75) ); INSERT INTO TempLocation SELECT * FROM Location; DROP TABLE Location; CREATE TABLE Location ( LocationId integer NOT NULL, Street varchar(100), City varchar(75), State varchar(75) ); INSERT INTO Location SELECT * FROM TempLocation; DROP TABLE TempLocation; You must add a NOT NULL constraint before any attempt to add a primary key, lest you receive an error message. 3. Next, add the primary key: ALTER TABLE Location ADD CONSTRAINT locationid_pk PRIMARY KEY (LocationId); 4. Finally, add the FOREIGN KEY constraint: ALTER TABLE Attendance ADD CONSTRAINT locationid_fk FOREIGN KEY (LocationId) REFERENCES Location(LocationId); How It Works 138 In the first step, you simply deleted the erroneous record in the Attendance table, the record with a LocationId of 99. Next, you specified which table you want to alter, much as with the other constraints. Before you can create a FOREIGN KEY constraint, however, you need to ensure that the column you specify as the primary key is actually subject to a PRIMARY KEY constraint. However, you can add a PRIMARY KEY constraint to only a column that has a NOT NULL constraint, and the LocationId column has no such constraint. Therefore, the second step added the ADD CONSTRAINT followed by the constraint name and set the primary key. The IBM DB2 syntax is particularly convoluted. It creates a temporary table identical in structure to the Location table. Then data from the existing Location table is copied to this temporary table. After that, the Location table is dropped and then re-created, but with a NOT NULL constraint on the LocationId column, the one to which you want to add a primary key. Finally, the temporary table is dropped.

Chapter 4<br />

If you’re using IBM DB2, you need to use the following code:<br />

CREATE TABLE TempLocation<br />

(<br />

LocationId integer NOT NULL,<br />

Street varchar(100),<br />

City varchar(75),<br />

State varchar(75)<br />

);<br />

INSERT INTO TempLocation SELECT * FROM Location;<br />

DROP TABLE Location;<br />

CREATE TABLE Location<br />

(<br />

LocationId integer NOT NULL,<br />

Street varchar(100),<br />

City varchar(75),<br />

State varchar(75)<br />

);<br />

INSERT INTO Location SELECT * FROM TempLocation;<br />

DROP TABLE TempLocation;<br />

You must add a NOT NULL constraint before any attempt to add a primary key, lest you receive<br />

an error message.<br />

3. Next, add the primary key:<br />

ALTER TABLE Location<br />

ADD CONSTRAINT locationid_pk PRIMARY KEY (LocationId);<br />

4. Finally, add the FOREIGN KEY constraint:<br />

ALTER TABLE Attendance<br />

ADD CONSTRAINT locationid_fk<br />

FOREIGN KEY (LocationId)<br />

REFERENCES Location(LocationId);<br />

How It Works<br />

138<br />

In the first step, you simply deleted the erroneous record in the Attendance table, the record with a<br />

LocationId of 99.<br />

Next, you specified which table you want to alter, much as with the other constraints. Before you can create<br />

a FOREIGN KEY constraint, however, you need to ensure that the column you specify as the primary key is<br />

actually subject to a PRIMARY KEY constraint. However, you can add a PRIMARY KEY constraint to only a<br />

column that has a NOT NULL constraint, and the LocationId column has no such constraint. Therefore, the<br />

second step added the ADD CONSTRAINT followed by the constraint name and set the primary key.<br />

The IBM DB2 syntax is particularly convoluted. It creates a temporary table identical in structure to the<br />

Location table. Then data from the existing Location table is copied to this temporary table. After that,<br />

the Location table is dropped and then re-created, but with a NOT NULL constraint on the LocationId column,<br />

the one to which you want to add a primary key. Finally, the temporary table is dropped.

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

Saved successfully!

Ooh no, something went wrong!