Creating a table having a group of attributes with same name - sql

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).

Related

How can I add rows to an SQL table without knowing how many column it has

I'm building an attendance system that tracks attendance in an SQL table.
I do this by adding a new column of a date to the table every time it's used.
now I have a problem adding new rows for new users because I don't know how many columns I have so I cant use INSERT INTO Table VALUES().
Is there any alternative way to do it?
Edit:
here's how it's supposed to look
and every day it's supposed to add a column for the date.
I don't really understand how can I do it with adding dates as rows
can someone elaborate?
I think you should probably tweak the design of your tables. Using the example of a school:
Student Table:
ID (Primary Key)
Name
(More student specific columns here)
Attendance Table:
ID (Primary Key)
StudentID (Foreign Key)
Date
Attended
TimeArrived
(More Attendance specific columns here)
In the above example, each day would get a new row added to the attendance table and you could avoid dynamically adding columns.

SQL database structure with two changing properties

Let's assume I am building the backend of a university management software.
I have a users table with the following columns:
id
name
birthday
last_english_grade
last_it_grade
profs table columns:
id
name
birthday
I'd like to have a third table with which I can determine all professors teaching a student.
So I'd like to assign multiple teachers to each student.
Those Professors may change any time.
New students may be added any time too.
What's the best way to achieve this?
The canonical way to do this would be to introduce a third junction table, which exists mainly to relate users to professors:
users_profs (
user_id,
prof_id,
PRIMARY KEY (user_id, prof_id)
)
The primary key of this junction table is the combination of a user and professor ID. Note that this table is fairly lean, and avoids the problem of repeating metadata for a given user or professor. Rather, user/professor information remains in your two original tables, and does not get repeated.

How would I go about inserting data into one-to-many(must) relation, psycopg2/python in PostgreSQL

Using psycopg2/python how would I go about inserting data if I'd have something like from this img.
GRUPA (class GROUP) must contain one or more STUDENT's
How to fill them up and how to add them after, I can't add a GROUP without a student and I can't add a STUDENT w/o a GROUP.
Must it be a transaction/deferrable-constraints to add them in batch/together just to create GROUP?
What options do I have, how would you do it?
Thanks.
I'm not sure why you have an id_group in the students table. It doesn't really make sense.
But if you do, make it null-able. Then you can:
Add a student into the table with a null value for id_group.
Add a group, with the student.
Update the students table with the value for the group.
However, you might also want to relax your requirements. For instance, perhaps a group could have no students. It wouldn't be a very active group, but the group can exist as a separate entity.
In fact, a more natural way to represent this data would be:
A students table with one row per student and no group reference.
A groups table with one row per group and no student reference.
A studentGroups table with one row per student in each group.

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.

Mysql how to avoid repeating myself

I have a table students with the following fields.
Id,FirstName,SecondName,Photo,Student_ID
I also have a table called class_of_2011 with the fields
Id,FirstName,SecondName,Photo,Student_ID,Subjects
I want to select some specific students from table students and have them in table class_of_2011,but I already have the names in table students.I am thinking the only way to do this is to copy the names i want to the table class_of_2011,but since there will be a class of 2012 and beyond,I feel like I will be simply copying data from one table to the other.
Is repeating myself inevitable in my case?
It looks like this could be normalized easily. Why not have your class_of_ tables simply have a foreign key to the student table's id column?
StudentId,Subjects
In this way, one student record could be associated with several classes, in case someone is on the 5-year plan.
I'm assuming that the Student_ID field in the Students table is their id number or something, and not the primary key of that table.
Students Table
Id,FirstName,SecondName,Photo,Student_ID
Subjects Table
Id,Subject
Student_Subjects Table
Id,Student_Id,Subject_Id,Year
You may then assign a student multiple subjects, for multiple years.
The class_of_2011 table should contain the primary key of the students table and none of the other "repeated" data. All of the other columns you're interested in can then be obtained by joining the two columns together in a query.
I would restructure the data if possible... Something like....
Student Table
ID, Name, Address, other common specific to student
GraduatingClass Table
YearGraduate, StudentID
Enrollment Table
StudentID, ClassID, SemesterID