Beginning SQL

Beginning SQL Beginning SQL

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

If you’re using DB2, the code is as follows: CREATE TABLE YetAnotherTable ( Column1 int, Column2 varchar(20) NOT NULL, Column3 varchar(12) NOT NULL ); ALTER TABLE YetAnotherTable ADD CONSTRAINT MyUniqueConstraint UNIQUE(Column2, Column3); This constraint is identical to the one created earlier for AnotherTable. Again, you can add more than one constraint to a table, just as you did with the constraint definition when you defined the table. Use ALTER TABLE to delete the constraint, and simply drop it as shown below, unless you’re using MySQL: ALTER TABLE YetAnotherTable DROP CONSTRAINT MyUniqueConstraint; If you’re using MySQL, the code is as follows: ALTER TABLE YetAnotherTable DROP INDEX MyUniqueConstraint; You can also use the preceding code to drop constraints created at the time of a table’s creation, as long as the constraint has a name. The table YetAnotherTable can be deleted: DROP TABLE YetAnotherTable; CHECK Constraint Advanced Database Design The CHECK constraint enables a condition to check the value being entered into a record. If the condition evaluates to false, the record violates the constraint and isn’t entered into the table. For example, allowing a column storing an age to contain a negative value doesn’t make sense. After all, how many people do you know who are minus 35 years old? The CHECK condition can be any valid SQL condition, similar to those found in a WHERE clause, although SQL can check only the table into which you insert a record. Note that although MS Access supports the CHECK constraint, its implementation is outside the scope of this book, and the code examples can’t be executed from the main MS Access query designer. You can add a CHECK constraint either at the time of a table’s creation or when you alter a table. The following Try It Out shows you how to use the CHECK constraint. 129

If you’re using DB2, the code is as follows:<br />

CREATE TABLE YetAnotherTable<br />

(<br />

Column1 int,<br />

Column2 varchar(20) NOT NULL,<br />

Column3 varchar(12) NOT NULL<br />

);<br />

ALTER TABLE YetAnotherTable<br />

ADD CONSTRAINT MyUniqueConstraint UNIQUE(Column2, Column3);<br />

This constraint is identical to the one created earlier for AnotherTable. Again, you can add more than one<br />

constraint to a table, just as you did with the constraint definition when you defined the table.<br />

Use ALTER TABLE to delete the constraint, and simply drop it as shown below, unless you’re using<br />

My<strong>SQL</strong>:<br />

ALTER TABLE YetAnotherTable<br />

DROP CONSTRAINT MyUniqueConstraint;<br />

If you’re using My<strong>SQL</strong>, the code is as follows:<br />

ALTER TABLE YetAnotherTable<br />

DROP INDEX MyUniqueConstraint;<br />

You can also use the preceding code to drop constraints created at the time of a table’s creation, as long<br />

as the constraint has a name.<br />

The table YetAnotherTable can be deleted:<br />

DROP TABLE YetAnotherTable;<br />

CHECK Constraint<br />

Advanced Database Design<br />

The CHECK constraint enables a condition to check the value being entered into a record. If the condition<br />

evaluates to false, the record violates the constraint and isn’t entered into the table. For example,<br />

allowing a column storing an age to contain a negative value doesn’t make sense. After all, how many<br />

people do you know who are minus 35 years old? The CHECK condition can be any valid <strong>SQL</strong> condition,<br />

similar to those found in a WHERE clause, although <strong>SQL</strong> can check only the table into which you insert a<br />

record. Note that although MS Access supports the CHECK constraint, its implementation is outside the<br />

scope of this book, and the code examples can’t be executed from the main MS Access query designer.<br />

You can add a CHECK constraint either at the time of a table’s creation or when you alter a table. The following<br />

Try It Out shows you how to use the CHECK constraint.<br />

129

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

Saved successfully!

Ooh no, something went wrong!