Importing Data From One database to another - sql

I wanted to know that, Suppose I have a table in one database with say 1000 records and I have similar table in another database with say 500 records.
Now my question is If I will try to import data from say DB1.Tbl1 to DB2.Tbl1, then what will happen? Is there any possibilities of duplicacy of the data?
I wanted the records of DB1.Tbl1 to copy into DB2.Tbl1 table. Please clear my confusion.

If you have same data in both tables you can first truncate 2nd table after that you can import data from 1st table by Insert Command or "import data" task

Try this
INSERT INTO DB1.dbo.Tbl1 SELECT * FROM DB2.dbo.Tbl2
This just moves the data. If you want to move the table definition, you have to do something else.
Note that, SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.
If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.

Try this:
We can copy all columns from one table to another, existing table:
INSERT INTO table2
SELECT * FROM table1;
Or we can copy only the columns we want to into another, existing table:
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

Related

How to copy table from one Data Base to another?

I want to copy some tables from my DB1 to my DB2. Tables in DB1 are same as tables on DB2 but data in the tables are different. I want to copy tables from DB1 to DB2 but to keep old tables and data on DB2. How I can do this with Microsoft SQL Server Management Studio? I tried to right click and do the export but before I have to click on Finish button looks like that will just copt all data from DB1 to DB2 and I do not want that. If anyone can help with this please let me know. Thank you.
You can export the tables from DB1 with another name to DB2 if you don't want to modify them. In the export wizard just change the name of the destination table.
So you want to merge the schema AND data from DB1 into DB2?
You should list out the exact requirements, the question is still vague even with that info.
What data do you want to keep, what is ok to blow out?
What schema do you want to keep, are you archiving the old tables? Changing table names?
If you are literally trying to merge db1 into db2 your issue is going to be in managing the relationship ids which will be getting reassigned since DB2 could already be using IDs that are present in DB1.
If you want to keep old data in the destination table (or juste update it), so you might use a Merge:
MERGE INTO db2.dbo.table1 B
USING (
SELECT *
FROM db1.dbo.table1) A
ON (A.Column1 = B.Column1 )
WHEN MATCHED THEN
-- update
WHEN NOT MATCHED THEN
-- insert new rows
USE db2;
CREATE TABLE table2 LIKE db1.table1;
INSERT INTO table2
SELECT * FROM db1.table1;
This is also a way to copy a table with its records to another database.

Huge Data Base in oracle

I had about 20,000,000 records
in a table (random data), and then I added empty column to that table...
but when I update that table to fill that column, the process was broken down..
I tried to use the cursor and the index but no results..
do you have any fast solution or any alternative solution?
Thank you in advance :)
Maybe the fastest way would be to create new_table as select * from existing table, and then inside the select statment of CTAS, calculate the value of the new column. After that, you can rename old table to something like table_bckp, then rename new table to the original table name, and then apply constraints, indexes, and other scripts previously saved from old table definitions.

Oracle SQL merge tables without specifying columns

I have a table people with less than 100,000 records and I have taken a backup of this table using the following:
create table people_backup as select * from people
I add some new records to my people table over time, but eventually I want to merge the records from my backup table into people. Unfortunately I cannot simply DROP my table as my new records will be lost!
So I want to update the records in my people table using the records from people_backup, based on their primary key id and I have found 2 ways to do this:
MERGE the tables together
use some sort of fancy correlated update
Great! However, both of these methods use SET and make me specify what columns I want to update. Unfortunately I am lazy and the structure of people may change over time and while my CTAS statement doesn't need to be updated, my update/merge script will need changes, which feels like unnecessary work for me.
Is there a way merge entire rows without having to specify columns? I see here that not specifying columns during an INSERT will direct SQL to insert values by order, can the same methodology be applied here, is this safe?
NB: The structure of the table will not change between backups
Given that your table is small, you could simply
DELETE FROM table t
WHERE EXISTS( SELECT 1
FROM backup b
WHERE t.key = b.key );
INSERT INTO table
SELECT *
FROM backup;
That is slow and not particularly elegant (particularly if most of the data from the backup hasn't changed) but assuming the columns in the two tables match, it does allow you to not list out the columns. Personally, I'd much prefer writing out the column names (presumably those don't change all that often) so that I could do an update.

Copying data want to keep to a new table and then rename

I have a Windows 2008 Ent. Server running SQL 2008 Ent. There's a SQL DB that is approximately 100Gb in size. One of the tables in this database accounts for 85Gb. After doing some analysis this one table has got to this size from one event being logged which has no value so it's been agreed that we can purge all instances of this event from this table. The table should be only around 1Gb in size once all occurrences of this event have been removed.
Someone suggested to me that a more efficient approach rather than going through trying to delete rows which include this event id to perhaps create the table again with no data. Copy the data that we want to keep to the new table (ommitting any row with event id). Rename the original table to something and then rename the new table to original table name.
The approach makes sense and I can see that perhaps this will be faster than deleting 100 rows at a time out of the original table. Can I please have some advice on how to go about this?
The query to copy everything to the new table goes like this:
SELECT * INTO dbo.NewTable FROM dbo.OldTable WHERE [event id] <> 6030
Then:
ALTER TABLE dbo.OldTable
RENAME TO dbo.OldTable_History;
And:
ALTER TABLE dbo.NewTable
RENAME TO dbo.OldTable;
If you want to create the table manually do it then and after that run this:
INSERT INTO dbo.NewTable
SELECT * FROM dbo.OldTable WHERE [event id] <> 6030

merge msaccess tables

How can i merge two identical tables of two msaccess identical db? For eg:
db1..table1
ID Name
1 Sanjana
2 Parul
3 Rohan
db2...table1
ID Name
1 Sarika
2 Deepak
I want to append the values of second table into first as follows:
ID Name
1 Sanjana
2 Parul
3 Rohan
4 Sarika
5 Deepak
The datatype for the ID field appears to be an autonumber. As such you can do the following:
INSERT INTO db1...table1
SELECT Name FROM db2...table1
You can use an append query:
INSERT INTO Table1 ( FName ) IN 'c:\docs\ltd.mdb'
SELECT A.FName
FROM A;
OK, heres an approach more suited to a beginner making use of the gui.
Backup both databases and store them away somewhere safe.
Do a compact and repair from the tools menu on both databases
Create a linked table in db1 pointing to the table in db2
To do this right click on some white space in the table view of database window and choose link table... Follow the wizard to select db2 and then select table1.
Use an append query to append the data from the linked table1 into the db1.table1
Click into the queries view of Access, create a new query in design view, change its type to Append (right click in free space where the tables appear and go to type->append) Then choose db1.table1 when prompted as the table to append to. Now add the linked table1 into the query, select the fields from which you want to take data (in the example it would just be Name). Note you do not want to take the id field across as this will need to be updated to follow on from where db1.table1 left off, im assuming this is set to autonumber.
Delete the linked table from db1
Im not 100% certain the sort order will be retained from db2.table1 when its appended to db1.table1 as in your example. In most database designs this wont be important but if it is someone else may be able to shed light - i imagine that if the ID field in both tables is also the primary key it will.
Well since it is access, you have two ways. the first is mentioned by LSFR Consulting
and the second would be to use the import wizard and tell the import to ignore the primary key column. That would merge the data from db2 into DB1 without having a primary key collision.
If this is a one time operation simple copy-n-paste will work.
Open both databases in MS Access. Open both tables. Select values to copy (right-click on Column header and Ctrl+C).
Proceed to target table. Make Name field selected in the last row (new record). For this purpose mouse over Name column left edge (cursor becomes a plus sign) and click to select the cell. Ctrl+V. Done.