I have the following table (MYAPPTABLE):
--------------------------------------------------
| ColumnName | Column Type |
--------------------------------------------------
| IdApp | INT (PK) |
--------------------------------------------------
| NameApp | NVARCHAR(50) |
--------------------------------------------------
My purpose is to see the change history (Insert, Update, Delete) made on the records of the table, example:
Making an insert:
INSERT INTO MYAPPTABLE (NAMEAPP) VALUES ('StackOverFlowApp')
Making a UPDATE:
UPDATE MYAPPTABLE SET NAMEAPP = 'StackOverFlowAppV2' WHERE NAMEAPP = 'StackOverFlowApp'
What I would like to see in a history Select:
--------------------------------------------------
| IdApp | NameApp |
--------------------------------------------------
| 1 | StackOverFlowApp |
--------------------------------------------------
| 1 | StackOverFlowAppV2 |
--------------------------------------------------
the first row is the insert, and the second is after update.
But if I do this using change tracking, the only thing that throws me is that rows were modified (table version), but no history values modified.
My purpose can be accomplished with any tool SQL Server, or would have to handle it manually?
Related
I'm trying to design a database that allows for filtering according to if a specific resource fills certain categories. I've gotten to the point where I can input data that seems to be how it should be filled out but I'm not sure how I should pull it out again.
The main resource table looks like this:
Table1 - resources
| resourceID | AutoNum |
| title | short text |
| author | short text |
| publish date | date |
| type | short text |
Table2 - Department Categories
| ID | AutoNum |
| 1 | Yes/No |
| 2 | Yes/No |
| fID| Number |
Table3 - Categories
| ID | AutoNum |
| cat | Yes/No |
| dog | Yes/No |
| bird | Yes/No |
| fID | Number |
I have built a form where you can fill in items to the resource ID, and at the same time check off the Yes/No boxes in tables 2 & 3.
I'm trying to use the primary key ID from table 1 and copy it into the table 2 & 3 with referential integrity to cascade deletes, updates. Which I think is the right way to do this.
Currently, I've learnt that I can implement a search function for the columns in table 1, this seems to work fine. However I am stuck with applying the relevant columns in table 2 and 3 as filters.
apply search>
[X] - Cats
Should only return records from table 1 where in table 3 the relevant column has a tick in the Yes/No box.
I hope I have explained this properly, very new to Access and databases so if you need clarity, don't mind offering.
I have a SQL view, which resolves some foreign key dependencies. The View looks like this:
id | title | plantName | customerName | lastUpdated
Where plantName and customerName are 2 resolved values of a foreign key.
There are 3 Tables in total:
Table 1
id | name | surname | idTable2 | idTable3 | lastUpdated
Table 2
id | plantName
Table 3
id | firstName | lastName
What i want to archieve is a trigger which updates the lastUpdated column in the View everytime a row shows another value.
The Problem here is, that lastUpdated inside the View should also be updated when a foreign key changes it's value.
Example view Output:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | raddison | mueller | 2020-02-26 10:04:03.000
Let's say I'll update Table 2 and update "raddison" to "mercure hotel". The View will look like this afterwards:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | mercure hotel | mueller | 2020-02-26 10:04:03.000
I now want that the lastUpdated Column in the View of row 2 representing the datetime where the foreign key has changed.
Desired output:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | mercure hotel | mueller | 2020-02-26 13:44:03.000
Is there any way to archieve this?
Thanks in Advance
In the comments, you confirmed that you want the following:
you want to update the "last_updated" column in table 1 whenever a
value changes in tables 1, 2 or 3, and include that "last_updated"
value in your view
This question shows you how to create a trigger to update a column when a record is modified.
You will need to create that trigger on tables 1, 2 and 3, and the update statement should update table 1's lastUpdated column.
Alternatively, you could create a lastUpdated on all 3 tables, and select the highest value in your view.
I have a Person table, each of who's records belongs to a parent record from the Company table.
One Person is designated as "Organizer" for their parent Company. Initially I handled this by having a recursive reference from the Company table identifying the Person record that was it's "Organizer" - but the software I'm using to build my application layer falls over - it can't handle recursive references.
I've changed tack, and have added a bit field to the Person table to identify whether the person is an "Organizer" or not, but neet to ensure that there is only one "Organizer" for each Company record. If I use an AFTER UPDATE trigger on the Person table, an update on Person triggers an update on Person - obviously I want to avoid recursive triggers.
How can I ensure that there is only ever one Person marked as the "Organizer" for it's parent Company?
+-----------+---------+---------+-----------+ +-----------+---------+---------+-----------+
| FirstName | Surname | Company | Organizer | | FirstName | Surname | Company | Organizer |
+-----------+---------+---------+-----------+ +-----------+---------+---------+-----------+
| John | Smith | 1 | True | | John | Smith | 1 | True |
| Mike | Jones | 1 | NULL | | Mike | Jones | 1 | NULL |
| Fred | Green | 1 | NULL | | Fred | Green | 1 | NULL |
| James | McMahon | 2 | NULL | | James | McMahon | 2 | NULL |
| Philip | Stills | 2 | NULL | Making Philip organizer ==> | Philip | Stills | 2 | True |
| Hector | Berlioz | 2 | True | 'triggers' this change ==> | Hector | Berlioz | 2 | NULL |
+-----------+---------+---------+-----------+ +-----------+---------+---------+-----------+
So seeing as no-one has given an answer here, I'll post what I eventually did:
Created a separate table called Organizer, with only two fields:
CREATE TABLE Organizer (
Company int NOT NULL UNIQUE,
Person int NOT NULL,
CONSTRAINT FK_Organizer_Company FOREIGN KEY (Company) REFERENCES Company(ID) ON DELETE CASCADE,
CONSTRAINT FK_Organizer_Person FOREIGN KEY (Person) REFERENCES Person(ID) ON DELETE CASCADE,
CONSTRAINT PK_Organizer_ID PRIMARY KEY (Company, Person)
);
By making the Company field unique, I can only ever have one organizer for any company.
ON DELETE CASCADE prevents me ending up with orphan organizer records for companies or people that don't exist.
Can't quite recall why I made the PRIMARY KEY both fields. Doesn't seem to hurt.
It was then just a matter of checking for an existing Organizer record and updating that if it existed, or inserting one if it didn't. I did this in the application layer, though I could just have easily have made a Stored Procedure that took Company.ID and Person.ID parameters, checked for Organizer records with the former, and updated the table accordingly. Could even throw in a check for whether the Person actually belongs to that company, and return a value accordingly to the application layer.
I'm at a bit of a loss as to how design this particular database. It has the following features:
Expected 3 million+ rows corresponding to 'users';
Each 'user' has associated with them ~10,000 unique boolean states;
These states are sparse in nature, and additional states will be added in the future, and likely shouldn't be stored in an ordered list;
The states will be updated frequently, at a rate of about 20 in the span of 2 hours, every 24 hours, on average per active 'user';
The obvious design is to have a lookup table between the users and the states, but I'm concerned this will not be fast enough, when looking up states per user, with an expected 5 billion+ rows in the lookup table.
Any advice is appreciated.
I would create a relation between 2 tables
--------------------------------------------------
| ID | username |
--------------------------------------------------
| 1 | bl-ro |
--------------------------------------------------
| 2 | darkmukke |
--------------------------------------------------
and then a relation for the bools
--------------------------------------------------
| ID | fk_user | bool |
--------------------------------------------------
| 1 | 1 | true |
--------------------------------------------------
| 2 | 1 | false |
--------------------------------------------------
for example i have table users, which have 3 fields:
id - login - password
---------------------
1 | john | *****
2 | jack | *****
3 | jane | *****
now i want that each user could have his own settings.
So, do i need to create three different tables, like
user_N_settings:
id | key | value
-------------------------
1 | save_data | True
or i should create one big table for all users instead?
users_settings:
id | key | value | user_id
---------------------------------------------
1 | save_data | True | 2
2 | some_opt | False | 3
One table for all users. A table per user would be very wrong.
One table. If all the setting values are of the same type then it may make sense to create one row per setting. If the attributes are all very different then create one column per setting.