SQL database design for bookmarking an item [closed] - sql

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 have a web application in which each user is allowed to add its own items.
These items are not public or shared/visible accross the users. In other words, if user 1 creates an item, that item is only belongs to user 1.
[table: item]
id title content
1 ... ...
2 ... ...
3 ... ...
[table: bookmark]
item_id user_id
1 1
2 1
3 2
I need a "bookmarks" page which shows those items that the user has bookmarked. Does it make sense to use a separate table bookmark like I did above?

If you use create a new column named bookmark and bookmark_reference of the type uniqueidentifier for both tables, and your code simply puts a GUID (or the SQL function NEWID()) when a user does a bookmark.
Your bookmark table replace item_id with bookmark_reference and another varchar(100) column table_reference.
Thus making your bookmarking functionality generic for the whole system, any table, can be used for bookmarking or any other text the user would want to record against this particular record of this particular table.
Like custom 'notes' against a customer for just this user.
To make a "public" bookmark/note, just use either a zero or a null in user_id.
Just giving you basic ideas. The ERP Syteline from INFOR uses this type of functionality, but they use a guid (uniqueidentifer) for the user_id equivalent field, making the table unreadable without a INNER JOIN to the users table. So using user_id will make everyone's life easier down the road.
So the structure would be like this:
[table: item]
id title content bookmark
1 ... ... {guid abc}
2 ... ...
3 ... ...
[table: bookmark]
bookmark_reference table_reference user_id
{guid abc} item 1
{guid def} item 1
{guid xyz} customer 2
{guid 123} user 1
Fun fact with this structure, a user could have a bookmark on a user!
Edit: Link to a StackOverflow article on using uniqueidentifier

Related

Remove sorting order differences while comparing 2 tables using sql [closed]

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 days ago.
Improve this question
I am trying to compare cell to cell of 2 tables.
There is a situation wherein a record in column C is in row 4 of table 1 while same record is in row 7 in table 2.
Hence during comparison, record in col C and row 4 is giving a mismatch as it's not same in table 2.
In reality, such mismatches are to be ignored since it exists somewhere within table 2 but just in different row.
What is the best way to ignore such mismatches.
I am not able to get with Exist function syntax.
For example record in tbl1."Col C" is to be seen if available in tbl2."Col C" and if this record is not found, then the mismatch has to be reported.
I am not able to get right syntax here either for exist function or sorting 2 tables and then comparing.

Picking unique records in SQL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Say I have a table with multiple records of peoples name, I draw out a prize winner every month. What is a query in SQL that I can use so that I pick up a unique record every month and the person does not get picked on the next month. I do not want to delete the record of that person.
create a new column as a flag named anything like 'prizeFlag' make it boolean take only 0 and 1 or anything as you like, make it's default value is 0 means not get a prize yet
when you select a random column update this filed with 1 means take a prize
when you select a random column next month, Add a condition in WHERE Clause say the prizeFlag not equal 1 to avoid duplication
One should store whether a person has already won. A date would make sense to allow people after say 10 years to win again.
ALTER TABLE ADD won DATE;
A portable way would be to use a random number function outside the SQL.
SELECT MIN(id), MAX(id), COUNT(*) FROM persons
With a random number one can get the next valid ID.
SELECT MIN(ID) FROM persons WHERE won NOT IS NULL AND ID >= ?
int randomno = minId + new Random().nextInt(maxId - minId);
preparedStatement.setInt(1, randomno)
UPDATE persons SET won = CURRENT_DATE WHERE ID = ?

Calculate number of exercises in each category in access 2016 [closed]

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 5 years ago.
Improve this question
I have two tables such as: Category and Exercise. In the first of them I have field names such as: CategoryID and CategoryName. In the second table there are field names such as: ExerciseID Exercise and CategoryID. I have to calculate the number of exercises in each category. For the exercises with provided data I have:
addition, subtraction, multiplication, division, enhancemen,
running, jumping, pole vault, shot put, literature, grammar.
The query has to output me:
Maths = 5
English = 2
PE = 4
How would I be able to calculate this within query in ms-access?
You should have a key to join the tables.
In addition to ExerciseID and Exercise, have a CategoryKey column which you can join to the Category table through CategoryID. Then you'd just query SELECT COUNT(*) as count FROM Exercise WHERE CategoryKey = 1 where 1 is the CategoryID you want to count. You can also do more complex joins in the future if you have new requirements.
Otherwise, you'll have to write a giant goofy sql switch statement, which I don't have much experience with, because it's usually not something you need to use if your table is structured correctly.

flexible table in SQL [closed]

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 7 years ago.
Improve this question
I have a form php page that sends information about jobs. Each user can register unlimited jobs, so I need a flexible table in my database that when user insert new job, the table will get the values and save the information as job 1,job 2 ... job n.
Is there a way or maybe a simpler way?
Just to expand on my comment:
Your best bet here is a table for users:
user_id | name | other | attributes | for | a | user | like | birthdate
And a seperate table for the user's relationship to a job
user_id | job_id
If you need to store information about a job, then do it in yet another table
job_id | job_name | other | attributes | for | a | job
Now your database schema doesn't have to change dynamically as new data comes in. The only thing that grows is your record count. This is proper database design. You can query for all the job names associated to a user:
SELECT user.name, job.job_name
FROM user
INNER JOIN user_job ON user.user_id = user_job.user_id
INNER JOIN job ON user_job.job_id = job.job_id
WHERE user.user_id = 123
The direction you were heading where your schema changes as new data comes in will cause you nightmare scenarios down the line and should be avoided at all costs. For instance, imagine the same query where the number of job columns grows as new data comes in:
SELECT user.name, user.job1, user.job2, user.job3... how do I know where to stop for this user?
FROM user
WHERE user_id = 123

Commonly concepts in adding contacts for staffs using master table [closed]

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 9 years ago.
Improve this question
Using MS SQL: The scenario is I have already a Contact master table (tbl_Customer)for all contacts in the company. The staffs suggested that they do not want to see all the contact and that they will only choose which contacts are applicable for them. The contact table is not gonna be store on the client like an outlook's pst file but on server side particularly on the database.
The methods i can use are:
1.) every time i add a staff i create a new independent table for the staff and the staff just adds the contact he/she needs from the master table via a program.
2.) I can change the contact master table to add say 50 fields with names staff01, staff02 and so on... I will make use of this fields as a marker that this rows of customer is a contact of the staff on the fields.
3.) I do a completely new contact list. Adds a field name "User". Mark the initial records via putting "ALL" on the field name. If a staff adds a contact, the table add a record with his/her name on the field "User". There will be an Auto numbered field as primary index.
Currently I'm tempted to use 3.) are there any other better method for my problem?
Why don't you have a staff table with a staff id - then every contact the staff in question wants can be a row in staffContacts. Column 1 is staffID and col 2 is contactID. Then you can join on the contact table to retrieve the details you want.
The contacts table then only needs be updated...You only need to add one table with two columns. You may wish to impose constraints that let fields be entered that are unique to staffID and contact ID and are valid staff members and contacts.
Nick.
If I understand correctly you want to have a list of the contacts that is filtered specifically based on a particular users preferences?
Personally I would create another table like this:
Create Table UserContactPreferences
(
ID int identity(1,1),
UserID int,
ContactID int,
)
And then each individual user can pick what contacts they want visible and you can add an entry here for it.
Then when you query the database you can do something like this, passing in the CurrentUser in the query:
SELECT c.*
FROM tbl_Customer c
JOIN UserContactPreferences up on c.contactID = up.contactID
WHERE up.UserID = #CurrentUser
This will then only return Customers that the user has specifically said they want to see.
In case many staff members have the same relevant contact - you should use a connection table, in which there will be 2 columns - one with the contact id , and one with the staff member id, use this table to query for staff's relevant contacts.
In case each contact has only one staff, than simply add a column with the relevant staff id, which will be a foreign key to the staff's table.
In any case and for other users to learn from:
method 1 you suggested is a big no no in SQL DB design, you need a really good reason for a DB with dynamically created tables.
method 2 is bad practice since most of the space will be wasted (unless defined as sparse columns but even though.. still space consuming) and also too specific - what will happen when you add a new staff member ? add a new column ? lock the whole DB for it and add many wasted space allocated?