0

i am relatively new in phpmysql.i need some help...

i am using wamp server in windows7.server version is 2.2.22 and php version is 5.3.13.Name of my database is db_attendance1. I have two table namely users and leave.In users table i have following column

empID(int)
username(varchar)
password(varchar)
mac(varchar)
ip(varchar)

and in leave table i have following column

lid(int)
empname(varchar)
username(varchar)
nod(int)
sdate(date)
edate(date)
reason(varchar)
action(varchar)

Now i want to use empID in leave table.how can i use it.i have to say that i already apply a code but its not working.Its giving the following error..

1005 - Can't create table 'db_attendance1.leave' (errno: 150) (Details...)

and in details its showing the following..

InnoDB Documentation

Supports transactions, row-level locking, and foreign keys

[ Variables | Buffer Pool | InnoDB Status ]

and the code

create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empID INT,
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);  

so whats wrong...please help me..

4

4 回答 4

0
create table users(

empID INT,
PRIMARY KEY(empID) /*<<< ADD THIS */
);
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empID INT,
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);  
于 2013-04-22T04:59:20.773 回答
0

INDEX(empID)在外键语句之前使用

于 2013-04-22T05:00:22.727 回答
0

试试这个来解决你的错误:

create table `leave`(
`lid` bigint(20) NOT NULL AUTO_INCREMENT,
`empID` INT(11),
`empname` VARCHAR(255) NOT NULL,
`username` VARCHAR(255) NOT NULL,
`nod` INT(11) NOT NULL,
`sdate` DATE,
`edate` DATE,
`reason` VARCHAR(255) NOT NULL,
PRIMARY KEY(`lid`),
FOREIGN KEY(`empID`) REFERENCES users(`empID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=311 ;
于 2013-04-22T05:01:09.007 回答
0

errno: 150是外键约束错误。SHOW ENGINE INNODB STATUS您可以通过在失败后立即在同一连接上发出查询来获取更多详细信息CREATE TABLE

需要注意的事项:

  1. 父表和属性是否存在?(users.empid 必须首先存在)
  2. 父属性和子属性是否具有相同的确切数据类型?(在这种情况下签名为 INT)
  3. 父表和子表中的属性是否都有索引(普通键、唯一键或主键)?
于 2013-04-22T05:01:33.930 回答