Sql query - how to create column in table - mysql

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

Related

header row extraction and insertion in different tables using sql studio

I would like to extract the row header from a table and insert it in another table with the same number of columns and data type, how can i do it in sql studio without having to retype the whole headers again ?
header row
second table
You can use create table like
CREATE TABLE new_tbl LIKE orig_tbl;
this create an empty table with the same column name of the orig_tbl
if you need to change the column name you can use alter table
ALTER TABLE your_table_name CHANGE `Column 0` `Extension Field 1` ;

MySQL ALTER TABLE sort columns alphabetically

I am maintaining a legacy application with MySQL database with most of the tables having 20+ columns and few have 100+. To make the it friendlier I am trying to alter all the tables to arrange all the columns sorted alphabetically.
What would be the appropriate ALTER TABLE queries ?
There is no way for changing column order in a mysql table. However, you can create a new table with the columns in your order, using for example:
CREATE TABLE newtable SELECT a,b,c,d,e,f,g FROM old_table_name
This way, your newly created table will have columns in your defined order, and you can drop the old table and rename the newtable to old name.
In order to create the above mentioned query, you just need to get column names from your old table and sort them, to do that programatically you can use something like this:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='database_name' AND TABLE_NAME = 'old_table_name'
ORDER BY column_name

How to delete old column after merging two in SQL

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

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.

change MySQL field type if field exists

I have the following query:
ALTER TABLE table CHANGE field1 field1 INTEGER
Is there anyway that I can add some sort of if exists to this statement so that MySQL will make sure that field 1 exists before attempting to change its field type?
ALTER TABLE in MySql does not take an IF EXISTS clause.
You can do a
describe table
or
show columns from table
to get the list of all columns and then check if field1 is present before you do a ALTER TABLE