The Specificity of Insertion Anomalies - sql

I'm currently trying to understand the nuances of Insertion/Deletion/Modification anomalies in SQL.
Currently, the example I'm trying to understand is as follows:
ENROLLMENT
StudentID(PK) StudentName ClassID ClassName
111 Joe E1 English1
222 Bob E1 English1
333 Mary H1 History1
The problem the example wants me to answer is:
Which of the following causes an insertion anomaly?
with the answers being
Inserting a Student without a Class
and
Inserting a Class without a Student
I don't really understand why one of these answers is more right than the other, why, or how. It seems to me like either could be acceptable. Thanks in advance.

You need to think in terms of how data is added to a system naturalistically (i.e. what series of events occur in the real world).
In this case you would create a set of classes, prior to registration, and then create and assign students to them when they turned up to register.
You would be unlikely to create a set of students and then create and assign classes to each one.
A class might only be able to hold 30 students. How do you deal with any extra students who want to be registered for that class?
If you register 100 students and then decide to create classes, which subjects do you create?
Why do students decide to turn up to register? [Presumably because of the classes on offer.]
You can create as many classes as you're able to fit into your time-table. The number of students that actually register might mean a class is cancelled, but it has to exist in the first instance.
In summary, "Inserting a Student without a Class" would be more likely to cause an insertion anomaly.

Related

Coupling between classes

Let's assume we have two classes: Patient and MedicalExamination. We want to get all examinations for a specific patient. Which one is better:
patient.getExaminations();
examination.get(patient);
How would you implement the second way? It would force you to loop over all examinations and see which one are about your patient.
The first method however will allow each patient to hold its own examinations and thus immediately retrieve it. Therefore my preference goes to this method.
Give the object as much information as you reasonably should (without lowering security concerns) so it can operate on its own.
Basically, an examination cannot exist without a patient. Therefore Patient would be the root entity of the aggregate Patient - MedicalExamination, and as such Patient (or PatientRepository) would be the way to retrieve patient details and examinations:
patient.getExaminations();
A doctor usually have a file for each patient, in each patient's file, there is a list of medical examination.
So, you get the patient from the doctor office, then you get the examination from the patient's file.
so:
patient.getExaminations();

Filtering user access based on tabled responses

I am building a "Survey" type application. The user answers a set of questions with pre-vetted answers.
Question: Where do you live?
Answers: England, Finland, Spain, France, Monrovia
The answers in this case would be in a DropDownList.
Once the user has completed the basic responses (location, age, sex etc) I would like to be able to prevent them accessing the rest of the survey based on their answers.
So for example, if they live anywhere but England I want to direct them to a page which says "Thanks, but Monrovian's can't complete this survey". I need my filtering to be user configurable (Table based) and I need to be able to have ANDs and ORs.
So one filter being the user MUST earn 100k+ a year.
Another being they must either live in Spain, or be female AND like model trains - "100k+ && (Spain || (Female && Trains))"
I would usually use Enums and bitmasking for this, but as my country list is 200+ items long, I can't think of a sensible way to store the filtering.
Hopefully I have made some sense and someone has a decent solution :)
I don't know if I can answer your question completely, but I'll try...
So, we have a bunch of Views that are only visible to the user if she previously chose some answers, like, she will see view#3 if she is older that 30, and view#4 if she is younger than 30 AND from China, and view#5 if she is older than 40 AND from Spain OR Italy, and so on...I want also to introduce the notion of **step**, and for each step we could have 1, 2, or more corresponding views. Each view should have a set of rules (like the ones above) that define if it is displayed or not.
How to create these rules? These rules could be simple instances of a Filter class/interface that, when asked, should return true/false. Like this:
interface Filter {
boolean apply();
}
Then you can create Filters like 'older than 30', 'from Spain', whatever. Remember that each view is configured with a set of rules (Filters) so it can answer yes/no if asked if it can display itself.
Next, how to apply these filters?
We could have a controller object that only knows about **steps** (each step can have one or more corresponding views, as I said), and, after the user pressed **next** at the current step, it should collect the answers and apply them against the rules attached to each view. Like, take the answers from step one, and apply them to all views from step two, and see which one matches (returns true). For example, at step two, you can have two separate views, one for young people, other for old people, and you apply the rules from each view to decide if you show the old or young view.
I could give you one code example, and you could also do research on your own, since I know nothing about your technical environment. I have used Google Guava's predicates on a similar problem and here it is: suppose we are dealing with Witch objects, and each of them has name(string), age(int) and spells(collection) attributes. If I have a list of witches and I need to sort them based on specific criteria, I can do:
// first I want to sort witches by age(natural ordering) then by spells,
// and then by name lexicographically
Ordering.natural()
.compound(new BySpellsWitchOrdering())
.compound(new ByNameWitchOrdering())
.sortedCopy(witchList);
The above line of code is going to take the witch list and return a list of sorted witches according to the criteria. Your situation is pretty similar.
Next, how to create the answers? For each view(page), you have possible answers, like, for view#1, you can have : age, sex, race, country. You can construct some answers, in the form of strings, ints, enums, and pass them to the controller, which in turn is going to apply them to each view corresponding to the next step.
As for how to store the rules in the database, as an example, you could have a column defining rule name (like, OLDER THAN) and one column for value, say, 30. Again, I do not know that much about your environment, and it is a really general issue, so I will stop here...

How to determine number of children of a record?

Good afternoon everyone!
I'm studying NHibernate, and decided to make some changes. Among them, I noticed that some fields are unnecessary. So I bring my doubt:
I have a list, let's call it Class_List within each study class, I can have N students for each class. Within the list Class_List, I also have other properties as simple as the name of the class.
How I see it is unnecessary to store how many students I have in the database, I would, in a single query, how many records I have. This, using NHibernate.
Is this possible? How?
Best regards,
Gustavo.
Edit: I've forgot to say one thing... I want to return this number of record, as a column. But this column is not mapped in my .hbm.xml file.
If students are mapped as a collection on Class, you can try using something like this:
var numberOfStudents = session.CreateCriteria<Class>()
.Add(Restrictions.IdEq(1))
.CreateCriteria("_students", "students")
.SetProjection(Projections.RowCount())
.UniqueResult<Int32>();
Where '1' is the id of the class (you can use other property) and '_students' is the name of the students collection.

How to retrieve a specific object through a relationship in core data

Say I have 2 managed objects in my model: Department and Employee (as discussed in the Core Data Programming Guide). If I already have a specific department retrieved, I know I can get all employees in that department through
NSSet *departmentsEmployees = aDepartment.employees;
but what if I want to find a specific employee (e.g. with employeeId = 123) in that set, change one of its attributes, then save the change? How do I do that? Can I do a targeted query on the set? Or would I have to loop through each employee to find the one I want?
It seems that it would be better to try to find it in the employees NSSet instead of doing a whole new query to the entire data model because I already have a specific department.
Thank you
One way to narrow the search is to use -[NSSet filteredSetUsingPredicate:].

How to name my enum elements?

I have a problem naming the elements in my application's data model.
In the application, the user has the possibility to create his own metamodel. He does so by creating entity types and a type defines which properties an entity has. However, there are three kinds of entity types:
There is always exactly one instance of the type.
For instance, I want to model the company I am working for. It has a name, a share price and a number of employees. These values change over time, but there is always exactly one company.
There are different instances of the type, each is unique.
Example: Cities. A city has a name and a population count, there are different cities and each city exists exactly once.
Each instance of the type defines multiple entities.
Example: Cars. A car has a color and a manufacturer. But there is not only one red mercedes. And even though they are similar, red mercedes #1 is different from red mercedes #2.
So lets say you are a user of this tool and you understood the concept of these three flavors. You want to create a new entity type and are prompted to choose between option 1, 2 and 3. How would you name these options?
Edit:
Documentation and help is available to the user. Also the user can be expecteted to have a technical/programming background, so understanding these three concepts should be no problem.
First of all let me make sure I understand the problem,
Here's what you have (correct me if I'm wrong):
#of instances , is/are Unique
(1,true)
(n,true)
(n,false)
If so,
for #of instances I would use single \ plural
for is\are unique (\ not unique) I would use unique \ ununique.
so you'll get:
singleUnique
pluralUnique
pluralUnunique
That's the best I could think of.. I don't know exactly who are your users and what is the environment, But if you have an option of adding tips (or documentation) that should be used for sure.