Is it possible to build a multigraph (interconnected multilayer network) with Memgraph?? Or will it be possible in near future?
If you are speaking of a multigraph as a graph that is permitted to have multiple relationships, that is relationships that have the same end nodes, that is possible in Memgraph.
You can create the same nodes and relationships as many times as you want, and that's why you have to be careful when to create and when to merge a node/relationship you are importing to your database.
There are two ways of having multiple relationships between the same start and end nodes. First, you can create relationships of different type and second, you can create relationships of the same type.
For example, let's say you have two nodes labeled as Person (Anna and James) and two types of relationships between them (LOVES, IS_MARRIED_TO). Then you can create that with:
CREATE (:Person {name: "Anna"})-[:LOVES]->(:Person {name: "James"}); MATCH (n:Person {name: "Anna"}), (m:Person {name: "James"}) CREATE (n)-[:IS_MARRIED_TO]->(m);
Now let's say you want to create another relationship from Anna to James of type LOVES. You can do that with:
MATCH (n:Person {name: "Anna"}), (m:Person {name: "James"}) CREATE (n)-[:LOVES]->(m);
Since I used CREATE and not MERGE, another relationship of type LOVES will be created. To verify that:
MATCH ()-[r:LOVES]->() RETURN count(r);
and you get 2, since there are 2 relationships of type LOVES.
Related
What is the correct database structure design for user table if I have instructor vs student?
Is it correct to create each one in separate table and the id is the user table
Like this?:
Or creating new flag field (1 or 2) to define the students from the instructors like this?:
I know both will work, but I'm asking to get the most professional answer for this problem.
For me i'm working on Laravel and creating a relations there is very easy.
The first option - separate tables for each subtype of user - is recommended when you have subtype-specific attributes, relationships or constraints; and a known fixed set of subtypes.
The second option - a type indicator column - is recommended when you don't have any subtype-specific attributes, relationships or constraints; and works better for user-managed subtypes.
It doesn't matter whether you have overlapping or disjoint subtypes. Either can be recorded in separate tables; or overlapping subtypes can be indicated via multiple boolean fields and disjoint via a single field.
I have a table say Table1 which has following columns
1. Id
2. Name
3. TransportModeId
4. ParkingId
5. ActivityId
Column 3,4,5 are the foreign keys and all three are simple list tables which has following columns
1. Id
2. Item
For simplicity I have shown 3 tables otherwise my actual schema contains almost 25 List table.
What should be the best Practice
Option 1.
Keep all list table separate which will create 25 tables but on the other hand i will have a clean modular schema
Option 2.
Make a table with self join and add all the items in that table in which ParentId null will represent the name of the table and it can have more than one references in other tables as described above and it has to be kept in some kind of common module
thanks
Option 1 is the way how it is normally done when designing a system that is not supposed to be very configurable by end user/implementator. It has several important advantages, two of them:
when you need to add an extra attribute to any of the enumerations (e.g. parking location to the Parking enumeration), it is quite simple and does not produce extra problems.
It is optimized for speed using relation database engine's native algorithms for linking records.
As for Option 2:
It is something called Generalization. You take more types with similar attributes (methods) and create a class/table with a structure that fits different purposes.
The self reference, as you speak about it, is not a good idea for Option 2, rather make a reference to another EnumerationType table containing type names like Parking, Activity etc. with id.
Using this approach could make sense in case you need to enable end user to configure the attributes himself within your app. But otherwise it could cause you problems when you find out, that different enumeration tables need to have different structures.
I have 3 entities in my system. Let's call them entity A,B and C. Entity A becomes Entity B, and in turn entity B becomes Entity C. I have to keep copies of all 3 entities.
My dilemma is how to represent them in a relational data. All 3 entities have similar fields. However, some fields are applicable to some entities and some are not.
What is the recommended approach in this case. Should I use one table and then make a 'Type' field ? The problem with this approach is that some fields will be empty for soem entities. Or should I create separate tables?
Any recommendation on best practice would be greatly appreaciated
this sounds like the manager -> personnel type of hierarchical design. You may have separate tables for each entities. But if they are of same base type when you want to get list of all people, then you have to union all tables. If you use a hierarchy column with a single table approach then you have to join the table with itself. Consider these two options while choosing one solution. Both are right but have their own difficulties.
I've been thinking about creating a database that, instead of having a table per object I want to represent, would have a series of generic tables that would allow me to represent anything I want and even modifying (that's actually my main interest) the data associated with any kind of object I represent.
As an example, let's say I'm creating a web application that would let people make appointments with hairdressers. What I would usually do is having the following tables in my database :
clients
hairdressers: FK: id of the company the hairdresser works for
companies
appointments: FK: id of the client and the hairdresser for that appointment
But what happens if we deal with scientific hairdressers that want to associate more data to an appointment (e.g. quantity of shampoo used, grams of hair cut, number of scissor's strokes,...) ?
I was thinking instead of that, I could use the following tables:
entity: represents anything I want. PK(entity_id)
group: is an entity (when I create a group, I first create an entity which
id is then referred to by the FK of the group). PK(group_id), FK(entity_id)
entity_group: each group can contain multiple entity (thus also other groups): PK(entity_id, group_id).
role: e.g. Administrator, Client, HairDresser, Company. PK(role_id)
entity_role: each entity can have multiple roles: PK(entity_id, role_id)
metadata: contains the name and type of the metadata aswell as the associated role and a flag that describes if its mandatory or not. PK(metadata_id), FK(metadata_type_id, role_id)
metadata_type: contains information about available metadata types. PK(metadata_type_id)
metadata_value: PK(metadata_value_id), FK(metadata_id)
metadata_: different tables for the different types e.g. char, text, integer, double, datetime, date. PK(metadata__id), FK(metadata_value_id) which contain the actual value of a metadata associated with an entity.
entity_metadata: contains data associated with an entity e.g. name of a client, address of a company,... PK(entity_id, metadata_value_id). Using the type of the metadata, its possible to select the actual value of a metadata for this entity in the corresponding table.
This would allow me to have a completely flexible data structure but has a few drawbacks:
Selecting the metadatas associated with an entity returns multiple rows that I have to process in my code to create the representation of the entity in my code.
Selecting metadatas of multiple entities requires to loop over the same process as above.
Selecting metadatas will also require me to do a select for each one of the metadata_* table that I have.
On the other hand, it has some advantages. For example, instead of having a client table with a lot of fields that will almost never be filled, I just use the exact number of rows that I need.
Is this a good idea at all?
I hope that I've expressed clearly what I'm trying to achieve. I guess that I'm not the first one who wonders how to achieve that but I was not able to find the right keywords to find an answer to that question :/
Thanks!
If I have a class called animal, dog and fish is the subclass.
The animal have attribute called "color".
Dog have the attribute called "tail length", and the fish don't have this attribute.
Fish have the attribute called "weight", the dog don't have this attribute.
So, I want to design a database to store this information. What should I do? Here is some ideas:
Idea 1:
Making an animal table, and the table have type, to find what kind of animal, if it is a dog, just get the result from dog table.
Animal:
color:String
type:int
Type:
Dog:0
Fish:1
Dog:
TailLength:int
Fish:
Weight:int
Idea 2:
Store only Dog table and Fish table in the database, remove the animal table.
Dog:
Color: String
TailLength: int
Fish:
Color: String
Weight: int
The two approaches you mentioned:
One table representing objects in the entire inheritance hierarchy, with all the columns you'd need for the entire hierarchy plus a "type" column to tell you which subclass a particular object is.
One table for each concrete class in your inheritance hierarchy, with duplicated schema.
can be supplemented by two others:
One table for each class in your inheritance hierarchy – you now have an Animal table, and subclasses have tables with foreign keys that point to the common set of data in Animal.
Generic schema – have a table to store objects, and an attribute table to support any set of attributes attached to that object.
Each approach has pros and cons. There's a good rundown of them here:
http://www.agiledata.org/essays/mappingObjects.html#ComparingTheStrategies
Also take a look at these SO topics:
Something like inheritance in database design
Help me to connect inheritance and relational concepts
Object-oriented-like structures in relational databases
How to do Inheritance Modeling in Relational Databases?
How do you effectively model inheritance in a database?
Finally, it should be noted that there are object-oriented databases (aka object databases, or OODBMSes) out there that represent objects more naturally in the database, and could easily solve this problem, though I don't think they're as frequently used in the industry. Here are some links that describe such DBs as compared to relational (and other) DBs, though they won't give you an entirely objective (heh) view on the matter:
What is the difference between graph-based databases and object-oriented databases?
"In defense of the RDBMS", Gavin King, 2007
"The relational database needs no defense" (in support of OODBMSes), Ted Neward, 2007
Object Oriented Database - why most of the companies do not use them
You could try it like this:
Animal
PK animal_id
FK animal_type
STRING animal_name (eg. 'Lassie')
AnimalTypes
PK animal_type
STRING animal_type_name (eg. 'Dog')
AnimalAttributes
PK attribute_id
STRING attribute_name (eg. 'tail length')
AnimalToAttributes
PK id
FK animal_id
FK attribute_id
INTEGER value (eg. 20)
This way you can have one or many attributes per animal (it's up to you to choose).
Use a one to zero or one relationship As you note, In database schema design language the tables are called class - sub-class or superclass
Create Table Animal
(animalId Integer Primary Key Not null,
Other columns generic to all animals)
Create Table Birds
(BirdId Integer Primary Key Not Null
references Animal(AnimalId),
-- other columns)