MySql create temporary table from source table without indexes - mysql

I'm trying to "duplicate" a table from my db creating a temporary one. However I need indexes are not copied.
I'm using this query right now:
CREATE TEMPORARY TABLE temp365 LIKE contactlens;
but the result table contains also indexes. I checked out the documentation and I don't see a way to copy the structure without indexes.
Because I can't have static name for indexes, I was wondering how I can drop all of them using simple SQL.
I was starting trying to avoid to copy them at all, but it seems not possible.

It's just to provide some examples to #wchiquito's comment.
It's pretty easy to create a copy of a table without indexes using CREATE TABLE ... SELECT command.
If you don't need to copy any rows from original table just provide a false value in WHERE clause or specify 0 in LIMIT one. Some examples:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] temp365 SELECT * FROM contactlens WHERE 0;
or a bit different way:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] temp365 SELECT * FROM contactlens LIMIT 0;
This way you'll get the same result as with CREATE TABLE ... LIKE, but without indexes.

Related

How to clone SQL table with index

I have a problem with a mysql table which I'd like to clone and give another name. When I try the
CREATE TABLE data1 AS SELECT * FROM data;
It gets stuck in execution, and never actually do anything, and I have to abort.
The thing is that my table "data" has some extra commands in the sql source like
CREATE INDEX keywords ON data (keywords);
It has a few of these, and I suspect that this is what's causing the issue, since I have been able to clone other tables without these extra commmands. I'm new in sql, so I have no idea how to overcome this problem. Anyone care to help?
Use is create table like syntax. to create a table with the same structure and after this you can copy the data.
https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html
a second solution is to use mysqldump to extract the table structur including the data, rename the table in dump file and Reimport the it.
-- To copy table with constraints
CREATE TABLE new_table_name LIKE old_table_name;
-- To copy without constraints
CREATE TABLE new_table_name
(SELECT * FROM old_table_name WHERE 0);
source

mysql: Is it a good practice to create a regular table instead of a temporary table for a query?

I need to put all the result of a query to a temporary table then select from it, but due to the nature of the temporary table, I cannot refer to it more than once in the same query. So is it ok to create a uniquely named (regular) table for this and drop it after the query is completed? Or is there a better way to do this?
I tried using Derived tables but I cannot access it from different blocks.
It's not necessarily a matter of whether it's good practice or not. Whether it's a regular table, temporary table or derived table they all serve a purpose when trying to accomplish a task. Therefore, creating a regular table or a temporary table all depends on you and what you want your application to accomplish at the end of the day.
But based on your problem it seems that you might need to create a regular table in this situation since you require access to the table more than once, and you can drop the table after you're finished with it.

Retrieve CREATE TABLE code of an already existing table?

Is there a way to do this?
In case the DBMS command history got cleaned or, in my case, when many ALTER TABLE were used in the course of time.
I'm using MySQL.
Yes, it is as simple as
SHOW CREATE TABLE yourtable;
This will include all the subsequent ALTER TABLE statements. You cannot retrieve the table's original state.
Here is the relevant documentation

Change column name without recreating the MySQL table

Is there a way to rename a column on an InnoDB table without a major alter?
The table is pretty big and I want to avoid major downtime.
Renaming a column (with ALTER TABLE ... CHANGE COLUMN) unfortunately requires MySQL to run a full table copy.
Check out pt-online-schema-change. This helps you to make many types of ALTER changes to a table without locking the whole table for the duration of the ALTER. You can continue to read and write the original table while it's copying the data into the new table. Changes are captured and applied to the new table through triggers.
Example:
pt-online-schema-change h=localhost,D=databasename,t=tablename \
--alter 'CHANGE COLUMN oldname newname NUMERIC(9,2) NOT NULL'
Update: MySQL 5.6 can do some types of ALTER operations without rebuilding the table, and changing the name of a column is one of those supported as an online change. See http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html for an overview of which types of alterations do or don't support this.
If there aren't any constraints on it, you can alter it without a hassle as far as I know. If there are you'll have to remove the constraints first, alter and add the constraints back.
Altering a table with many rows can take a long time (though if the columns involved are not indexed, it may be trivial).
If you specifically want to avoid using the ALTER TABLE syntax created specifically for that purpose, you can always create a table with almost the exact same structure (but different name) and copy all the data into it, like so:
CREATE TABLE `your_table2` ...;
-- (using the query from SHOW CREATE TABLE `your_table`,
-- but modified with your new column changes)
LOCK TABLES `your_table` WRITE;
INSERT INTO `your_table2` SELECT * FROM `your_table`;
RENAME TABLE `your_table` TO `your_table_old`, `your_table2` TO `your_table`;
For some ALTER TABLE queries, the above can be quite a bit faster. However, for a simple column name change, it could be trivial. I might try creating an identical table and performing the change on it in order to see how much time you're actually looking at.

Could someone help me understand and convert this MySql statement into Postgresql?

so the code I am working on has this statement executed by PHP (note:This is taken from the PostgreSQL log file so it doesn't include any PHP stuff):
CREATE temporary table IF NOT EXIST temp tablename(id int primary key,
shared int default 0) replace select 1, userid as id
from tablefoo where sharedid = 1337
I don't quite understand what's going on here exactly, I know what a temporary table is, and I can quite accurately guestimate what IF NOT EXIST does, but what is replace doing here? I know replace is like insert but it replaces stuff as well, but in this case, nothing is specified for it to replace with, so does it just replace something with nothing and why the Select 1, I know that pretty much just tells you if your table has rows or something, but what is the point of using it here?
After some research, I found that IF NOT EXIST and replace do not exist in PostgreSQL. Most online sources suggest that SQL functions be used to replace them.
Should I use an SQL function to emulate IF NOT EXIST? If so, what would I write (sorry, I am pretty new to SQL) or should I just use a PHP function.
What about replace?
Sorry for the trouble, thanks for your time, oh and if you guys aren't busy or anything, you could also tell me about how to emulate "ignore", my current solution involves arbitrarily removing it.
Many uses in MySQL for temporary tables can be replaced in PostgreSQL with common table expressions or ordinary subselects.
WITH someCTE AS (
SELECT
...
) SELECT/UPDATE/DELETE ... WHERE sometable.column = someCTE.another_column;
Look into CREATE TABLE documentation. Temporary tables are just as name suggests not permanent:
If specified, the table is created as a temporary table. Temporary
tables are automatically dropped at the end of a session, or
optionally at the end of the current transaction (see ON COMMIT
below). Existing permanent tables with the same name are not visible
to the current session while the temporary table exists, unless they
are referenced with schema-qualified names. Any indexes created on a
temporary table are automatically temporary as well.
In particular temp tables are stored in diffrent (non-public) schema, e.g.:
=> Create Temporary Table someTempTable (value integer);
CREATE TABLE
=> \dt someTempTable
List of relations
Schema | Name | Type | Owner
-----------+---------------+-------+----------
pg_temp_2 | sometemptable | table | postgres
(1 row)
PostgreSQL doesn't have IF NOT EXISTS like in MySQL's CREATE TABLE, so you can't use it. If you want to create some table you need to firstly drop existing one (if it exists). Fortunately you could use SQL command DROP TABLE IF EXISTS to handle this:
=> Drop Table If Exists someTempTable;
DROP TABLE
=> Drop Table If Exists someTempTable;
NOTICE: table "sometemptable" does not exist, skipping
DROP TABLE