MySQL - erroe code 1005 - Asked By abinav shankar on 09-Jan-12 12:50 AM

Hi

I have created a stored procedure for creating tables and when i execute it it is getting executed and when i call the stored procedure i get the error
 
error code 1005:cannot create the table
 
this is my stored procedure pl correct me where i went wrong
 
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_CreateTables`()
BEGIN
CREATE TABLE `tblassociatedetails` (
  `AssociateId` int(11) NOT NULL ,
  `AssociatePwd` varchar(30) NOT NULL,
  `AssociateFName` varchar(30) NOT NULL,
  `AssociateMName` varchar(30) NOT NULL,
  `AssociateLName` varchar(30) NOT NULL,
  `ManagerId` int(11) NOT NULL,
  `DesigId` int(11) NOT NULL,
  `DeptId` int(11) NOT NULL,
  `UserRoleId` int(11) NOT NULL,
  `SecQuestID` int(11) DEFAULT NULL,
  `Sex` varchar(6) NOT NULL,
  `FatherName` varchar(30) NOT NULL,
  `MotherName` varchar(30) NOT NULL,
  `CurrentAdd` varchar(1000) NOT NULL,
  `PermanentAdd` varchar(1000) NOT NULL,
  `EmergContactAdd` varchar(1000) NOT NULL,
  `EmergContactNo` varchar(20) NOT NULL,
  `SecAnswer` varchar(40) NOT NULL,
   KEY `DeptId` (`DeptId`),
  KEY `DesigId` (`DesigId`),
  KEY `UserRoleId` (`UserRoleId`),
  CONSTRAINT `DeptId` FOREIGN KEY (`DeptId`) REFERENCES `tbldepartment` (`DeptId`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `DesigId` FOREIGN KEY (`DesigId`) REFERENCES `tbldesignation` (`DesigId`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `UserRoleId` FOREIGN KEY (`UserRoleId`) REFERENCES `tbluserrole` (`UserRoleId`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ;
CREATE TABLE `tbldepartment` (
  `DeptId` int(11) NOT NULL AUTO_INCREMENT,
  `DeptName` varchar(20) NOT NULL,
  `Description` varchar(30) NOT NULL,
   PRIMARY KEY (`DeptId`)
);
CREATE TABLE `tbldesignation` (
  `DesigId` int(11) NOT NULL AUTO_INCREMENT,
  `DesigName` varchar(20) NOT NULL,
  `Description` varchar(30) NOT NULL,
   PRIMARY KEY (`DesigId`)
);
CREATE TABLE `tblleaverequest` (
  `LeaveReqId` int(11) NOT NULL AUTO_INCREMENT,
  `AssociateId` int(11) NOT NULL,
  `ManagerId` int(11) NOT NULL,
  `LeaveTypeId` int(11) NOT NULL,
  `LeaveStatusId` int(11) NOT NULL,
  `NoOfDays` float NOT NULL,
  `From` datetime NOT NULL,
  `To` datetime NOT NULL,
  `AddressOnLeave` varchar(1000) NOT NULL,
   PRIMARY KEY (`LeaveReqId`),
  KEY `LeaveTypeId` (`LeaveTypeId`),
  KEY `LeaveStatusId` (`LeaveStatusId`),
  CONSTRAINT `LeaveStatusId` FOREIGN KEY (`LeaveStatusId`) REFERENCES `tblleavestatus` (`LeaveStatusId`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `LeaveTypeId` FOREIGN KEY (`LeaveTypeId`) REFERENCES `tblleavetype` (`LeaveTypeId`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ;
CREATE TABLE `tblleavestatus` (
  `LeaveStatusId` int(11) NOT NULL AUTO_INCREMENT,
  `LeaveStatus` varchar(15) NOT NULL,
   PRIMARY KEY (`LeaveStatusId`)
) ;
CREATE TABLE `tblleavetype` (
  `LeaveTypeId` int(11) NOT NULL AUTO_INCREMENT,
  `LeaveType` varchar(45) NOT NULL,
   PRIMARY KEY (`LeaveTypeId`)
) ;
CREATE TABLE `tblloginhistory` (
  `LoginHistId` int(11) NOT NULL AUTO_INCREMENT,
  `AssociateId` int(11) NOT NULL,
  `LoggedIn` datetime NOT NULL,
  `LoggedOut` datetime NOT NULL,
  PRIMARY KEY (`LoginHistId`)
) ;
CREATE TABLE `tblsecretquestion` (
  `SecQuestID` int(11) DEFAULT NULL,
  `SecQuestion` varchar(100) DEFAULT NULL
);
CREATE TABLE `tbluserrole` (
  `UserRoleId` int(11) NOT NULL AUTO_INCREMENT,
  `UserRole` varchar(15) NOT NULL,
   PRIMARY KEY (`UserRoleId`)
);
END
Web Star replied to abinav shankar on 09-Jan-12 01:09 AM
you need to check one by one these konwn cause of this error

Known Causes:


  1. The two key fields type and/or size is not an exact match. For example, if one is INT(10) the key field needs to be INT(10) as well and not INT(11) or TINYINT. You may want to confirm the field size using SHOW CREATE TABLE because Query Browser will sometimes visually show just INTEGER for both INT(10) and INT(11). You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same. (More about signed vs unsigned here).
  2. One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field. (thanks to Venkatesh and Erichero and Terminally Incoherent for this tip)
  3. The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this. (Thanks to Niels for this tip)
  4. One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message – it just won’t create the key.) In Query Browser, you can specify the table type.
  5. You have specified a cascade ON DELETE SET NULL, but the relevant key field is set to NOT NULL.  You can fix this by either changing your cascade or setting the field to allow NULL values. (Thanks to Sammy and J Jammin)
  6. Make sure that the Charset and Collate options are the same both at the table level as well as individual field level for the key columns. (Thanks to FRR for this tip)
  7. You have a default value (ie default=0) on your foreign key column (Thanks to Omar for the tip)
  8. One of the fields in the relationship is part of a combination (composite) key and does not have it’s own individual index. Even though the field has an index as part of the composite key, you must create a separate index for only that key field in order to use it in a constraint. (Thanks to Alex for this tip)
  9. You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship (Thanks to Christian & Mateo for the tip)
  10. The name of your foreign key exceeds the max length of 64 chars.  (Thanks to Nyleta for the tip)

D Company replied to abinav shankar on 09-Jan-12 01:24 AM
Change the CHARSET and COLLATE to utf8 and to utf8_general_ci and then try again.