I want to design an application, where we can add many category and each category can have a parameters. And I want to create a new product to the category and her parameters. Relations between
parameters and category is many-to-many (categories_table, parameters_table)
Category "Test" parameters:
id | key | type | def_value |
==============================
1 | color | text | red
==============================
2 | serial| text | 0
etc.
And now i want to create a new product with those parameters, so i have a question. Should i simply create many-to-many or maybe create new table with records like parameters ? Ex,
test1_table -> color, srial, etc.,
Or
products -> parameter_id,value
cause the relationship is many to many you have to create another table with the columns that are the foreign keys in the relation ---
tip: because you will have a table called category and each category can have parameters you should create another table with a foreign key that have the parameters
example
table_category - record 1 :color
table category_parameters - record
1 : color,azul -- record 2 : color,verde etc
and the foreign key is the primary key of category in this case ('COLOR')
Related
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I have 2 table on my MySQL database. And My Tables like above.
Note: Its an offline and old script.
My Products Table ;
| ID | ProductName | ProductCategory |
---------------------------------------------
| 1 | Example Name | 1,2,3,4,5,6,7 |
---------------------------------------------
| 2 | Example Name | 1,2,10,11,12 |
---------------------------------------------
And My Query is like below for list my products by category.
mysql_query("SELECT * FROM products where FIND_IN_SET('".$catid."', ProductCategory)");
I can sort them for desc or asc but i want to sort them manually like i desired.
i need an idea.
Your database design violates first normal form. You SHOULDNOT insert multiple values into a column by separating with commas.
Your database schema should look like below
create table ProductTable(ProductID integer primary key, ProductName varchar(30));
create table CategoryTable(CategoryID integer primary key, CategoryName varchar(30));
create table ProductCategoryRelationTable(ProductID integer, CategoryID integer);
Why we created ProductCategoryRelationTable is because one Product can have multiple Categories and a category can belong to multiple products (multi multi relationship). Also ProductCategoryRelationTable should have composite primary key on (ProductID,CategoryID) and foreign key constraints with ProductTable and CategoryTable.
Once you have created above tables, try to express your queries around these tables.
I'm doing a project in laravel 4, and I have come to a stop.
tasks table:
task_id (PK)
task_title
measurements table:
measure_id (PK)
measure_title
routines table:
routine_id (PK)
date
time
value
emp_id (FK)
emps table:
emp_id (PK)
first_name
last_name
user_name
email
user_type
Now what I am confused about is how I would model these relationships in eloquent, because I can't seem to figure it out. In the phpmyadmin DB, I currently have two tables connecting the tasks and measurements to routines which have task_routine : task_id (PK), routine_id (PK) and measure_routine : measure_id (PK), routine_id(PK).
Hope that was as clear as it sounded in my head when I wrote it, and thanks for your help!
At first in your all tables change the PK field from *_id to just id. If you want to make a relation for a table with another table then the convention is that, you should keep a foreign key in the child table using the parent table's name_id (name should be in singular form), for example, to build a relation between emps and routines table you should use emp_id foreign key in the routines table and your emps table should contain id PK. So, your emps table is parent table and the routines is the child of emps and in both tables records should be like this (custom convention could be used):
Table emps (parent):
id (pk) | first_name | more fields ...
1 | Mr | ...
2 | Miss | ...
3 | Mrs | ...
Table routines (child):
id (pk) | emp_id (FK) | more fields ...
1 | 1 | ... - Child of emps table's first record
2 | 3 | ... - Child of emps table's third record
3 | 3 | ... - Child of emps table's third record
Here, each id of emps table used as foreign key in the routines table in emp_id field. So, emps table has three related records in routines table and the first record in emps table relates to first record in the routines table and the third record in the emps table (with id 3) has two related records in routines table (second and third) because both records contains id 3 of emps table's third record in emp_id field.
Now building relation using Laravel:
If your parent table has only one related table in child table then use one-to-one, for example, if the record with id 1 (first record) in emps table has only one related record in routines table using emp_id field with value of 1 and if it can't be in more than one record in the routines table then it's one-to-one relation and in the example records given above is not one-to-one relationship because in the routines table there are two records with same id/pk of emps table available so it goes to one-to-many relationship, so it could be read as emps on record has many routines. To build the one-to-many relation we may use:
class Emp extends Eloquent {
public function routines()
{
return $this->hasMany('Routine');
}
}
The Routine model:
class Routine extends Eloquent {
public function emp()
{
return $this->belongsTo('Emp');
}
}
In one-to-many relationship a parent table may contain multiple child records with same id but if a record in child table has many parents as well then it should be a many-to-many relationship. For example, if we want to build a relation in routines table something like this (Not possible to use same primary key twice):
id (pk) | emp_id (FK) | more fields ...
1 | 1 | ...
2 | 2 | ... - Record 2(id) has parent 2(emp_id)
2 | 3 | ... - Record 2(id) has also parent 3(emp_id)
In this case this is a many-to-many relationship and it's not possible to use same id/PK twice in one table so we need to build the many-to-many relationship between emps table and routines table using a third table (known as pivot table) to maintain the many-to-many relationship. So, it'll look something like following table:
Table Name should be emp_routine but there is other way to use a
different name but follow this convention.
emp_routine Pivot Table:
id (pk) | emp_id (id/PK of emps) | routine_id (id/PK of routines) | more fields ...
1 | 1 | 1 | ...
2 | 2 | 1 | ...
3 | 1 | 2 | ...
Here, both parent and child has more than one related records in both tables and this pivot table maintains the relationship.
To build the relationship using Laravel we may use:
class Emp extends Eloquent {
public function routines()
{
return $this->belongsToMany('Routine');
}
}
The Routine model:
class Routine extends Eloquent {
public function emp()
{
return $this->belongsToMany('Emp');
}
}
In this case, we don't need a foreign key field (emp_id) in the routines table because we are using a pivot table to maintain the relationship but if it's there, it won't be problem.
It should be notice that whenever you insert/update any record in the parent table you also have to insert/update relational data in the pivot table as well and Laravel provides helpful methods for these, so check the manual (relationship).
Suppose I have a movie listing website and I want post a movie into different categories (eg: Drama , Action) And these categories should come from another table so that i can show a particular movie in two or more categories . how is it possible ?
What you are looking for, I believe is a many-to-many table relationship. You would have a table containing all of your movies (their names, duration, etc). And another table containing a list of all possible categories (action,drama,comedy,etc).
The trick will be to have an additional third table containing the movie to category relationship. You reference the movie's id and the category id, like this -
id | movie_id | category_id
---|----------|-------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 2 | 1
In this example, movie id 1 is in category 1 and 2. Movie id 2 is in category 1 and 3. So you see, a movie can be in more than one category.
You build a table that holds movie ids and category ids:
movie_category(movie_id*,category_id*)
'*' = (component of) PRIMARY KEY
I have several entries (that will grow into thousands) in a database that I want to be able to index via keywords.
So say I have a table such as:
id | username | email | description
And I want to attach multiple keywords to that user such as:
Tall | Blonde | Male | Skinny
How would I structure the table for that? Much like the keywords used in stackoverflow at the bottom of the new question?
Thanks
you need three tables
users:
id | username | email | description
tags: (will contain Tall, Blonde, Male, Skinny etc... in the name field)
id | name
users_tags:
user_id | tag_id
to make a user have a tag, you add a row to the users_tags table with the ids of the associated items.
both user_id and tag_id should be foreign keys, referring to the appropriate table, and combined, they should form a unique key (possibly primary key).
If I understood correctly, have 3 tables:
- users (user_id, username)
- keywords (keyword_id, keyword)
- user_keywords (user_id, keyword_id)
and have relations between them (on same column names as of in an example).
This is a many-to-many relationship.
You have a table of keywords and a table of objects (which can be associated with keywords).
You then have a third table which has two columns which form a composite key, each of which is a foreign key on on of the other two tables.
e.g.
object_keyword_relationship
===========================
object_id | keyword_id
1 | 1
1 | 2
I'm new to database design so please bear with me. I'm using PHP and MySQL.
I have a 'movies' table that contains some details about a movie. This includes genres, which have an (if I understand correctly) many to many relationship with movies, implying a single movie can belong to different genres and a single genre can belong to different movies.
From what I gather about database design, storing this kind of relationship in one table is not a good idea as it will either violate First Normal form or Second Normal form rules.
How would I design my tables to avoid this; would I have to create a table for each genre separately or... ?
This leads my to my next question: separate tables need to have foreign keys to identify which information belongs to what row. In theory, if I had a unique key identifying each movie which I would then like to use to identify a director in a separate table, how would I create this relationship in MySQL?
Thank you for your time - if I've made anything unclear please let me know and I will try my best to clarify.
This includes genres, which have an (if I understand correctly) many
to many relationship with movies, implying a single movie can belong
to different genres and a single genre can belong to different movies.
That's right.
From what I gather about database design, storing this kind of
relationship in one table is not a good idea
Right. You're looking for something loosely along these lines.
create table movies (
movie_id integer primary key
-- other columns
);
create table genres (
genre varchar(15) primary key
-- other columns?
);
create table movie_genres (
movie_id integer not null,
genre varchar(15) not null,
primary key (movie_id, genre),
foreign key (movie_id) references movies (movie_id),
foreign key (genre) references genres (genre)
);
For directors, assuming there is only one director per movie, you can use this instead of the movies table above
create table movies (
movie_id integer primary key,
director_id integer not null,
foreign key (director_id) references directors (director_id) -- not shown.
-- other columns
);
You need one table Movies, one table Genres and one table Movies_and_Genres. The first two contain unique primary keys which you can create using the mysql autoincrement field type. The third table contains pairs of those primary keys.
create table movies (id integer not null primary key auto_increment, title ... );
create table genres (id integer not null primary key auto_increment, genre ... );
create table movies_and_genres (id_movie integer not null, id_genre integer not null);
As for the directors, this is a question of data modeling. If a movie can have more than one director, then you need a directors and a movies_and_directors table. Otherwise you need only the directors table and a director column in the movies table.
you need/should use junction tables
table movie:
| id | title | rating |
-----------------------
| 1 | foo | 5 |
| 2 | bar | 4 |
table genre:
| id | name |
----------------
| 1 | comedy |
| 2 | romance |
table director:
| id | name |
----------------
| 1 | hello |
| 2 | world |
table movie_genre:
| id | movie_id | genre_id |
----------------------------
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
table movie_director:
| id | movie_id | director_id |
-------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
You're quite right, what you need here is to have a table of movies (e.g. tbl_movies);
pk id
name
... etc
a table for genres (eg tbl_genres);
pk id
name
... etc
and a table linking the two (eg tbl_movie_genres);
fk id_movies
fk id_genres
You can either set the pk of the tbl_movie_geners to be the two foreign keys, or you can set a standalone pk (e.g id like in the tbl_movies and tbl_genres tables above).
this way you can list as many genres per movies and they're linked through the tbl_movie_genres table; eg:
tbl_movies:
id name
1 Movie 1
2 Movie 2
tbl_genres:
id name
1 Horror
2 Action
3 Rom Com
tbl_movie_genres
id_movies id_genres
1 3
2 1
2 2
Would show you that 'Movie 1' is a rom com and 'Movie 2' is an action horror.
To satisfy the many-many relationship use a join table that holds foreign keys to both the movie and genre tables:
MOVIE
-----
ID (PK)
GENRE
-----
ID (PK)
MOVIE_GENRE
-----------
MOVIE_ID (FK that references MOVIE(ID))
GENRE_ID (FK that references GENRE(ID))
Here the MOVIE table has a primary key of ID, the GENRE table has a primary key of ID, and the MOVIE_GENRE table has two foreign key references: one to MOVIE.ID and another to GENRE.ID. The primary key for MOVIE_GENRE could either be the composite key of (MOVIE_ID,GENRE_ID) as that will be unique but you could use a synthetic key as well.
For dealing with the director table and relationship, if it is a one-to-many relationship (one director for many movies), simply add a foreign key to the MOVIE table:
DIRECTOR
--------
ID (PK)
MOVIE
-----
ID (PK)
DIRECTOR_ID (FK TO DIRECTOR(ID))
If the off chance you need to support another many-to-many relationship (many directors for many movies), use the join table approach like above.