I have been looking for a way to duplicate a row, and insert it back to the table, but with a different id value (whose type is auto increment).
I could do this by specifying every column manually, but as there are many columns, and as the columns can be added or removed in the future, I want to use some easy query to do this without having to specify every column name.
Try this:
CREATE TEMPORARY TABLE temp_table SELECT * FROM source_table WHERE ...;
ALTER TABLE temp_table DROP COLUMN column_with_auto_increment;
INSERT INTO source_table SELECT * from temp_table; DROP TABLE temp_table;
Try
INSERT INTO new_table (attr1, attr2, attr3) SELECT oldatr1, oldatr2, oldatr3 FROM old_table WHERE <the filter you want>
It also may work if new_table and old_table are the same.
Related
I'm trying to remove duplicates from a table where FieldA,FieldB and FieldC are identical. I want to keep the record where FieldD is NOT NULL.
I generally remove duplicates (and prevent future ones) like so:
CREATE TABLE newtable LIKE oldtable;
INSERT newtable SELECT * FROM oldtable group by FieldA,FieldB,FieldC;
Drop Table oldtable;
Alter Table newtable RENAME oldtable;
CREATE Unique INDEX UniqueIndex ON oldtable (FieldA,FieldB,FieldC)
However I am unclear how to modify this to include the Not Null FieldD. It occurs to me I could use a Max(Char_Length(FieldD) but that simply seems to return the max value for each group, not the record with the max valule
For now I did the following though not (IMHO) a perfect solution
Update Table1 as T1
Inner Join Table1 as T2
On T1.FieldA=T2.FieldA
And T1.FieldB=T2.FieldB
And T1.Field=T2.FieldC
Set T1.FieldD=T2.FieldD
Where T1.FieldD is NULL and T2.FieldD is NOT NULL
This allowed me to standardize FieldD to a single non-null value and then I was able to easily remove dupes using the sequence I posted above:
CREATE TABLE newtable LIKE oldtable;
INSERT newtable SELECT * FROM oldtable group by FieldA,FieldB,FieldC;
Drop Table oldtable;
Alter Table newtable RENAME oldtable;
CREATE Unique INDEX UniqueIndex ON oldtable (FieldA,FieldB,FieldC)
In an ideal world I'd have figured out an update query to remove the dupes per the question but this intermediary step worked fine for now.
Leaving the question open and unsolved in case someone has a more direct solution to my question.
I have crate a db in MySQL which has a lot of tables. I want the value of one table to be automatically saved on another table too.
For example I write something on: table1.lastname, I want this to be also stored in table2.lastname .
How is this called and how I can do that with PHP My Admin?
CREATE TABLE new_table_name LIKE old_table_name
Create trigger after_insert on new_table
like this
CREATE TRIGGER `AFTER_INSERT` AFTER INSERT ON `new_table_name` FOR EACH ROW BEGIN
insert into new_table_name (column_names) values (column_values) ;
END
For first you must create table for data store.
Then you must create trigger on wanted table for catch event and insert data in early created table.
This will do what you want:
INSERT INTO table2 (lastname)
SELECT lastname
FROM table1
If you want to include all rows from table1. Otherwise you can add a WHERE statement to the end if you want to add only a subset of table1.
I hope this helps.
If the table doesn't exist, you can create one with the same schema like so:
CREATE TABLE table2 LIKE table1;
Then, to copy the data over:
INSERT INTO table2 SELECT * FROM table1
Or If the tables have different structures you can also:
INSERT INTO table2 (`col1`,`col2`) SELECT `col1`,`col2` FROM table1;
EDIT: to constrain this..
INSERT INTO table2 (`col1_`,`col2_`) SELECT `col1`,`col2` FROM
table1 WHERE `foo`=1
I want to achieve the following use the following command to add a column to an existing table:
ALTER TABLE foo ADD COLUMN bar AFTER COLUMN old_column;
Can this option take substantially longer than the same command without the AFTER COLUMN option, as follows?
ALTER TABLE foo ADD COLUMN bar;
Will the first command use a greater amount of tmp table space during execution to perform the action?
Context: I have a very large table (think over a billion rows) and I want to add an additional column using the AFTER COLUMN option, but I don't want to be penalized too much.
Here's what I would do:
CREATE TABLE newtable LIKE oldtable;
ALTER TABLE newtable ADD COLUMN columnname INT(10) UNSIGNED NOT NULL DEFAULT 0;
I don't know the type of your column. I give an example with INT. Now here you can specify WHERE you want to add this new column. By default it will add it at the end unless you specify the AFTER keyword, if you provide it, you will have to specify in the order you will insert otherwise you need to put it at the end.
INSERT INTO newtable SELECT field1, field2, field3 /*etc...*/, newcolumn = 0 FROM oldtable;
OR, if you added it between columns:
# eg: ALTER TABLE newtable ADD COLUMN columnname INT(10) UNSIGNED NULL AFTER field2;
INSERT INTO newtable SELECT field1, field2, newcolumn = 0, field3 /*etc...*/ FROM oldtable;
You can add a where clause if you want to do them in batch.
Once all the records are there
DROP TABLE oldtable;
RENAME TABLE newtable to oldtable;
Create another table and alter the new table. ( like Book Of Zeus did )
And using ALTER TABLE newtable DISABLE KEYS and ALTER TABLE newtable ENABLE KEYS before and after the inserting query can make it faster. ( like below )
CREATE TABLE newtable ....;
ALTER TABLE newtable ....;
ALTER TABLE newtable DISABLE KEYS;
INSERT INTO newtable ....;
ALTER TABLE newtable ENABLE KEYS;
DROP TABLE oldtable;
While the other answers are useful as examples of the syntax required to add columns to a table, the answer to the actual question was provided by N.B.:
You'd get more CPU usage since records would have to be shifted.
From the memory usage point of view - it'd be the same with AFTER
COLUMN option and without it.
In most cases, a tmp table is created. There are MySQL engines that support hot schema changes (TokuDB being one) that don't create
the tmp table and waste tons of resources.
However, if you're doing this with MyISAM or InnoDB - I'd say that
"AFTER COLUMN" option will take slightly more time due to record
shifting.
– N.B.
I have to change the values in my table and create an temporary table from that. But not with UPDATE, since I have to keep the original table.
for example a table like Table(id, date), I have to create a temp table by changing the date values. The ones which are NULL must be CURRENT_DATE().
How can I manage that??
Create temporary table with (id,date)
then
INSERT INTO tempoaryTable (id,date)
SELECT id, IFNULL(date,CURRENT_DATE())
FROM yourTable
You can use INSERT-SELECT form to copy all values from original table to temporary, setting corresponding field to CURRENT_DATE().
Syntax might be off as I don't now mysql
insert into temp_table
select id,coalesce(date,current_date) from mytable
How to store results from following query into another table. Considering there is an appropriate table already created.
SELECT labels.label,shortabstracts.ShortAbstract,images.LinkToImage,types.Type
FROM ner.images,ner.labels,ner.shortabstracts,ner.types
WHERE
labels.Resource=images.Resource
AND labels.Resource=shortabstracts.Resource
AND labels.Resource=types.Resource;
If the table doesn't exist (and you e.g. don't want to create it because it may have lots of column names) you can create it on the fly...
Query:
CREATE TABLE another_table SELECT /* your query goes here */
You can use the INSERT INTO TABLE SELECT....syntax:
INSERT INTO new_table_name
SELECT labels.label,shortabstracts.ShortAbstract,images.LinkToImage,types.Type
FROM ner.images,ner.labels,ner.shortabstracts,ner.types
WHERE labels.Resource=images.Resource AND labels.Resource=shortabstracts.Resource
AND labels.Resource=types.Resource;
if your table dosen't exist then
CREATE TABLE new_table SELECT //write your query here
if your table exist then you can just insert query
INSERT INTO new_table SELECT //write your query here
For more check here and here
INSERT INTO another_table SELECT /*your query goes here*/
In SQLite Studio, I noticed that "AS" keyword is needed:
Query:
CREATE TABLE another_table AS SELECT /* your query goes here */