Home > Mobile >  Set a Table in Oracle to Only Accept Certain Values for a Column
Set a Table in Oracle to Only Accept Certain Values for a Column

Time:06-05

I am looking to create a table that will only accept certain values in my database and I came across the default values option but I am sure that this won't be something that would work but just wanted to clarify.

As far as I am aware the default option, would allow you to store a value for a row unless this is overwritten by something else.

What I am looking to do is create a table where the STATUS column would only have the values OPEN or CLOSED so my output looks like the following:

ORDER_ID: 001, 002, 003
STATUS: OPEN, CLOSED, OPEN
ORDER_DATE: 2021-05-29 09:01:25, 2021-05-31 17:35:40, 2021-06-01 15:33:55

The table I have created so far looks like:

CREATE TABLE ORDERS (
    ORDER_ID NUMBER NOT NULL,
    STATUS VARCHAR2(6) NOT NULL,
    ORDER_DATE DATE NOT NULL,
    PRIMARY KEY(ORDER_ID)
);

I am guessing I need to change the create table query for the order_id to include AUTO_INCREMENT but its more the status that I want to understand in terms of how to only accept certain values.

I would appreciate if someone could advise on what I need to do.

Thanks

CodePudding user response:

You need to create a constraint. Either a check constraint with hardcoded values that the column is allowed. Or you can create a foreign key to a table which contains the allowed values.

  • Related