简体   繁体   中英

MySQL Alter table add column if not exists

i have error with my if. ERROR is "IF" is not valid in this position, expecting EOF, ALTER, ANALYZE...

my code looks like this can you help me please:)

IF NOT EXISTS (
  SELECT
    1
  FROM
    INFORMATION_SCHEMA.COLUMNS
  WHERE
    TABLE_NAME = 'clients' AND COLUMN_NAME = 'dateOfRegister')
BEGIN
  ALTER TABLE realestate
    ADD dateOfRegister DATE NOT NULL
END;

add delimter like this to your request.

delimiter |
IF NOT EXISTS (
  SELECT
    1
  FROM
    INFORMATION_SCHEMA.COLUMNS
  WHERE
    TABLE_NAME = 'clients' AND COLUMN_NAME = 'dateOfRegister')
BEGIN
  ALTER TABLE realestate ADD dateOfRegister DATE NOT NULL;
END;
delimiter ;

but it seems that your request will not do what you want to do. it is better to create a procedure or a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM