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;
Related
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
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 have a MySQL query which uses 3 tables with 2 inner joins. Then, I have to find the maximum of a group from this query output. Combining them both is beyond me. Can I break down the problem by storing the output of the first complicated query into some sort of temporary table, give it a name and then use this table in a new query? This will make the code more manageable. Thank you for your help.
This is very straightforward:
CREATE TEMPORARY TABLE tempname AS (
SELECT whatever, whatever
FROM rawtable
JOIN othertable ON this = that
)
The temporary table will vanish when your connection closes. A temp table contains the data that was captured at the time it was created.
You can also create a view, like so.
CREATE VIEW viewname AS (
SELECT whatever, whatever
FROM rawtable
JOIN othertable ON this = that
)
Views are permanent objects (they don't vanish when your connection closes) but they retrieve data from the underlying tables at the time you invoke them.
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.
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