Backup & remove specific records - SQL Server - sql

Is there anyway to:
Remove specific records from the table using a query?
Make a backup from specific records and restore them into another SQL Server instance somewhere else?

1) If ID is the table's PK (or it is unique) you can just use DELETE FROM TABLE_NAME WHERE ID IN (3, 4). You better check if this will not delete other items (or open a transaction, which is always good).
2) If it is just those 4 records and both databases are on the same server (and both tables have the same schema) you can just do (with the same worries that I have expressed in the answer above)
insert into DESTINATION
select * from SOURCE where id between 73 and 76;
Edit: If you really need to do something more like a row backup you can use the bcp utility:
bcp "select * from SOURCE where id between 73 and 76" queryout "file.dat" -T -c
bcp DESTINATION in file.dat -T -c

DELETE FROM ListsItems
WHERE ID = (3, 4);
It will remove your record.
Modify it....

Related

Delete multiple table in DB2

How to delete multiple tables in single query in DB2?
DROP TABLE tableName1,tableName2;
If you tables are in hierarchy - you can use
DROP TABLE HIERARCHY operation, if they aren't - unfortunatly, you can't delete tables in single query and you may delete them separatly.
I tried a lot of things but it never worked, Ultimately this is what works.
db2 "Select 'DROP TABLE ', tabname, ';' from syscat.tables where owner='DB2INST1'" >> filename
This would essentially generate a file called filename, It will have the drop table command for all the tables, You have to open this file up and REMOVE all the tables you don't want to drop and leave the ones which you need. Verify again.
Run this using:
db2 -tvf filename.

Hive insert with multiple select

I want to execute something like this in hive:
insert into mytable values (select count(*) from test2), (select count(*) from test3));
Is there a way to do this?
Why would you need to create a hive table with row count as a column? Assuming that you have to log the row count everyday, I am not sure if we could do this in hive.
But you can try running a shell script something like this if you want a snap shot of the row count of all the tables...
$hive -e 'use schema_name; show tables' | tee tables.txt
This stores all tables in the database in a text file tables.txt
Now, write a shell script to get the counts of all the tables that were gathered
while read line
do
echo "$line "
eval "hive -e 'select count(*) from $line'"
done
change the file permissions for the file generated now
$chmod +x count_tables.sh
$./count_tables.sh < tables.txt > counts.txt
If you are looking for a logging the row count periodically, you can store the rowcounts in a csv, by writing in the values as comma separated values and create an external table pointing to the file.
something like
$./count_tables.sh < tables.txt | sed 's/\t/,/g' > counts.txt
Hope that's the best way to achieve this
I found out the answer. It should be something like this:
INSERT INTO TABLE mytable
SELECT c1,c2 FROM
(SELECT count(*) FROM test2) AS c1
JOIN
(SELECT count(*) FROM test3) AS c2;

pg_dump vs COPY (SELECT * FROM my_table)

I need to copy the contents of a table from one database to another database with an identical table which is currently empty.
I plan to dump the table data from the old table and then simply import it to the empty table in the new database. However, I ran into some behavior I don't understand using pg_dump.
I try to dump the table data to a file with the command:
pg_dump -a -t '"my_table"' my_database > /tmp/my_table.sql
This works, but I only get 8 records and there are over 1000 records in the table if I view the table like so:
SELECT * FROM my_table;
So, I tried to use the COPY command to generate a .csv file and I see similar behavior:
COPY my_table TO '/tmp/my_table.csv' WITH CSV HEADER;
I get the same 8 records as pg_dump. But, with:
COPY (SELECT * FROM my_table) TO '/tmp/my_table.csv' WITH CSV HEADER;
I get all 1266 records.
I would assume these commands should all return the same data, but obviously, I'm wrong. What is the difference?
Is it possible that my_table is part of an inheritance hierarchy? I ask because http://www.postgresql.org/docs/9.0/interactive/sql-copy.html#AEN58984 has this:
COPY only deals with the specific table named; it does not copy data to or from child tables. Thus for example COPY table TO shows the same data as SELECT * FROM ONLY table. But COPY (SELECT * FROM table) TO ... can be used to dump all of the data in an inheritance hierarchy.
You should be able to check by running:
SELECT * FROM ONLY my_table;
If that returns just the 8 records then we're on the right track, and we just need to find the child tables (for which How to find child tables that inherit from another table in PSQL will be helpful).
If not then I'm not sure - I wondered if maybe Rules or Triggers were getting involved, but I can't see how at the moment. Still, maybe it gives someone else an idea...?

Combining two Postgresql similar tables located on two different machines

I have two Windows machines (PC1 & PC2) with PostgreSQL in both. In PC1 I have the table:
And in PC2 I have the same table with the following records:
I want to combine both tables and put them in PC1 to be like (the order is not important):
How can do that? I am using PostgreSQL 9.2 & pgAdminIII. I prefer,if possible, to transfer the data using USB stick rather than a network.
This is what i would have done:
PC1-> pgadmin-> yourtablename -> right click -> backup
File options: format:plain, encoding:your_ecnoding
dump options #1: only data, use column inserts.
this will create the sql query. replace yourtablename with yourtablename2 and excecute it on PC2
delete duplicate records and add data:
delete from yourtablename2 where id in (select id from yourtablename)
insert into yourtablename
select * from yourtablename2
drop table yourtablename2
You could just dump your data out:
pg_dump --data-only dbname > outfile.sql
This will give you a file of all the data. The big issue if you'll have to worry about duplicates when inserting this data back into the other node.
This is how you'd import the data:
psql --set ON_ERROR_STOP=on dbname < outfile.sql
Another solution for you if you continue to need these two databases synced is to use some of PostgreSQL's replication strategies. http://www.postgresql.org/docs/9.2/static/warm-standby.html#STREAMING-REPLICATION

SQL Command for copying table

What is the SQL command to copy a table from one database to another database?
I am using MySQL and I have two databases x and y. Suppose I have a table in x called a and I need to copy that table to y database.
Sorry if the question is too novice.
Thanks.
If the target table doesn't exist....
CREATE TABLE dest_table AS (SELECT * FROM source_table);
If the target table does exist
INSERT INTO dest_table (SELECT * FROM source_table);
Caveat: Only tested in Oracle
If your two database are separated, the simplest thing to do would be to create a dump of your table and to load it into the second database. Refer to your database manual to see how a dump can be performed.
Otherwise you can use the following syntax (for MySQL)
INSERT INTO database_b.table (SELECT * FROM database_a.table)
Since your scenario involves two different databases, the correct query should be...
INSERT INTO Y..dest_table (SELECT * FROM source_table);
Query assumes, you are running it using X database.
If you just want to copy the contents, you might be looking for select into:
http://www.w3schools.com/Sql/sql_select_into.asp. This will not create an identical copy though, it will just copy every row from one table to another.
At the command line
mysqldump somedb sometable -u user -p | mysql otherdb -u user -p
then type both passwords.
This works even if they are on different hosts (just add the -h parameter as usual), which you can't do with insert select.
Be careful not to accidentally pipe into the wrong db or you will end up dropping the sometable table in that db! (The dump will start with 'drop table sometable').
insert blah from select suggested by others is good for copying the data under mysql.
If you want to copy the table structure you might want to use the show create table Tablename; statement.