Trigger whenever a value in a view has changed - sql-server

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.

Related

Using tables to categorise resources with

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.

Restrict a bit field to true in only one child record at a time

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.

Dynamic form field configuration in mvc

I have multiple employee groups in my project. Based on the selection of different employee group,submission form fields will differ in the view. If the fields are static then it is easy to do the CRUD operations. I need to create the fields dynamic in nature. I must be able to configure the form fields in SQL server table.For employee Group 1, 5 fields, for Group 2, 6 fields etc. If i want to add new field to any of the group in future also it should work. How can i achieve this?
Thanks
Have you considered using many-to-many relationship between tables Employees and Fields. Something like this:
You'll have a table that holds the list of all possible fields (first name, last name and etc). Then to connect the Employees table and the Fields table with many-to-many relationship you'll need some kind of table (EmployeesFields). Table EmployeesFields holds the value for the each field that employee has.
For example:
EMPLOYEES
+----+------------------+
| Id | CreatedAt |
+----+------------------+
| 1 | 02.08.2017 11:21 |
+----+------------------+
FIELDS
+----+-----------+------------------+
| Id | Name | CreatedAt |
+----+-----------+------------------+
| 1 | FirstName | 02.08.2017 11:24 |
| 2 | LastName | 02.08.2017 11:26 |
+----+-----------+------------------+
EMPLOYEESFIELDS
+------------+---------+-------+------------------+
| EmployeeId | FieldId | Value | CreatedAt |
+------------+---------+-------+------------------+
| 1 | 1 | John | 02.08.2017 11:34 |
| 1 | 2 | Doe | 02.08.2017 11:39 |
+------------+---------+-------+------------------+
I think this is the most simple solution. It just shows the basic idea. Modify it to fit your needs.

Relationships Between Tables in MS Access

I'm new in DataBases at all and have some difficulties with setting relationships between 3 tables in MS Access 2013.
The idea is that I have a table with accounts info, a table with calls related to this accounts and also one table with all the possible call responses. I tried different combinations between them but nothing works.
1st table - Accounts : AccountID(PK) | AccountName | Language | Country | Email
2nd table - Calls : CallID(PK) | Account | Response | Comment | Date
3rd table - Responses: ResponseID(PK) | Response
When you have a table, it usually has a Primary Key field that is the main index of the table. In order for you to connect it with other tables, you usually do that by setting Foreign Key on the other table.
Let's say you have your Accounts table, and it has AccountID field as Primary Key. This field is unique (meaning no duplicate value for this field).
Now, you have the other table called Calls and you have a Foreign Key field called AccountID there, which points to the Accounts table.
Essentially you have Accounts with the following data:
AccountID| AccountName | Language | Country | Email
1 | FirstName | EN | US | some#email.com
2 | SecondName | EN | US | some#email.com
Now you have the other table Calls with Many calls
CallID(PK) | AccountID(FK) | ResponseID(FK) | Comment | Date
1 | 1 | 1 | a comment | 26/10
2 | 1 | 1 | a comment | 26/10
3 | 2 | 3 | a comment | 26/10
4 | 2 | 3 | a comment | 26/10
You can see the One to Many relationship: One accountID (in my example AccountID=1) to Many Calls (in my example 2 rows with AccountID=1 as foreign keys, rows 1 & 2) and AccountID=2 has also 2 rows of Calls (rows 3 and 4)
Same goes for the Responses table
Using this table structure:
Accounts : AccountID(PK) | AccountName | Language | Country | Email
Calls : CallID(PK) | AccountID(FK) | ResponseID(FK) | Comment | Date
Responses: ResponseID(PK) | Response
Accounts.AccountID is referenced by Calls.AccountID. 1:n – many calls for one account possible, but each call concerns just one account.
Responses.ResponseID is referenced by Calls.ResponseID. 1:n – many calls can get the same response from the prepared set, but each call gets exactly one of them.
To actually define the Relationships in Access, open the Relationships window...
... then follow the detailed instructions here:
How to define relationships between tables in an Access database

databases design

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.

Resources