header row extraction and insertion in different tables using sql studio - mysql

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` ;

Related

adding computed column in destination table from temporary table

I have a stored procedure in which the data is populated into the destination table using a temporary table(in this i perform all the computation).
the problem i am facing is i have two columns which are getting its value by concatenating two other columns.
i.e.
concat(status_code|status_reason) as STATUS
concat(prior_status_code|prior_status_reason) as PRIOR_STATUS
Now this concatenation i have done it in temporary table.
i have to load its data into a destination table and the destination table doesn't have the columns STATUS and PRIOR_STATUS.
for loading the data i am using
insert into Destination( column_names,Status,prior_status)
select (column_names) from temporary table
so when i execute this part it throws me an error that columns are invalid as not present in the table.
P.S. I have used alter command to add those two columns in the destination. Still not getting the desired result.
Is there any other way to do this and where am i going wrong in this?
please help.
Thanks in advance
try this to add you field. it is important that you put the fieldnames in Backquotes. status is a reserved word.
ALTER TABLE youTable
ADD COLUMN `status` VARCHAR(100),
ADD COLUMN `PRIOR_STATUS` VARCHAR(100);
you can also specify the position of the field in the table like:
ALTER TABLE youTable
ADD COLUMN `status` VARCHAR(100) AFTER fieldxx,
ADD COLUMN `PRIOR_STATUS` VARCHAR(100) BEFORE fieldzz;

how to add column to a mysql table by selecting columns from another table

I know how to create table based on columns of other mysql table
as
create table table_name as select column1,column2 from table2;
Now i want to know how to add column to a table by selecting columns from another table,
something like below
alter table table1 add column as select column from table2
Is that possible?
You can get a table's structure with the following query:
select *
from information_schema.table_name
Which you can then use the result to either create or alter another table.

Create as Select and add an extra column

I want to create a MySQL table as a copy of another table like this:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
The twist is that I want, if possible, to add at the time of creation another empty column, that will be populated at a later time.
I know that I can just create it as above and use ALTER TABLE afterwards, but my thinking is that, given a large amount of data, the ALTER is gonna take a long time (please contradict me if this is wrong), that can be saved if what I want is possible.
So, say I want an extra extra_col - varchar(64), what would my original query be?
Thanks.
As documented under CREATE TABLE ... SELECT Syntax:
You can create one table from another by adding a SELECT statement at the end of the CREATE TABLE statement:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
MySQL creates new columns for all elements in the SELECT.
[ deletia ]
Notice that the columns from the SELECT statement are appended to the right side of the table, not overlapped onto it.
[ deletia ]
In a table resulting from CREATE TABLE ... SELECT, columns named only in the CREATE TABLE part come first. Columns named in both parts or only in the SELECT part come after that. The data type of SELECT columns can be overridden by also specifying the column in the CREATE TABLE part.
Therefore:
CREATE TABLE new_tbl (
extra_col VARCHAR(64)
) SELECT * FROM orig_tbl
#user1703809 You have a workaround if you want to place the added column at the end by placing the extra column in the Select statement as :
CREATE |TEMPORARY| TABLE IF NOT EXISTS new_tbl
SELECT *, REPEAT('-',64) extra_col
FROM orig_tbl;
This will create your new table with an added column "extra_col" - varchar(64). Furthermore, you may create the table as temporary if it suits you, and if you just want to create an empty table for further use, just add a "LIMIT 0" at the end of the statement.
Furthermore, this way, you may add a column in any position of the field list at the Select statement.
It's been sometime since you asked the question but I'm still hoping to be of some help.

INSERT ... SELECT to same table

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.

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