Findout Recently updated records in SQL Server - sql-server-2008

I have a table with 4 columns:
--------------------------------------
| ID (PK) | Name | Addr | Mobile |
--------------------------------------
Someone updated some records without ID. So how to find the recently updated records or updated Ids in SQL Server?

Related

MySQL Moving table from varchar to int

I am moving an old Mantis table that had a varchar(64) category_id column to a new Mantis table that has a int(10) category_id column.
The simplified structure is as follows
bug_table (Old DB)
+----+-------------+-------------+--------+
| id | project_id | category_id | report |
+----+-------------+-------------+--------+
| 1 | 0 | Server | crash |
| 2 | 0 | Database | error |
| 3 | 1 | Server | bug |
| 4 | 1 | Server | crash |
+----+-------------+-------------+--------+
category_table (New DB)
+----+------------+----------+
| id | project_id | name |
+----+------------+----------+
| 0 | 1 | Server |
| 1 | 1 | Database |
| 2 | 2 | Server |
| 3 | 2 | Database |
+----+------------+----------+
I need a magical query that will replace category_id in the bug_table with the numerical category_id in the category_table. Thankfully I am able to match rows by project_id and categories by name.
Here is the query I am working on but have gotten stuck in the complexity
UPDATE bug_table b SET b.category_id = c.id USING category_table WHERE b.category_id = c.name
I like to approach such a task a little differently than you do for a new lookup/reference table.
To me, the new category table would only have id and name columns. There are only two rows based on the sample data: Server and Database. Yes, I realize there could be other names, but those can easily be added, and should be added, before proceeding to maximize the id matching that follows.
Next I would add a new column to the bug table that could be called 'category_new' with the data type that will store the new category id. Alternatively, you could rename the existing category_id column to category, and the new column for the id's could then be column_id.
After all that is done then you can update the new column by joining the category on names and set the id that matches: (note this assumes the non-alternative approach mentioned in step 2)
UPDATE bug_table JOIN category_table ON bug_table.category_id = category_table.name
SET bug_table.category_new = category_table.id
After that runs, check the new column to verify the updated id's.
Finally, after successful update, now the old category_id column (with the names) from the bugs_table can be dropped, and the category_new column can be renamed as the category_id.
=====
Note that if you decide to go with the alternative column approach mentioned, of course the query will be similar but differ slightly. Then only a column drop is needed at the end
If there are other tables to apply the same category changes, the operation (basically steps 2 through 5) would be similar for those tables too.

MySQL database with many tables

I want to create some number of tables (lets say as sub tables) and make all sub tables to point to one main table. If i query with main table I should be able get details from all sub tables together or even from individual subtable table details. I am using python interface for querying.
This is my requirement. Kindly suggest me some ideas. Thanks in advance.
I don't know why are you not using mySQL on terminal to use queries.Why are you using python?
So as per your requirement let us take an example.Let us create a database book_info where there are 4 tables-
tbl_books
tbl_author
tbl_book_reviews
tbl_book_sales
Let tbl_book be the main table.
It will contain-
| book_id | book_title | publication | price | ISBN |
tbl_author will contain-
| author_id | first_name | last_name | DOB | address | email_id |
tbl_book_reviews will contain-
| review_id | book_id | | author_id | Review |
And finally tbl_book_sales will contain-
| book_sale_id | book_id | years | city | quantity
So you can get data from all the tables using Joins as book_id is used as a foreign key in every table.
Try creating tables and executing queries on terminal or mysql Workbench.

Versioning Database

we are currently disguessing our Versioning Strategy.
We looked at the Hibernate Envers and first it looked helpful for us.
But now we are heading to a problem that caused by our complex data structure.
We have much table that we want to versioning that are referencing each other.
Foo is referencing to bar and multiple times to user. All of these Table should become an history table like 'Foo_log','bar_log' and user_log.
With hibernate Envers this works fine, it creates the log tables like the original ones and creates an revision table. The log table have the revision ID and the revision Type.
Our problem is now that we would like to be able to create new Entries for the past and with that our current strategy doesn't work anymore.
Example 1:
Foo should be created for the past, let's say 01.01.2015. The referenced bar and 'user's should be the Entry that's active on 01.01.2015.
Example 2:
Foo should be created for the past, let's say 01.01.2015.
An new bar should be created also for 01.01.2015 and be referenced in Foo.
user should be the Entry that's active on 01.01.2015.
Our idea is to find the next Revision ID by the date 01.01.2015 and fetch the referenced Entries by that Revision ID. This only works when we are able to create Revision ID's in between.
Revision Info:
| REV ID | DateTime |
+----------+-------------+
| 1 | 01.01.2001 |
| 2 | 01.01.2002 |
| 3 | 01.01.2004 |
| 4 | 01.01.2011 |
Now we have to create an Revision for 01.01.2009, to be able to we must set steps for the Revision ID:
Revision Info:
| REV ID | DateTime |
+----------+-------------+
| 1 | 01.01.2001 |
| 11 | 01.01.2002 |
| 21 | 01.01.2004 |
| 31 | 01.01.2011 |
Now we could create an entry for 01.01.2009 with REV ID 22.
This has the problem that we are maybe running out of REV ID's to create entries in the past.
Do you have any suggestions or another idea?

Compare specific field from two different database table

I'm actually developing a synchronization tool in vb.net, I have two database that have on each table records field GUID, this field help to have the same PK on both database. On each record there is also a field called lastUpdated, this field have a milliseconds value, so prevent two user to update the record in the same time. My question is, how I can compare the records of the same table from different db? For example:
ONLINE_DATABASE
TABLE_1
| ID | GUID | NAME | LASTUPDATED |
| 5 | 054ba092-b476-47ed-810b-32868cc95fb| John | 06-01-2016 17:01:12.472438 |
CLIENT_DATABASE
TABLE_1
| ID | GUID | NAME | LASTUPDATED |
| 9 | 054ba092-b476-47ed-810b-32868cc95fb| Jack | 06-01-2016 18:01:12.472438 |
How you can see I've update the record from client application, so I need to apply the same change to online database. Now I've a thousand records to check in about ten tables. So my question is, how I can made a system that do this? Actually I tough to read each row with a MySqlCommand reader but I also think that this procedure is slow... Suggest?
NOTICE THAT: the table have the same name in both db

Mysql - backup partial data

Is there an easy way to backup and restore partial data from a mysql database while maintaining the FK constraints?
Say if I have 2 tables
| CustomerId | CustomerName |
-----------------------------
| 12 | Bon Jovi |
| 13 | Seal |
and
| AddressId| CustomerId | City |
---------------------------------------
| 1 | 12 | London |
| 2 | 13 | Paris |
The backup would only take customer 12 and address 1.
My goal is to take a large database from a production server and replicate it locally, but with partial data.
Due to fairly complicated schema, a custom query is not an option. Also I can't rely on the existence of a main table from which one would get the related rows.
Thanks
You could replicate specific customers manually and by adding an FK constraint on the address table replication will fail to insert/update these records.
For replicating specified tables in the db http://dev.mysql.com/doc/refman/5.1/en/replication-options-slave.html#option_mysqld_replicate-do-table .
Use this parameter to silently skip errors on replication http://dev.mysql.com/doc/refman/5.1/en/replication-options-slave.html#sysvar_slave_skip_errors .