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
Related
I would like to know what the best way is to create a custom lookup for a field in my table, my situation is as following:
I have Form A which has a datasource to Table A, a field on that datasource has a lookup method:
public void lookup(FormControl _formControl, str _filterStr)
{
changeCompany(companyInfo.DataArea)
{
super(_formControl, _filterStr);
}
}
The field has an EDT, which has an relation to a Table.
The table has multiple fields, 1 of them is field: GroupType (Enum), with 2 options: Suppliers and Customers.
Form A is showing all records, both with Suppliers and Customers, but i would like to filter on the records with only has the value Suppliers in Column C.
Based on the information above, what is the best way to create this custom lookup?
You can create Related field fixed relation between your tables
TableB = TableB.Id
Enum::Suppliers = TableB.GroupType
Or create a custom lookup
and set a range for field GroupType.
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'm trying to model a "simple" table of 3 columns:
userId | topicId | views
The table should be able to:
have repeating userId
have repeating topicId
enforce unique combination of userId and topicId
In other words, there are many topicId's associated with a userId, and views will be a number that is incremented.
How would I create my indexes if I need to do the following queries:
get a list of records for provided userId
get and update one unique record for provided userId and topicId
Since you have more topicIds than userIds you should create your table with:
hash key - topicId
range key - userId
Add a global secondary index with:
hash key - userId
range key - topicId
You can then insert and update records using the combo topicId + userId and query for lists of topics for a specific user id.
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