检查表上的约束

时间:2013-06-20 18:04:27

标签: sql sqlanywhere

我想修改以下DDL以添加CHECK约束,以便商店的经理(FK employee_number是商店表)在同一商店(员工表中的FK store_code)工作,并且商店提供所有产品,如果它类型是'本地'。

有人可以帮忙吗?

CREATE TABLE employee(
  employee_number CHAR(5) NOT NULL,
  name VARCHAR(30),
  store_code CHAR(5)
  PRIMARY KEY(employee_number),
  FOREIGN KEY(store_code) REFERENCES store
  )

CREATE TABLE store(
  store_code CHAR(5) NOT NULL,
  type VARCHAR(15),
  employee_number CHAR(5),
  PRIMARY KEY(store_code),
  FOREIGN KEY(employee_number) REFERENCES employee
  )


CREATE TABLE product(
  product_code CHAR(5) NOT NULL,
  description VARCHAR(150),
  cost DEC(10,2),
  PRIMARY KEY(product_code)
  )

CREATE TABLE stocks(
  store_code CHAR(5) NOT NULL,
  product_code CHAR(5) NOT NULL,
  PRIMARY KEY(product_code, store_code),
  FOREIGN KEY(product_key) REFERENCES product,
  FOREIGN KEY(store_code) REFERENCES store
  )

1 个答案:

答案 0 :(得分:0)

你可以使用触发器来实现这个

您可以查看此问题以获取示例Throw an error in a MySQL trigger