Gibt es eine Möglichkeit zu überprüfen, ob eine Spalte in einer mySQL-Datenbank vorhanden ist, bevor (oder so) die Anweisung ALTER TABLE ADD coumn_name
ausgeführt wird? Eine Art IF column DOES NOT EXIST ALTER TABLE
Sache.
Ich habe ALTER IGNORE TABLE my_table ADD my_column
versucht, aber es wird immer noch der Fehler ausgegeben, wenn die hinzugefügte Spalte bereits vorhanden ist.
BEARBEITEN: Use Case ist das Upgrade einer Tabelle in einer bereits installierten Web-App. Um die Dinge einfach zu halten, möchte ich sicherstellen, dass die benötigten Spalten vorhanden sind. Wenn dies nicht der Fall ist, fügen Sie sie mit ALTER TABLE
hinzu.
Da MySQL-Steueranweisungen (z. B. "IF") nur in gespeicherten Prozeduren funktionieren, kann eine temporäre Anweisung erstellt und ausgeführt werden:
DROP PROCEDURE IF EXISTS add_version_to_actor;
DELIMITER $$
CREATE DEFINER=CURRENT_USER PROCEDURE add_version_to_actor ( )
BEGIN
DECLARE colName TEXT;
SELECT column_name INTO colName
FROM information_schema.columns
WHERE table_schema = 'connjur'
AND table_name = 'actor'
AND column_name = 'version';
IF colName is null THEN
ALTER TABLE actor ADD version TINYINT NOT NULL DEFAULT '1' COMMENT 'code version of actor when stored';
END IF;
END$$
DELIMITER ;
CALL add_version_to_actor;
DROP PROCEDURE add_version_to_actor;
Glaubst du, du kannst es versuchen ?:
SELECT IFNULL(column_name, '') INTO @colName
FROM information_schema.columns
WHERE table_name = 'my_table'
AND column_name = 'my_column';
IF @colName = '' THEN
-- ALTER COMMAND GOES HERE --
END IF;
Es ist kein One-Liner, aber können Sie wenigstens sehen, ob es für Sie funktioniert? Zumindest beim Warten auf eine bessere Lösung ..
Erstens habe ich eine Reihe von Hilfsfunktionen und Prozeduren, mit denen ich Fremdwörter, normale Schlüssel und Spalten verwende. Ich lasse sie nur in der Datenbank, damit ich sie bei Bedarf verwenden kann.
Hier sind sie.
delimiter $$
create function column_exists(ptable text, pcolumn text)
returns bool
reads sql data
begin
declare result bool;
select
count(*)
into
result
from
information_schema.columns
where
`table_schema` = 'my_database' and
`table_name` = ptable and
`column_name` = pcolumn;
return result;
end $$
create function constraint_exists(ptable text, pconstraint text)
returns bool
reads sql data
begin
declare result bool;
select
count(*)
into
result
from
information_schema.table_constraints
where
`constraint_schema` = 'my_database' and
`table_schema` = 'my_database' and
`table_name` = ptable and
`constraint_name` = pconstraint;
return result;
end $$
create procedure drop_fk_if_exists(ptable text, pconstraint text)
begin
if constraint_exists(ptable, pconstraint) then
set @stat = concat('alter table ', ptable, ' drop foreign key ', pconstraint);
prepare pstat from @stat;
execute pstat;
end if;
end $$
create procedure drop_key_if_exists(ptable text, pconstraint text)
begin
if constraint_exists(ptable, pconstraint) then
set @stat = concat('alter table ', ptable, ' drop key ', pconstraint);
prepare pstat from @stat;
execute pstat;
end if;
end $$
create procedure drop_column_if_exists(ptable text, pcolumn text)
begin
if column_exists(ptable, pcolumn) then
set @stat = concat('alter table ', ptable, ' drop column ', pcolumn);
prepare pstat from @stat;
execute pstat;
end if;
end $$
delimiter ;
Wenn diese vorhanden sind, ist es ziemlich einfach, sie zu verwenden, um Spalten und Einschränkungen auf Existenz zu prüfen:
-- Drop service.component_id
call drop_fk_if_exists('service', 'fk_service_1');
call drop_key_if_exists('service', 'component_id');
call drop_column_if_exists('service', 'component_id');
-- Drop commit.component_id
call drop_fk_if_exists('commit', 'commit_ibfk_1');
call drop_key_if_exists('commit', 'commit_idx1');
call drop_column_if_exists('commit', 'component_id');
-- Drop component.application_id
call drop_fk_if_exists('component', 'fk_component_1');
call drop_key_if_exists('component', 'application_id');
call drop_column_if_exists('component', 'application_id');
Machen Sie einen Zählsatz mit dem Beispiel von John Watson.
SELECT count(*) FROM information_schema.COLUMNS
WHERE COLUMN_NAME = '...'
and TABLE_NAME = '...'
and TABLE_SCHEMA = '...'
Speichern Sie dieses Ergebnis in einer Ganzzahl und machen Sie dann die Bedingung, um den ADD COLUMN
-Satz anzuwenden.
Obwohl dies ein ziemlich alter Beitrag ist, fühle ich mich trotzdem gut dabei, meine Lösung für dieses Problem zu teilen. Wenn die Spalte nicht existiert, würde definitiv eine Ausnahme auftreten, und dann erstelle ich die Spalte in der Tabelle.
Ich habe gerade den folgenden Code verwendet:
try
{
DATABASE_QUERY="SELECT gender from USER;";
db.rawQuery(DATABASE_QUERY, null);
}
catch (Exception e)
{
e.printStackTrace();
DATABASE_UPGRADE="alter table USER ADD COLUMN gender VARCHAR(10) DEFAULT 0;";
db.execSQL(DATABASE_UPGRADE);
}
DELIMITER $$
DROP PROCEDURE IF EXISTS `addcol` $$
CREATE DEFINER=`admin`@`localhost` PROCEDURE `addcol`(tbn varchar(45), cn varchar(45), ct varchar(45))
BEGIN
#tbn: table name, cn: column name, ct: column type
DECLARE CONTINUE HANDLER FOR 1060 BEGIN END;
set cn = REPLACE(cn, ' ','_');
set @a = '';
set @a = CONCAT("ALTER TABLE `", tbn ,"` ADD column `", cn ,"` ", ct);
PREPARE stmt FROM @a;
EXECUTE stmt;
END $$
DELIMITER ;
Diese Syntax funktioniert für mich:
SPALTEN VON <Tabellenname> WIE '<Spaltenname>' ANZEIGEN
Mehr in diesem Beitrag: https://mzulkamal.com/blog/mysql-5-7-check-if-column-exist?viewmode=
Sie können eine Prozedur mit einem CONTINUE-Handler erstellen, falls die Spalte vorhanden ist (bitte beachten Sie, dass dieser Code in PHPMyAdmin nicht funktioniert):
DROP PROCEDURE IF EXISTS foo;
CREATE PROCEDURE foo() BEGIN
DECLARE CONTINUE HANDLER FOR 1060 BEGIN END;
ALTER TABLE `tableName` ADD `columnName` int(10) NULL AFTER `otherColumn`;
END;
CALL foo();
DROP PROCEDURE foo;
Dieser Code sollte keinen Fehler auslösen, falls die Spalte bereits vorhanden ist. Es wird einfach nichts tun und den Rest des SQL-Prozesses fortsetzen.