任何人都可以帮我正确地为MYSQL中的CHECK CONSTRAINT编写正确的语法。我的表格如下,并且在声明STATUS的CHECK约束时出错。
CREATE TABLE EventRequest (
EventNo CHAR(8) NOT NULL,
DateHeld DATE NOT NULL,
DateReq DATE NOT NULL,
FacNo CHAR(8)NOT NULL,
CustNo CHAR(8) NOT NULL,
DateAuth DATE,
Status CHAR(8)NOT NULL,
EstCost DECIMAL(9,4)NOT NULL,
EstAudience INT(6) NOT NULL,
BudNo VARCHAR2(8),
CONSTRAINT PK_EVENTREQUEST PRIMARY KEY (EventNo),
CONSTRAINT FK_FACNO FOREIGN KEY (FacNo) REFERENCES FACILITY (FacNo),
CONSTRAINT FK_CUSTNO FOREIGN KEY (CustNo) REFERENCES CUSTOMER (CustNo),
CONSTRAINT Check_Status
CHECK (Status IN ('Approved','Pending','Denied') ) ) ;
答案 0 :(得分:3)
MySQL不支持CHECK约束。
多年来一直是一个愿望清单项目(https://bugs.mysql.com/bug.php?id=3464)。
MySQL团队发布了一个关于变通方法的博客:http://mysqlserverteam.com/new-and-old-ways-to-emulate-check-constraints-domain/
在您的情况下,您可以考虑:
persisted
然后创建一个表CONSTRAINT Check_Status FOREIGN KEY (Status) REFERENCES StatusTypes (Status)
,其中包含要限制它的三行。
答案 1 :(得分:1)
感谢那些试图提供帮助的人,最终设法使其能够使用下面的代码
CREATE TABLE EventRequest (
EventNo VARCHAR(8) NOT NULL COMMENT 'Event number',
DateHeld DATE NOT NULL COMMENT 'Event date',
DateReq DATE NOT NULL COMMENT 'Date requested',
CustNo VARCHAR(8) NOT NULL COMMENT 'Customer number',
FacNo VARCHAR(8) NOT NULL COMMENT 'Facility number',
DateAuth DATE COMMENT 'Date authorized',
Status VARCHAR(20) NOT NULL COMMENT 'Status of event request' CHECK
(Status IN ('Pending', 'Denied', 'Approved')),
EstCost DECIMAL(15,4) NOT NULL COMMENT 'Estimated cost',
EstAudience DECIMAL(11,0) NOT NULL COMMENT 'Estimated audience' CHECK
(EstAudience > 0),
BudNo VARCHAR(8) COMMENT 'Budget number',
CONSTRAINT PK_EVENTREQUEST PRIMARY KEY (EventNo),
CONSTRAINT FK_EVENT_FACNO FOREIGN KEY (FacNo) REFERENCES FACILITY (FacNo),
CONSTRAINT FK_CUSTNO FOREIGN KEY (CustNo) REFERENCES CUSTOMER (CustNo) );