How do i display the same recurring product in a table? I.e. Same product (macbook) that has been serviced at different locations - sql

I am fairly new to SQL queries. I want to display the same product that has been serviced at three different labs or service stations.
I want to list and display the macbook represented by the id at the three different stations (or more) being displayed too.

Ok, so normally on stackoverflow, people tend to show what they tried and why it didn't work. In fact this question will probably be closed as too broad, but I empathize with starting with sql. But I will match the broadness of your question with a broad answer.
You should splt the tables up. I might try to have three tables:
A table describing each product with unique id
A table describing each service location with a unique id
A table describing each transaction with a unique id consisting of product ID and location id
Then when you need the data you would perform a join which would return the table containing the information you want.

Related

Survey Database - additional question about table structure

I'm referencing a closed stack overflow post,
Database design for a survey
The top answer in the post gives a great overview of the tables to use for a survey database, with a good table-relationship view. My question piggybacks on the survey tables, particularly the table which stores the top level survey records. For surveys which are given in specified periods (initial survey, 6 month after, 12 month after, etc.), if the survey does not have different questions and answers (just a duplicate of the initial survey), would it make more sense to create different surveys in the top level survey table as different survey records, or would it be better to add a field in the completed survey table as a secondary identifier.
For example, if a survey (let's call it "Health assessment") has an initial survey, then one 6 month after, which would be the better solution:
make two records in the survey table - "Health assessment - initial" and "Health assessment - 6 month", so that in the completed survey table the surveyID would be different for both surveys. This would, I suppose, require records for both surveys in other tables (survey_question and survey_question_answer). Seems like a lot of duplication.
make one record in the survey table - "Health assessment", and add an additional designation in the completed survey table (answer) where the user could put "Initial" or "6-month".
Hopefully that makes sense. I think either solution might work; I just want the more optimal solution.
Keeping to this existing design; Assuming SurveyID is a unique Primary Key, the first suggestion, as you already recognized, will cause redundant data unless the intention is for the questions to change over time. The second option is not great either because I believe it would invalidate the purpose of the StartDate, EndDate, and IsOpened fields on the Survey table.
What about creating a Survey_Instance table? The Survey table would hold the Survey ID and description and the new table would hold the Start, End, and IsOpened properties for each time the survey posted. The responses could be linked via "SurveyInstanceID" and the questions could be linked to the Survey table. This has the added benefit of linking responses to a specific time frame so you could observe how the responses might change over time.

How can I use forms in libreoffice base to add/modify data in a many-to-many relationship?

I am essentially trying to create a glorified contacts list using LibreOffice Base. Many of our contacts have multiple addresses (office, mailing, home), and sometimes multiple people have the same address.
I've created a simple contacts table with the Contact ID, Last Name, and First Name. I've created an address table with an Address ID, City, State, etc. I've also created a junction table with Contact ID and Address ID, and connected the three tables using the relationships tool.
Now I want to add everything into a single form. I watched this youtube video, which was very helpful, but I want to be able to add new cities instead of only selecting from a pre-established list. So I followed along with the video, but set the columns to "combo box" instead of "list box." However, when I try that I get an error message:
Error inserting the new record
SQL Status: 23000
Error code: -177
Integrity constraint violation - no parent SYS_FK_94 table: Address Table in statement [INSERT INTO "Contact-Address Junction" ( "Address ID","Contact ID") VALUES ( ?,?)]
I assume there's something obvious that I'm missing, but at this point I'm pretty stuck.
E: I took more screenshots to show the relationships, tables, and how things connect in the form:
In the video you cite, this is kind of silly as there is no need for a many to many relationship. The author could have simply added a Movie ID field to the genre Table, (so multiple genre records can point to each movie record).
I have a complex many-to-many contacts structure that I'm in the process of porting from MS Access to LibreOffice. It has 4 primary data tables: Groups, Address, Phones, and People. And there are 6 link tables to connect those primary tables: GroupAddress, GroupPhone, GroupPerson; AddressPhone, AddressPerson; and finaly PhonePerson. Unlike the video you cite above with only 2 fields, in my link tables there are 3 fields. GroupAddress for example has GroupAddressID (the unique id for the link table itself), GroupID (which points to the Groups table), and AddressID (which points to the addresses table). This allows any number of addresses per each group, and at the same time, any number of groups per each address.
So the whole thing has maximum data flexibility: For example it allows for unlimited number of people per group, each person can have an unlimited number of phones or addresses, each address an unlimited number of people, etc.
Implementing it in LO: Because in LO you can't edit a query based on more than one table (you can view it, but not edit it), like you can in Access, (and therefore you also can't build a form with a table that can edit a query based on more than one table), in LO this is not as clean as it is in Access.
In Access it works very well and the link tables manage themselves! In LO you have to manually edit the link tables in a separate table. I'm starting to think about how I might use some macros to improve on this, but for the moment it's bare bones.
To give you a better idea, the first form to edit Groups, which also edits the group's addresses, people, and phones (not just those tables, but also the links to those tables) looks like this:
Group lookup pulldown (used to find a group record)
(this is a drop down box with custom code that helps me find group records)
Group editing fields, e.g. Group name, category, url, etc
Then below that
Group-Person links table
Then to the right of that
Persons table (plural) - to create new person, then..
And below that
Person table (singular)
[pointed to by Group-Person link]
to view person pointed to.
Group-Address links || (similar structure to above)
(for example, when a business has two or more addresses)
Group-Phone links || (similar structure to above)
(this is for phone#'s that the group owns directly, not personal phones of the group's members, and not phones tied to specific addresses).
----------
Then there are 3 other similar forms,
one for Addresses w/ Person, Group & Phone links;
one for Phones w/ Group, Address & Person links;
and one for People w/ Group, Address and phone links;
Here is a screen shot of the first of the 4 editing forms, to edit the groups and the links associated with the group:
Hope this helps. I would be interested to see what you develop. Thanks.

How distribute many columns in one table in SQL Server 2012 database

I am thinking about problem in our database.
I have one table for our products. It has few columns that it's for all products common. But products belongs to a manufacture. And each manufacture need some columns for the specification of product. So I am thinking about distributions for our table..
I think that have it all in one table is waste for memory. Because for example I have 20k products for Apple and 30k for Asus, 40k for MSI.. so if I have it all in one table for columns for apple will be NULL for 70k records..
Another idea was that I have few tables for each manufacture and in products has some key that pointing to specific table with columns for Apple.. for example key can be apple1, apple2 and so on. But with this idea it was quite difficult to show all products with theirs specific columns.
So I want to ask if someone thinking about this problem in database.
I am using SQL Server 2012 for our database.
Thanks for any help to this problem.
You can use such structure...
It's just example...
If You want to compare specifications, table with product data will be more complex...
Databases are built to handle information as a collection of related sets ... and to use selection criteria to get you what you want to work with. You should design depending on how your information elements work .. something like:
Manufacturer table (with information that may be peculiar to a
manufacturer (e.g. address, telephone, etc.)
Product table , with a foreign key reference to Manufacturer
You can then select information for, say, Apple with something like
select xxx
from Product p, Manufacturer m
where p.manufacturerID = m.manufacturerID and m.name = "Apple"

Creating Table Relationships

I am working on a VB.net (VS-2010, Win XP Pro 2 SP3), Employee Management Project. I need to keep track of Employee Leave Attendance and also each Equipment assigned to an Employee. How can I achieve this using SQLlite.
It will be very useful if you could provide me with examples as I am completely new to the field of SQL and VB.net
I think this can be done with two tables where one has the primary key while the other has a foreign key, but I am not sure. Also how many tables will I need for storing data in Leave and Equipment Form.
I went through other questions but I was unable to figure out a solution for my problem.
(Sorry, I cannot provide with images as this site prevents me from posting images without 10 reps)
Most problems are only as complex, and as simple as you make them. Out of habbit, nearly all tables end up with a unique ID field. There are exceptions, which I will call "link" tables, eg, ones that provide connection details between two data tables.
Now, in your senario
You would need a "holiday" table, where each row will contain the employee unique ID and either a start/finish date, eg, if they take half a day, it needs to be visible, or, just a year and value, eg in 2011, I booked, 2 lots of 35 hours, and 1 lot of 4 hours eg, Ive taken 2 weeks and half a day.
For the equipment, you would need a data table, since an item can only got to 1 employee, it depends if you're going to use this for booking or not, but if its just like a library, eg I currently have a loaner laptop, then you can just have an employee field in the equipment table. If you need a booking system, then you would require link tables and more complex.
Best way to work out your tables is to try and group your data, and then write the items on peices of paper and see how you as a human do it. After a while you end up able to do so in your head.

SQL Design - Accounting for unknown values

Say I have a table called HoursCharged
ChrgNum(varchar(10))
CategoryID(uniqueidentifier)
Month(datetime)
Hours(int)
CategoryID is a foreign key reference to another table in my database, which is just a name/ID pairing. ChrgNum is guaranteed to be unique outside of this database, and so I only check to see if it exists in my db already.
You should also know that this tool support several different groups from one database (hence the globally unique CategoryID, since different groups may potentially name the Categories the same thing, I needed to differentiate them).
This table is populated from a CSV file. The idea is that every combination of ChrgNum, CategoryID, and Month is going to be unique. The report that is being run to create the import file can only see a certain range (i.e. a year). Therefore the algorithm looks something like this:
IF (ChrgNum exists in database, CategoryID exists in database,
combo of ChrgNum/CategoryID/Month DOES NOT exist in table HoursBurned)
THEN add a new row for this entry
ELSE IF (ChrgNum exists in database, CategoryID exists in database,
combo of ChrgNum/CategoryID/Month DOES exist in table HoursBurned)
THEN update the existing row with the new Hours value.
This is all fine and good, except now my boss wants me to account for hours charged, in a particular month, for a known ChrgNum and an unknown Category.
My question is, how do I account for this? If I simply insert a NULL CategoryID, what happens if a totally seperate group charges hours to the same number and category? My other idea was to create a new table for the unknown Categories, but if I do this, and the first import has two unknown categories while the next one has one of the two again (which can happen) what do I do?
My head has been swirling around this for hours. Any help is appreciated!
Your boss presented you with this problem, so why not ask them? Because this really sounds like a business problem. If you have groups which are reporting on categories you don't know about then surely you should be attempting to synchronize your database with the systems which feed it?
Otherwise, what is wrong with having a single "Unknown" category? You are being asked to track the hours assigned to categories you don't currently track.
It seems to me like you should be adding the unrecognized categories to the existing categories table on the fly. If the problem is then distinguishing between categories with the same name from different groups, don't you already have that problem now?