I need to create DB of store that has books, CDs and DVDs. There are 3 categories of books: programming, cookery and e.g. spirituality. All goods have name, price, bar code. Books have pages and parametrs specialized for each category of books -- language for programming books, main ingredient for cook books, and age limit for spirituality. CDs and DVDs are devided to misic, video and Software. So, when I design DB I have a problem in connecting table: I have to take data from three different tables for one field.
+-----------+
| Tables |
+-----------+
|Categories |
|Languages |
|Main_Ingred|
|Age_Limits |
|Books |
+-----------+
+--------------+
| Books |
+--------------+
|id |
|category_id |
|price |
|bar_code |
|pages |
|special_param.| // Here I have to use data from 3 different tables
+--------------+
I think it is not right decision. Could you help me?
And I'll have the same issue for design of common table of goods.
Try this:
Books, CD-s, DVD-s and whatsoever go to table products (i added the fields all of your products have)
+--------------+
| Products |
+--------------+
|id |
|name |
|price |
|bar_code |
+--------------+
then there is a table categories
+--------------+
| Categories |
+--------------+
|id |
|name |
+--------------+
then connect the two tables (i have made many to many just in case one product goes to more categories)
+--------------------+
| Product2Categories |
+--------------------+
|id |
|product_id |
|category_id |
+--------------------+
last is the table for those special attributes like language, age limit, ingredients etc.
+--------------+
| Attributes |
+--------------+
|id |
|name |
+--------------+
then you need to connect the Product with the special attribute and assign the correct value:
+---------------------+
| Attributes2Products |
+---------------------+
|id |
|product_id |
|attribute_id |
|value |// varchar(255)
+---------------------+
Now this is ok but its not ideal because different attributes have different type of values, like language for programing books is a varchar, or age limit for spirituality is an integer, in the example above all the special attribute values are varchars.
If u want to optimize the database above you would need to edit the Attributes table:
+--------------+
| Attributes |
+--------------+
|id |
|name |
|type | //type of data it recives
+--------------+
and then create as many of Attributes2Products tables as there are datatypes you intend to use like if attribute is an integer type u save the special attribute value in table:
+---------------------+
| IntegerAtt2Products |
+---------------------+
|id |
|product_id |
|attribute_id |
|value |// int(11)
+---------------------+
or if attribute is a string type
+---------------------+
| VarcharAtt2Products |
+---------------------+
|id |
|product_id |
|attribute_id |
|value |// varchar(255)
+---------------------+
and so on.
Hope this helps.
Related
Say I have a table Dogs
mysql> select * from dogs;
+----------+-------------+----------+
| dog_id | tail_length | owner_id |
+----------+-------------+----------+
| dog-id-1 | 1 cm | owner-1 |
| dog-id-2 | 2 cm | owner-2 |
| dog-id-3 | 3 cm | owner-3 |
+----------+-------------+----------+
Where dog_id is a primary key and owner_id is unique.
Now I want sharing of dogs to be possible. so the table can be.
+----------+-------------+----------+
| dog_id | tail_length | owner_id |
+----------+-------------+----------+
| dog-id-1 | 1 cm | owner-1 |
| dog-id-2 | 2 cm | owner-2 |
| dog-id-3 | 3 cm | owner-3 |
| dog-id-3 | 3 cm | owner-1 |
| dog-id-3 | 3 cm | owner-1 |
+----------+-------------+----------+
But it is not possible as dog_id is a primary key and owner_id is unique in the table. A dog can possible be shared with 10000+ users.
Due to constraints of backward compatibility I cannot remove the primary and unique key constraints of the original table and I have to use mysql to do this. What would be the best strategy to achieve sharing?
Additional Constraint: I can only query through dog_id and not owner_id.
What you have is a many-to-many relationship.
A dog can have many owners
An individual owner can own many dogs
This is a common problem in relational database design. What you need for a many-to-many relationship is to define another table.
+----------+----------+
| dog_id | owner_id |
+----------+----------+
| dog-id-1 | owner-1 |
| dog-id-2 | owner-2 |
| dog-id-3 | owner-3 |
| dog-id-3 | owner-1 |
| dog-id-3 | owner-1 |
+----------+----------+
I can only query through dog_id and not owner_id
I don't understand why this is relevant. The data organization is about Third Normal Form, not about how you will query the data.
The only alternative you have is to store multiple owner ids in one column of one row, which is not a valid design for a relational database. See my answer to Is storing a delimited list in a database column really that bad?
Create another table Owners and supply a Foreign Key to the table Dogs.
I have table 'orderby'
+-----------------+
| id | data |
+-----------------+
| 1 | 4,2,5,6 |
+-----------------+
and second table data
+-----------+
| id | ... |
+-----------+
| 2 | ... |
+-----------+
| 4 | ... |
+-----------+
| 5 | ... |
+-----------+
| 6 | ... |
+-----------+
i want to sort data table by orderby tables data column.
like this:
+-----------+
| id | ... |
+-----------+
| 4 | ... |
+-----------+
| 2 | ... |
+-----------+
| 5 | ... |
+-----------+
| 6 | ... |
+-----------+
i tried this query: select * from data order by field(id,(select group_concat(data) from orderby))
but not works.
The best solution would be to add a column orderby INT UNSIGNED to your data table, and use numbers to get the correct order.
If you need the ordering to be user dependant, you'll have to use a separate table with user id, data id (from your data table) and orderby value.
A single orderby value as you are using in your code, will not work. (It might be possible, but I think it would be hard to implement and it would not perform well.)
+---------+ +-----------+ +---------+
| USER | | USER_LANG | | LANG |
| id_user | | id_user | | id_lang |
| name | | id_lang | | name |
| | | years | | |
+---------+ +-----------+ +---------+
I want to write query for saving data from user and user_lang in database at same time...is there some insert join or what?
No there isn't. You can only select or delete from multiple tables at once.
If table structure would have been same; it would have been possible. But in your case you will have to use multiple queries. If you want to ensure integrity between table data then use Stored Procedures.
try this
INSERT INTO LANG (id_lang,name)
VALUES (SELECT ul.id_lang ,u.name
FROM `USER` u
INNER JOIN `USER_LANG` ul
ON u.id_user = ul.id_user
)
I have a complicated query to resolve and I don't know how to get the correct results. First of all, let me show you the tables I have:
+------------+
| users |
+------------+
| id |
| first_name |
| last_name |
+------------+
+--------------+
| clients |
+--------------+
| id |
| users_id |
| uid |
| access_token |
+--------------+
+---------------+
| users_follows |
+---------------+
| users_id |
| follow_id |
+---------------+
+-------------------+
| users_connections |
+-------------------+
| id |
| users_id |
| clients_id |
| uid |
| name |
+-------------------+
Our website uses Facebook Connect, so EACH user connected has a client UID (Facebook UID). For a functionality of the website I need a query that does the next: select all the the people you follow (users_id=ME) plus the users_connections but if a users_connection is also someone I follow do not include it on the final result. Finally, for this rows if it's a "following" I need users.first_name and users.last_name and if it's a connection NOT registered on our website I need users_connections.name.
I will have a lot of rows an probably people can have a lot of people who follow so a NOT IN and id's concatenated I think that it's not the best way to scale it.
Can anyone bring me some light?
Thank you in advance!
I have finally stumbled across a problem that I can't already find the answer to on SO...
I am working on a database that will store recorded sampled data recorded over time. Originally, the client had built a table that was very specific to the data that they were currently recording, however, they would are concerned that as they expand, the collected data may begin to vary, and more, or at least different, columns may be required.
Current model:
+------------------+ +------------------+
| FACILITIES | | DATA_RECORD |
+------------------+ +------------------+
| ID | | ID |
| NAME | | FACILITY_ID |
| DESC | | TIMESTAMP |
| etc. | | TEMP_WATER |
+------------------+ | TEMP_AIR |
| pH_WATER |
| etc... |
+------------------+
I think the database should be designed as follows:
+------------------+ +------------------+ +------------------+
| FACILITIES | | DATA_RECORD | | COLUMNS |
+------------------+ +------------------+ +------------------+
| ID | | ID | | ID |
| NAME | | FACILITY_ID | | NAME |
| DESC | | details etc.. | | DESC |
| etc. | +------------------+ | UNITS, etc... |
+------------------+ +------------------+
+------------------+
| DATA_POINT |
+------------------+
| ID |
| DATA_RECORD_ID |
| COLUMN_ID |
| VALUE |
+------------------+
My questions are:
Is this the best way to design the database, or is there a better way that I am not familiar with.
How do I form the query statement to return each data record with each of its associated columns?
Is there a generally accepted good data-type to use as DATA_POINT.VALUE? e.g. float, decimal...?
Thank you so much for your help.
Sincerely,
Nate K.
Looking at your requirements, I think you'd be leaning more towards the Entity-attribute-value type of design, which in general is tough to query (although it is fairly simple to set up) and not very scalable.
You can search for EAV models on this site or on google to see discussions about this.
http://www.simple-talk.com/content/print.aspx?article=843
Entity Attribute Value Database vs. strict Relational Model Ecommerce
http://tkyte.blogspot.com/2009/01/this-should-be-fun-to-watch.html
For your case, can you post the different types of Facilities and datapoints that you'd need to collect? May be you can use types and subtypes to model this?