Two multivalued attributes in one entity - database

I am trying to create an entity (order) with a primary key: order ID. Foreign Key: customer ID, and with two multivalued attributes, Product ID and Quantity. Because an order may have multiple products, and each product has specific quantity. The idea is that since they are multivalued then a separate table will be created for them in this way [orderID,ProductID,Quantity]. Is it a correct reasoning?
ER diagram

Related

How to classify this schema?

I have such schema:
The essence of this scheme is in organization of the entry point for all products of some company, that gives some flexibility.
How it works:
We create a list of tables in the table "tables" (where name is the name of the table in database, pk_name is the name of the primary key of this table)
We create a list of products in "products" (where table_id is the table identifier in "tables", pk_value is the value of the primary key)
Also, we create tables like "some_product", "another_product", etc. They contain different fields for a specific product
The questions are:
How such schemes are called? For example, EAV is also designed for
database flexibility, but in EAV columns are stored as records in
the database.
Therefore, I can not understand is it advisable to compare this scheme with EAV or not?
What analogies of this schema are there, to understand what is better to use?
What are disadvantages of this schema?
I'm novice in DB, so I hope that my questions are not stupid.
Thank you!
In the example you have shown both some_product and another_product tables have the same attributes and types. It would be better to have one product table in that case. If different attributes apply to different types of product in different tables then that is an example of subtyping.
Attributes that are common to all products would go in the common products table (the supertype table). I would expect to see a product type attribute in that table to differentiate the various types of product.
The tables table is unnecessary. All DBMSs provide access to the metadata about tables and primary keys so there is no reason to capture that in your own table.

Cannot make relation between composite key of primary key and foreign key

I have two tables:
"Projects", that have three (3) field. One composite key of two (2) fields: Donor_Source & Project_Number and Project title
Please note that Donor_Source field is indexed as Yes(Duplicates OK) and Project_Number field is indexed as Yes(No Duplicates).
It has to be this way because a donor can support multiple projects.
Lastly there is also the PRF_Table, it has many fields but since I want to relate it to the Project table, I made two fields that are used as foreign keys of Projects table:
Please note that both fields of the foreign key are indexed as: NO.
As I was trying to relate the two tables, I managed to relate of project field from both tables but could not relate the donor source field of both tables:
As can be seen from the picture above, I managed to get many:1 relation between PRF_Table & Project, which is correct. PRF_Table can have many records on a specific project, but that project is listed only once in the Project table
The problem rises when trying to relate the Donor_Source field: I always get indeterminate relation (something that I want to avoid). I guess the problem might be because the Donor_Source field in the Project table, although indexed, it still can have duplicates and it of course has duplicates in the PRF_Table.
What should I do in order to get many:1 relation (PRF_Table:Projects)?
All fields in a compound key must be addressed to create referential integrity.
Thus, you must:
Create field Agrmnt_ID in PRF_Table and include that in the relation to the junction table.
Include field Donor_Source in PRF_Table in the relation to table Projects.
You are not required to create a field for Agrmnt_ID in your PRF_Table to have referential integrity. What you are doing so far between PRF_Table and PRF-PO_Junction_Table is fine.
Regarding the link between Projects and PRF_Table, it appears that your intentions are for each record in Projects to be able to relate to multiple records in PRF_Table. If so, then your solution is to change your Primary Key in Projects and consequently your relationship between the two tables.
In table Projects, remove your current composite Primary Key and create a single AutoNumber field (i.e. named ProjectID) as your Primary Key.
Now, in the Projects table, create your unique index on the Donor_Source and Project_Number fields (a composite, unique index), which will give you the same effect as your current composite Primary Key scenario, each Donor can be on multiple Projects, but the same Donor can't be on the same Project more than once.
Now, you will create the same field in PRF_Table that you created as your new Primary Key in Projects from step 1 (i.e. ProjectID)
Create your relationship between your new Primary Key in Projects and your new field in PRF_Table. This will allow every Project/Donor record in Projects to have multiple records in PRF_Table.
Composite Primary Keys are most useful in junction tables, like how you are using one with PRF-PO_Junction_Table. However, in any other link, you want try and have a single Primary Key field and use just a unique composite index to enforce uniqueness in two or more fields.

Normalising image album database

Above is my normalised database structure for my app. I am going to store Users, and their favorited Images. Images might be alone (hosted on reddit) or in albums (hosted on imgur), and they always have a title.
Question is - is the database set up correctly? I have this feeling that i have something wrong with ImageAlbum and Image table relationship.
EDIT: This might work?
The main issue with the original design is that the intended relationship between a user and an image would not be possible, as the two tables are not connected.
As a general rule of thumb, if there's a 1-1 or a 1-many relationship between tables, you can rely on constraints. I.e. 1 customer can place many orders. You have a Customer table with a CustomerID PK column, and an Order table containing an OrderID PK column, and a Foreign Key constraint to the CustomerID column of the Customer table. That establishes the relationship, and ensures that you cannot place an order if you are not a customer.
An order typically consists of one or more products, and a product typically can be purchases in multiple orders. In cases like this, you cannot set up this relationship the same way. A common workaround for that is to do so using an intermediate table that establishes the many-to-many relationship.
So building on the earlier tables, we also have a Product table, with a ProductID column as a PK. To set up the relationship between Order and Product, you would then credit an OrderProduct table, with FKs pointing to the OrderID and ProductID in question (and probably also something indicating quantity of products for this particular order, and perhaps something like a FK to a Discount or campaign table, and whatnot).
So in your scenario, I would establish the relationship between Image and User using a similar approach, and simply adding a UserImage table to allow for the many-to-many relationship. You then also add an AlbumImage table to determine the many-to-many relationship between images and albums.
As indicated in the comments, there's no need to have an AlbumTitle table, really. It would naturally belong to the Album table. The ImageTitle would belong in the UserImage table, because every user can add their own title to an image.

Make a entry mandatory if exist in a table

I have the next design doubt:
I have athlete entity, the athlete can have many nationalities, so I have second table called countries. Then between athlete and countries there is a many-to-many relationship. I create another table athlete_country to resolve the many-to-many relationship.
My question: Is there a way to achieve that athlete_country entry be mandatory for any entry in the athlete table?
I am working on postgresql. Is there a way in another database server?
No, this is not possible to do it this way for logical reason: athlete_country tables references athlete table, and if you do back reference (in fact you can do it) you will not be able to insert any row in either table because each row should reference to the row in another table, which isn't inserted yet.
The solution is to use many-to-one relationship in addition to many-to-many which you have described. For example, you can add "primary_country" field to athlete table which references directly to the country table. In that case you can be sure that any athlete has relationship with at least one country, specified in "primary_country" field and, optionally, with other countries listed in the athlete_country table.
create table country(id serial primary key, name text);
create table athlete(id serial primary key, name text, primary_country int references country(id));
create table athlete_country(athlete_id int references athlete(id), country_id int references country(id), primary key (athlete_id, country_id));

Handling Id for multiple entities in solr/Lucene

I'm using solr dataimporthandler to index multiple tables which are actors, actress, directors and movies. Each of these tables have an id field which starts from 1. Solr's schema has an unique key field. Does this field need to be unique for the entire index or just the entity? For example, if there are both an actor and movie with id 1, will solr be able to recognize them or I'll have to make a global unique key for each entity?
it needs to be unique across the entire index. This is easily achieved if you just create an id by appending some prefix per table to the table id. For example, when select-ing from actors table:
SELECT CONCAT('ACT-', id) as solrid, ...
And index solrid as the doc id.

Resources