简体   繁体   English

sql - 向表中添加一个值大于指定值的列

[英]sql - add a column to table whose value is greater than specified value

CREATE TABLE Article
(
     ArCode CHAR(5) LIKE A% PRIMARY KEY, 
     ArName VARCHAR2(30) NOT NULL,  
     Rate NUMBER(8, 2), 
     Quantity NUMBER(4) >= 0 DEFAULT 0, 
     Class CHAR(1) CHECK(Class IN('A', 'B', 'C'))
);

Here I want to add a column named Quantity whose values must be greater than or equal to zero or default to zero.这里我想添加一个名为Quantity的列,其值必须大于或等于零或默认为零。 Also, a column ArCode which should start with A ;此外,一列ArCode应以A开头; Is the above code syntactically correct?上面的代码在语法上是否正确?

It probably depends on your db backend.这可能取决于您的数据库后端。 But I'd say you would need a CHECK function like for the Class column.但是我想说您需要一个类似于 Class 列的 CHECK 函数。

My guess (untested as I don't have an SQL db handy right now) would be something like this:我的猜测(未经测试,因为我现在手头没有 SQL 数据库)将是这样的:

CREATE TABLE Article
(
     ArCode CHAR(5) PRIMARY KEY CHECK (ArCode LIKE 'A%'),
     ArName VARCHAR2(30) NOT NULL,  
     Rate NUMBER(8, 2), 
     Quantity NUMBER(4) DEFAULT 0 CHECK (Quantity >= 0), 
     Class CHAR(1) CHECK(Class IN('A', 'B', 'C'))
);

You didn't specify the RDBMS that you're using - but in general, in ANSI SQL you can use check constraints to limit the allowed values.您没有指定您正在使用的 RDBMS - 但一般来说,在ANSI SQL您可以使用检查约束来限制允许的值。

You can find some general information about it in this link: https://en.wikipedia.org/wiki/Check_constraint您可以在此链接中找到有关它的一些一般信息: https : //en.wikipedia.org/wiki/Check_constraint

and you can look for your specifics for the RDBMS that you're using.您可以查找您正在使用的 RDBMS 的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM