is it possible in MySQL? In Oracle I could do:
SELECT *
INTO table
FROM view
In MySQL, this does not work:
INSERT INTO table FROM view;
Remember that the table does not exist. I want it to be created based on output from the view.
In MySQL you can create a new table LIKE another table but that doesn't work with views.
You can also create a new table that contains everything from a select, that works from views, selects, joins and everything else. Note that the new table will hold all data from the select so you have to be tricky. Like this.
create table table_from_view select * from view_name where 1 = 0;
You have to add indexes afterwards if you need them.
You can add a SELECT right after table name:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
More information available here: http://dev.mysql.com/doc/refman/5.1/en/create-table-select.html
Related
I know SHOW CREATE TABLE gives you the script in order to create a table based on an existing table but I would like to get the script for the return of a query.
Example : I have lots of really long and complex queries, but now I would like to create tables for what these queries fetch.
Is there a way to do this?
To get the creation script, you can put the results of your query into a temporary table, then use SHOW CREATE TABLE:
CREATE TEMPORARY TABLE query1 AS
SELECT * FROM YourQuery
LIMIT 0; --Add LIMIT 0 to avoid putting all the data into the table TY Bill Karwin
SHOW CREATE TABLE query1;
If you can access the end destination for the table (and you want the results included), you can skip a step and just use CREATE..SELECT:
CREATE TABLE permanent1 AS
SELECT * FROM YourQuery;
I need some assistance with creating temporary tables in phpMyAdmin sql from data in a current table. For example, I have a table called Animals with several columns, one of these columns is called Animal_Size, and I am required to create a temporary table called Small Animals consisting of the animals in the Animals table where the size is small.
Can someone guide me as to the best way to go about doing this?
I've seen some examples but a lot of them don't seem to work.
If you need to select all small animals into new table, you want to use this query:
CREATE TABLE small_animals SELECT * FROM animals WHERE animal_size = 'small'
If you need real temporary table, then just add TEMPORARY into the query:
CREATE TEMPORARY TABLE small_animals_temp SELECT * FROM animals WHERE animal_size = 'small'
Update: Because of problem within the parser in phpMyAdmin, add AS before the SELECT, i.e.:
CREATE TABLE small_animals AS SELECT * FROM animals WHERE animal_size = 'small'
Note: A TEMPORARY table is visible only to the current connection, and
is dropped automatically when the connection is closed.
I can "copy" a table using:
CREATE TABLE copy LIKE original_table
and
CREATE TABLE copy as select * from original_table
In the latter case only the data are copied but not e.g primary keys etc.
So I was wondering when would I prefer using a select as?
These do different things. CREATE TABLE LIKE creates an empty table with the same structure as the original table.
CREATE TABLE AS SELECT inserts the data into the new table. The resulting table is not empty. In addition, CREATE TABLE AS SELECT is often used with more complicated queries, to generate temporary tables. There is no "original" table in this case. The results of the query are just captured as a table.
EDIT:
The "standard" way to do backup is to use . . . . backup at the database level. This backs up all objects in the database. Backing up multiple tables is important, for instance, to maintain relational integrity among the objects.
If you just want a real copy of a table, first do a create table like and then insert into. However, this can pose a challenge with auto_increment fields. You will probably want to drop the auto_increment property on the column so you can populate such columns.
The second form is often used when the new table is not an exact copy of the old table, but contains only selected columns or columns that result from a join.
"Create Table as Select..." are most likely used when you have complex select
e.g:
create table t2 as select * from t1 where x1=7 and y1 <>2 from t1;
Now, apparently you should use Create Like if you don't need such complex selects. You can change the PI in this syntax also.
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.
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;