• Home
  • Services
    • DBA Support
    • DBA Consultancy Services
    • PostgreSQL Support
    • Website Maintenance
  • Courses

    About Courses

    • List Of Courses
    • Become an Instructor
    Greenplum Database

    Greenplum Database

    $2,000.00 $1,500.00
    Read More
  • Company
    • FAQs
    • About Us
    • Contact
  • Events
  • Portfolio
  • Blogs
    • Blog – RayaFeeL
    • Blog – PostgreSQL Support
    • Blog – PostgreSQL Migration
    • Blog – All DB’s
    • Blog – Linux
    • Blog – Medical Coding
      • Cart

        0

    Have any question?
    (+91)8838953252
    ITsupport@rayafeel.com
    RegisterLogin
    RayaFeeL
    • Home
    • Services
      • DBA Support
      • DBA Consultancy Services
      • PostgreSQL Support
      • Website Maintenance
    • Courses

      About Courses

      • List Of Courses
      • Become an Instructor
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00
      Read More
    • Company
      • FAQs
      • About Us
      • Contact
    • Events
    • Portfolio
    • Blogs
      • Blog – RayaFeeL
      • Blog – PostgreSQL Support
      • Blog – PostgreSQL Migration
      • Blog – All DB’s
      • Blog – Linux
      • Blog – Medical Coding
        • Cart

          0

      Databases

      • Home
      • All courses
      • Databases
      • PostgreSQL Database
      CoursesIT & SoftwareDatabasesPostgreSQL Database
      • BASIC POSTGRESQL
        6
        • Lecture1.1
          PostgreSQL Brief History 30 min
        • Lecture1.2
          PostgreSQL Features 20 min
        • Lecture1.3
          PostgreSQL Monitoring Tools Overview 30 min
        • Lecture1.4
          PostgreSQL – Oracle Vs PostgreSQL 30 min
        • Lecture1.5
          PostgreSQL Data Types 30 min
        • Lecture1.6
          PostgreSQL Maximum Objects Size 30 min
      • VMWARE & POSTGRESQL INSTALLATION
        9
        • Lecture2.1
          Vmware Installation 30 min
        • Lecture2.2
          Creating Linux Virtual Machine 01 hour
        • Lecture2.3
          PostgreSQL Installation Types 30 min
        • Lecture2.4
          PostgreSQL GUI Installation 30 min
        • Lecture2.5
          PostgreSQL Text Mode Installation 30 min
        • Lecture2.6
          PostgreSQL Unattended Mode Installation 30 min
        • Lecture2.7
          Configure the network & Disk Partition 30 min
        • Lecture2.8
          How to install PostgreSQL10 On Linux Server ? 20 min
        • Lecture2.9
          PostgreSQL -11 Installation (rpm & source code) 30 min
      • POSTGRESQL DATABASE
        6
        • Lecture3.1
          Connect Postgres Server 10 min
        • Lecture3.2
          PostgreSQL startup / shutdown /restart the postgresql server 30 min
        • Lecture3.3
          PostgreSQL .bash_profile Set Up 30 min
        • Lecture3.4
          PostgreSQL Database Creation 30 min
        • Lecture3.5
          PostgreSQL Connect Database 30 min
        • Lecture3.6
          PostgreSQL Drop Database 30 min
      • POSTGRESQL TABLE
        16
        • Lecture4.1
          PostgreSQL Languages 30 min
        • Lecture4.2
          PostgreSQL Create Table 30 min
        • Lecture4.3
          PostgreSQL Select Table 30 min
        • Lecture4.4
          PostgreSQL Alter Table 30 min
        • Lecture4.5
          PostgreSQL Drop Table 30 min
        • Lecture4.6
          PostgreSQL Truncate Table 30 min
        • Lecture4.7
          PostgreSQL Rename 30 min
        • Lecture4.8
          PostgreSQL Comment 30 min
        • Lecture4.9
          PostgreSQL Insert 30 min
        • Lecture4.10
          PostgreSQL Update 30 min
        • Lecture4.11
          PostgreSQL Table Delete 30 min
        • Lecture4.12
          PostgreSQL Merge Table 30 min
        • Lecture4.13
          PostgreSQL UNION 30 min
        • Lecture4.14
          PostgreSQL UNION ALL 30 min
        • Lecture4.15
          PostgreSQL INTERSECT 30 min
        • Lecture4.16
          PostgreSQL DISTINCT 30 min
      • USER/OBJECTS PRIVILEGE AND ROLES ASIGNING
        3
        • Lecture5.1
          PostgreSQL Grant 30 min
        • Lecture5.2
          PostgreSQL Revoke 30 min
        • Lecture5.3
          Postgres user creation and restrict DDL statement & other database access 30 min
      • TRANSACTIONS - MVCC
        3
        • Lecture6.1
          PostgreSQL Commit 30 min
        • Lecture6.2
          PostgreSQL Rollback 30 min
        • Lecture6.3
          PostgreSQL Savepoint 30 min
      • POSTGRESQL USER/SCHEMA MANAGEMENT
        9
        • Lecture7.1
          PostgreSQL User Creation 30 min
        • Lecture7.2
          PostgreSQL User Creation Using Utility 30 min
        • Lecture7.3
          PostgreSQL Drop user 30 min
        • Lecture7.4
          PostgreSQL Drop User Using Utility 30 min
        • Lecture7.5
          PostgreSQL Password Changing and Views 30 min
        • Lecture7.6
          PostgreSQL Group/role 30 min
        • Lecture7.7
          Alter PostgreSQL User/Role/group 30 min
        • Lecture7.8
          PostgreSQL Schema 30 min
        • Lecture7.9
          PostgreSQL user creation and restrict DDL & database access 30 min
      • POSTGRESQL CONSTRAINTS
        6
        • Lecture8.1
          PostgreSQL Constraints 30 min
        • Lecture8.2
          PostgreSQL Primary Key 30 min
        • Lecture8.3
          PostgreSQL UNIQUE Constraint 30 min
        • Lecture8.4
          PostgreSQL CHECK Constraint 30 min
        • Lecture8.5
          PostgreSQL NOT NULL Constraint 30 min
        • Lecture8.6
          PostgreSQL Foreign Key 30 min
      • POSTGRESQL ADVANCE DATA TYPE
        5
        • Lecture9.1
          PostgreSQL DOMAIN Data Type 30 min
        • Lecture9.2
          PostgreSQL Alter Domain 30 min
        • Lecture9.3
          PostgreSQL Drop DOMAIN 30 min
        • Lecture9.4
          PostgreSQL Json Data Type 30 min
        • Lecture9.5
          PostgreSQL Sequence 30 min
      • POSTGRESQL VIEWS
        1
        • Lecture10.1
          How to Create PostgreSQL View 30 min
      • POSTGRESQL MONITORING OBJECT USUAGE/SIZE
        1
        • Lecture11.1
          How to Get Table Size, Database Size, Indexes Size, schema Size, Tablespace Size, column Size in PostgreSQL Database 30 min
      • POSTGRESQL DATABASE ARCHITECTURE
        4
        • Lecture12.1
          PostgreSQL Tablespace 30 min
        • Lecture12.2
          PostgreSQL UnderStanding Of Objects 30 min
        • Lecture12.3
          PostgreSQL Architecture 30 min
        • Lecture12.4
          PostgreSQL Query Flow 30 min
      • POSTGRESQL BACKUP AND RECOVERY
        13
        • Lecture13.1
          PostgreSQL Backup & Recovery Overview 30 min
        • Lecture13.2
          PostgreSQL Database Backup Tools – Pg_dump 30 min
        • Lecture13.3
          PostgreSQL Database Backup Tools – Pg_dumpall 30 min
        • Lecture13.4
          PostgreSQL Database Backup Tools – Pg_basebackup 30 min
        • Lecture13.5
          PostgreSQL COPY data From Text File 30 min
        • Lecture13.6
          PostgreSQL COPY data to Text File 30 min
        • Lecture13.7
          PostgreSQL Clone Table 20 min
        • Lecture13.8
          Postgres Database Schema Migration 30 min
        • Lecture13.9
          PostgreSQL Database clone/migration 30 min
        • Lecture13.10
          PostgreSQL Compress backup format 30 min
        • Lecture13.11
          PostgreSQL Archivelog 30 min
        • Lecture13.12
          PostgreSQL Point In Time Recovery 30 min
        • Lecture13.13
          Taking Backup On Postgres Slave (Standby) Server 30 min
      • POSTGRESQL PERFORMANCE TUNING
        5
        • Lecture14.1
          PostgreSQL Index 30 min
        • Lecture14.2
          PostgreSQL Reindex 30 min
        • Lecture14.3
          PostgreSQL PerformanceTuning 30 min
        • Lecture14.4
          Understanding Postgres VACUUM | VACUUM FULL | VACUUM ANALYZE 30 min
        • Lecture14.5
          Postgres Autovacuum Configuration/Setup 30 min
      • HIGH AVAILABILITY, LOAD BALANCING, AND REPLICATION
        11
        • Lecture15.1
          POSTGRESQL STREAMING REPLICATION 30 min
        • Lecture15.2
          SSL Streaming replication with PostgreSQL 10 30 min
        • Lecture15.3
          How to make Linux partition and configure the network on Redhat Linux7.3 30 min
        • Lecture15.4
          How To Fix Firewall Issues while connecting the other server? 30 min
        • Lecture15.5
          How to install the PostgreSQL10 On Linux ? 30 min
        • Lecture15.6
          How to Configure the cascade replication On PostgreSQL 10.3 ? 30 min
        • Lecture15.7
          How to add extra one slave an existing PostgreSQL cascade replication without down time ? 30 min
        • Lecture15.8
          PostgreSQL Switchover 30 min
        • Lecture15.9
          Postgres Failover 30 min
        • Lecture15.10
          Postgres Upgrade 30 min
        • Lecture15.11
          PostgreSQL Upgrade 9.5 to 11.3 . 30 min

        PostgreSQL CHECK Constraint

        • CHECK constraint is the most generic constraint type. It allows you to specify that the value in a certain column must satisfy a Boolean (truth-value) expression.
        • which allows the user to define a condition, that a value entered into a table, has to satisfy before it can be accepted. The CHECK constraint consists of the keyword CHECK followed by parenthesized conditions. The attempt will be rejected when update or insert column values that will make the condition false.
        • Every time you insert a new row, PostgreSQL checks by using CHECK condition. If the condition is true the values will be inserted on particular table  otherwise it would give back an error message and reject the changes. The same process is carried out for the update existing data.
        PRACTICAL 1. CREATION OF CHECK CONSRAINTS:

        CREATE TABLE products1 (
            product_no integer CHECK(product_no > 0),
            name text,
            price numeric CHECK (price > 0)
        );
        
        postgres=# \d products1
             Table "public.products1"
           Column   |  Type   | Modifiers 
        ------------+---------+-----------
         product_no | integer | 
         name       | text    | 
         price      | numeric | 
        Check constraints:
            "products1_price_check" CHECK (price > 0::numeric)
            "products1_product_no_check" CHECK (product_no > 0)
        PRACTICAL 2. CREATION OF CHECK CONSRAINTS WITH CONSTRAINT NAME:

        CREATE TABLE products2 (
            product_no integer,
            name text,
            price numeric CONSTRAINT positive_price CHECK (price > 0),
            CONSTRAINT product_count CHECK (product_no > 0)
        );
        
        postgres=# \d products2
             Table "public.products2"
           Column   |  Type   | Modifiers 
        ------------+---------+-----------
         product_no | integer | 
         name       | text    | 
         price      | numeric | 
        Check constraints:
            "positive_price" CHECK (price > 0::numeric)
            "product_count" CHECK (product_no > 0)
        There are two CHECK constraints in the products2 table:
        First one is the PRICE of the products2 must be greater than 0. If you try to insert someone with PRICE below 0, you will receive an error message.This check will prevent from updating invalid Price in terms of their semantic meanings.
        Second one also the product_no  must be greater than 0,which is obvious.
        PRACTICAL 3. INSERTING OPERATION ON CHECK CONSTRAINTS:

        postgres=# insert into products2 values(1,'COFFEE',8),(2,'MILK',10),(3,'CHICKEN',12);
        INSERT 0 3
        postgres=# insert into products2 values(1,'COFFEE',8),(2,'MILK',10),(3,'CHICKEN',12);
        INSERT 0 3
        postgres=# insert into products2 values(0,'COFFEE',8),(2,'MILK',10),(3,'CHICKEN',12);
        ERROR:  new row for relation "products2" violates check constraint "product_count"
        DETAIL:  Failing row contains (0, COFFEE, 8).
        
        postgres=# insert into products2 values(1,'COFFEE',0),(2,'MILK',10),(3,'CHICKEN',12);
        ERROR:  new row for relation "products2" violates check constraint "positive_price"
        DETAIL:  Failing row contains (1, COFFEE, 0).
        Becouse check constraint column product_no and price  condition should be greater than 0 otherwise you will get error.

        postgres=# select * from products2;
         product_no |  name   | price 
        ------------+---------+-------
                  1 | COFFEE  |     8
                  2 | MILK    |    10
                  3 | CHICKEN |    12
                  1 | COFFEE  |     8
                  2 | MILK    |    10
                  3 | CHICKEN |    12
        (6 rows)
        PRACTICAL 4.POSTGRESQL CHECK CONSTRAINTS FOR EXISTING TABLES:

        postgres=# CREATE TABLE VILLAGE (
          ID integer,
         village_NAME VARCHAR (50),
         area_code integer
        );
        
        postgres=# \dt village 
                  List of relations
         Schema |  Name   | Type  |  Owner   
        --------+---------+-------+----------
         public | village | table | postgres
        (1 row)
        To add a (multicolumn) unique constraint to VILLAGE TABLE:

        postgres=# ALTER TABLE VILLAGE ADD CONSTRAINT CHECK_NAME1 CHECK(ID>10);
        ALTER TABLE
        Change the column area_code data type from integer to varchar becouse we need to set (char_length(AREA_CODE) = 5)

        postgres=# alter table village alter column  area_code type varchar;
        ALTER TABLE
        postgres=# ALTER TABLE VILLAGE ADD CONSTRAINT CHECK_NAME2 CHECK (char_length(AREA_CODE) = 5);
        ALTER TABLE
        
        postgres=# \d village
                      Table "public.village"
            Column    |         Type          | Modifiers 
        --------------+-----------------------+-----------
         id           | integer               | 
         village_name | character varying(50) | 
         area_code    | character varying     | 
        Check constraints:
            "check_name1" CHECK (id > 10)
            "check_name2" CHECK (char_length(area_code::text) = 5)
        Here id should be greater than 10 and area_code should be equal to 5 character otherwise you will receive error from following example i explained.

        postgres=# insert into village values(11,'COFFEE','65483');
        INSERT 0 1
        postgres=# insert into village values(11,'COFFEE','6548'); 
        ERROR:  new row for relation "village" violates check constraint "check_name2"
        DETAIL:  Failing row contains (11, COFFEE, 6548).
        PRACTICAL 5. DROPPING CHECK CONSTRAINT USING ALTER TABLE STATEMENT:

        postgres=# alter table VILLAGE drop constraint check_name1 ;
        ALTER TABLE
        postgres=# \d village                                       
                      Table "public.village"
            Column    |         Type          | Modifiers 
        --------------+-----------------------+-----------
         id           | integer               | 
         village_name | character varying(50) | 
         area_code    | character varying     | 
        Check constraints:
            "check_name2" CHECK (char_length(area_code::text) = 5)
        PRACTICAL 6. RENAME CONSTRAINT:

        postgres=# ALTER TABLE VILLAGE RENAME CONSTRAINT check_name2 TO check_name3;
        ALTER TABLE
                                                                                                       
        postgres=# \d village                                                                                               
                      Table "public.village"
            Column    |         Type          | Modifiers 
        --------------+-----------------------+-----------
         id           | integer               | 
         village_name | character varying(50) | 
         area_code    | character varying     | 
        Check constraints:
            "check_name3" CHECK (char_length(area_code::text) = 5)
        PRACTICAL 7. POSTGRESQL CONSTRAINT VIEWS:
        if you want see all constraint name and table name(relname) you need to join pg_stat_all_tables and pg_constraint views

        postgres=# select a.conname,a.contype,a.conrelid,b.relname,b.relid from pg_constraint a,pg_stat_all_tables b where a.conrelid=b.relid;
                  conname           | contype | conrelid |    relname     | relid 
        ----------------------------+---------+----------+----------------+-------
         prikey                     | p       |    24676 | films          | 24676
         production                 | u       |    24697 | k              | 24697
         distributorc_did_check     | c       |    24702 | distributorc   | 24702
         uni_key                    | u       |    24761 | india          | 24761
         code_title                 | p       |    24710 | primtab        | 24710
         students_student_id_key    | u       |    24753 | students       | 24753
         pkey                       | p       |    24735 | postgres3      | 24735
         uni_constraint2            | u       |    24784 | mobiles        | 24784
         mobiles_pkey               | p       |    24784 | mobiles        | 24784
         uni_constraint             | u       |    24773 | cellphones     | 24773
         cellphones_pkey            | p       |    24773 | cellphones     | 24773
         con1                       | c       |    24706 | distributorsct | 24706
         postgres1_order_no_key     | u       |    24725 | postgres1      | 24725
         mul_uni_key                | u       |    24766 | batch          | 24766
         distributors_pkey          | p       |    24690 | distributors   | 24690
         distributors_name_check    | c       |    24690 | distributors   | 24690
         products1_price_check      | c       |    24798 | products1      | 24798
         products1_product_no_check | c       |    24798 | products1      | 24798
         product_count              | c       |    24806 | products2      | 24806
         positive_price             | c       |    24806 | products2      | 24806
         check_name2                | c       |    24814 | village        | 24814
         postgres_ord_no_key        | u       |    24715 | postgres       | 24715
        (22 rows)
        Prev PostgreSQL UNIQUE Constraint
        Next PostgreSQL NOT NULL Constraint

        ADVERTISEMENT

        Latest Courses

        PostgreSQL Database

        PostgreSQL Database

        $600.00 $500.00
        Greenplum Database

        Greenplum Database

        $2,000.00 $1,500.00

        Preview Course

        Free

        Latest Posts

        PostgreSQL Patching version 9, 10,11
        10Oct2019
        Tools for PostgreSQL
        16Sep2019
        Postgres user creation and restrict DDL & database access
        13Sep2019

        Recent Forum Topics

        • thought behind whiteboard activity
        • Are you going to take your first ste
        • How to start working on an application?
        • please let me know pre requirements to increase work_mem
        • how to copy some data in one table to another table in postgres

        2ndquadrant.in

        (+91) 8838953252

        ITsupport@rayafeel.com

        Company

        • About Us
        • Contact
        • Our Team
        • Blog

        COURSES

        • List Of Course
        • Become An Instructor
        • Events
        • Postgres Support Blog

        Support

        • DBA Support
        • Consultancy Services
        • Postgres Migration Blogs
        • Forum

        Recommend

        • Groups
        • Login
        • FAQs
        • SignUp

        IT Services by rayafeel.com. Powered by Rayafeel Technologies Pvt Ltd.

        • Privacy
        • Terms

        Become An Instructor?

        Join thousand of instructors and earn money hassle free!

        Get Started Now

        Login with:

        Login with Google Login with Twitter Login with LinkedIn Login with Microsoft

        Login with your site account

        Lost your password?

        Not a member yet? Register now

        Register a new account

        Are you a member? Login now

        Modal title

        Message modal