i'm asking this question after googling with no luck. it has been asked before on SOF but that's long ago MySQL Alter syntax to drop a column if it exists. Hope things have changed since.
Is there a straight forward way to drop a column in table if it exists.
I'm using MySql 5.6, i would like to wrap this alter statement around an if to avoid any issue if the script runs twice:
ALTER TABLE xyz
DROP COLUMN abc,
ADD COLUMN ghi DATE NOT NULL AFTER column4;
Thanks
You may check if column exists and after perform the other operations.
See here: MySQL, Check if a column exists in a table with SQL
Related
I regularly take caution when creating tables for database. This time I came across a situation to alter a table.
I wanted to change the Data Type of a field/column in Table.
To change the data type of a column in a table: General Syntax
ALTER TABLE table_name ALTER COLUMN column_name datatype
SQL Query :
alter table employee modify column ename varchar(40);
I Rarely use Alter and Modify commands. This time when tried, it throws me an Error as ORA-00905: missing keyword.
I surfed the Internet and referred StackOverflow raised questions, but there is no relative answer found.
I also referred this Oracle Documentation :
Oracle Reference Documentation
But It did not help.
Do this
ALTER TABLE table_name MODIFY column_name datatype
Try This:
ALTER TABLE Shopper MODIFY MobileNo(varchar2(15))
I was looking for an answer on how to combine two columns into a new one and found this to be the perfect answer. However, since I really have NO experience with MySQL I was wondering how I would delete the old columns, too, when the table is edited.
ALTER TABLE tableName DROP COLUMN columnName
To drop columns (multiple) after merging it you can use the following Syntax.
ALTER TABLE table_Name DROP COLUMN columnName1, DROP COLUMN columnName2;
See ALTER TABLE Syntax
you can alter the table to drop the column. the syntax for that is
ALTER TABLE yourTable DROP COLUMN yourColumn
Is it possible to use the LIKE statement on ALTER TABLE similar to CREATE TABLE in MySQL?
Eg. 'CREATE TABLE db.tbl1 LIKE db.tbl2'
This clones a database table's structure. I want to alter an existing table with the same columns but to pick up the primary keys of another table.
I was thinking of something like 'ALTER TABLE db.tbl1 LIKE db.tbl2' but this throws back an error.
Any ideas?
Thanks
I required a similar thing and settled to use the following procedure:
ALTER TABLE tbl1 RENAME tbl1_old;
CREATE TABLE tbl1 LIKE tbl2;
INSERT INTO tbl1 SELECT * FROM tbl1_old;
DROP TABLE tbl1_old;
Altough this is not a single statement it should do the job. Only problem could be different index settings (UNIQUE, etc.) which cause errors when doing the "INSERT INTO" of the original table contents.
Everything ALTER TABLE can do is explained here.
As you can see importing indexes from another table is not mentioned. You could probably do that with some clever information_schema querying, but I don't think it would be worth the cost.
It seems you can't.
I tried writing query using exists, but no success so far. Searching hasn't helped so far.
If you attempt to alter a table that does not exist, the query will fail with an error: Table 'database.table' doesn't exist
MySQL does support ALTER IGNORE TABLE, but that only turns errors into warnings if you're attempting to create a unique index while there are values in the table that violate that index.
If you would like to make sure that you do not produce any database queries, I would suggest ensuring the table's existence using SHOW TABLES LIKE 'tablename' before running your ALTER TABLE query.
I ran into a problem where one version of my database has a column that is in the wrong position. Is there a way in mysql to re-index or exchange columns?
ALTER TABLE tbl CHANGE old_col new_col integer AFTER sec_col