Can anyone help me with this?
Posted: 23.04.2013, 18:06
I'm trying to create 4 tables. I set the tables up with MySQL Workbench and it worked fine. I used the same SQL for my Table creation in Delphi and I keep getting errors. I split the SQL up into smaller chunks. The Recipes Table - np problem, Ingredients, No problem, but as soon as I start putting in the references and Constraints, I just keep getting this Error 150 and I can't see the mistake / problem anywhere. Can anyone spot it?
Procedure CreateDatabase;
var
sdatabase, A, B, C, D, E, Csql, Dsql, Esql, Fsql : AnsiString;
Begin
sdatabase:='CREATE DATABASE IF NOT EXISTS RecABS';
A:='USE RecABS;';
B:='DROP TABLE IF EXISTS recipes;';
C:='DROP TABLE IF EXISTS recipeingredients;';
D:='DROP TABLE IF EXISTS ingredients;';
E:='DROP TABLE IF EXISTS types;';
Csql:='CREATE TABLE IF NOT EXISTS recipes ( ' +
' recipeid int(10) NOT NULL AUTO_INCREMENT,' +
' itemname varchar(50) default NULL,' +
' type int(10) NULL default NULL,'+
' description varchar(200) NULL default NULL,' +
' piclocation varchar(60) NULL default NULL,' +
' batchsize varchar(15) NULL default NULL,' +
' time2cook varchar(10) NULL default NULL,' +
' comments varchar(200) NULL default NULL,' +
' PRIMARY KEY (recipeid)' +
' )ENGINE=INNODB DEFAULT CHARSET=latin1; ';
Dsql:='CREATE TABLE recipeingredients ( ' +
' recipeid int(10) NULL,' +
' ingredientid int(10) NULL,' +
' amount varchar(20) default NULL,' +
' recipe_recipeid INT(10) NOT NULL,' +
' Ingredient_ingredientid INT(10) NOT NULL,' +
' PRIMARY KEY (recipeid),' +
' INDEX fk_recipeingredients_recipe1_idx (recipe_recipeid ASC),' +
' INDEX fk_recipeingredients_ingredient1_idx (ingredient_ingredientid ASC),' +
' CONSTRAINT fk_recipeingredients_recipe1,' +
' FOREIGN KEY (recipe_recipeid),' +
' REFERENCES recipe (recipeid),' +
' ON DELETE NO ACTION,' +
' ON UPDATE NO ACTION,' +
' CONSTRAINT fk_recipeingredients_ingredient1,' +
' FOREIGN KEY (ingredients_ingredientid),' +
' REFERENCES ingredients (ingredientid),' +
' ON DELETE NO ACTION,' +
' ON UPDATE NO ACTION' +
' )ENGINE=INNODB DEFAULT CHARSET=latin1; ';
Esql:='CREATE TABLE ingredients ( ' +
' ingredientid int(10) NOT NULL,' +
' name varchar(50) NULL,' +
' supplier varchar(50) NULL,' +
' PRIMARY KEY (ingredientid),' +
' UNIQUE INDEX iname (name ASC)' +
' )ENGINE=INNODB DEFAULT CHARSET=latin1; ';
Fsql:='CREATE TABLE types ( ' +
' typeid int(10) NOT NULL,' +
' name varchar(50) NULL,' +
' recipe_recipeid INT(10) NOT NULL,' +
' PRIMARY KEY (typeid),' +
' UNIQUE INDEX iname (name ASC),' +
' INDEX fk_types_recipe1_idx (recipe_recipeid ASC),' +
' CONSTRAINT fk_types_recipe1,' +
' FOREIGN KEY (recipe_recipeid),' +
' REFERENCES recipe (recipeid),' +
' ON DELETE NO ACTION,' +
' ON UPDATE NO ACTION' +
' )ENGINE=INNODB DEFAULT CHARSET=latin1; ';
DataModule2.dbMyRecipes.Disconnect;
DataModule2.dbMyRecipes.HostName:='192.168.0.199';
DataModule2.dbMyRecipes.User:='henry';
DataModule2.dbMyRecipes.Protocol:='mysql';
DataModule2.dbMyRecipes.Port:=3306;
DataModule2.dbMyRecipes.Password:='3tbhfhcf';
// connect
try
DataModule2.dbMyRecipes.Connect;
except
on E: Exception do showmessage(E.Message);
end;
//create database
Try
DataModule2.dbMyRecipes.ExecuteDirect(sdatabase);
except
on E: Exception do showmessage(E.Message);
end;
//create tables
Try
DataModule2.dbMyRecipes.ExecuteDirect(A);
DataModule2.dbMyRecipes.ExecuteDirect(B);
DataModule2.dbMyRecipes.ExecuteDirect(C);
DataModule2.dbMyRecipes.ExecuteDirect(D);
DataModule2.dbMyRecipes.ExecuteDirect(E);
DataModule2.dbMyRecipes.ExecuteDirect(Csql);
DataModule2.dbMyRecipes.ExecuteDirect(Dsql);
DataModule2.dbMyRecipes.ExecuteDirect(Esql);
DataModule2.dbMyRecipes.ExecuteDirect(Fsql);
showmessage('success create tables..!');
except
on E: Exception do showmessage(E.Message);
end;
End;
Procedure CreateDatabase;
var
sdatabase, A, B, C, D, E, Csql, Dsql, Esql, Fsql : AnsiString;
Begin
sdatabase:='CREATE DATABASE IF NOT EXISTS RecABS';
A:='USE RecABS;';
B:='DROP TABLE IF EXISTS recipes;';
C:='DROP TABLE IF EXISTS recipeingredients;';
D:='DROP TABLE IF EXISTS ingredients;';
E:='DROP TABLE IF EXISTS types;';
Csql:='CREATE TABLE IF NOT EXISTS recipes ( ' +
' recipeid int(10) NOT NULL AUTO_INCREMENT,' +
' itemname varchar(50) default NULL,' +
' type int(10) NULL default NULL,'+
' description varchar(200) NULL default NULL,' +
' piclocation varchar(60) NULL default NULL,' +
' batchsize varchar(15) NULL default NULL,' +
' time2cook varchar(10) NULL default NULL,' +
' comments varchar(200) NULL default NULL,' +
' PRIMARY KEY (recipeid)' +
' )ENGINE=INNODB DEFAULT CHARSET=latin1; ';
Dsql:='CREATE TABLE recipeingredients ( ' +
' recipeid int(10) NULL,' +
' ingredientid int(10) NULL,' +
' amount varchar(20) default NULL,' +
' recipe_recipeid INT(10) NOT NULL,' +
' Ingredient_ingredientid INT(10) NOT NULL,' +
' PRIMARY KEY (recipeid),' +
' INDEX fk_recipeingredients_recipe1_idx (recipe_recipeid ASC),' +
' INDEX fk_recipeingredients_ingredient1_idx (ingredient_ingredientid ASC),' +
' CONSTRAINT fk_recipeingredients_recipe1,' +
' FOREIGN KEY (recipe_recipeid),' +
' REFERENCES recipe (recipeid),' +
' ON DELETE NO ACTION,' +
' ON UPDATE NO ACTION,' +
' CONSTRAINT fk_recipeingredients_ingredient1,' +
' FOREIGN KEY (ingredients_ingredientid),' +
' REFERENCES ingredients (ingredientid),' +
' ON DELETE NO ACTION,' +
' ON UPDATE NO ACTION' +
' )ENGINE=INNODB DEFAULT CHARSET=latin1; ';
Esql:='CREATE TABLE ingredients ( ' +
' ingredientid int(10) NOT NULL,' +
' name varchar(50) NULL,' +
' supplier varchar(50) NULL,' +
' PRIMARY KEY (ingredientid),' +
' UNIQUE INDEX iname (name ASC)' +
' )ENGINE=INNODB DEFAULT CHARSET=latin1; ';
Fsql:='CREATE TABLE types ( ' +
' typeid int(10) NOT NULL,' +
' name varchar(50) NULL,' +
' recipe_recipeid INT(10) NOT NULL,' +
' PRIMARY KEY (typeid),' +
' UNIQUE INDEX iname (name ASC),' +
' INDEX fk_types_recipe1_idx (recipe_recipeid ASC),' +
' CONSTRAINT fk_types_recipe1,' +
' FOREIGN KEY (recipe_recipeid),' +
' REFERENCES recipe (recipeid),' +
' ON DELETE NO ACTION,' +
' ON UPDATE NO ACTION' +
' )ENGINE=INNODB DEFAULT CHARSET=latin1; ';
DataModule2.dbMyRecipes.Disconnect;
DataModule2.dbMyRecipes.HostName:='192.168.0.199';
DataModule2.dbMyRecipes.User:='henry';
DataModule2.dbMyRecipes.Protocol:='mysql';
DataModule2.dbMyRecipes.Port:=3306;
DataModule2.dbMyRecipes.Password:='3tbhfhcf';
// connect
try
DataModule2.dbMyRecipes.Connect;
except
on E: Exception do showmessage(E.Message);
end;
//create database
Try
DataModule2.dbMyRecipes.ExecuteDirect(sdatabase);
except
on E: Exception do showmessage(E.Message);
end;
//create tables
Try
DataModule2.dbMyRecipes.ExecuteDirect(A);
DataModule2.dbMyRecipes.ExecuteDirect(B);
DataModule2.dbMyRecipes.ExecuteDirect(C);
DataModule2.dbMyRecipes.ExecuteDirect(D);
DataModule2.dbMyRecipes.ExecuteDirect(E);
DataModule2.dbMyRecipes.ExecuteDirect(Csql);
DataModule2.dbMyRecipes.ExecuteDirect(Dsql);
DataModule2.dbMyRecipes.ExecuteDirect(Esql);
DataModule2.dbMyRecipes.ExecuteDirect(Fsql);
showmessage('success create tables..!');
except
on E: Exception do showmessage(E.Message);
end;
End;