ALTER TABLE with Large number of rows - mysql

I have an sales table with millions of records and I want to extend it with a new field. I know ALTER TABLE would certainly lead never ending process. Is there a faster way to do this?

Create table TEMP_TABLE WITH similer to your old table with additional column
INSERT INTO TAMP_TABLE (SELECT * FROM TABLE);
DROP TABLE OLD_TABLE;
RENAME TABLE TEMP_TABLE TO OLD_TABLE_NAME;

Related

how can i move all data of one table to another table

I need to transfer all data of one table to another dumping table.
My purpose is to get table ready for daily transaction and previous data should be moved to another table which stores every days data.
i need mysql syntax for this, thank you in advance for your support and help
You can try these queries:
This query will copy the data and structure, but the indexes are not included:
CREATE TABLE new_table SELECT * FROM old_table;
To copy everything, including database objects such as indexes, primary key constraint, foreign key constraints, triggers run these queries:
CREATE TABLE new_table LIKE old_table;
INSERT new_table SELECT * FROM old_table;
To insert data into an existing table, use this :
INSERT INTO table2 SELECT * FROM table1

Creating new table like old table

When we create a copy of a table in this condition old table CONSTRAINT are not copied, I used
create table new_table like old_table;
so what i do that we can also copy the old_table CONSTRAINT.
From the CREATE TABLE docs
CREATE TABLE ... LIKE does not preserve any DATA DIRECTORY or INDEX DIRECTORY table options that were specified for the original table, or any foreign key definitions.
From the CREATE TABLE... SELECT docs
CREATE TABLE ... SELECT does not automatically create any indexes for you. This is done intentionally to make the statement as flexible as possible. If you want to have indexes in the created table, you should specify these before the SELECT statement.
Conclusion
So in other words it is not directly possible..
Work around
In my projects I almost always use SHOW CREATE TABLE old_table to get the SQL and manually tweak the code.
Try this
CREATE TABLE new_table LIKE old_table;
Then you insert with
INSERT INTO new_table SELECT * FROM old_table;
It's already specified in the documentation.
CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;
Try this both queries
CREATE TABLE new_table LIKE old_table;
INSERT INTO new_table SELECT * FROM old_table;

Create a temporary table in a SELECT statement without a separate CREATE TABLE

Is it possible to create a temporary (session only) table from a select statement without using a create table statement and specifying each column type? I know derived tables are capable of this, but those are super-temporary (statement-only) and I want to re-use.
It would save time if I did not have to write up a create table command and keep the column list and type list matched up.
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)
From the manual found at http://dev.mysql.com/doc/refman/5.7/en/create-table.html
You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.) To create temporary tables, you must have the CREATE TEMPORARY TABLES privilege.
In addition to psparrow's answer if you need to add an index to your temporary table do:
CREATE TEMPORARY TABLE IF NOT EXISTS
temp_table ( INDEX(col_2) )
ENGINE=MyISAM
AS (
SELECT col_1, coll_2, coll_3
FROM mytable
)
It also works with PRIMARY KEY
Use this syntax:
CREATE TEMPORARY TABLE t1 (select * from t2);
Engine must be before select:
CREATE TEMPORARY TABLE temp1 ENGINE=MEMORY
as (select * from table1)
ENGINE=MEMORY is not supported when table contains BLOB/TEXT columns
As I understand it, a SELECT statement will work on the temporary table if you're using it in something like phpMyAdmin, but following that SELECT, the temporary table will be gone. This means set up exactly what you want to do with it first, and don't view any results till your 'action' statements that change the data (DELETE, UPDATE) are complete.

Pass index to temporary table from regular table?

I am creating a temp table with a query like this:
CREATE TEMPORARY TABLE temp_table
SELECT * FROM regular_table
WHERE 1
But regular_table has FULLTEXT index on some of the fields. I try to do a FULLTEXT search on the new temporary table and I get an error telling me "Can't find FULLTEXT index matching the column list". So obviusly the index is not copying over to the new table. Is there a way to force this?
Thanks.
You could use CREATE TEMPORARY TABLE temp_table LIKE regular_table, but that will create all the indexes, so when you do INSERT INTO temp_table SELECT * FROM regular_table, the indexes will be rebuilt - which could be lengthy.
Or, you can create the table and add the index afterwards:
CREATE TEMPORARY TABLE temp_table
ALTER TABLE temp_table ADD FULLTEXT INDEX (foo,bar,baz)
INSERT INTO temp_table SELECT * FROM regular_table
but the index will be, again, updated on every insert.
Probably the most efficient way would be to create the temp table, insert all, build index afterwards:
CREATE TEMPORARY TABLE temp_table
ALTER TABLE temp_table ADD FULLTEXT INDEX (foo,bar,baz)
ALTER TABLE temp_table DISABLE KEYS
INSERT INTO temp_table SELECT * FROM regular_table
ALTER TABLE temp_table ENABLE KEYS
Again, you will have to wait for the index to build, except it will happen in one chunk, with the last ALTER statement.
A temporary table is exactly the same as any other table except that it will be dropped at the end of the session. The only way to have the same indexes (from within the database) is to create them on the table as you would any other table.
Now there is a bit of a hack. You can copy the physical files on disk to a new name and have a clone of the table which includes indexes but I'm assuming you're doing this within an app so that might not be very practical.

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