我正在尝试设计创建应用程序,该应用程序应该跟踪已完成的作业和这些作业的文书工作状态。它需要以下格式的数据库(MySQL):
5表:
以下是表格应该如何相关:
约束:
我的sql:
USE ppwk;
CREATE TABLE store (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
account VARCHAR(3) NOT NULL,
secondary_account VARCHAR(64),
number VARCHAR(11) NOT NULL,
address VARCHAR(64),
address2 VARCHAR(64),
city VARCHAR(64),
state VARCHAR(2),
zip VARCHAR(12),
phone VARCHAR(14)
);
CREATE TABLE crew (
crew_code VARCHAR(6) PRIMARY KEY NOT NULL,
address VARCHAR(64),
address2 VARCHAR(64),
city VARCHAR(64),
state VARCHAR(2),
zip VARCHAR(12),
phone VARCHAR(14),
phone2 VARCHAR(14),
phone3 VARCHAR(14),
phone4 VARCHAR(14),
fax VARCHAR(14),
email VARCHAR(64)
);
CREATE TABLE job (
work_order INT(6) PRIMARY KEY NOT NULL,
svc_date DATE NOT NULL,
resvc_date DATE,
level VARCHAR(2),
description TEXT,
store_id INT(11),
crew_code VARCHAR(6),
FOREIGN KEY (store_id) REFERENCES store(id),
FOREIGN KEY (crew_code) REFERENCES crew(crew_code)
);
CREATE TABLE status (
work_order INT(6) PRIMARY KEY,
status VARCHAR(64) NOT NULL,
added_on DATETIME,
closed_on DATETIME,
FOREIGN KEY (work_order) REFERENCES job(work_order)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE comment (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
work_order INT(6),
act_comment TEXT,
clr_comment TEXT,
act_last_updated DATETIME,
clr_last_updated DATETIME,
INDEX (work_order),
FOREIGN KEY(work_order) REFERENCES job(work_order)
ON UPDATE CASCADE
ON DELETE CASCADE
);
问题1:这个sql是否符合我的要求?
问题2:我对外键有点困惑。如果我将一个外键添加到指向“商店”的“作业”,它是否将“作业”变成属于“商店”的子表?或者是相反的方式?
谢谢。
答案 0 :(得分:1)
只要存储和存储,您就可以创建新工作。
CREATE TABLE JOB (
IDJOB CHAR(20) NOT NULL,
IDCREW CHAR(20) NOT NULL,
IDSTORE CHAR(20) NOT NULL,
PRIMARY KEY (IDJOB,IDCREW,IDSTORE))
TYPE = MYISAM
ROW_FORMAT = DEFAULT;
CREATE TABLE CREW (
IDCREW CHAR(20) NOT NULL,
PRIMARY KEY (IDCREW))
TYPE = MYISAM
ROW_FORMAT = DEFAULT;
CREATE TABLE STORE (
IDSTORE CHAR(20) NOT NULL,
PRIMARY KEY (IDSTORE))
TYPE = MYISAM
ROW_FORMAT = DEFAULT;
CREATE TABLE STATUS (
IDSTATUS CHAR(20) NOT NULL,
PRIMARY KEY (IDSTATUS))
TYPE = MYISAM
ROW_FORMAT = DEFAULT;
CREATE TABLE COMMENT (
IDCOMMENT CHAR(20) NOT NULL,
PRIMARY KEY (IDCOMMENT))
TYPE = MYISAM
ROW_FORMAT = DEFAULT;