简体   繁体   English

在 Oracle 中将表设置为仅接受列的某些值

[英]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.

相关问题 限制列以仅接受2个值 - Restricting a column to accept only 2 values 如何根据 Oracle DB 中的某些列值编写创建表脚本? - How to write a create table script based on certain column values in Oracle DB? Oracle 查询以从第 1 列返回第 2 列中某些值的值 - Oracle query to return values from column 1 for certain values in column 2 如何允许oracle表列在列中具有多个行,但仅具有相同的值。 使用约束 - How to allow oracle table column to have multiple rows in column but sames values only. Using Constraints Oracle SQL-将来自多个表的结果集中的同一列中的值合并 - Oracle SQL - Combine values in the same column in one column in the result set from multiple table 仅当某些值时,Oracle SQL约束2列值 - Oracle SQL constrain 2 columns values only if certain values 在oracle中仅使用accept接受受限制的字符 - Accept only restricted characters using accept in oracle 尝试仅从必须匹配特定列的所有值的表中返回不同的 ID 条目 - Trying to only return distinct ID entries from a table where all values of a certain column must be matched oracle sql-如果该列中没有空值,则仅选择 - oracle sql - Select ONLY if there are NO null values in that column Oracle SQL 仅在表存在时删除列 - Oracle SQL only drop a column if a table exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM