Solved

Add Column To Mysql Table

Posted on 2009-07-13
2
371 Views
Last Modified: 2012-05-07
Hi to all,
I want to check field if not exists so add a field to table.
But i want to it just PURE SQL... with not php or etc...
is there any way?
/*
i want to add myfield  if it's not exists. just PURE SQL pls..
*/
ALTER TABLE mytable
ADD myfield varchar(20)

Open in new window

0
Comment
Question by:erenpasa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 24841033
Just add it anyway. If the field exists, it will return an error. If it doesn't exist, it will add it. If this is part of some longer script, run it with mysql --force < file.sql
0
 

Accepted Solution

by:
erenpasa earned 0 total points
ID: 24841179
DELIMITER $$
DROP PROCEDURE IF EXISTS AddColumn$$
CREATE PROCEDURE AddColumn(
      IN dbName TINYTEXT,
      IN tableName TINYTEXT,
      IN fieldName TINYTEXT,
      IN fieldDef TEXT)
BEGIN
      IF NOT EXISTS (
            SELECT * FROM information_schema.COLUMNS
            WHERE column_name=fieldName
            AND table_name=tableName
            AND table_schema=dbName
            )
      THEN
            SET @ddl=CONCAT('ALTER TABLE ',dbName,'.',tableName,
                  ' ADD COLUMN ',fieldName,' ',fieldDef);
            PREPARE stmt FROM @ddl;
            EXECUTE stmt;
      END IF;
END$$
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question