Page 22 - SQL
P. 22

Chapter 2: ALTER TABLE




        Introduction



        ALTER command in SQL is used to modify column/constraint in a table


        Syntax



            •  ALTER TABLE [table_name] ADD [column_name] [datatype]


        Examples


        Add Column(s)



         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


         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).


        Drop Constraint


         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.


        Add Constraint


         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.




        https://riptutorial.com/                                                                                4
   17   18   19   20   21   22   23   24   25   26   27