Beginning SQL

Beginning SQL Beginning SQL

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

IBM’s DB2 doesn’t allow you to alter a column so that it doesn’t allow NULLs. You can either delete the table and redefine it, this time making sure the column has a NOT NULL constraint, or add a CHECK constraint. The CHECK constraint is covered later in this chapter, but to add a NOT NULL CHECK constraint in DB2, the syntax is as follows: ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (column_name IS NOT NULL) So, to add a NOT NULL CHECK constraint for Column2 of MyTable, you would write the following code: ALTER TABLE MyTable ADD CONSTRAINT Column2_NotNull CHECK (Column2 IS NOT NULL); With the NOT NULL constraint added to the database systems, test it out by executing the following SQL: INSERT INTO MyTable(Column1,Column3) VALUES (123,’ABC’); You should get an error message similar to Cannot insert the value NULL into column ‘Column2’. The new record isn’t added to the table. Consider this final issue. What if the column in your table that you want to make subject to a NOT NULL constraint already contains NULL records? You can easily deal with that by using a bit of SQL, (like that shown in the following example) to update all those records containing NULLs and set them to whatever default value you think best: UPDATE MyTable SET Column2 = ‘’ WHERE Column2 IS NULL; You must do this prior to adding a NOT NULL clause in order to avoid error messages. If you ran the examples against the Film Club database, delete MyTable from the database, as it’s not needed: DROP TABLE MyTable; UNIQUE Constraint Advanced Database Design The UNIQUE constraint prevents two records from having identical values in a particular column. In the MemberDetails table, for example, you might want to prevent two or more people from having identical email addresses. You can apply the UNIQUE constraint in two ways: add it when creating the table, or add it after creating the table. 125

IBM’s DB2 doesn’t allow you to alter a column so that it doesn’t allow NULLs. You can either delete the<br />

table and redefine it, this time making sure the column has a NOT NULL constraint, or add a CHECK constraint.<br />

The CHECK constraint is covered later in this chapter, but to add a NOT NULL CHECK constraint in<br />

DB2, the syntax is as follows:<br />

ALTER TABLE table_name<br />

ADD CONSTRAINT constraint_name<br />

CHECK (column_name IS NOT NULL)<br />

So, to add a NOT NULL CHECK constraint for Column2 of MyTable, you would write the following code:<br />

ALTER TABLE MyTable<br />

ADD CONSTRAINT Column2_NotNull<br />

CHECK (Column2 IS NOT NULL);<br />

With the NOT NULL constraint added to the database systems, test it out by executing the following <strong>SQL</strong>:<br />

INSERT INTO MyTable(Column1,Column3)<br />

VALUES (123,’ABC’);<br />

You should get an error message similar to Cannot insert the value NULL into column<br />

‘Column2’. The new record isn’t added to the table.<br />

Consider this final issue. What if the column in your table that you want to make subject to a NOT NULL<br />

constraint already contains NULL records? You can easily deal with that by using a bit of <strong>SQL</strong>, (like that<br />

shown in the following example) to update all those records containing NULLs and set them to whatever<br />

default value you think best:<br />

UPDATE MyTable<br />

SET Column2 = ‘’<br />

WHERE Column2 IS NULL;<br />

You must do this prior to adding a NOT NULL clause in order to avoid error messages.<br />

If you ran the examples against the Film Club database, delete MyTable from the database, as it’s not<br />

needed:<br />

DROP TABLE MyTable;<br />

UNIQUE Constraint<br />

Advanced Database Design<br />

The UNIQUE constraint prevents two records from having identical values in a particular column. In the<br />

MemberDetails table, for example, you might want to prevent two or more people from having identical<br />

email addresses.<br />

You can apply the UNIQUE constraint in two ways: add it when creating the table, or add it after creating<br />

the table.<br />

125

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

Saved successfully!

Ooh no, something went wrong!