logical issue in sql query - sql

I'm asked to create an application for employees attendance, and I have a logical problem:
I have emp table with id and name, the second table is attendance with id, empid, date, check (true or false), id is the PK.
There will be a form to submit each employee attendance by it self. The user will have a form with the name of employee with a drop down box populated with all employees and a date input text to add the date, and finally yes or no drop down box. My problem is the user can enter 1 or more attendance for the same date and the same employee, which might end with an employee with 40 days attendance in one month.
I'm using google apps engine with datastore, so, any suggestions to solve this issue?

You should be able to create a unique key (empid, date).
It works like a second primary key - does not alow duplicates.

Related

Creating a table having a group of attributes with same name

I am actually trying to create a student database and I need my table to be something of this type
In the above picture the date is something like a super-column name to columns-A,B,C,D which are subject names.
My data will be inserted in those A,B,C,D columns.
My final aim is to access attendance in all subjects based on the date.
Something like:-
select 27-01-2020 from 'table-name';
the above query should give me the attendance in all subjects on 27-01-2020 date
Is there any way to create such a table or similar to this one?
Assuming you have a table for the students and a table for the subjects, you could use a third table, representing the attendance of the students to the different subjects.
Such table will have a date, a reference to the subject, a reference to the student and an attendance column, possibly of type boolean, denoting if the student was present during that day at that subject. You may also add a slot, in case there are more than one hour of lecture for a subject on that day (but this is up to you and your needs).
There should also be a uniqueness constraint on the tuple (date, student, subject), so that there cannot be a student present at two different lectures the same day (unless you use the slot, to which the uniqueness constraint should also span).

Oracle SQL - future date/employers age problem

I've got a problem with age of employeers.
In one table I have e_date_of_birth, e_employee_number, e_employee_name, and
in a second table w_employee_since.
I have an inquiry that returns employees with valid contracts for a particular day (e.g. 2016/01/01) and shows the current age of the employee on that day.
I need to add an inquiry that shows employees with valid contracts as of a specific date (e.g. 2016/01/01), and also their age of another date (e.g. 2017/01/01) in the same query results.
Without more details about your tables, particularly wherever you have the contracts information, a generic query to find employee age on a given date would look something like this.
SELECT FLOOR(MONTHS_BETWEEN(DATE '2017-01-01', e_date_of_birth)/12) AS age
FROM DUAL;

Dynamic Form Elements in Access

I am working on a project in Access 2016 and VBA, that contains a table that stores the details of students (StudentID, EnrollmentNo, Name, Center, etc) and another that contains the attendence (having fourcolumns: ID, StudentID, DateOfClass, IsPresent).
I want to create a form displaying multiple rows, each row belonging to a each student displaying (Enrollment No, Name, DateOfBirth, DateOfClass, IsPresent) and finally want to input the isPresent field using check box and then save the information for all/add new information.
Is there any way to do this ?
Here I am concerned about the form. How do I CREATE such a form (like Datasheet view) where I have a list of students column wise (with their details like enrollment number etc.) and IsPresent (check box type thing) in the next. So that If I like to input attendence in the attendance table I could just set the date and mark the checkboxes in front of students and then press a button which adds all the data to the table.
No table of classes/subjects? Only 2 tables? You have to do data entry into Attendance. Could only create records for students that attend so the IsPresent field would not be needed. If you want record for student even if not attending then, yes, need IsPresent.
If you want to 'batch' create records for all students for a particular date, run INSERT SELECT sql action.
INSERT INTO Attendance(StudentID, DateOfClass) SELECT StudentID, [enter date value] AS DateOfClass FROM Students;

Assign unique ID to duplicates in Access

I had a very big excel spreadsheet that I moved into Access to try to deal with it easier. I'm very much a novice. I'm trying to use SQL via Access.
I need to assign a unique identifier to duplicates. I've seen people use DENSE_RANK in SQL but I can't get it to work in Access.
Here's what I'm trying to do: I have a large amount of patient and sample data (20k rows). My columns are called FULL_NAME, SAMPLE_NUM, and DATE_REC. Some patients have come in more than once and have multiple samples. I want to give each patient a unique ID that I want to call PATIENT_ID.
I can't figure out how to do this, aside from typing it out on each row. I would greatly appreciate help as I really don't know what I'm doing and there is no one at my work who can help.
To illustrate the previous answers' textual explanation, consider the following SQL action queries which can be run in an Access query window one by one or as VBA string queries with DAO's CurrentDb.Execute or DoCmd.RunSQL. The ALTER statements can be done in MSAcecss.exe.
Create a Patients table (make-table query)
SELECT DISTINCT s.FULL_NAME INTO myPatientsTable
FROM mySamplesTable s
WHERE s.FULL_NAME IS NOT NULL;
Add an autonumber field to new Patients table as a Primary Key
ALTER TABLE myPatientsTable ADD COLUMN PATIENT_ID AUTOINCREMENT NOT NULL PRIMARY KEY;
Add a blank Patient_ID column to Samples table
ALTER TABLE mySamplesTable ADD COLUMN PATIENT_ID INTEGER;
Update Patient_ID Column in Samples table using FULL_NAME field
UPDATE mySamplesTable s
INNER JOIN myPatientsTable p
ON s.[FULL_NAME] = p.[FULL_NAME]
SET s.PATIENT_ID = p.PATIENT_ID;
Maintain third-norm principles of relational databases and remove FULL_NAME field from Samples table
ALTER TABLE mySamplesTable DROP COLUMN FULL_NAME;
Then in a separate query, add a foreign key constraint on PATIENT_ID
ALTER TABLE mySamplesTable
ADD CONSTRAINT PatientRelationship
FOREIGN KEY (PATIENT_ID)
REFERENCES myPatientsTable (PATIENT_ID);
Sounds like FULL_NAME is currently the unique identifier. However, names make very poor unique identifiers and name parts should be in separate fields. Are you sure you don't have multiple patients with same name, e.g. John Smith?
You need a PatientInfo table and then the SampleData table. Do a query that pulls DISTINCT patient info (apparently this is only one field - FULL_NAME) and create a table that generates unique ID with autonumber field. Then build a query that joins tables on the two FULL_Name fields and updates a new field in SampleData called PatientID. Delete the FULL_Name field from SampleData.
The command to number rows in your table is [1]
ALTER TABLE MyTable ADD COLUMN ID AUTOINCREMENT;
Anyway as June7 pointed out it might not be a good idea to combine records just based on patient name as there might be duplicates. Better way will be treat each record as unique patient for now and have a way to fix patient ID when patient comes back. I would suggest to go this way:
create two new columns in your samples table
ID with autoincrement as per query above
patientID where you will copy values from ID column - for now they will be same. But in future they will diverge
copy columns patientID and patientName into separate table patients
now you can delete patientName column from samples table
add column imported to patients table to indicate, that there might be some other records that belong to this patient.
when patients come back you open his record, update all other info like address, phone, ... and look for all possible samples record that belong to him. If so, then fix patient id in those records.
Now you can switch imported indicator because this patient data are up to date.
After fixing patientID for samples records. You will end up with patients with no record in samples table. So you can go and delete them.
Unless you already have a natural key you will be corrupting this data when you run the distinct query and build a key from it. From your posting I would guess a natural key would be SAMPLE_NUM. Another problem is that if you roll up by last name you will almost certainly be combining different patients into one.

How to express these model relationships

I'm not sure about the best way to express a relationship between models in my Laravel 4 application. I have three models that I am using to store information in a database: Employer, Employee and User. Basically, the Employer and Employee models are just going to contain meta information about the User model. I think the following expression is the best I can think of:
Employer has many Employee
Employee belongs to one Employer
Employer belongs to one User
Employee belongs to one User
In the database, I am going to have a user_id foreign key in both the employers and employees tables that reference the id field in the users table. Is this the best way to model this domain?
It depends. Can an employee have more than one employer? Can an employee be employed at more than one place? Do you want to delete records if an employee leaves (and lose history)?
If you can delete the employee record when the employee leaves you can just have an employer_id field in the employee table. But just be prepared that you won't be able to easily add capabilities if you need them in the future.
If you want to prepare for the future: You might want to have a table that says
employer_id
employee_id
start_date
end_date
And even then if an employee comes and then leaves you have records with the same employer_id, employee_id. So that makes it look like you need an internal key and have say:
employment_record_id // this is the primary key
employer_id
employee_id
start_date
end_date
I'm not sure exactly what users are but ask yourself the same types of questions:
Can a user exist with an employee record
Can you have employees without a user record
Can a employees user record change
Can a users employee record change