Copy Data From One Database To Another In Same Table - mysql

I want to copy data from one database which has table named domains to another database which also has the table named domains to it.
I have tried doing it using phpmyadmin but it doesn't copy maybe because of Auto_increment values. it just doesn't get copied to another database.table.
I want to know what can be done in this regard? also I don't want to copy old ID(auto_increment) values from first database to another.
phpmyadmin response.
#1136 - Column count doesn't match value count at row 1
both the structures are same in the databases still this.
My Query.
INSERT INTO `site1`.`domains`
SELECT * FROM `site33`.`domains`
^ This much is fixed.
now comes the auto_increment problem I Get:
#1062 - Duplicate entry '1' for key 'PRIMARY'

You could do something like this:
INSERT INTO database2.table1 (field2,field3)
SELECT table2.field2,table2.field3
FROM table2;
Note:
Do not include the column which is auto increment in your insert and select statement.
Sql Fiddle example

You are using an insert statement that has an incorrect number of values, for instance:
for a table with columns a, b and c, these are all invalid:
INSERT INTO YourTable VALUES (1, 2);
INSERT INTO YourTable(b, c) VALUES (1, 2, 3);
INSERT INTO YourTable(a, b, c) VALUES (2, 3);
The number of columns in the column list must match the number of values in the value list.
You may only omit the column list, but only if you specify one value for each column and in the order in which the columns exist in the table. This is bad practice, though. It is better to always specify the exact columns you need.

Related

Insert into table from another with different columns count (MySQL)

Question, why this dose not work?
create table one (a int(1) default 1, b int(2));
create table two (b int(1));
insert into one select * from two;
error:
Column count doesn't match value count at row 1
a know it, a can count, but why, philosophically?
database knows, what the name of inserting column from table two is b, knows that the column a in table one has a default value equal 1..
so, what problem of executing this query?
And general - How can i do this differently, not manual, without information of a columns and their count, if this way is impossible?
I know this:
table two always have all the same columns, that the table one have. But table one have another columns too, that have a some default values.
Is there some way to do that? insert all data from two in one, and fill the remaining columns by some default or other values!
Need help!
Thank you very match!
When you run:
insert into one
select * from two;
The SQL engine automatically puts in the columns that are implied.
For the insert, this is the list of columns in declaration order.
For the *, this is the list of columns in declaration order.
There is no "matching" of columns by names, only lists of columns in each table.
So, the query is really:
insert into one(a, b)
select b from two;
That looks like an error to me.
Moral of the story? Write that code that you intend. Always include columns lists, particularly for insert statements. So write:
insert into one(b)
select b from two;

MySQL: Making 2 values unique

I'm trying to make 2 values unique, like if I have the values (5, 10) the same values can't be added again.
I'm currently selecting from the table the values x and y, checking if they both together exists on the table if they don't exists insert them, in other words
"Select * from location where x=? and y=?"
if no result is returned it will continue to insert the values.
This is typically accomplished by creating a unique index on both columns combined (a multi-column index).
Then, MySQL will prevent you from inserting duplicates. You can go ahead and try to insert the record, and if you get a duplicate key error, you know it already exists.
Alternatively, another way to handle it is to use INSERT IGNORE, so that no error occurs if you try to insert a duplicate row. Still, it won't insert, so you simply check the affected ROW_COUNT() to see if the insert was successful.
Using a unique index and catching the failure on the insert is more performant than selecting then trying to insert because in the case you do insert, MySQL only has to perform one search, rather than two.

How to insert whilst removing rows which do not match the insert values

I want to insert/remove a table's rows by comparing existing rows with rows that I wish to insert. Then remove any rows which don't match my 'insert rows' and insert any rows that don't already exist (from my insert values).
I could delete all rows each time and then insert but I wonder if there's a more efficient way?
(data will likely only change by a couple of rows be that removed or added)
For example I may have the values (1, 2, 3, 4, 7) which I wish to insert. The database holds (1, 2, 3, 6) so I would want to remove '6' and insert '4' and '7'. These are keys on the table but would be affected by some WHERE clause.
Also the actual scenario is that a user may have 'items' which belong to them. Their items can change i.e. be added to or removed from and I want all user_items stored in one table where each instance of an item has a unique id.
It's the clear and insert routine. Fast and easy.
TRUNCATE table;
INSERT INTO table VALUES (blaah);

insert in database with column number

i want to insert in a table using columns order not name
insert into tableName(1,2,5) values('val1','val2','val3');
i dont want to use
insert into tableName values('val1','val2','val3');
because the table does not contain just 3 columns
how can i do it
because columns name are encrypted so I can not rely on this
insert into tableX("cCGSvKJVQXnt8A==","aDOlOQrPfg==","qsdcx112")
values('val1','val2','val3');
is there any idea how can i deal with this
thank
You can't use the ordinal number of a column in an insert statement. However, you can accomplish what you're trying to do (insert values into specific columns in a table) using the column names instead.
Presume your table has five columns; I'm going to call them "Alpha", "Bravo", "Charlie", "Delta", and "Echo", since you haven't given us the schema for your table, but replace these names with the names of the columns actually in your table. I'm guessing that your third and fourth column (my "Charlie" and "Delta") are nullable. You can then insert a tuple/row in your table with the other three columns filled using syntax like this:
INSERT INTO TableName(Alpha, Bravo, Echo) VALUES ("val1", "val2", "val3");
If, per your comments above, your column names are unprintable characters (which is a terrible, terrible idea), you can explicitly insert NULLs into the missing columns:
INSERT INTO TableName VALUES ("val1", "val2", NULL, NULL, "val3");
but the weakness here is that, if additional columns are subsequently added to your table's schema, the insert statement will start failing.
You need to put the column names where you have the 1,2,5. You can't use the column number.
insert into tableName(1,2,5) values("val1","val2","val3");

Insert to Access 2007 table with some values from other tables

I am using Access 2007 [normally SQL Server] I'm trying to insert records into a table whereby certain values are ID's from other tables. For example:
insert into table values ((select id from another_table), 1, 'Hello', etc)
This is possible in SQL Server.
I get an error that says, Query must contain at least one table or something...
Anyone know the syntax for this in Access? I've tested all the selects and they all produce the expected results, but when inserted in the above type of statement, I get the error.
I don't want to extract too much data into memory, so I'd prefer to get the above to work, instead of loading the ID's and names as a collection of objects.
I am not exactly sure what you want, but perhaps you are looking at it from the wrong angle?
INSERT INTO Table (ID, FK_ID, F2)
SELECT 1, T2_ID, "Hello"
FROM Table2
WHERE ID=1