SQL ALTER TABLE
ALTER command in SQL is used to modify column/constraint in a table
Syntax[edit | edit source]
- ALTER TABLE [table_name] ADD [column_name] [datatype]
Add Column(s)[edit | edit source]
ALTER TABLE Employees ADD StartingDate date NOT NULL DEFAULT GetDate(), DateOfBirth date NULL
The above statement would add columns named
StartingDate which cannot be NULL with default value as current date and
DateOfBirth which can be NULL in Employees table.
Drop Column[edit | edit source]
ALTER TABLE Employees DROP COLUMN salary;
This will not only delete information from that column, but will drop the column salary from table employees(the column will no more exist).
Add Primary Key[edit | edit source]
ALTER TABLE EMPLOYEES ADD pk_EmployeeID PRIMARY KEY (ID)
This will add a Primary key to the table Employees on the field
ID. Including more than one column name in the parentheses along with ID will create a Composite Primary Key. When adding more than one column, the column names must be separated by commas.
ALTER TABLE EMPLOYEES ADD pk_EmployeeID PRIMARY KEY (ID, FName)
Drop Constraint[edit | edit source]
ALTER TABLE Employees DROP CONSTRAINT DefaultSalary
This Drops a constraint called DefaultSalary from the employees table definition.
Note:* Ensure that constraints of the column are dropped before dropping a column.
Alter Column[edit | edit source]
ALTER TABLE Employees ALTER COLUMN StartingDate DATETIME NOT NULL DEFAULT (GETDATE())
This query will alter the column datatype of
StartingDate and change it from simple
datetime and set default to current date.
Add Constraint[edit | edit source]
ALTER TABLE Employees ADD CONSTRAINT DefaultSalary DEFAULT ((100)) FOR [Salary]
This adds a constraint called DefaultSalary which specifies a default of 100 for the Salary column.
A constraint can be added at the table level.
Types of constraints
- Primary Key - prevents a duplicate record in the table
- Foreign Key - points to a primary key from another table
- Not Null - prevents null values from being entered into a column
- Unique - uniquely identifies each record in the table
- Default - specifies a default value
- Check - limits the ranges of values that can be placed in a column
To learn more about constraints, see the Oracle documentation.