Search code examples
postgresql-9.6pgadmin-4

How to Add a Check Constraint in a Table in pgadmin 4?


I have created a table named Card in the database and I want to add a check constraint to the Card_type column in pgadmin? How can I do it? Card Types can be Debit_Card or Credit_Card.

Created Card Table[![Added column in the table[![Sql of Card Table]2]3]


Solution

  • As mentioned you want to add the card type in card table. So, I assume that you want to add the CHECK Constraint and add card type like 'Debit Card' OR 'Credit Card'. Which means that no other card details can be entered apart from the above two.

    Steps: 1] Right click on the Card and select the Properties option which is at the bottom.

    2] Select Constraint in that columnFor reference

    3] Click on the check option as shown in the image.

    4] Add a new row by clicking the '+' option.

    5] Type the Column name which you want to edit i.e Card_Type in the Name column

    6] Click on the Edit row option and select Definition option.

    7] Add the following code

    "Card_Type"::text = ANY (ARRAY['Debit_Card'::character varying::text, 'Credit_Card'::character varying::text])
    

    Click on Save button.