How to Use Primary Key and Foreign Key in PostgreSQL Database



How to Use Primary Key and Foreign Key in PostgreSQL Database

How to Use Primary Key and Foreign Key in PostgreSQL Database

How to Use Primary Key and Foreign Key in Database PostgreSQL

Hey guys, welcome to my channel Fathir Habib, in this time I will show you How to use primary key and foreign key in postgresql, please create database  on this time i will create the database fathir habib underscore db, click on save, now fathir habib underscores db was created, right click fathir habib underscores db select query tool, please type create table department id integer not null, department underscores name varchar thirty, primary key id and then execute the query.
  
Now we will insert data insert into department id department underscore name values one sales and then execute the query, now we will select all from department and then execute the query, see the data has been inserted, now we will insert data with same id, what will be happen if we insert the same id, insert into department id department name values one marketing and then execute the query, the system said duplicate key violates unique constraint department underscores pkey, the detail key id equals one is already exists, it’s a mean that the primary key is a column in a table which must contain a unique value which can be used to identify each and every row of a table uniquely, how if we change the value id to be two, and then execute the query, see the query returned successfully, now we will select all from department and then execute the query, see the data has been inserted, now we will create table employees, id underscores department integer , name varchar 50, primary key id underscores department constrains fk underscores employee, foreign key id underscores department, references department id, and then execute the query.
  
Now we will insert data, insert into employees, id underscore department name values one fathir now execute the query, now we will select all from employees, see the data has been inserted, now what if we insert data into the employees table whose id  value does not exist in the department table, insert into employees, id underscore department name values eight habib and then execute the query

it shows an error, it’s said insert or update on table employees violates foreign key constraint fk  underscores employee, the detail is the key id underscores department equal is eight is not present in table department, it’s mean that a foreign key constraint specifies that the values in a column or a group of columns must match the values appearing in some row of another table, we say this maintains  the referential integrity between to related table.
  
How if we change the id to be two and then execute the query, see the query returned successful, now we select all from employees, you can see that the data has been inserted, thanks for watching don’t forget to subscribe, like share and don’t forget share this video to your friends, thank you and bye bye

Hastag :
#primarykey
#foreignkey
#postgresql

Comments are closed.