Beginning SQL

Beginning SQL Beginning SQL

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

Chapter 4 128 You can add more than one constraint to a table, so long as each constraint has a different name and is separated by a comma: CREATE TABLE AnotherTable ( Column1 int, Column2 varchar(20), Column3 varchar(12), CONSTRAINT MyUniqueConstraint UNIQUE(Column2, Column3), CONSTRAINT AnotherConstraint UNIQUE(Column1, Column3) ); If you’re using DB2, you would write the following statement: CREATE TABLE AnotherTable ( Column1 int NOT NULL, Column2 varchar(20) NOT NULL, Column3 varchar(12) NOT NULL, CONSTRAINT MyUniqueConstraint UNIQUE(Column2, Column3), CONSTRAINT AnotherConstraint UNIQUE(Column1, Column3) ); You can delete AnotherTable from the database: DROP TABLE AnotherTable; So far you’ve learned how to add a UNIQUE constraint at the time of a table’s creation. However, using the ALTER TABLE statement, you can add and remove a UNIQUE constraint after the table’s creation. Chapter 1 discussed the ALTER TABLE statement in relation to adding and removing columns in a table. To add a constraint, you specify which table to alter and then state that you want to ADD a constraint. The SQL code required to define a constraint with an ALTER TABLE statement is identical to how you create a constraint at table creation. The following code demonstrates how to add a constraint called MyUniqueConstraint to a table called YetAnotherTable: CREATE TABLE YetAnotherTable ( Column1 int, Column2 varchar(20), Column3 varchar(12) ); ALTER TABLE YetAnotherTable ADD CONSTRAINT MyUniqueConstraint UNIQUE(Column2, Column3);

Chapter 4<br />

128<br />

You can add more than one constraint to a table, so long as each constraint has a different name and is<br />

separated by a comma:<br />

CREATE TABLE AnotherTable<br />

(<br />

Column1 int,<br />

Column2 varchar(20),<br />

Column3 varchar(12),<br />

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

CONSTRAINT AnotherConstraint UNIQUE(Column1, Column3)<br />

);<br />

If you’re using DB2, you would write the following statement:<br />

CREATE TABLE AnotherTable<br />

(<br />

Column1 int NOT NULL,<br />

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

Column3 varchar(12) NOT NULL,<br />

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

CONSTRAINT AnotherConstraint UNIQUE(Column1, Column3)<br />

);<br />

You can delete AnotherTable from the database:<br />

DROP TABLE AnotherTable;<br />

So far you’ve learned how to add a UNIQUE constraint at the time of a table’s creation. However, using<br />

the ALTER TABLE statement, you can add and remove a UNIQUE constraint after the table’s creation.<br />

Chapter 1 discussed the ALTER TABLE statement in relation to adding and removing columns in a table.<br />

To add a constraint, you specify which table to alter and then state that you want to ADD a constraint.<br />

The <strong>SQL</strong> code required to define a constraint with an ALTER TABLE statement is identical to how you<br />

create a constraint at table creation.<br />

The following code demonstrates how to add a constraint called MyUniqueConstraint to a table called<br />

YetAnotherTable:<br />

CREATE TABLE YetAnotherTable<br />

(<br />

Column1 int,<br />

Column2 varchar(20),<br />

Column3 varchar(12)<br />

);<br />

ALTER TABLE YetAnotherTable<br />

ADD CONSTRAINT MyUniqueConstraint UNIQUE(Column2, Column3);

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

Saved successfully!

Ooh no, something went wrong!