Creating Relationship between two tables in PostgreSQL | Set Primary key & Foreign key using pgAdmin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn how to make the relationship between two tables using primary key and foreign key in postgress SQL database so now guys let's get started so guys here you see we have created One fun. DB database and inside that fun. DB you find a schemas so inside the schemas we have one public folder so inside this public folder you will find a tables so inside that tables only we have to create are tables okay so either you can directly start from this public create and create the table or else you can go to that and just right click on that table create and table name so let's click here so guys now we are going to do the relationship between two tables so guys let's take an example of category and products so let me add categories and then move to the columns let's click on ADD row that is plus icon over here and add the column first will be ID it's going to be integer and not null primary key and set up the auto increment part just click on this edit button go to constraints and click on identity and here the column identity it's going to be always and the increment should start from one okay and leave all these things blank it will automatically UT the data so now guys let me click again here to collapse and now let's add the second row that is name category name and I'm going to select with the character varing 250 F length and now guys let's click to save click save so here you will see that we have a constraint column in that categories so inside this constraint you can just see go to the properties here so you'll find that we have one primary key that is referring to the ID okay so primary key setup is done and now guys let's create the second table as products so right click on your table and create create table so now let's get the product table name here products and go to the columns let's add one row and the column Name ID first integer data type not null and primary key true and set the auto increment just go to the edit button and click on constraints click on identity and auto increment to one start from one okay so Auto increment setup is done now let's collapse it and now let's add one new row for the relationship which is going to be related with the category stable so let's add category ID so this product belongs to one category so you can store that category ID here and let's make the data type as integer that's it and now guys let's add more two Fields product name then character varing to 55 let's add one more about the price again let's select with the character weing let's give 100 and now guys let's set up with the relationship that is foreign key connecting with the category stable so you can just go to this constraints here and here you see primary key and then foreign key so let's click on this foreign key and add a rowle so you're going to tell what relationship you're going to give foreign key so let me tell my products table that is column category ID give the foreign key F we will understand as foreign and then key okay so F key is foreign key now and then guys let's move to this General inside this General you'll find whatever you have typed here column name and definition you're not going to touch anything and go to the columns guys so inside this columns you have to take the local column so let's select that so my local column is category ID to which table you want to refer that is categories okay and to what column in that category you are going to refer it's basically ID okay and now guys you can just click on add so here you have added and now go to action and here you see guys there are two things which is on update and on delete so guys you just need to go through this about what is restate Cascade and setel set default and no action okay so if you want to know what does this all restate cascet setel default no action means so if you want a detailed explanation please comment below so on update I will take no action and on delete I'm going to restrict it okay so when you delete the category please stop deleting that category because your product is linked with some categories okay now let's continue with the save let's click here to save perfect so your products table also is created so now guys let us see the relationship is established or not so you can just go to this products and you have constraints over there so you can see that you have one primary key and your foreign key okay and if you want to see in detail just open that and go to that foreign key so here you will find that the category and your referenced ID to which table it is referring also so guys we have successfully seen how to set up a relationship between two tables okay so guys in this video that's it thank you for watching this video please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 7,090
Rating: undefined out of 5
Keywords: fundaofwebit, Creating Relationship between two tables in PostgreSQL, Creating Relationship between two tables using pgAdmin in postgresql, postgresql foreign key, primary key and foreign key in postgresql, foreign key in postgresql, how to create foreign and primary key in postgresql database, set primary key and foreign key postgresql, Creating Relationship between two tables in PostgreSQL | Set Primary key & Foreign key using pgAdmin, postgresql database tutorial
Id: FRT38sQeZ-w
Channel Id: undefined
Length: 5min 51sec (351 seconds)
Published: Mon Oct 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.