Microsoft SQL Server
Chapter 5 - DDL (Data Definition Language)
ALTER TABLE
The ALTER TABLE statement allows you to make changes to the structure of an existing table. This can include adding new columns, modifying existing columns, or dropping columns. It can also be used to add or remove constraints. Below are examples and explanations for common modifications.
Key Concepts
1. Adding a New Column
- Use
ALTER TABLE ... ADDto add a new column to an existing table.
2. Modifying an Existing Column
- Use
ALTER TABLE ... ALTER COLUMNto change the data type or properties of an existing column.
3. Dropping a Column
- Use
ALTER TABLE ... DROP COLUMNto remove a column from the table.
4. Adding Constraints
- You can add new constraints like
PRIMARY KEY, FOREIGN KEY, CHECK, or UNIQUE.
5. Dropping Constraints
- Use
ALTER TABLE ... DROP CONSTRAINTto remove constraints from the table.
Code Samples
1. Adding a New Column
- Add a
LastUpdatedcolumn to theStoretable.
ALTER TABLE Store ADD LastUpdated DATETIME;
- Add a
Descriptioncolumn to theProductstable.
ALTER TABLE Products ADD Description VARCHAR(255);
2. Modifying an Existing Column
- Change the
Locationcolumn in theStoretable to accept a maximum of 200 characters.
ALTER TABLE Store ALTER COLUMN Location VARCHAR(200);
- Modify the
Pricecolumn in theProductstable to allow up to 15 digits with 4 decimal places.
ALTER TABLE Products ALTER COLUMN Price DECIMAL(15, 4);
3. Dropping a Column
- Remove the
PhoneNumbercolumn from theCustomertable.
ALTER TABLE Customer DROP COLUMN PhoneNumber;
- Remove the Description column from the Products table.
ALTER TABLE Products DROP COLUMN Description;
4. Adding Constraints
- Add a
CHECKconstraint to theStockQuantitycolumn in theProductstable to ensure it is always positive.
ALTER TABLE Products ADD CONSTRAINT chk_StockQuantity CHECK (StockQuantity >= 0);
- Add a
FOREIGN KEYconstraint to theSalestable to reference theStoreIDin theStoretable.
ALTER TABLE Sales ADD CONSTRAINT fk_Store FOREIGN KEY (StoreID)REFERENCES Store(StoreID);
5. Dropping Constraints
- Drop the
CHECKconstraint from theProductstable.
ALTER TABLE Products DROP CONSTRAINT chk_StockQuantity;
- Drop the
FOREIGN KEYconstraint from theSalestable.
ALTER TABLE Sales DROP CONSTRAINT fk_Store;
Combined Code Sample
Here is all the code combined for easier reference:
-- Adding a New Column
ALTER TABLE Store ADD LastUpdated DATETIME;
ALTER TABLE Products ADD Description VARCHAR(255);
-- Modifying an Existing Column
ALTER TABLE Store ALTER COLUMN Location VARCHAR(200);
ALTER TABLE Products ALTER COLUMN Price DECIMAL(15, 4);
-- Dropping a Column
ALTER TABLE Customer DROP COLUMN PhoneNumber;
ALTER TABLE Products DROP COLUMN Description;
-- Adding Constraints
ALTER TABLE Products ADD CONSTRAINT chk_StockQuantity CHECK (StockQuantity >= 0);
ALTER TABLE Sales ADD CONSTRAINT fk_Store FOREIGN KEY (StoreID) REFERENCES Store(StoreID);
-- Dropping Constraints
ALTER TABLE Products DROP CONSTRAINT chk_StockQuantity;
ALTER TABLE Sales DROP CONSTRAINT fk_Store;
To gain complete access, login with gmail or outlook, no need of signup. click here


Comments Not Found