I have a table with main-ids and user-ids. Each user-id has a set of their own unique main-ids, but multiple user-ids can have the same main-id. Is there anyway to increment a main-id for a specific user without having to do 2 queries?
If you mean like this:
User ID Main ID
1 1
1 2
1 3
2 1
2 2
2 3
Then you're going to need to make an INSERT trigger that finds the next MainID for that user and stores that.
Related
I have a table usersbooks:
idUser - int
idBook - int
bookSummary - text
and this situation:
idUser idBook bookSummary
1 1 modifiedInfoAboutTheBookByTheUser
1 2 modifiedInfoAboutTheBookByTheUser
1 3 modifiedInfoAboutTheBookByTheUser
2 1 modifiedInfoAboutTheBookByTheUser
2 3 modifiedInfoAboutTheBookByTheUser
3 2 modifiedInfoAboutTheBookByTheUser
3 3 modifiedInfoAboutTheBookByTheUser
I need to insert a record like
#user #book initalInfoAboutTheBook
for each combination book user (with the corresponding initial infos), but some record are still there and must NOT be overwritten or updated for they could be different from the initial ones. Some are missing, like user 2 that has not book 2 and user 3 that has not book 1, and these are the one that must be inserted.
Assuming I can make several queries into a for cycle, one cycle for each book, like
"INSERT INTO usersbooks
idBook='+idBook+' ..... "
with idBook 0 to 10, for example
what must be the INSERT query to get the desired result without touching the existing record?
idUser idBook bookSummary
1 1 modifiedInfoAboutTheBookByTheUser
1 2 modifiedInfoAboutTheBookByTheUser
1 3 modifiedInfoAboutTheBookByTheUser
2 1 modifiedInfoAboutTheBookByTheUser
2 2 initalInfoAboutTheBook (newly inserted!)
2 3 modifiedInfoAboutTheBookByTheUser
3 1 initalInfoAboutTheBook (newly inserted!)
3 2 modifiedInfoAboutTheBookByTheUser
3 3 modifiedInfoAboutTheBookByTheUser
NB: There are TWO problems here: how to avoid touching existing record and how to execute the query for any user already present in the db
an easy solution can be this.
you can try to make a primary key with the two fields idUser and idBook,
then use an Insert Ignore. that way avoid the duplicate key error, and insert just when the primary key(the combination of that two ids) doesn't exists.
I am designing a mysql database and have come across these relations which will grow in the future.
Suppose Customer is tied to 2 different tables Policies and Options.
Each customer has multiple relationship with policies and likewise with options. Since I am keeping a details and history of the table as well every time I add a relation with customer, I will have to maintain another 2 tables. To calculate the price the customer owes, I will have to go thru customer_policies then customer_options and calculate the total price. Also the number of tables increases as the relationship increases.
If customer has a relation with policies it will have 2 tables -
customer_policies and customer_policies_details.
If customer has one more relation with options, it will add 3 more -
customer_options, and customer_option_history.
Like wise, it will keep on adding 2 more tables if there is one more
relation and the problem grows and grows.
I have tried 2 different options which I have mentioned below. I wanted to know what is the best way to solve this problem so that the table can be maintained as the relation grows.
Option 1:
customer_policies:
CustomerPolicyId CustomerId PolicyId Status
1 1 1 Active
2 1 2 Active
customer_policies_details:
CustomerPolicyDetailsId CustomerPolicyId Price
1 1 10
2 2 20
customer_options:
CustomerOptionId CustomerId OptionId Status
1 1 1 Active
2 1 2 Active
customer_options_details:
CustomerOptionDetailsId CustomerOptionId Price
1 1 10
2 2 20
Option 2:
Create a single table customer_selections and use Type and Id field instead like so:
customer_selections:
CustomerSelctionId CustomerId Type Id Status
1 1 Policy 1 Active
2 1 Policy 2 Active
3 1 Option 1 Active
4 1 Option 2 Active
customer_selection_details:
DetailsId CustomerSelctionId Price
1 1 10
2 2 20
3 3 10
4 4 20
To create a history of this I just have to create a customer_selections_details and keep track of all changes.
There should be better ways to solve this problem.
How can we update table 1 such that it will replace b field value of table 1 by that of table 2 where a field value are found same?
Suppose I have two tables
table 1
fields a b c
1 5 10
1 5 8
2 5 0
1 4 11
and
table 2
fields a b
1 6
1 7
2 5
1 4
I'm going on 6th form knowledge so I'll leave the code for you to do, but here's basically how I'd do it:Select all values from table 2For each value, select the rows from table 1 with the matching 'a' valueCount number of matching valuesIf it's over one, update table 1 set 'b' as the new value where 'a' matches
Edit: Oh, just realised, the 'a' values aren't unique, unless both tables have matching ID for each row, I'm not sure you can do it.
I have table with colimns
ID|NAME|AGE
1 |name1|40
1 |name2|45
2 |name3|30
2 |name4|39
result i want like this
ID1|NAME1|AGE1|ID2|NAME2|AGE2
1 |name1|40 | 2 |name3|30
1 |name2|45 | 2 |name4|39
there are around 5k rows.
Thanks.
You can get a full product of the tables:
select table.col1,table.col2,table.col3,table2.col1,table2.col2,table2.col3
from table, table2
where table.col1='test' and table2.col2='test1'
The result may have duplicate records from the both tables. But as you don't have any primary keys that's possibly not an issue for you.
I have a table User that stores user information - such as name, date of birth, locations, etc.
I have also created a link table called User_Options - for the purpose of storing multi-value attributes - this basically stores the checkbox selections.
I have a front-end form for the user to fill in and create their user profile. Here are the tables I have created to generate the checkbox options:
Table User_Attributes
=====================
id attribute_name
---------------------
1 Hobbies
2 Music
Table User_Attribute_Options
======================================
id user_attribute_id option_name
--------------------------------------
1 1 Reading
2 1 Sports
3 1 Travelling
4 2 Rock
5 2 Pop
6 2 Dance
So, on the front-end form there are two sets of checkbox options - one set for Hobbies and one set for Music.
And here are the User tables:
Table User
========================
id name age
------------------------
1 John 25
2 Mark 32
Table User_Options
==================================================
id user_id user_attribute_id value
--------------------------------------------------
1 1 1 1
2 1 1 2
3 1 2 4
4 1 2 5
5 2 1 2
6 2 2 4
(in the above table 'user_attribute_id' is the ID of the parent attribute and 'value' is the ID of the attribute option).
So I'm not sure that I've done all this correctly, or efficiently. I know there is a method of storing hierarchical data in the same table but I prefer to keep things separate.
My main concern is with the User_Options table - the idea behind this is that there only needs to be one link table that stores multi-value attributes, rather than have a table for each and every multi-value attribute.
The only thing I can see that I'd change is that in the association table, User_Options, you have an id that doesn't seem to serve a purpose. The primary key for that table would be all three columns, and I don't think you'd be referring to the options a user has by an id--you'd be getting them by user_id/user_attribute_id. For example, give me all the user options where user is 1 and user attribute id is 2. Having those records uniquely keyed with an additional field seems extraneous.
I think otherwise the general shape of the tables and their relationships looks right to me.
There's nothing wrong with how you've done it.
It's possible to make things more extensible at the price of more linked table references (and in the composition of your queries). It's also possible to make things flatter, and less extensible and flexible, but your queries will be faster.
But, as is usually the case, there's more than one way to do it.