Data AccessCore JavaApp FrameworksViewTestingBuildingDeploymentDev ToolsSecurityOpinions

Tuesday, April 28, 2009

ALTER TABLE SQL - ADD COLUMN and Foreign Key Constraint

In this short tutorial, we will alter a table in a MySQL database by adding a column to the table along with a new foreign key constraint.

First we will alter the table user and add a column called zipcodeId, which is an integer.

ALTER TABLE user ADD COLUMN zipcodeId INT


The zipcodeId field references a field in another table called zipcode. Next, we'll define the foreign key constraint.

ALTER TABLE User ADD CONSTRAINT FK_User_1 FOREIGN KEY (zipcodeId) REFERENCES Zipcode (id) 


That was quick and easy. Note, all of this information is readily available in the MySQL online documentation at: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

No comments:

Post a Comment