简体   繁体   中英

Postgres vachar check constraint

I have a varchar attribute named "Urgent" in my Patient table. And I want this attribute to be either "Yes" or "Not" and not anything else. I did something but I can't verify if it's works because Postgres does not work on my computer and I didn't find anything on the web. So I would like to confirm if what I did is correct or not. Here is what I did:

CREATE TABLE public.Examen(
    NumExamen         VARCHAR (50) NOT NULL ,
    SignesCliniques   VARCHAR (1000) NOT NULL ,
    Urgent            VARCHAR (50) NOT NULL ,
    DateExamen        DATE  NOT NULL ,
    NumInfirmier      VARCHAR (50) NOT NULL  ,
    CONSTRAINT Examen_PK PRIMARY KEY (NumExamen),
    Urgent CHECK (Urgent='Yes' or Urgent='No')
)WITHOUT OIDS;

You code works. But instead of "yes"/"no", why not use the built in boolean data type?

CREATE TABLE public.Examen (
    NumExamen         VARCHAR(50) NOT NULL PRIMARY KEY,
    SignesCliniques   VARCHAR(1000) NOT NULL ,
    Urgent            BOOLEAN NOT NULL ,
    DateExamen        DATE  NOT NULL ,
    NumInfirmier      VARCHAR(50) NOT NULL  
) WITHOUT OIDS;

Incidentally, you can test your code on multiple sites on the web, including DB Fiddle, SQL Fiddle, and Rextester.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM