[英]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:我要做的是创建一个表,其中 STATUS 列只有值 OPEN 或 CLOSED 所以我的输出如下所示:
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.我猜我需要将 order_id 的创建表查询更改为包括 AUTO_INCREMENT 但它更多的是我想了解的状态,即如何只接受某些值。
You need to create a constraint.您需要创建一个约束。 Either a
check
constraint with hardcoded values that the column is allowed.允许该列的硬编码值的
check
约束。 Or you can create a foreign key to a table which contains the allowed values.或者,您可以为包含允许值的表创建外键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.