Sunday 23 September 2012

Methods to Create a Table in DB

In Previous post “SQL Table”, we’ve explained about the definition of the Table. This article will explain about “what are the ways we can create table in SQL Server”.

We can create a Table in Database in many ways, but most main method are,
  • Using SQL Scripts 
  • Using Database Tool
Using SQL Scripts: 

There are many database tools allow you to create tables without writing SQL State scripts, but given that tables are the container of all the data, all it is one of the fundamental object of the database, so it would be good if you learn thru SQL Scripts.

The CREATE TABLE statement is used to create a table in a database.

Syntax of CREATE TABLE:


Following are the basic syntax of creating a table
 

        Create table
        (
            column1 ,
            column2 ,
            column3 ,
            <...>  <...>  <...>
        )
Sample Table: EMPLOYEE
 

Execute the following Query at SQL Server management studio; it will create a Table called “Employee”
 

        Create table Employee
        (
            Emp_ID  int not null,
            First_Name varchar(50),
            Last_Name varchar(50),
            Date_of_Joining date,
            Department  varchar(50)
        )


Using Database Tool (SSMS):
 

In this method, we could create a table using the tool, SQL Server Management Studio(SSMS). 

Follow the steps below to create a table.
 

1. Open SSMS from Start Menu as shown below

2. Login/connect to the SQL Server by providing proper credentials


3. After logged in, Decide the database in which you are planning to create a table. Here am planning to create a table under “TestDB”, refer the exhibit below


4. Right click on the the Node “Tables”, select “New Table”


5. A spreadsheet will appear with 3 columns like “Column Name”, “Data Type”, “Allow Nulls”


6. Fill the sheet with columns name and its data type as shown below


7. Click Save Button from the Toolbar or Press Ctrl+S Keys
 

8. You will see a popup window with a input field, enter the table name here , press OK Button

Now you can see a new Table, Which you created, under Tables Node.


Hope this helps!.. please leave your comments here if you need any help on this..


Happy Learning.. :)

No comments:

Post a Comment