In this tutorial we will guide you on creating tables in SQL Server. As we know, the main components in databases are the tables, in which the data actually resides. Tables contain rows and columns, where columns act like the entity identifiers such as employee name, employee salary etc and rows contain the data values for those entities. So in this tutorial we will see how to create tables in SQL Server.
Step 1 – Navigating to the Desired Database
Creating tables in SQL server is very easy. It can be done via commands which have to be written in the Query Editor.
For that, first of all, let’s move to the database where the table has to be created. Expand the Databases folders from top and further expand the required database inside which table has to be created.
After that, right click on that Database and open up the Query Editor.
Step 2 – Creating Table
In the Query Editor we will write the following queries:
create table Inventory (
ID int,
Product varchar(50),
Quantity int,
Price decimal (18,2)
);
The query here means that we are interested to create a new table which would be called Inventory. It will have an ID column for the Product Ids and its Data type would be an Integer.
It will have a column for Products containing the product names, so its data type would be Varchar.
It will also contain a Quantity field with Integer set as its data type
And last of all, a column for Price that will accept data as real numbers. Therefore we will declare its type as decimal and set its maximum length as 18 with a precision of 2 decimal places.
After that is done, click on the Execute Button on top. A message will appear below the Query Editor that the query has been executed successfully.
Step 3 – Identifying the Table
Now for verifying whether the table has been made, just click on the Tables icon and then on the Refresh Icon, located under the Object Explorer.
With that done, our newly created Inventory table would appear over there. Click on the Inventory table and expand its columns. The fields would appear to be created exactly like we had mentioned in the query earlier.
Step 4 – Drop Table
Now let’s move on to how to drop tables in SQL. To delete a table and all of its contents, we use the DROP table command in SQL. Over here, let’s drop the table we just made. For that, simply write:
drop table Inventory
With that done, let’s execute the command. Now when we will Refresh the Object Explorer, we can see that the Inventory table has been deleted.
And that was how to create table in SQL.