Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading