Hi i m using SQL 2008 R2.What is the faster way to insert data of 10 mil. records from one table A into another empty table B.
Table A and B dont have same schema similar but not the same.
It's only 10 million rows. Just use a normal INSERT
INSERT tableB (col1, col2, col3, ...)
SELECT col1, col2, col3, ... FROM tableA
or
--assumes no table b already
SELECT col1, col2, col3, ..., INTO TableB FROM tableA
.. now add some columns etc
Your using sql?
why not create a new table
and then insert records from table a into new table b??
Related
If you don't know what are the columns(or how many columns are there) in a table, how to check whether a particular column exists or not in that table using MySQL?
For example,
A table "DemoTable" contains columns Col1, Col2, Col3 with some data. And you write a select query like,
Select col1, col2, col3, col4 from DemoTable. But col4 does not exists in the DemoTable, so it will through an error. How to check whether col4 exists in the DemoTable in the select query or before the select query? Without a procedure.
There are two tables with same name in two DB's, in those tables there are few columns with same name, while others columns have different names. For example, DemoTable has columns col1, col2, col3, col4 in one DB and in other DB, DemoTable has columns col1, col2, col3, col5, col6. But the condition is, a single select query should be used to fetch data from the table without an error, so we need to check whether that columns first exists or not. Query like Select col1, col2, col5 from DemoTable from any DB.
This question already has answers here:
INSERT INTO ... SELECT without detailing all columns
(11 answers)
Closed 8 years ago.
I have a syntax that can copy all records from tables that have the same fields:
INSERT INTO table2 SELECT * FROM table1 WHERE id = '7'
So I would like to know how do I keep copying all values except the 'id' field (because him is primary key and auto increment).
You list the columns:
insert into table2(col1, . . . coln)
select col1, . . ., coln
from table1
where id = '7';
Under most circumstances, you should list the columns explicitly. You can run into problems even when tables have the same columns but in a different order.
(The one exception in code that I would write is in a script where table1 is created from table2 using create table as.)
You have to list all the columns specifically:
INSERT INTO table2 (col1, col2, col3, ...)
SELECT col1, col2, col3, ...
FROM table1
WHERE id = '7'
You'd need to specify the fields you're copying explicitly:
INSERT INTO table2 (col1, col2, col3, etc)
SELECT col1, col2, col3, etc
FROM table1
WHERE id = '7'
I have 2 tables in the DB:
Table1
Table1_Temp
The Table1_Temp was generated from a CSV. it is almost identical to Table1 but different because all Table1_Temp fields are VARCHAR and it has some irrelevant fields.
I need to move the Data from Table1_Temp to Table1 but to keep the structure of Table1, and disregard the unnecessary fields from Table1_Temp.
How can I do it?
Choose the columns to use and cast them to the necessary type in your select
insert into table1 (col1, col2, col3)
select cast(col1 as signed), col5, col7
from Table1_Temp
if both tables are on a different database (and different column)
INSERT INTO db1.table1 (Acol1, Acol2, Acol3)
SELECT Bcol1 AS Acol1, Bcol2 AS Acol2, Bcol3 AS Acol3
FROM db2.table1_temp
This will only work if both databases are under 1 server (in this instance "localhost")
I have two tables,
tblA(id, num, col1, col2, col3),
tblB(col1, col2, col3)
col1, col2 and col3 are the same in both tables. Now I have following sql:
declare #num...(same type as num)
insert into tblA
select #num, * from tblB
id in tblA is an indentity column.
But I got following error,
Column name or number of supplied values does not match table definition.
Can anyone help me to fix it?
Can you please try providing the column names as well,
declare #num...(same type as num)
insert into tblA(num, col1, col2, col3)
select #num, * from tblB
Please don't worry about identity column as it will get filled automatically.
Just INSERT using named columns, and skip the identity column - it will be filled automatically:
INSERT INTO tblA (num, col1, col2, col3) SELECT #Num, col1, col2, col3 FROM tblB
I think the error message is quite explicative: the SELECT and the INSERT has to have the same number of columns.
in your case
declare #num...(same type as num)
insert into tblA(num,col1, col2, col3)
select #num,col1, col2, col3 from tblB
if the key on tblA is not auto-generated you have to consider it in the INSERT
more info here
It simply based on your column name they should be of same type:
insert into tblA(col1,col2,col3)
select col1,col2,col3
from tblB
there is a existing table A. suppose i want to add all or specific(column) values of an existing table A to table B using foreign key, how do i do it in MySQL?
and if there is any new insert or update in table A it should automatically insert into table B also..
To automatically update Table B from changes in Table A would require triggers that MySQL supports but phpMyAdmin does not. If instead you're looking to insert rows into Table B from Table A on an ad-hoc basis then that's simple
INSERT INTO TABLEA (COL1, COL2, COL3)
SELECT FROM TABLEB (COL1, COL2, COL3)
WHERE (SELECT COUNT(*) FROM TABLEA WHERE TABLEA.COL1 = TABLEB.COL1) = 0
The above SQL does a simple copy from TableB into TableA. The WHERE clause ensures only records which don't already exist are inserted.
You can use insert/update trigger for that.
Why should you want to do that?
Why don't you just reference the data in table A using the forign key in table B you mentioned?
insert into tableb(columns) select columns from tablea