Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Bit of a carry on from this question here: Database design for school attendance system
I am currently doing something similar but have a few additional questions. So the school has approx 200 students but this is constantly changing. They attend for roughly 200 days and its required to keep the attendance data for 7 years. This is my create table statement here:
CREATE TABLE attendance (
studentID INTEGER REFERENCES students (studentID),
date DATE,
present BOOLEAN
);
My question is, is this the best way to create the table?
I haven't really done any direct database stuff from a UI before so my proposed solution was:
First a local variable that would use C# DateTime.Today function to get todays date;
Another local variable which would get the StudentID from the UI;
And then the attendance value which is a boolean, again set by the UI and stored as a 0/1;
All of these then combined into the Insert into SQL statement.
Is this the best way? Any tips or help would be appreciated thanks.
I'd propose this table instead:
CREATE TABLE StudentAttendance (
StudentId int NOT NULL REFERENCES Students (StudentId),
Date date NOT NULL,
PRIMARY KEY( StudentId, Date )
)
I don't feel there is a need for a present column because the presence, or lack of, a row can indicate if a Student was present that day or not.
The table has a composite key comprised of StudentId + Date - this means there cannot be duplicate records (i.e. only 1 row per student per day)
"Id" is an abbreviation for Identity, it is not an initialism so it should not be all-uppercase (i.e. use Id instead of ID).
Update
After having read the comments by other users, especially #JNevill, I recognise that you probably would want to store student absence reasons, which means you will need an IsPresent bit column (as a row could now mean an is-present or is-absent) with a Notes nvarchar(max) column, but we can extend this to a tinyint to store an enum value for different types of attendance records:
enum StudentAttendanceType
{
Present = 0,
UnauthorisedAbsence = 1,
AuthorisedAbsence = 2,
MedicalLeave = 3,
// etc...
}
CREATE TABLE StudentAttendance (
StudentId int NOT NULL REFERENCES Students (StudentId),
Date date NOT NULL,
Type tinyint NOT NULL,
Notes nvarchar(max) NOT NULL
PRIMARY KEY( StudentId, Date )
)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this issue in my c# MVC App where i have a receipt and this receipt has a receipt number and i select the last receipt number inserted in the database and add 1 to it, the problem appears when 2 users submitting the receipt at the same time. What happens is that they both get the same receipt number and they both insert into the database, unfortunately one is inserted and the other gives exception and rollback the whole operation. So my question is there a way to prevent this from happening, in other words reserve a receipt number taken by user, so it won’t be used by the other user.
Thanks in advance.
If using sql server you should probably use an IDENTITY column. Another option would be to use a GUID as the key.
So you need to reset the receipt number for each new year? You could make a separate call to get the receipt number first without doing inserts into other tables. This would reserve a number and return immediately. You could use an identity column for this, either a separate table per year or reset the identity value and truncate at the start of each new year. Or you could have a table for Ids with Year and ReceiptNumber as the PK and insert a new row with incrementing receipt number by one.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am creating a web app using ef core, and I have three entites, Student, Teacher, and Project. Now each of these tables needs to have multiple Links (Id & Url columns here) that point somewhere on the web. What is the best way to design this relation.
I've thought adding a StudentId, TeacherId, and ProjectId nullable column to the Links table. I've thought of creating a Links table for each entity. I've also thought of using a discriminator column, but still, it doesn't feel right.
How do I properly design this?
Note: The Student and Teacher table have a one to one relation with a user table, so I could just put the foreign key there, but the Project table is unrelated to these.
Based on the comments the link, you have a set of links for some or each of your entities in question.
But these are attributes, even a link array is an attribute of those entities, as a link is not an entity on its own. Unfortunately, 1NF requires each attribute to have a unique value. But let's be serious: is this an unbreakable constraint? Of course not. Some RDBMS do have array columns. SQL Server does not. But it has XML and JSON column types. EF has some other tools to provide the same.
Actually it depends on what do you want to do with those links: are they somehow processed with SQL or not. If only the upper tiers are handling them - and I suppose they are processed on the visualization tier - from the database point of view the link array is a single attribute.
If those links are passing trough your business logic, and they are only used during rendering, you can simply store them as comma-separated lists, or JSONS text in a nvarchar with a string property counterpart in the EF entity class, and split/parse them only on the visualization tier. That would make the less concentrated effort.
If your business logic needs them separately, EF does have support for complex types. You can still store your arrays as JSON text in a nvarchar column and serialize-deserialize them transparently. Check here: https://www.codeproject.com/Articles/1166099/Entity-Framework-Storing-complex-properties-as-JSO even better solution here: https://entityframework.net/knowledge-base/14779740/can-i-embed-an-object-in-an-ef-entity--serialize-on-save--deserialize-on-access--
SPARSE is still there of course, if applicable.
You can use below three mapping tables. Which are maintain your many to many relationship.
Like: 4 to 5 students working on same link.
2 teachers handling same link.
create table student_link_mapping(slmid int identity(1,1), studentid int not null, linkid int not null)
create table teacher_link_mapping(tlmid int identity(1,1), teacherid int not null, linkid int not null)
create table project_link_mapping(plmid int identity(1,1), projectid int not null, linkid int not null)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I wanna create a web-based bingo game using MVC and EF, I want to store 24 numbers for each card in the database table as a record,I'm not sure how to create my Bingo cards table,one way that comes to my mind is design a table with 24 integer field for each cell's number this way when I fetch the record in business logic I can't traverse the numbers because they are in the separate fields(actually I can but I think this way is not the right way to do)the other way is I concatenate all numbers together and separate it with comma and store it as a string so when I fetch the record I can split the string of comma and have all numbers in an array or something
so what is the best solution for this situation ?
how can I design my table for storing numbers of a bingo card ?
Good question!
You're right, storing it in 24 different integer fields is definitely a bad way to go. Concatenating them with commas might seem like a reasonable compromise, but it's inflexible, as you're treating them as text, not integers, so later on if you wanted to run queries such as SELECT MAX(CardNumber) FROM BingoCardNumbers WHERE CardID = 1 you couldn't do anything like that.
My suggestion would be to have two different tables, BingoCards and BingoCardNumbers. The BingoCards table would have a CardID, as well as other information relevant to the card, such as who the card belongs to, so maybe you'd want a UserID column in there too. No card numbers are stored in this table.
Then, in the BingoCardNumbers table, that would have three columns, CardID, which is the primary key from the first table, NumberIndex (1-24, which is the index of the number in the card), and finally CardNumber, which is the actual number itself. Then, if you want to get all the numbers for a particular card, you can simply do:
SELECT CardNumber FROM BingoCardNumbers WHERE CardID = 1 ORDER BY NumberIndex
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My question is there any data type is there in MySQL to insert date of birth into the database table directly else which data type i can choose to insert date of birth into the table?
You're looking for a date, so use the data type DATE
Use the DATE datatype. See here: http://dev.mysql.com/doc/refman/5.1/en/datetime.html the DATE type is distinct from DATETIME in that it ignores the time. You must use the YYYY-MM-DD format in your INSERT OR UPDATE SQL if you're not using parameters (which you should be).
CREATE TABLE People (
dateOfBirth DATE NOT NULL
)
INSERT INTO People ( dateOfBirth ) VALUES ( '1966-07-01' )
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table named product where there is a field named id its the primary key here. I need to put this key to my another table pro_size_tbl as below
pro_size_tbl
==================
id
productid
sizeid
the matter is when the user adds a product the sizes of the products are saved in session and I am trying to save them in pro_size_tbl by the product key what I just made.
The thing I am doing right now is I am adding the product in the product table and in the next line I am retrieving the max id of the product table and using it as the key for the pro_size_tbl.
My question is is there any better way to do this thing?
This is simple.You can use LAST_INSERT_ID().See this sample:
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();
There is a function LAST_INSERT_ID() which can give you the desired value.
But you need to use right below the insert statement.
owh. from what i understood, u need to put auto generated mysql id to another table?
if so, here is the real deal
$sql_tbl1 = "insert here for first table";
$result_tbl1 = mysql_query($sql_tbl1) or die(mysql_error());
$id = mysql_insert_id();
$sql_tbl2 = "insert here value ($id, 'bla', 'bla')";
mysql_query(sql_tbl2);
LAST_INSERT_ID() function is ok but it is doing the same work as i am doing taking the maximum value.i guess more or less both way are the same.