postgresql约束(字段a>字段b)

时间:2014-03-30 13:38:24

标签: sql postgresql constraints

我正在根据讲师给我们的数据做一个tastrade架构。我使用SQL窗格构建表而不是图形用户界面(PgAdminIII)

我有这两个:

12   MAX_ORDER_AMT Curr       8      4    max_order_amt > min_order_amt
13   MIN_ORDER_AMT.Curr       8      4    max_order_amt > min_order_amt
...
...

为了创建它我使用[部分代码]

"Max_Order_Amt" money,
"Min_Order_Amt" money,
...
...

为了添加Max_Order_amt> min_order_amt约束,我必须使用什么?

我尝试使用" max_order_amt> min_order_amt"但它失败了。

非常感谢你的帮助

使用的语言: PostegreSQL

图形用户界面 PgAdminIII

编辑:这是完整的创建表代码

CREATE TABLE oltp.customer
("Customer_ID" character (6) NOT NULL,
"Company_Name" character (40) NOT NULL,
"Contact_Name" character (30) NOT NULL,
"Address" character (60),
"City" character (15),
"Region" character(15),
"Postal_Code" character(10),
"Phone" character(24),
"Fax" character(24),
"Max_Order_Amt" money CHECK (Max_Order_Amt > Min_Order_Amt),
"Min_Order_Amt" money CHECK (Max_Order_Amt > Min_Order_Amt),
"Discount" numeric(2) CHECK (Discount >=0),
"Sales_Region" character(4),
CONSTRAINT customers_pkey PRIMARY KEY ("Customer_ID"))
WITH 
( OIDS = FALSE);
alter table oltp.customer
OWNER to postgres;

ERROR:  column "max_order_amt" does not exist

********** Error **********

ERROR: column "max_order_amt" does not exist
SQL state: 42703

以下是Tastrade架构

CUSTOMER
(Customer Information)
Beware of updating or deleting instances from this table as other tables refer to this table’s PK
Number of data records:       92      
Fd   Field Name    Type   Width    Dec   Notes
 1   CUSTOMER_ID   Char       6          PK, system generated, not null
 2   COMPANY_NAME  Char      40          not null
 3   CONTACT_NAME  Char      30          not null
 4   CONTACT_TITLE Char      40          
 5   ADDRESS       Char      60          
 6   CITY          Char      15          
 7   REGION        Char      15          
 8   POSTAL_CODE   Char      10          
 9   COUNTRY       Char      15          
10   PHONE         Char      24          
11   FAX           Char      24          
12   MAX_ORDER_AMT Curr       8      4    max_order_amt > min_order_amt
13   MIN_ORDER_AMT.Curr       8      4    max_order_amt > min_order_amt
14   DISCOUNT      Nume       2           discount >= 0
15   SALES_REGION  Char       4          
** Total **                       302   

tastarade Schema 12-14 正在向我提供'不存在'错误。我按照8.1手册进行了操作。它停在Max_Order_Amt,但当我尝试其他时,它给了我同样的错误

再次

1 个答案:

答案 0 :(得分:1)

在创建表格时使用check约束,如

"Max_Order_Amt" money CHECK (Max_Order_Amt > Min_Order_Amt)

在此处详细了解Check约束http://www.postgresql.org/docs/8.1/static/ddl-constraints.html

修改

删除列名周围的"双引号。试试如下

CREATE TABLE customer
(Customer_ID character (6) NOT NULL,
Company_Name character (40) NOT NULL,
Contact_Name character (30) NOT NULL,
Address character (60),
City character (15),
Region character(15),
Postal_Code character(10),
Phone character(24),
Fax character(24),
Max_Order_Amt money CHECK (Max_Order_Amt > Min_Order_Amt),
Min_Order_Amt money,
Discount numeric(2) CHECK (Discount >=0),
Sales_Region character(4),
 CONSTRAINT customers_pkey PRIMARY KEY (Customer_ID));

在此处查看演示小提琴http://sqlfiddle.com/#!15/60767

<强> EDIT1:

如果您真的希望在列名称周围保留"(出于某种原因),请将检查约束设为如下所示

"Max_Order_Amt" money CHECK ("Max_Order_Amt" > "Min_Order_Amt"),
"Min_Order_Amt" money CHECK ("Max_Order_Amt" > "Min_Order_Amt"),
"Discount" numeric(2) CHECK ("Discount" >=0),