So a quick question here, I have two tables within my database which is connected to a Yii2 web application.
My two tables are named attributes and people, I have a CRUD area for both of them so I can manipulate the the contents of each table separately.
The two tables are extremely simple at the moment and are structured as follows.
Attribute Table:
attribute_id | attribute_name
------------------------------
People Table:
person_id | person_name
------------------------------
Now the attribute names will be items like hair color, eye color, weight, etc
Now what I want is to be able to assign an attribute to a particular person with a value, for example if person1 existed, I want to assign hair color (Assuming hair color already exists as an attribute) with a value such as 'brown' to person1.
At the moment I kind of have a mental block and don't really know how to go forward from this point on, could anyone point me in the right direction or help me out here?
Cheers.
You need another table
person_attributes
-----------------
person_id
attribute_id
value
Related
I'm a beginner trying to set up a database structure for a few million records.
My SKU format looks like this:
DESIGN LOCALITY PRODUCT TYPE GENDER COLOR
234324 45454 100 VN M BL
Example table:
SKU | DESCRIPTION | SIZE
I don't really need separate MySQL table columns for each of the fields within the SKU, as they are all contained within the SKU and can be stored in 1 column named SKU.
Yet, will this make my job of doing queries much more difficult and inefficient? Should I setup 1 column for each of the fields within the SKU?
Example table 2:
SKU | DESIGN | LOCALITY | PRODUCT | TYPE | GENDER | COLOR | DESCRIPTION | SIZE
Any help is much appreciated.
Since you need to query against the individual components of your SKU field, I recommend storing each component in a separate column. If you want to include the entire SKU as a field as well (as your "table 2" example suggests), I recommend making it a calculated column. Otherwise you could have update anomalies.
I would also recommend indexing each column individually, but there are others who will recommend a single index that includes all the fields. This is the subject of much debate in database-design circles :).
I am not sure how to formulate the question, so I didn't find anything useful enough regarding my problem.
1. In PRO(projects) I have 3 columns: "organizer", "partners", "other". I want to use information from table ORG(organisations) in all of these 3. Also, I need to show more than one partner. Is it possible? For example, I have
org:
name |country|city |
apple |shop |fruits part |
cherry|plate |big |
orange|plate |little |
banana|shop |frozen fruits|
I want to show in view.php:
All projects:
name |organizer|partner |other |place |
salad |banana |apple,cherry |orange |plate,little|
salad2|banana |apple |orange |plate |
Info for place in PRO is taken from two tables, country and city. But country and city are also used by organisation. organisation's country doesn't equal project's country(for example, project takes place in London, but none of the participants'organisations are based in London).
Are all of these things doable with what I already have?
I get "circled" relationship thanks to country/city double usage, is it allowed?(my teacher said no or should be avoided -? I don't remember- and I got different opinions from web).
If you want to Add multiple partners, there are really two ways to do it.
You could add a table to store partner information you could then have a column that contains a foreign key to the projects primary key that they are assigned to.
(this would be best if a partner is never assigned to multiple projects)
this is really an extension of #1. except in this case if you need to have projects assigned to multiple partners and partners assigned to multiple projects you could add a another table with only two columns. The first would be a foreign key to a project primary key, and the second would be a foreign key to partner's primary key. This way if you want to query for all of the partners assigned to project you could do:
Select * from Partners_Table where id=(Select partner_id from CrossReference_Table);
you can do the same the other way too.
Your Country Fields should be foreign keys to your country table, Same with city because your projects are not necessarily in the same city as there respective organization, therefor they should not be linked to the organization's city and country field. Everything else looks good from what I can tell.
I have a database in MySQL that currently lists approximately 1500 concerts and events. Now, the plan is to add setlists (list of the songs performed at the concerts) for all the concerts in the database. Basically this will mean a lot of repeated values (songs performed at many concerts), and I would really appriciate some input on what the best approach would be.
I initially started out with a database similar to this;
| eventID | edate | venue | city | setlist |
The field setlist was basically text data, where I could paste the list of songs and parse through it to put each song on a new line with php. This works, and editing the text and running order was like editing a text document. Now, obviously this was pretty simple, but has drawbacks and limitations. Simple things like getting stats on songs performed is probably very difficult, right?
So, what is the best way to store the setlist value?
Create a new table that adds a new row for each song performed, and that has a foreign key linking to eventID? How would I best retain (and edit, if needed) the running order of the songs in that table? Any other suggestions?
Thanks for any input or advice on this, as I would love to get some help before I start adding all the data.
I would create a table that holds each song performed at a specific event:
| songId | eventID | song |
Where eventID can be duplicated in multiple rows to show each song performed at that event.
This way you can query all the times a specific song was performed, and also get all songs (the setlist) for a specific event by querying on the eventID.
I have a table where I have information about each asset I have (Laptop, Screen, Desktop, Scanners, etc..). I also have another table where I have custom column for each asset (so for a specific desktop I can have a custom column called "Color")... quick shematic :
Assets Table
AssetID Description
---------------------------
1 Desktop HP79801
Assets Custom Column
AssetID Column1 Column2
-----------------------------------
1 Color BluRay ?
Now I want to store the value for each column. The way I currently do it for each unit is :
AssetID UnitID Column1 Column2
-----------------------------------
1 1 Blue Yes
1 2 Blue No
1 3 Black No
1 4 Blue Yes
Now my question, is there a more efficent way to do this and how can I increase the number of column to infinity in an efficent way also ? Is it possible ?
In the end, how to store custom product attributes with effecienty ?
In short, yes.
What you have there isn't scalable in the slightest. What you want is to have a table for your assets, a table for the custom parameters and a "link" table. Then, enter multiple values into your link table rather than multiple columns.
As an example:
Assets Table
AssetID Description
---------------------------
1 Desktop HP79801
Attributes Column
AssetAttr Column1
-----------------------------------
1 Color
2 Size
Link table
AssetID UnitID AttrId AttrValue
-----------------------------------
1 1 1 Blue
1 1 2 Big
1 2 1 Orange
1 2 2 Giant
If your attributes are all model specific though, you may also want to add a AssetId column to your assets custom column, but I wouldn't personally do that unless none of your assets at all shared attributes.
You may also want to tag this further. You may want to annotate your attributes table with a data type, and maybe have another table related to the attributes table holding the possible data values, then, in your link table, insert the ID of the entry from the aforementioned table.
Another solution would be to use a schema less db altogether. My favorite is Mongo. Another option would be CouchDB or one of the many key-value databases out there.
Not to start a noSQL war here but these databases handle situations like that a lot more gracefully - a single document could store all that data and be retrieved without any joins or views.
So, I'm dealing with an enormous online form right now. It's separated into different sections visually, but those tend to change. With around 300 fields, it almost seems ridiculous to put them into a single table, though if I separate them and someone decides to move a field to a different section on the front end on several different occasions, it will become a mess in the database and fields won't match their front end sections.
I'm essentially asking: What is the best way to organize something like this in a normalized fashion?
You could move the field names to another table and reference them in the value table.
Example
field_id | field_name
------------------------
1 | first_name
2 | last_name
Then reference from the values:
value_id | field_id | value
--------------------------------
1 | 1 | John
2 | 2 | Doe
3 | 1 | Max
4 | 2 | Jefferson
If you're going to use a SQL database, then the Entity-Attribute-Value model (EAV) described above is probably a good answer. You might also want to mix in a couple of denormalized tables with common or specialized data.
Another option might be a document store though; this sounds like just the kind of problem that inspired data stores like MongoDB. In MongoDB you just store everything as a giant json document. If some data isn't needed for some records and is left out, it isn't considered "bad" in the way sparsely populated wide SQL database tables are.
You can group your fields. Separate them as components and you will probably notice, that you can make multiple tables out off that one. Also by separating table you cant make form with, for example:
fieldset tags
separate it in multiple steps (it think the best solution)
multiple ajax requests for each form after previous is filled
form separated by open/close javascript windows
Database design, object design, and form design are three very different elements. If there are relationships between the data in a one to may fashion, you should have different tables to normalize the data. If however, everything is a One-to-one relationship then having all 300 in the same table is perfectly acceptable. I find it difficult to believe that there is a logical or even physical construct that has 300 elements unto itself; but it's possible. If you start getting into attribute data of something lets say we're talking about a vehicle. We could be talking about a car, a truck, a semi, a motorcycle, a bicycle, etc... each of those types of vehicles have different properties which would be managed in separate tables to normalize the data. moving elements of them to different pages wouldn't make a whole lot of sense; but moving common attributes might. For example I wouldn't ask about color on section 1 and again in section 4. But I might section things out to describe make, model, and then custom attributes.