How to create new table in PostGre SQL

 First, let us create a new database with the name walmart.

We are trying create a department table for walmart as it has many departments and each department is under a division.


On the next screen, we give it its name walmart and save it.


Now we can see that database got created



Next, we will create table under this database. From the dropdown menu on the top, selects tools and then select Query Tool


It will open new window where we can write SQL code. We are going to name this table as departments and we want 2 columns in it- department and division.

Another important thing to note is we want department column to be the primary key. There can be only 1 primary key and it acts as a constraints for the column. It prevents any duplicate or null values in the column as it is unique key.


The confirmation indicates that table was created successfully. Note that we want to limit the number of characters that can be entered in each entry of the table is 100 and that's why using datatype varchar, we have used 100 in the bracket. If you know that the entries are going to be really small then you can reduce it to 50 if need be.

A good practice is to keep some margin for future purpose. May be the list you have right now  is showing all small department names but in future if a new department with longer name needs to be created then you should have enough room in your column to allow it.




And we can see under walmart database schema, that the table with the name departments is there now.



At the moment we have created empty table without any entry in any of its rows. Something like this...


Next, we will add data in these columns. We have already prepared data to be entered in this table. 


We will always confirm after executing the query that it was successful.

And if we want to see how departments table looks like, we can check that now.



Using this we can create as many tables we need under this database.

This is a manual process and hence it is recommended only when table to be created is small.

Many times, we are already given .csv or excel file to upload it on SQL server. How to upload that?
We will take one example of that in the next article.





No comments:

Post a Comment

Complex query example

Requirement:  You are given the table with titles of recipes from a cookbook and their page numbers. You are asked to represent how the reci...