Thursday, June 18, 2009

How do I create a simple table in a database on SQL Server 2008 and populate it?

Let us say you want to create a table such as the one shown in this example (PrincetonTemp2) and populate it with some values. There is more than one way to create a table in SQL Server 2008.This one uses Create Table query to create a table. You then edit the table to insert the values. Values can also be inserted using code. In here, the query creates the table and you then manually insert the data.

Step 1: Create table using the query
In SQL Server 2008 right click the database in which you want to create a table and choose New Query






Type in the Query window as shown in the next figure. Check the syntax of the query by clicking the icon at the far right of the figure. Execute the query by clicking the (!)Execute.








You will have created a table called PrincetonTemp2. You can see it in the Tables node after you refresh the database by right clicking the database node and choosing Refresh from the list.

Step 2: Populate the table by editing the table :
Right click the table in the Management Studio after expanding the database node.










From the drop-down click on Edit Top 200 Rows.
In the table that shows up fill in the data you want to insert. The data has to match the query you used to create the table. One line inserted is shown in the next figure.