Creating table in SQL Server

You can create table in MS SQL Server either of the following way,
•Enterprise Manager
•Transact SQL (T-SQL) statement

Here I will show you how to create table using T-SQL. The syntax is as below for creating table,

CREATE TABLE <TABLE_NAME> (<COLUMN_NAME> <DATA_TYPES>);



Below is an example of create a table

CREATE TABLE Airlines_Master (Aircode Char(2), Airline_Name Varchar(25), Country Int, DateCreate DateTime);

Once table is created you can modify the table. Add new column, drop exsiting column and alter exsiting column data type. The syntax for modifying a table using T-SQL is (syntax shows how to add new column),

ALTER TABLE <TABLE_NAME> ADD <COLUMN_NAME> <DATA_TYPE>;

In the example below we will modify the Airlines Master table and add a new column,

ALTER TABLE Airlines_Master ADD NoOfAircraft Int;

To change the datatype of a column the example is as follow,

ALTER TABLE <TABLE_NAME> ALTER <COLUMN_NAME> <DATA_TYPE>;

In the example below we will change the Country column of Airline Master table data type from Int to Varchar,

ALTER TABLE Airlines_Master ALTER COLUMN Country Varchar(50);

To drop a column from a table the syntax and example is below,

ALTER TABLE <TABLE_NAME> DROP COLUMN <COLUMN_NAME>;
ALTER TABLE Airlines_Master DROP COLUMN Country;

 

 

Leave a Reply