简体   繁体   中英

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:

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 it's more the status that I want to understand in terms of how to only accept certain values.

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.

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