Question about creating table, relation and cardinality - sql

I have a problem creating a database (I am still learning). I need to create a database for a company that generates a report like the one in the image.
Report to reply
I have the tables created as follows, but I have a problem in the t_stack with the t_tracking_number.
DB model and Relationship
The part that I don´t know how to do, is create the t_stack table, to register an id_stack, an id_driver, and then insert many tracking_numbers. at this moment according to the image, I should register for every track_number, an id_stack and an id_driver.
If someone can give me an idea of ​​how to do it, or if I must change whatever in the database it doesn't matter. Sorry if I didn't make myself clear, this is my first project and I want to do my best. Thank you.

You have to add in your t_stack table an id (ID_stack), identity increment as a PrimaryKey. Then you can have all tracking_numbers and combinations you need.

Related

Relational Table design

I am designing a feature for a database, but I am stuck on design.
I have a table called AgendaItems, this table is a table with Agenda Items that could be assign to possible multiple users.
Users is a table that contains a record of user names, containing a fixed amount of 17 names.
How would I design these tables possibly another table that keeps track of who is working on what Agenda Item. Keep in mind multiple users could work on an agenda Item and users could work on multiple items.
I am not sure who to design this, and wondering if it would even work?
Thanks
I don't know if I understood your problem but I think your relationship is N-N.
So, you need to create another table (UsersAgendaItems). This table must contain the AgendaItems ID and Users ID, where both of then are FK.
Your PK could be a composite PK. This way you can know what user is related with what AgendaItems.
But I don't know if that is what you want. If this is not your case, please, try to explain a little bit more!
Thanks!

Trying to make my database more dynamic

I am trying to figure out what the best way to design this database would be. Currently what I have works, but it requires me to hard-code values where I would like it to be dynamic in the future.
Here is my current database design:
As you can see, for both the Qualities and the PressSettingsSet tables, there are many columns that are hard-coded such as BlownInsert, Blowout, Temperature1, Temperature2, etc.
What I am trying to accomplish is to have these be dynamic. Each job will have these same settings, but I would like to allow the users to define these settings. Would it be best to create a table with just a name field and have a one-to-one relationship to another table with a value for the field and a relation to the Job Number?
I hope this makes sense, any help is appreciated. I can make a database diagram of how I think it should work if that is more helpful to what I am trying to convey. I think that what I have in mind will work, but it just seems like it will be creating a lot of extra rows in the database, so I wanted to see if there is possibly a better way.
Would it be best to create a table with just a name field and have a one-to-one relationship to another table with a value for the field and a relation to the Job Number?
That would be the simplest - you could expand that by adding data-effective fields or de-normalize it by putting it all in one table (with just a name and value field).
Are the values for the settings different per job? If so then yes a "key" table" with the name ans a one-to-many relationship to the value per job would be best.

To find out the User name or ID who updated the address of the other staff

This is my first post on this forum and hope I will get an answer.
I have very limited info with me about my database.
The query is like:
I wanted to know who has updated the address of the other staff, surely it is updated from the Java based application, but I came to know that in my database I have audit schema and in that I can find out the user name who updated the address.
But I don't know in which table this information will be available as we have around 1000+ tables in my database.
Could you please assist me to find out the exact table where this info will be available.
Aija, this is a difficult question to answer as there are so many possibilities, however we maybe able to help you narrow it down. Tables like this often start with audit, history, change, etc. or the reverse and have that appended to the end of the file they are tracking. E.g. audit_personnel or personnel_change. You say you have 1,000+ tables. That is a lot, but I have worked with bigger. It is still feasible to go through the list by the name of the table one by one. When databases get this big, naming standards come into play. Have a look at the way the table names are put together, and you will be able to narrow down your search a lot.
Thanks for your input
i have gone throgh all the tables by name starting/ending with name audit .i found one table audit trail in that there are multiple tables but i could not able to find the info which is expected.
even iam not sure these tables are coming under my prebvillage or its in under sys or any other user.
Another option then is the single table audit control. In this style, the table has 4 major components. First the data being changed which will be something like the table and field, maybe recid. Second is the original data. Third is the new data. Fourth is the who and when of the change. If this is the style, then you will need to know which table it is that you want to track. Then you will need something like "select * from [audit table] where [audit table].[monitored table] = [target table]".

Required an expert suggestion in TSQL

I have a scenarios where Table A has reference to Table B , Table B has a reference on Table C column ...etc.
To implement an update task in my project I ought to implement it in two phase logic
i.e. delete the row first and add the latest again.
But unfortunately , when I try to delete a row in table A it has reference which in turn has reference to other table and so on. Hence my logic of delete and add does not work in a proper way all the time. Even if it is deleted and added again , the sequence at which it is being added is last i.e. as a new record. Hence I am losing all the earlier references track in the same order as old one.
Hence I would like to delete a row from a table without effecting the references i.e. for time being it should allow to ignore reference , once i added it again i.e update record then i need to re-enforce/enable back the reference.
Is it possible to do in such a way ? or there any other logic works in similar fashion or replace the original intention ? could anyone please provide your expertise advice on this ?
How general logic of windows service pack works ? can any one elaborate on that? or share some info or doc or blog regarding the same?
Thank you so much.
Regards,
Shyam
What you want to do is a bad practice, I would rethink your design. It doesn't let you delete the parent record because there are child records. That is what the database ii supposed to and to try to circumvent it is a 100% guarantee of bad data.
If what you are trying to accomplish is to move the child records to a new parent, that can be done but you add the new record first and then make updates. It is best if you have some field to be able to define what old record it used to be associated with or a mapping table to use to make many changes. Then you would need to run updates for every child table. This kind of thing shoudl be a one time change, not a regular practice. It certainly shoudl virtually never happen from the application and shoudl only be done by a qualified databse developer.
If what you are trying to accomplish is to inactivate the parent so it can no longer be used for some purposes(such as creating new orders) and leave the details for reporting (wouldn't want to lose the finacials for old orders), then you should put an active flag on the table and use that to filter records instead. Often this means creating a view of only active records and pointing the code to the view insted of directly to the table.

Denormalizing and adding multiple ids to one row

I'm currently working on a SQL Server relational database which will be accessed through the Entity Framework. I have a couple of questions that I cannot seem to find proper answers to.
Please see my database model diagram:
1.
I'm not quite sure if the ProblemType implementation is the right way to go. As it has been set up now, both columns allow null values in ProblemType since it will either contain a ProblemTypeRoadID or a ProblemTypeVandalismID. So the question is really if it is possible to de-normalize the model by removing the ProblemType table and relate both ProblemTypeRoad and ProblemTypeVandalism in the ProblemDesc table?
2.
Looking at the Problem table you will find a column named HistoryIDs. This column should contain a comma-separated string with all the HistoryIDs related to a Problem. A HistoryID is essentially a ProblemDescID (since a ProblemDesc gets its IsHistory column set to true whenever it is updated) as the ProblemDesc is copied whenever changed.
My question is: what is the best way to add multiple ProblemDescIDs to the HistoryIDs column? If it was something like "1,5,7" it would be easy to find all the histories for a given ProblemDesc.
3.
Any other suggestions for the diagram is also very welcome :)
Thanks in advance!
EDIT: Please consider the revised diagram:
Now that you have given me some great suggestions for improvement, I have tried to implement most of your suggestions. ProblemType has been removed and ProblemHistory added. Changes have also been made to how WorkerComment and Media relate to their parent tables.
Would this implementation work?
Thanks!
You can remove the problemtype table, and just relate problemtyperoad and problemtypevandalism to the problemtype table with the problem type id
No, it shouldn't. You should have a table ProblemHistory with columns ProblemID and HistoryID with a row for each History.
It seems you can have a worker comment that relates to multiple problems. Should it be the other way around? Similarly for media and ProblemDescs. Can only one worker work on a problem? Also, MSSQL has a geography type that you can use instead of CoordinateLat and CoordinateLng.