How to delete old column after merging two in SQL - mysql

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

Related

mysql 5.6 alter table drop column if exists

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

Alter table for a datediff (MySQL)

I am trying to alter a table to change one of my column values to datediff() but I keep getting an error and I don't see where my issue is. Here is my code
alter table bids
modify column 'timeLeft' datediff(curdate(),'dueDate');
if you are trying to change values you should not be using alter table you should be using UPDATE

Sql query - how to create column in table

I have in Msql database several tables with lots of rows and columns in them. I need to create a new column in one of these tables.
How do I create an SQL query for inserting a new column in a specific table
via PhpMyAdmin?
alter table table_name add column new_column int after id
In my example I have added a new column that has data type int after id column. Very easy.
Syntax:
ALTER TABLE tableName ADD columnName columnType;
Example:
ALTER TABLE people ADD hometown VARCHAR(50);
You can use this query in phpMyAdmin by going to the SQL tab after selecting your database.
Also, you may use the graphical user interface to add a column by following the steps:
Select the database on the left side of the screen
Select the table on the left side of the screen
Go to the "Structure" tab
Under the list of all existing columns, you have the option to add new fields (columns)
Simple google revealed this and this
ALTER TABLE contacts ADD email VARCHAR(60);
ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]
Link: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
The command is ALTER TABLE, you can find the details here

ALTER TABLE LIKE

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.

MySQL - How do I use CONCAT in ALTER TABLE?

How do I concatenate in ALTER TABLE?
I tried this, but it didn't work:
$sql1="ALTER TABLE t1 ADD iod = CONCAT('10.1234','/',id)";
id is a different column in the same table.
You're misusing ALTER TABLE. It is intended to modify the data definition (structure) of a table, not its values.
If you want to modify the values in a table, you should use one of the following types of queries:
INSERT
UPDATE
DELETE
Use update after you add a column to fill it out:
ALTER TABLE t1 ADD iod varchar(150)
UPDATE t1 SET iod = CONCAT('10.1234','/',id)
Any new rows that you add would have to include the proper value of iod. Computed columns would solve this, but I don't think they're available on MySQL.