I am trying to populate a table with a subset of data from a select query and I want this table to be recreated at regular intervals.
I tried to create an event which inserts records but it duplicates the records.
I then tried to drop and then create the table in an event but it tells me the table already exists
This is what I am trying to achieve:
DROP TABLE IF EXISTS tablea;
CREATE TABLE tablea;
INSERT INTO tablea
SELECT * from tableb WHERE etc.
What is the proper way to achieve this?
are you in the right schema? I ask as your drop table command is fine and if you are in the right schema (use whateverdb;), it will work.
Your commands all look ok if they are summarised.
if tablea exists in your current schema:
drop table if exists tablea;
is fine
to create a table, you need to specify the fields eg.
create table tablea (column1 varchar(20), column2 int, etc etc);
or better still, if the tables are the same structure, you could ignore that part and create the table with a create table like statement:
drop table if exists tablea;
create table tablea like tableb;
insert into tablea select * from tableb where column1 = whatever;
If tablea doesn't need the same properties as tableb, you can even do:
drop table if exists tablea;
create table tableb as select * from tableb where column1 = whatever;
The only disadvantage of this method over the stament above it is that it will not carry through the defined keys and special properties (if there are any).
And finally, if you are going to be recreating tablea over and over again, then, after you have initially created tablea (create table tablea like tableb; ) you can just keep rerunning these two lines below:
truncate tablea;
insert into tablea select * from tableb where whatever;
will be best as truncate (empty the contents) is much quicker than drop and leave your keys/properties in tact.
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 a large table I am trying to add a field to, so far it has been much more efficient due to the table sizes I am working with to do
CREATE TABLE A LIKE B;
INSERT B SELECT * FROM A;
When making changes vs doing any Alter Table (Index, FIelds, etc.)
However if I do
CREATE TABLE A LIKE B;
Alter TABLE B ADD Fieldx varchar(100);
INSERT B SELECT * FROM A;
I cannot do that anymore as I get a field count mismatch. Clearly I can "just" do:
CREATE TABLE A LIKE B;
Alter TABLE B ADD Fieldx varchar(100);
INSERT Into B(Field1,..,FieldN) SELECT Field1,...,FieldN FROM A;
However I am trying to do this with a large # of tables each of which has a large # of fields and I've already got a good batch process that works with * and would be much much harder to try to explicitly push each field for each table into each Insert and Select.
Is there any way to express the
INSERT B SELECT * FROM A;
When B now has 1 more column than A in a way that will not return a count error?
As long as the new column is added to the end of the original columns, you can do:
INSERT INTO B
SELECT *, ""
FROM A;
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 write a Mysql query for the following scenario.
1.check if a table( ex: tableA) exists.
2.Check if data is there in the table.
3.If tableA exists and data is there in the table move all data to another table( ex: tableB) (tableB there in db and both tables are having same structure)
4.drop tableA
Is it possible to write a mysql query avoiding mysql stored procedure ?
I was able to do first three with the below query, but drop table was not possible.
Hope it helps!
There are two tables: table_1(old),table_2(new). Both have one column "ID".
insert into table_2(id) #inserts into table
select case when
(
select count(*) from table_1 a #checks if there are any records in the table
where exists #checks if table exists
(select 1 from table_1 b where a.id=b.id))>0 then
id
else 0 end
from table_1
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.