I have 2 tables :
StudentInfo
Attendance
The StudentInfo table has fields about students like: RollNo, FirstName, LastName etc...
The Attendance table has the fields: RollNo, FirstName, LastName, InClass(Yes/No) and Date.
I want to duplicate the common fields from StudentInfo to Attendance and when I add a new entry in the StudentInfo table it will automatically add them to the Attendance table.
Also, when I add attendances in the Attendance table then I want to be able to view the data by months or dates, so that I can see on what day how many students attend the class.
Can you suggest a way for me to store attendance of students like we store on register, your help will be really appreciated!
Related
I am working on a project in which people can search books with ratings. The site has a login form which leads to a dashboard. I am wondering how to add books to a "saved books" list on the dashboard. I am using a SQLite Database so an array is not an option. Any suggestions for how to save books to a saved books table for each user would be greatly appreciated.
My User Model:
class User(UserMixin, userDB.Model):
id = userDB.Column(userDB.Integer, primary_key = True)
username = userDB.Column(userDB.Text(30), unique =True)
password = userDB.Column(userDB.Text(30))
My book model:
class Product(Base):
__tablename__ = "books"
title = Column(Text)
price = Column('price', Integer)
img = Column('image', String)
rating = Column('rating', String)
id = Column('id', String,primary_key=True,)
You can create a table "savedbooks", for example, in the same database with columns: id(primary key autoincrement), id_user(foreing key user) and id_book(foreing key id in books).
After you can retrieve the saved books by id of the user in "savedbooks" table.
I have three Tables:
Teachers
PK: A_pk
Students
PK: B_pk
Post
PK: C_pk
FK: A_pk
FK: B_pk
I have a Website page where users write different posts.
When teachers post in that group, I will insert data into the Post table like this:
A_pk = teacherName
C_pk = Post_text
When students post in that group, I will insert data into the Post table like this:
B_pk = studentName
C_pk = Post_text.
Reason is that I want to keep record which user posted data in my group.
Now the Question is how to insert record in Post table?
You cannot have one field in your table be a foreign key to two different tables. What you need to do is change your data structure to either hold your students and teachers in the same table and use that primary key in your Post table, or to have two columns in your Post table - one for the Teacher primary key and one for the Student primary key and populate the appropriate one based on who made the post.
There is absolutely no need nor reason to have the ID of two different tables within the same field. Doing so would be bad design.
Finally--- I found the Solution.
Actually its very simple... Like you can have nullable FK.so when you insert for student then A_PK can be null and vice versa. –
If teacher posted data in group than
if(A_pk != null) { Insert A_pk and Insert C_pk }
If student posted data in group than
else if(B_pk != null) { Insert B_pk and Insert C_pk }
Thanks alot Sir
KumarHarsh 1 hour ago
I have a bidding website, each user can bid his own price for a product.
The schema:
productId: S // the product, probably the Hash key
day: S // for example: "2016-08-05"
userId: S // the user id that offer his bid for this day
bidPrice: N
I don't know how to design the table(s), i need to be able to a query like this:
SELECT userId, bidPrice FROM product-history WHERE productId = "sony-tv" AND day between "2016-01-05" AND "2016-05-05"
However, the day field cannot be a range key because i can't store multiple rows per productId+day
I also need to be able to update the bidding price for a user... something like:
UPDATE product-history SET price = 12 WHERE productId = "sony-tv" and day="2016-01-05" and userId = "steve"
What the best way to approach this?
Here is the design for the above use case.
Hash Key : User Id
Range Key : Product Id
Each user can bid on a product id.
This design allows an user to bid on multiple product ids as well.
Create global secondary index (GSI) on product id and date:-
You can use the GSI to query the database using product id and date. While creating the GSI, there is an option to include only few attributes or all the attributes from main table.
GSI Link
Limitation:-
The user bidding history of a product id would not be available. For example, if an user is changing the bidding on a product id multiple times, it would not be available on this data model.
I have 3 tables. Blog and Tag have a many to many relationship. BlogTag is a junction table with a quantity column.
**Blog**
BlogID
Title
**Tag**
TagID
Name
**BlogTag**
BlogID
TagID
Quantity
I'm not sure how I handle the quantity column. I'd like it to store how many Blogs have a certain Tag Name
How do I deal with the quantity column when adding a new blog that has tags?
Thanks!
Well, you'd need to calculate the quantity:
var q = (from b in Context.Blogs
where b.BlogTags.Any(t => t.Tag.TagId == someId)
select b).Count();
So you:
1. Add the blog
2. SaveChanges
3. For each tag on the new blog:
1. Calculate the quantity, as above.
2. Update the BlogTag.Quantity.
4. SaveChanges
I have a table for users (username, password .. etc) and another one for products (product name, price, quantity, username .. etc)
I want the username from the first table to be inserted (with the rest of the product info) into the product table when the user put it for sale.
How can I do this?
I'm using visual web developer 2008 express
INSERT INTO product_information (username,product_name, ...., Date)
SELECT username, 'Book',....., Date
FROM users;
something along that line
Your question doesn't exactly make sense. It sounds like you want to insert into one table (Products) and one of the fields is also in another table (User.username). Where does the rest of the data for Products come from? Can't you just insert the username directly?
Insert into products (name, price, username) values (?, ?, ?)
Use whatever mechanism you want to pass in the data to that SQL statement.
If, however, you actually have a third table, say, 'Prod_for_sale' and you want to insert a bunch of data into that table, from both Users and Products, you can do something like this (as suggested by Rick J, but this example has two tables):
insert into products for sale (username, user_country, product_name, product_price)
select u.username, u.country, p.name, p.price
from products p, users u
where p.id = ?
and u.username = ?
Then you can copy whatever data you want from the tables in question into the destination table.
A word of advice though: you should be using a user ID instead of a username to identify the user's data in the database. This will allow you to change your username policy later or allow for usernames to be updated, etc. Generally try to make all your identifiers numbers and you'll save yourself hassle in the future.
If you have Linq (.NET 3.5 / C# 3.0):
var users = from u in userTable
from p in productTable
where p.username = u.username
select new productDetails()
{
username = u.username,
price = p.price
}
then newtable.insertAllOnSubmit(users);
if in straight sql
insert into newtable
select table1.value1, table2.value1
from table1, table2
where table1.username = table2.username
Edit:
to clarify... value1 and value2 are columns from the corresponding cases. You'll have one for each parameter you want to insert. And your newtable becomes table1