mariaDB问题。尝试使用约束条件创建具有多项选择输入的表

时间:2020-03-29 12:03:36

标签: mysql mariadb constraints multiple-choice

我尝试执行以下代码

CREATE TABLE ShoppingCentreShop (
CentreCode varchar(255),
ShopNo int,
Category  varchar(255),
CustomerRating int,
**CONSTRAINT CHK_Category CHECK ( Category in ('FASHION', 'ENTERTAINMENT','FOOD')),**
CONSTRAINT CHK_CustomerRating CHECK ( CustomerRating between 1 and 5),
CONSTRAINT fk_ShoppingCentre_CentreCode FOREIGN KEY (CentreCode)  REFERENCES ShoppingCentre(CentreCode),
CONSTRAINT fk_Shop_ShopNo FOREIGN KEY (ShopNo)  REFERENCES Shop(ShopNo)
);

我得到以下错误代码

  1. “应该输入符号名称!没有反引号的保留关键字不能用作列名。 (在“检查”位置147附近)
  2. 意外的语句开始。 (在“类别”附近的位置155)
  3. 无法识别的语句类型。 (在位置164的“中”附近)

错误出现在粗体代码行上 ” 我是MySQL的初学者,因此非常感谢您

编辑:我也得到以下错误行 “#1005-无法创建表shopping_centresshoppingcentreshop(错误号:150“外键约束格式不正确”)(详细信息...)”

1 个答案:

答案 0 :(得分:0)

为什么不使用ENUM()?

CREATE TABLE ShoppingCentreShop
(
  CentreCode varchar(255),
  ShopNo int,
  Category ENUM('FASHION','ENTERTAINMENT','FOOD'),
  CustomerRating int,
  CONSTRAINT CHK_CustomerRating CHECK (CustomerRating BETWEEN 1 AND 5),
  CONSTRAINT fk_ShoppingCentre_CentreCode FOREIGN KEY (CentreCode) REFERENCES ShoppingCentre(CentreCode),
  CONSTRAINT fk_Shop_ShopNo FOREIGN KEY (ShopNo) REFERENCES Shop(ShopNo)
)