Tuesday, 26 December 2017

Alter table using sql queries in sql server


Add column in table

>> ALTER TABLE table_name ADD column_name column-definition;
     ALTER TABLE employees ADD last_name VARCHAR(50);

Change column type in table
>> ALTER TABLE table_name  ALTER COLUMN ColumnName ColumnType
   ALTER TABLE mstVillage ALTER COLUMN Lat decimal(18,9)

Add multiple columns in table
>> ALTER TABLE table_name ADD column_1 column-definition,column_2 column-definition,... column_n column_definition;
  ALTER TABLE employees ADD last_name VARCHAR(50), first_name VARCHAR(40);


Modify column in table
 >> ALTER TABLE table_name ALTER COLUMN column_name column_type;
     ALTER TABLE Emp ALTER COLUMN Col_name VARCHAR(75) NOT NULL;


Drop column in table
>> ALTER TABLE tbl_Name DROP COLUMN column_name;
   ALTER TABLE Emp DROP COLUMN Col_name;


Rename column in table
>> sp_rename 'table_name.old_col_name', 'new_col_name', 'COLUMN';
   sp_rename 'employees.last_name', 'lname', 'COLUMN';


Rename table
>> sp_rename 'old_tbl_name', 'new_tbl_name';

   sp_rename 'employee', 'emp';

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...