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.
Related
I'm a beginner in MySQL, and I accidentally created a table with a column named
(price decimal(2,2));
It needs to be decimal(4,2) to allow 4 digits. Since I've already created it, what is the easiest way to update that decimal value to decimal(4,2)? Or do I need to drop that column completely, and re-create it with the correct numbers?
ALTER TABLE mytable MODIFY COLUMN mycolumn newtype
example:
ALTER TABLE YourTableNameHere MODIFY COLUMN YourColumnNameHere decimal(4,2)
Just ALTER TABLE with the MODIFY command:
ALTER TABLE `table` MODIFY `price` DECIMAL(4,2)
This would allow for 2 decimals and 2 full numbers (up to 99.99). If you want 4 full numbers, use 6,2 instead (which would allow up to 9999.99).
It's not a matter of 'UPDATE' it's a matter of changing your table's structure. For that, use ALTER TABLE with MODIFY clause:
ALTER TABLE YourTableName MODIFY COLUMN price DECIMAL(4,2);
sqlfiddle demo
use CHANGE
ALTER TABLE table_name CHANGE OLD_COLUMN_NAME OLD_COLUMN_NAME datatype;
an example
ALTER TABLE table_name CHANGE price price decimal(4,2);
You can change it dynamically:-
ALTER TABLE ".$row["table_name"]." MODIFY ".$row1["Field"]." decimal(".$rdataType[0].",".$increaseValue.")
For more info:
Repository
I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.
Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL
Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.
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
I would like to copy the structure and the content of one mysql table to another, adding all of the columns and values of that table to the already existing ones in the other table.
I could do it manually, but since I'm talking about a large amount of columns, it would be great if there were some sort of ALTER statement to help me do that.
EDIT:
To explain myself better:
I first need to add the columns contained in table B (column_name, data_type) to table A (which already has its own set of columns). Once that is done, I can copy the content, which is easy.
I guess the real question is: is there a way to add the columns contained in table B to another table (table A) which has columns of its own?
To build on flavianatill's second solution, it seems to me that the export/import step is not needed. If I understand the problem correctly, the following one-liner should do it.
CREATE TABLE IF NOT EXISTS merged_table AS (SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id);
Sorry, I would have put this in a comment but I lack the reputation!
This will copy all data from a source table to a target table. You can specify which columns should go to which. by changing the names of targetColumn.. and sourceColumn....
INSERT INTO targetTable (
targetColumn1
targetColumn1
targetColumn1
....
targetColumnN
)
SELECT
sourceColumn1
sourceColumn1
sourceColumn1
....
sourceColumnN
FROM sourceTable
You can also create sourceTable by doing
CREATE TABLE targetTable LIKE sourceTable
EDIT A method to pull all data from sourceTable to targetTable, however removing targetTable if it exists
DROP TABLE IF EXISTS targetTable;
CREATE TABLE targetTable LIKE sourceTable;
INSERT INTO targetTable SELECT * FROM sourceTable;
EDIT If you need to keep old data, you may need to remap it but you can merge in other tables
CREATE TABLE targetTable LIKE sourceTable;
INSERT INTO targetTable SELECT * FROM sourceTable;
INSERT INTO targetTable ( fieldsToInsertTo ) SELECT fieldsToSelectFrom FROM oldTargetTable ON DUPLICATE KEY ......;
DROP TABLE IF EXISTS oldTargetTable;
RENAME TABLE targetTable TO oldTargetTable;
This will however potentially either require ON DUPLICATE KEY UPDATE ..... logic, or simply INSERT IGNORE on the second if you are happy throwing away any PRIMARY/UNIQUE key conflicting rows. This assumes you have sourceTable you want to copy and merge with data from oldTargetTable. The table targetTable is just a temporary name.
If you wanted to prefer data from the old table then just swap the order you perform the INSERTs of course
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