How to map two tables having large database? - mysql

I am doing this in mySQL.
Currently I have two tables: 1. universitiestable 2. coursestable
Table 1. has list of all universities in the world.
Table 2. has all the courses list.
Universities Table has following columns:
id University_Name
1 NYU
2 Yale
3 Oxford
Courses table has following Columns:
id Course Univ_ids
1 BS 1:2
2 MS 3:1
3 Phd 1:2:3
now is this the correct approach to store data in this scenario where i need all the courses provided by each university listed in universitiestable

I myself figured out the solution.
I need to create a third table which stores the mappings.
third table will be like this with the following fields table name:university_courses
id course_id university_id
1 1 2
2 3 3

Related

mySql one to many relation table structures

Folks can you please give your suggestions for my question regarding mysql joins.
My Table structures:
place table:
place_id place_name city
1 Hotel Golconda Hyderabad
2 Paradise Hotel Hyderabad
3 Hotel Mayuri Hyderabad
place_tags
tag_id tag_name
1 Valet Parking
2 Air Conditioned
3 Buffet
4 Bar
5 Family Dining
places_info Table:
place_id tag_id
1 1
1 2
1 3
2 1
2 5
3 1
3 4
The above is all my tables which are containing the place names and address in places table, all the facilities of the restaurants in tags table and mapping of the facilities of each place in places_info table.
Is this my table structures are correct to get the places which had "Valet parking and Buffet". How can write a join query for this type of results to get.
Most Importantly we had millions of places in places table and also in the places_info table. How to achieve maximum performance with this type of table structure? Or shall I need to change the table structures?
Please guide me.
This'd be the basic structure for "places with valet AND buffet":
SELECT place_id, COUNT(places_info) AS cnt
FROM place
LEFT JOIN places_info ON place.place_id = places_info.place_ID
AND tag_id IN (1, 3)
^^^^---- two tags: valet(1) + buffet(3)
GROUP BY place.place_id
HAVING cnt = 2
^^^---- must have both tags
For a places which have NEITHER of the tags, or only one, the count would come back 0, or 1, and get dumped by the HAVING clause.

Movie Relation Database Design

I'm creating a movie information database but stuck at designing relational table between 1 movie with the others.
For example: Avengers is a prequel of Avengers 2, but should I store the reverse relation (sequel) in my database? And should I separated the relationship into separated table? Currently I have 3 alternative, but which is really the best practice for designing database?
Alternative 1: Two tables with pair of relationship stored inside the relation mapping
Movies: id, title, ...
Relation_maps: id, movie_id, related_movie_id, relation_text, reverse_relation_text
So, if I have 2 movies: Avengers, and Avengers 2. I'll have one relation mapping with relation_text of "prequel" and reverse relation text of "sequel"
Alternative 2: Split relation into separated table and store the reverse relationship as an id of the table itself
Movies: id, title, ...
Relation_maps: id, movie_id, related_movie_id, relation_id
Relations: id, reverse_relation_id (self-refer to id), relation_text
Relationship text, will be stored in another table with reverse relationship stored as an self-referring id. So in relations table, It'll become like this:
id | reverse_relation_id | relation_text
1 | 2 | prequel
2 | 1 | sequel
Alternative 3: Split relation into separated table and reverse relationship will be shown through conditional code
Movies: id, title, ...
Relation_maps: id, movie_id, related_movie_id, relation_id
Relations: id, relation_text
Same with alternative 2, except reverse relationship will be shown through conditinal statement in view code. If this is better, should I really split the relation text or combine it in relation_maps table?
Are there any better alternatives in terms of performance and best practice? Thanks in advance.
Maybe something like this:
Relations
id name
1 Star Wars
Relations_Detail
relation_id movie_id relation_type sequence
1 40 prequel 0
1 50 prequel 1
1 60 prequel 2
1 10 debut 3
1 20 sequel 4
1 30 sequel 5
You would have a multi-column primary key of (relation_id, movie_id).

Advice on database design for multiple catalogs

I have a relatively simple database design that I will simplify here for the sake of brevity.
I have 4 tables => Products, Product_Sizes, Stores, Catalogs.
There are 6 stores and each store has it's own unique customized catalog, an assortment of different products and sizes that is chosen from the Products and Product_Sizes tables.
I am wondering how to best design the Catalogs table. My ideas are:
id store_id product_id product_size
1 1 53522 1
2 1 40299 1
3 2 43326 1
4 2 43326 2
OR
id store_id product_id product_sizes
1 1 53522 1
2 1 40299 1
3 2 43326 1,2
Each store has it's own unique (and only one) catalog, so a query to fetch all entries by store_id will result in that store's catalog.
Another approach would be to create another table of the combined products and product_size into it's each own unique table, let's call it Products.
id product_id product_size
1 1 1
2 1 2
3 1 3
4 1 4
This separate table gives me a unique id for all the possible products and their size variants. This would result in a single id per product for each Catalogs entry.
I would love to hear some critique and better suggestions as I feel this just isn't right and can't put my finger on how to better design this. Also, if we were to ever implement more than 1 catalog per store, I know this current design would cause me grief. Any feedback would greatly appreciated!!
If it were me, I would create the catalog table as an entity table, with it's own id. If that catalog only belong to one store, I would add a store_id column as a foreign key.
I would then add a catalog_products table. This would have a foreign key to the catalog table, as well as a foreign key to the products and/or product_sizes table. (If a product_size belongs to only one products table, then just the products_sizes table.)
To resolve a many-to-many relationship, we typically add a third relationship table.
a catalog has zero, one or more products (product_sizes)
a product (product_size) appears in zero, one or more catalogs
The relationship table gets us "one-to-many" relationships, which we can represent:
a catalog_product appears in exactly one catalog
a catalog contains zero, one or more catalog_product
a catalog_product is related to exactly one product
a product belongs to zero, one or more catalog_product
id store_id product_id product_size
1 1 53522 1
2 1 40299 1
3 2 43326 1
4 2 43327 2
Different product id for same product name with different sizes.

Many to Many relation between two tables

I would like to create a relational database in which there are two tables Users and products. Each item of each table can be related to many items of the second table.
My current implementation is as follows:
Two main tables-
->Users
User ID
UserInfo
->Products
Product ID
ProductInfo
Two different lookuptables
->UserToProduct
UserID
ProductID
->ProductToUSer
ProductID
UserID
Each time a relation from a user to a product is added, i just add an extra row to the first lookup table, and vice versa.
Is this the right way to do it? Are there any standard models for such scenarios that I can refer to?
You don't need two lookup tables, all you need is users_products. As far as resources, there are zillions, just google "database many to many".
UPDATE
Consider this data:
products
------------
id info
------------
1 car
2 flute
3 football
users
------------
id info
------------
10 bob
20 tim
30 manning
Now, as a simple example, let's say manning owns a football and and a car. Let's say tim owns a flute and a football. Now here's your lookup table:
users_products
----------------------
user_id product_id
----------------------
20 2
20 3
30 3
30 1
That's it. Now you can do queries like "give me all the users that have cars", or "give me all the cars that a user has", etc.
Cheers
You really don't need or want two different lookup tables. You should just have one (either of your tables, UserToProduct or ProductToUser, would be fine). The primary key of the lookup table should be a composite key consisting of both ProductID and UserID.

Advice on linking product codes to a product in a MySQL database

I need some advice of how to setup my tables I currently have a product table and a product codes table.
In the codes table I have an id and a title such as:
1 567902
2 345789
3 345678
there can be many items in this table.
In my product table I have the usual product id,title, etc but also a code id column that I'm currently storing a comma separate list of ids for any codes the product needs to reference.
in that column I could end up with ids like: 2,5,6,9
I'm going to need to be able to search the products table looking for code ids for a specific set this is where I've come into problems trying to use id IN ($var) or FIND_IN_SET is proving problematic I've been advised to restructure it I'm happy to do just wondering what the best method would be.
Sounds like you have two choices. If this is a 1 to many relationship, then you need to have the foreign key in the code table, not the product table.
i.e.
codeId code productId
1 567902 2
2 345789 6
3 345678 9
4 345690 9
The other option is to have another table which contains productId and codeId (both as foreign keys), this is a many-to-many relationship. This is what you should go for if a code can be assigned to multiple products (I assume not). It will look something like this:
codeId productId
1 2
1 10
2 6
3 9
4 9
I think the first option is what you need.