ALTER TABLE/ORDER BY CLAUSE



In this post , we will be knowing use of ALTER command on table. The ALTER command is used to change the structure of the table. For ex Adding a column, Deleting a column, Or Changing the Data Type of the column etc.

  • Changing data type of column.




In above figure , we see that the data type of age is int. Let's change it to varchar(10)

ALTER TABLE Winform_1
ALTER column Age varchar(10)


Data type of Age is changed.

The ALTER table syntax is different for MYSQL , DB2 etc.



  • Adding column to table.
To add the column to the table , following ALTER command is used.


ALTER TABLE WinForm_1
ADD address varchar(20)



Column address is added.



  • Dropping a column.
Using ALTER command , we can also drop or delete column from the table. See the below command to know how can we do it.

ALTER table WinForm_1
DROP column address





In this post, we will be studying order by clause. The order by is used to select data from database in particular order or particular column.


Syntax

SELECT column_name1,column_name2 from table_name
ORDER BY column_name ASC|DESC


Note- If you omit ASC keyword from order by clause , it will sort in ascending order by default.


ID Name City

State
1 Raj Mumbai

Maharashtra
2 Rahul Nashik

Maharashtra
3SaurabhNagar

Maharashtra
4Ryanpanaji

Goa


Example

SELECT * from Detail ORDER BY ID desc


Output.
ID Name

City State
4Ryan

panajiGoa
3Saurabh

NagarMaharashtra
2Rahul

NashikMaharashtra
1Raj

MumbaiMaharashtra


We can also use multiple columns in order by clause.
For example, Consider the following table.


ID Name

State
1 Anoop

Delhi
2 Ankit Pandey

Pune
3

4
Gaurav Kapoor

Arjit Singh
Delhi

Punjab


Example

SELECT * FROM [dbo].[tbl_Record] order by Name,State



Output

ID Name

State
1 Anoop

Delhi
2 Ankit Pandey

Pune
3

4
Gaurav Kapoor

Arjit Singh
Delhi

Punjab
ALTER TABLE/ORDER BY CLAUSE ALTER TABLE/ORDER BY CLAUSE Reviewed by LanguageExpert on August 21, 2017 Rating: 5

No comments