select count(*) into @x from information_schema.STATISTICS where TABLE_SCHEMA = ( select DATABASE() ) and INDEX_NAME = IndexName and TABLE_NAME = TableName;
if @x > 0 then
set @s = concat( 'drop index `' , IndexName , '` on `' , TableName , '`;' );
prepare stmt1 from @s; execute stmt1;
deallocate prepare stmt1; call FAG_DebugPrint(@s); end if;
end;
go
create procedure FAG_AddForeignKey(
in Table1Name text, in Field1Name text, in Table2Name text, in Field2Name text, in xCommentEng text, in xCommentGer text
) begin
set @MyConstraintName = concat( Table1Name , '_' , Field1Name , '_' , Table2Name , '_' , replace(replace(Field2Name collate latin1_general_ci, ',', '_') collate latin1_general_ci, ' ', '') , '_FOREIGN_KEY' );
set @x=0;
select count(*) into @x from information_schema.TABLES as T left join information_schema.TABLE_CONSTRAINTS as TC on T.TABLE_NAME = TC.TABLE_NAME
where T.TABLE_SCHEMA = ( select DATABASE() ) and TC.CONSTRAINT_TYPE = 'FOREIGN KEY' and T.TABLE_NAME = Table1Name and TC.CONSTRAINT_NAME = @MyConstraintName;
if @x = 0 then
set @s = concat( 'alter table `' , Table1Name, '` add constraint `', @MyConstraintName , '` foreign key (`' , Field1Name , '`) references `', Table2Name, '` (`' , Field2Name, '`);' );
prepare stmt1 from @s; execute stmt1;
deallocate prepare stmt1; call FAG_DebugPrint(@s); end if;
end;
go
create procedure FAG_AddUniqueKey(
in TableName text, in FieldName text,
in xCommentEng text, in xCommentGer text
) begin
set @MyConstraintName = concat( TableName , '_' , replace(replace(FieldName collate latin1_general_ci, ',', '_') collate latin1_general_ci, ' ', '') , '_UNIQUE' );
set @x=0;
select count(*) into @x from information_schema.TABLES as T left join information_schema.TABLE_CONSTRAINTS as TC on T.TABLE_NAME = TC.TABLE_NAME
where T.TABLE_SCHEMA = ( select DATABASE() ) and TC.CONSTRAINT_TYPE = 'UNIQUE' and T.TABLE_NAME = TableName and TC.CONSTRAINT_NAME = @MyConstraintName;
if @x = 0 then
set @s = concat( 'alter table `' , TableName, '` add constraint `', @MyConstraintName , '` unique (' , FieldName , ');' );
prepare stmt1 from @s; execute stmt1;
deallocate prepare stmt1; call FAG_DebugPrint(@s); end if;