Create an SQL table drawing columns from another - mysql

I have an SQL table with the columns (primaryKey,name,age,weight) and I want to generate a new table containing all the data from the original but only the columns (primaryKey,age). Can anyone suggest the SQL command for that.

CREATE TABLE new_tbl SELECT primary_key,age FROM orig_tbl ;
Based on the docs.

Make a trigger, so when you insert a record, it insert another to the other table: Here is the manual Here is an example

Related

How can I create a MySQL temporary table table and display its data in one query?

I am trying to write a single query to create a new temporary table and get its values in MySQL. The reason behind wanting to do it in one query is I need to pass the query as a variable in a javascript code. When I try adding two queries, the code throws an error.
I tried the following query but it doesn't do the trick:
SELECT * FROM CREATE TEMPORARY TABLE IF NOT EXISTS temp
SELECT student.id,
student.name,
student.age FROM student;
Is there a way I can successfully create a temporary table and display its results in one query?
Thank

how to create a new table from `describe` statement in sql?

I want to create a new table with rows of the column names. Essentially, I want to put the output of a decribe statement into a new table, so that I can use a where clause to only extract certain column names. I'm using sparksql.
How do I do this? Thanks.
No need to create an additional table just use either SHOW COLUMNS command instead of describe with filter, or query information_schema.columns table with a select.

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.

Mysql query to copy the structure of a table to create another table

Looking for help in creating a Mysql query to copy the structure of an existing table to create another table.
To create a table as in an exact replica of another table:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
If you want to also copy the contents of the table you can do:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
INSERT INTO `new_table_name` SELECT * FROM `old_table_name`;
If you want to copy the table structure including its keys, then you should use:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
To copy the whole table
CREATE TABLE `new_table_name` SELECT * FROM `old_table_name`;
It will create the table and insert all the data from the old table but without bringing the keys from the old table. So you will need to set the keys for the new table.
Its old thread but if someone needed. If you want create query that will create exactly copy of existing table.
show create table <existing table name>
MySQL query to copy the structure of a table to create another table structure without data another way is...
CREATE TABLE `table_name_new` select * from `table_name_old` limit 0;
also you can use this:
CREATE TABLE 'new_table_name' SELECT * FROM 'pattern_table' where 1=0;

Transferring all rows of a MySQL table to another

I want to bulk insert all rows from one table to another. I am confused on how to use Select with Insert. Is there a way that a new table is automatically created if it does not exist?
There are two ways to do this:
One is to INSERT INTO ... SELECT, which will insert the resultset of your query into an existing table with the same data structure as your query
INSERT INTO MyTable_Backup
SELECT * FROM MyTable
The other is to CREATE TABLE ... SELECT ..., which will create a new table based on the data structure of your query an insert the resultset.
CREATE TABLE MyTable_Backup
SELECT * FROM MyTable;
However one thing to note is that this will not match the indexes of the source table. If you need indexes, you need to add them manually.
trigger and/or select into are recommended here