How to capture column changes into new columns in a SQL Server table? [closed] - sql-server

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have a SQL Server table. I need to keep track of 3 columns in that table. Whenever a change happens in one of those columns, the historic information present in that column needs to be moved to a separate column. Every time the change happened, the history column should keep track of all the changes that are being done on that column. May I know how to implement this process?
Example;
ORIGINAL TABLE VIEW:
enter image description here
CHANGE1 TABLE VIEW:
enter image description here
CHANGE2 TABLE VIEW:
enter image description here
CHANGE3 TABLE VIEW:
enter image description here
I read few docs online and they said that I need to enable track changes of SQL database and establish track changes on the table that I need to work with. After that I do not know what to do.
Do I need to write a code that concatenates the data present in the original columns to HIST columns? If so, How can I do it when the changes happen at random point of day? (If I follow this, is there a point to enable change tracking?)

Related

How can I save Pending Changes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have a SQL table Infos that stores some user information, the user can update his data but needs Administrator validation first,
the primary key of Infos is the referenced a lot in some others tables
Wy question is where chould I store the pending information that need validation ?
Is it a good idea to store them as JSON in a column ?
Treat this as a data modeling question. If the user information requires validation before it can be used in the application, introduce a Infos_UpdateRequest table or somesuch, that the administrator can use, and if approved copy the values from there to Infos.
You could create an Infos_Pending table with the same schema as Infos and the primary key being also a foreign key to Infos. So, your admins could load the pending records:
approval would first update the Infos record with the Infos_Pending values and then delete the Infos_Pending record
rejection would simply remove the Infos_Pending record
I would look at adding a pending approval and is active column to your table.
It would then be easy to identify which of your rows should be displayed. You could even keep older rows around for undo/history purposes.
This would mean you'd need to make changes to the sprocs/code used at your display layer, though.
I would suggest not using json or xml unless it's a situation where you always intend to chnage the entire set of values in contains.

How to overcome save data in table same time by two users [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am implementing a seat booking application. I have a problem. There is a booking table. If two users try to book a same seat exact same time it will be a problem. How can I overcome this problem. I am using Java 8 and PostgreSQL.
A database system is designed to handle concurrent queries like in your situation by itself.
You however need to make sure, that a seat can only be booked once.
Solution #1 If you insert a new entry in the booking table for each booked seat
INSERT INTO booking (booking_id, seat_id, customer_id)
SELECT ?,?,?
WHERE NOT EXISTS (SELECT seat_id FROM booking WHERE seat_id=?)
This solution will check if there is already a booking entry in the database with the seat_id you are trying to book. This can be also achieved by putting an unique constraint on the seat_id attribute in your table. However in this case your query will fail with an error - since the unique constraint is violated when inserting a duplicated value - while in the solution above the query will execute sucessfully and just not insert any value. This solution is prefered.
Solution #2 If you have one row for every seat in your database and just need to update if its booked
UPDATE booking
SET booking_id=?, booked=true
WHERE seat_id=? AND booked = false
Depending on your JDBC library you are able to replace the ? with your real values.
The database system will handle concurrent queries and only allow one query to pass.

In one to many relationship does there exist element from first table that are not connected to element from second table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
when we have two tables, suppose A and B,there exists one to many relationship between these two, than can there be element from A that are not connected to any element from B?
One to many relationship between relational tables is a concept that primarily means that if there is a record in table A, then it can have many associated records in table B.
For e.g. customer_id record in table A can have many loans for that customer in table B.
So 1 customer_id associated with many loan_ids in another table.
Coming back to your question, it is possible that 1 loan_id in loan_table can have multiple guarantors in guarantor_table and it is also possible that a loan does not have a guarantor associated with it. So even though there is one to many relationship between the tables it does not mean it will always have many records in table B for one record in table A.
Check this link - What is the difference of partiality and optionality when drawing ER diagrams

objects(such as person) and items (such salary items) data design structure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This may be a classical requirement. Two key concepts objects and items, a time-change demand, the items can changed very quickly.
Such as a person have so many salary items, and these items will be added and changed or deleted.
Our solution is so simple, it's a 500 columns table, and map items to columns.
This beyond to hard code. This solutions have so many disadvantage.
**What's the classical solution about this requirement**
As I gather from your question, this is a classical one-to-many (1-M) relationship. The one side is the (object) and the many side is the (item). The item should have a column that points to its 'parent object'. This is column could contain the value of the Primary Key of the parent table (usually referred to as Foreign Key). The child table (the item) could have validity period (such as start date, end date) as well as other control columns in addition to the business columns (such as salary amount, etc.). This design is typical in Normalized database modeling.
There are many advantages of this design. One of them that pertains to you case, is that you don't have to have large number of columns, instead, you'd have several rows which allows you to fetch just what you want to satisfy most business queries. Similarly Create and Update operations will deal with much smaller rows, hence increasing the performance of the application.

Transfer database value to excel in a specific cell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
To start with, I have no idea yet even a code on how to do this. To make things clearer, I want to transfer a database value to excel to a specific location. Lets say I have a saved template (excel) naming RISK.xls. I have 4 tables that has a common column named riskId in which i want to get a specific value to be transferred. Let's put it this way,
Table 1 in database(Department):i want to transfer column department where riskId = 50(sample)
Table 2 in DB(Rate): I want to transfer column inherent where riskId = 50(sample)
table 3 in DB(treatment): i want to transfer column description where riskId = 50
table 4 in DB(strategy): i want to transfer column details where risjId = 50
now i want to transfer selected value in table to be transferred in my excel. Here goes:
Selected Value in Table 1 transferred to excel cell A5.
Selected Value in Table 2 transferred to excel cell B5.
Selected Value in Table 3 transferred to excel cell C5.
Selected Value in Table 4 transferred to excel cell D5.
Plus this option: those tables are related to each other so if in excel table 1 value has multiple table 2 value and each item in multiple table 2 value has its own multiple value which is in table 3 and so on. So one A5 = B5 to B10(sample) and b5 = C5 to c10) like this.
Hope anyone can help me with this.
Thanks and feel free to ask me about my problem.
You could query the database using the "External Data" feature in Excel and create a specific SQL statement that will return the values in a specific worksheet. After that's done, you can reference what's in those cells using formulas.
Check this out to get you started:
http://www.spreadsheetsmadeeasy.com/excel-create-pivot-table-using-sql/

Resources