我正在尝试创建一个数据库,其中包含员工信息,例如他们的姓名,时间表,预订的假期等,以及有关正在执行的项目以及项目所针对的公司的信息。我的代码如下:
CREATE TABLE IF NOT EXISTS tblcompany (
companyid INT(11) UNSIGNED NOT NULL,
custfirst VARCHAR(50),
custlast VARCHAR(50),
company VARCHAR(50),
custphone VARCHAR(50),
custemail VARCHAR(50),
PRIMARY KEY (companyid),
INDEX (companyid),
CONSTRAINT FOREIGN KEY (companyid)
REFERENCES tblproject (companyid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tblemployee (
employeeid INT(11) UNSIGNED NOT NULL,
employeefirst VARCHAR(50),
employeelast VARCHAR(50),
employeephone VARCHAR(50),
employeeemail VARCHAR(50),
PRIMARY KEY (employeeid),
INDEX (employeeid),
CONSTRAINT FOREIGN KEY (employeeid)
REFERENCES tbltimesheet (employeeid),
CONSTRAINT FOREIGN KEY (employeeid)
REFERENCES tblholiday (employeeid),
CONSTRAINT FOREIGN KEY (employeeid)
REFERENCES tblannualleave (employeeid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tblholiday (
holidayid INT(11) UNSIGNED NOT NULL,
employeeid INT(11) UNSIGNED NOT NULL,
holidayfrom DATE,
holidayto DATE,
holidayhalfday BOOLEAN,
holidayreason VARCHAR(50),
INDEX (employeeid),
PRIMARY KEY (holidayid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tblannualleave (
annualleaveid INT(11) UNSIGNED NOT NULL,
employeeid INT(11) UNSIGNED NOT NULL,
annualleavetaken INT(11),
annualleaveremain INT(11),
anuualleavetotal INT(11),
INDEX (employeeid),
PRIMARY KEY (annualleaveid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tblproject (
projectid INT(11) UNSIGNED NOT NULL,
projectname VARCHAR(50),
projecttype VARCHAR(50),
companyid INT(11) UNSIGNED NOT NULL,
projectnotes VARCHAR(50),
PRIMARY KEY (projectid),
INDEX (projectid),
CONSTRAINT FOREIGN KEY (projectid)
REFERENCES tbltimesheet (projectid)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS tbltimesheet (
timesheetid INT(11) UNSIGNED NOT NULL,
employeeid INT(11) UNSIGNED NOT NULL,
projectid INT(11) UNSIGNED NOT NULL,
timesheetdate DATE,
timesheethours INT(11),
timesheetnotes VARCHAR(50),
INDEX (employeeid),
PRIMARY KEY (timesheetid)
) ENGINE=InnoDB;
我一直在环顾四周,尝试过一切,这可能就是这么简单。我已经将所有数据类型更改为类似的数据类型,看看这是否可以解决问题,但没有运气。我得到的错误代码是:
错误代码:1215。无法添加外键约束 0.063秒
CREATE TABLE IF NOT NOT EXISTS tblcompany(companyid INT(11)UNSIGNED NOT NULL,custfirst VARCHAR(50),custlast VARCHAR(50),
公司VARCHAR(50),custphone VARCHAR(50),custemail VARCHAR(50),PRIMARY KEY(companyid),INDEX(companyid),
CONSTRAINT FOREIGN KEY(companyid)REFERENCES tblproject (companyid))ENGINE = InnoDB11:15:57 CREATE TABLE IF NOT NOT EXISTS tblcompany(companyid INT(11) UNSIGNED NOT NULL,custfirst VARCHAR(50),custlast VARCHAR(50),公司VARCHAR(50),custphone VARCHAR(50),
custemail VARCHAR(50),PRIMARY KEY(companyid),INDEX (companyid),CONSTRAINT FOREIGN KEY(companyid)REFERENCES tblproject(companyid))ENGINE = InnoDB错误代码:1215。无法添加 外键约束0.063秒
感谢您的期待..
答案 0 :(得分:3)
答案 1 :(得分:0)
我认为您将解决在相反的订单中创建表的问题,否则您将遇到与tblemployee相同的问题。
Foreigh Key需要参考表已经存在。
答案 2 :(得分:0)
当它引用的表尚不存在时,您无法创建外键。您以后可以随时创建外键。
使用外键创建表后,必须首先创建包含引用字段的表。
您还可以先创建所有表,然后在第二步中创建所有外键。