How to design a "dynamic" relational database - mysql

We're building a new piece of software for our company, where we want to manage our inventory.
The goal for the tool is to be customizable by the customer.
My part is mostly on the DB side. We have chosen MariaDB as our DB engine, and while we are working with the rather static functionality of a relational DB, we want to realize a rather dynamic solution.
Our chief programmer has explained to me the basics of the concept I shall implement into our DB:
We want a table which basically just consists of other tables.
Lets call it "maintable".
Maintable shall then reference its "attributes", which are the other tables.
For example, maintable references "Workstations".
"Workstations" then contains attributes like CPU, RAM, Drives, PSU etc..
And now comes the part which I didn't completely understand. The actual VALUES to these attributes in "Workstations" shall not be inserted into "Workstations". Instead, they are packed into another (junction?) table.
The reason for this approach is that the customer shall be able to customize the DB to his needs.
When the customer wants to add another attribute, he shall be able to do so. For example, if a new PSU now requires another attribute for an additional serial number, then the customer shall be able to simply create this new attribute in the front-end input form and then persist it to the DB.
If someone could point to good tutorials explaining this type of DB concept, then I would be glad as well! :=)

Related

Right design to structurally ensure data consistency

In my current design, I have app_group, student and group_article:
To structurally ensure that a group_article is only associated with a student from that same group, the foreign keys "publisher" and "app_group" are taken from the join entity group_member (1) as opposed to having them issued from student and app_group individually. This way, someone with the right to insert new records into the database cannot introduce incoherent data such as adding an article that have been written by a student that isn't even in that group which would be poor design. Now, I want generalize this approach into multiple students or multiple groups. I now have group_message, group_message_in and group_message_out which is an inheritance chain (group_message is the base which is an abstract entity in Symfony, and both group_message_in and group_message_out extend it):
Initially, I was planning to embed the group foreign key on the base class (group_message) and have the sender/recipient (respectively on group_message_out and group_message_in) be taken from student directly:
However, this will leave the database vulnerable to incoherence as per the first example, eg: student from group A can be associated with a message that targets student from group B which is not desirable (only students from the same group can exchange group_message).
I'm well aware that I can amend this risk in code but I want a similar solution to (1) and to know if this is achievable with Doctrine since MySQL itself might have ways of solving a similar problem that aren't supported by Doctrine.
A relational solution to your problem would look something like this:
The integrity that you seek would be achieved by the PK-FK relationships and by assigning a student to a group using the groupName colums.
Your question then becomes something like "How can I use Doctrine to do the same thing?"
To the best of my knowledge Doctrine uses a set of PHP libraries to create what its proponents call a "persistence layer" that stores what it calls "Entities". With Doctrine, the term "Entity" is a synonym for "Class" in the OO paradigm.
In other words Doctrine stores classes in the data layer.
And now we can see the problem.
A relational schema is a structure of relations which is a completely different kind of artefact than a collection of classes.
The OO/Relational divide has been called an "impedance mismatch". Unfortunately this term obscures more than it reveals.
To quote from the Wikipedia article: "There have been some attempts at building object-oriented database management systems (OODBMS) that would avoid the impedance mismatch problem. They have been less successful in practice than relational databases however, partly due to the limitations of OO principles as a basis for a data model."
I suggest that you also review Ted Neward's article "The Vietnam of Computer Science."
This new answer shows the object-role model, the relational schema that it generates and the logic that is implied by the new constraint (shown by the red arrow)
The object- role model.
This is the logic that is asserted by the fact type Student(.id) is a member of Group(.name)
Now as the domain expert, you can read this verbalization and tell me whether it is True or False in your domain.
Please note that all I did as the modeler, was to change the constraint (shown by the red arrow) and the ORM tool called NORMA generated the new verbalization that you see here.
When the domain expert agrees that the model conforms to the requirements then it takes a few seconds to generate the SQL DDL that can then be used to create a new database schema in an RDBMS.

Need help in designing a database schema for a SaaS application

I am a developer and have never worked on DB before (designing a DB). I am designing a database for an employee management system which is a Node.js + Express application using MySQL as its DB.
I already have the required tables, columns sorted out but there are still few unknowns I am dealing with. This is my plan so far and I need your input on it.
The end users using this application will be small - mid size companies. The companies won't be sharing the tables in the database. So if there is a table named EmployeeCases I plan to create a new EmployeeCases table for each existing company or a new one who signs up for this application. I am planning to name the table as EmployeeCases_989809890 , where "989809890" will be the company id (or customer id). So if we have 3-4 companies who signed up for us, then all the tables (at least the ones which a company uses) will be recreated and named as TableName_CompanyId. My questions, is this a good way to go? Is there a better way?
All the employee's data is held by the Employee table, including their login and password. Now each Employee table in DB will be named as Employee_CompanyId (as per my plan above). My question is, when an employee logs in, how will I know which Employee table to query to? Or should I remove the login from the Employee table and create a universal Users table where all the employees will be stored? The Users table will also have the CompanyId as one of its column and I will read the CompanyId from there which will be used to query other tables.
Any reference, website or blogs on this type of design will be appreciated.
Thanks.
I don't recommend this approach, I think you should either:
A) Put all the information in the same tables and have a companyId column to sort them out
OR
B) Have separate databases for each company and use the appropriate database using the code.
The thing is, with your approach, you'll have a hard time maintaining your application if you have multiple copies of the same table with different names. If you decide to add a column to one of the tables, for instance, you will have to write as many SQL scripts as you have table instances. You'll also have a bad time with all of your unique identifiers.
Here are some advantages/disadvantages of each design:
A) Put all the information in the same tables and have a compagnyId column to sort them out
Advantages:
Simplest
Allow usage of foreign key / constraints
Great for cross / client data extraction
Disadvantages:
Not portable (a client can't just leave with his/her data)
Can be perceived as less secure (I guess you can make the case both ways)
More likely to have huge tables
Does not scale very well
B) Have separate databases for each company and use the appropriate database using the code.
Advantages:
Portable
Can be perceived as more secure
Disadvantages:
Needs more discipline to keep track of all the databases
Needs a good segregation of what's part of your HUB (Your application that tracks which client access which database) and that's part of your client's database.
You need a login page by company (or have your clients specify the company in a field)
An example of an application that uses this "two-step login" is Slack, when you sign-in you first enter your team domain THEN your user credentials.
I think Google Apps for Work as the same approach. Also, I think most CRM I worked with has a separate database for their clients.
Lastly, I'd like to direct you to this other question on stackoverflow that links to an interesting example.
You shouldn't split your tables just because companies won't share their information. Instead, you should have a companyId column in each table and access to the relevant data for each query. This should be implemented in your backend

MySQL Workbench - Normalizing a List of Data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
About the Program
Excuse the vague title, but I'm rather new to mySQL and databases. I'm a developer in VB.NET and need to create a program that connects to a database. The program is a guide for a game, and part of the program gives you directions to your next objective when you select a mission. For example, from the application (made in WinForms), you select a "mission" from a combo-box control, and a Textbox will populate according to the objectives in the database. A further example could be displayed as this:
You Select "The First Misson" from the Combo-Box control.
The Textbox Control now contains a numerical list of objectives in order, like so:
Locate John.
John asks you to find a weapon
Give the weapon to John
Help John fight off the monster
John asks you to heal him with a potion
Help John reach the portal at the end of the tunnel
Speak to John about your rewards
Now, the question is how may I display a list like the one above using a database? I was reading up on normalization; however, I still do not see how this may be done.
This is an example of the application (just a mock up, but very similar):
Now say the user selected a misson from the Combo-box located at the top of the application. The application would populate like so:
Ideas via Normalization
I understand I could create two seperate tables containing data relating to the misson. For example, I might be able to create a table called "Stages", and a table called "Missons", but I see this as inefficient, not to mention populating the database itself would take ages. I quickly found that you could not (obviously) store a list in a single cell. So being just a programmer, and not someone who works with databases, I this is very frustrating.
(If it may help, I can also alter the application in which is retrieving data from the database to allow a list to be displayed a different way.)
The Overall Issue
There may be "work-arounds" to address this problem, but I don't know what to do. I do not understand how you could display such data using a database. I understand how you connect to the database and retrieve data using queries, just not with a list. Also, an elaborate answer would be much appreciated for a newbie at this like me.
From what I read, you seem to be primarily stuck up on database relationships. No worries! When you want a "list" in a database, this means you are looking for a one-to-many relationship, or a many-to-many relationship. Here is a brief tutorial that explains the differences between these types of relationships.
One of the first steps in database design is to define your entities (i.e. your tables.) In general, your entities will typically be major nouns from your business problem. In this case, you clearly have the following entities (tables): Mission and Objective. The only other data present on your form is a textbox for mission location and a textbox for mission requirements. Both of these fields can be single properties of the Mission table.
The next step is to define your entity relationships. You might define your relationships as follows:
A Mission has one-to-many Objectives
An Objective belongs to one Mission
Finally, lets list out our known properties:
A Mission has a name (text)
A Mission has one location (text)
A Mission has one requirement (text)
Now we can create a basic database schema. So. What would this look like?
Each table has it's own primary key, denoted by the key image in the picture above. The primary key is a unique identifier for a row of data and is usually represented by an integer. The Objective table also has a foreign key, the missionID field. This is how the one-to-many relationship is defined between these tables.
This way, if we create a Mission with missionID 1, we can assign this missionID to many Objectives. Just like a list, but in the syntax of a relational database. See the images below:
In order to retrieve information about both entities at once, you might use a JOIN query. Hope this helps!
You probably should read up on entities and relationships.
Entities are database parlance for things like a game, a mission, a requirement, and a player (a person).
Relationships are database parlance for statements like
each game has one or more missions in it
each mission has one or more requirements in it, displayed in order
zero or more players can be playing each game.
Each table in your rdms is a collection of instances of a particular entity. So, you'll have a requirement table, for example, that will record the following information for each requirement, each in its own row of the table, for example
game_id the game for which this is a requirement
mission_id the mission for which this is a requirement
requirement_ordinal 1,2,3,4 etc for which requirement in sequence this one is.
description the text describing the requirement: "Speak to John about normalizing your rewards."
You may declare that a particular combination of the first three items uniquely identifies each requirement, which is to say that (game_id,mission_id,requirement_ordinal) is the primary key for the requirement table.
Desktop apps like MySQL Developer and MS Access have graphic design tools that let you draw this stuff in a charts. Here's a writeup. http://docs.oracle.com/cd/A87860_01/doc/java.817/a81358/05_dev1.htm This business of entity-relationship modeling can get very elaborate and abstruse, so be careful not to dive in too deep.
Working this all out is what database design is about. I can't design this whole thing for you, but I can offer you some jargon and a conceptual framework to help you figure out how to search for it.

Adding tables via DAO to a database

As a general question which would really help me "connect the dots" with my studies.
I am currently doing exercises working with DAO and Learning how to add tables automatically. Although i have been working with databases for many years, i question, what type of scenerarios would it be vantagious to use this function. When is it necessary to add tables to a database in an automatic way? Up until now, in all my experiences the tables i need have Always been defined from the beginning and I cant think of a situation where I could of benefited from using this function. For example, i use frequently delete queries to help me clear tables and re-populate them, but when would it be necessary to actually "create" a new table"?
Yes, I have seen a scenario where new tables were created 'on the fly' (either via SQL create, or just DAO). With a shared database on a server, the application called for importing Excel data that a particular user was responsible for, so a table was created on the fly. Multiple users, changes in staff, need to keep data independent, etc. we could create their own table (name based on userid) that they had interfaces to do whatever they wanted with their own data. Not a typical scenario, but worked well for this application.

Mysql database design

currently Im working on a project that, at first glance, will require many tables in a database. Most of the tables are fairly straightforward however I do have an issue. One of the tables will be a list of members for the website, things like username, password, contact info, bio, education, etc will be included. This is a simple design, however, there is also a need for each member to have their availability entered and store in the database as well. Availability is defined as a date and time range. Like available on 4/5/2011 from 1pm to 6pm EST, or NOT available every friday after 8pm EST. For a single user, this could be a table on its own, but for many users, Im not sure how to go about organizing the data in a manageable fashion. First thought would be to have code to create a table for each user, but that could mean alot of tables in the database in addition to the few I have for other site functions. Logically i could use the username appended to Avail_ or something for the table name ie: Avail_UserBob and then query that as needed. But im curious if anyone can think of a better option than having the potential of hundreds of tables in a single database.
edit
So general agreement would be to have a table for members, unique key being ID for instance. Then have a second table for availability (date, start time, end time, boolean for available or not, and id of member this applies to). Django might sound nice and work well, but i dont have the time to spend learning another framework while working on this project. The 2 table method seems plausable but Im worried about the extra coding required for features that will utilize the availability times to A) build a calender like page to add, edit, or remove entered values, and B) match availabilities with entries from another table that lists games. While I might have more coding, I can live with that as long as the database is sound, functional, and not so messy. Thanks for the input guys.
Not to sound like a troll, but you should take a look into using a web framework to build most of this for you. I'd suggest taking a look at Django. With it you can define the type of fields you wish to store (and how they relate) and Django builds all the SQL statements to make it so. You get a nice admin interface for free so staff can login and add/edit/etc.
You also don't have to worry about building the login/auth/change password, etc. forms. all that session stuff is taken care of by Django. You get to focus on what makes your project/app unique.
And it allow you to build your project really, really fast.
djangoproject.org
I don't have any other framework suggestions that meet your needs. I do... but I think Django will fit the bill.
Create a table to store users. Use its primary key as foreign key in other tables.
The databases are written to hold many many rows in a table. There are not optimized for table creation. So it is not a good idea to create a new table for each user. Instead give each user an unique identifier and put the availability in a separate table. Provide an additional flag to make an entry valid or invalid.
Create a table of users; then create a table of availabilities per user. Don't try to cram availabilities into the user table: that will guarantee giant grief for you later on; and you'll find you have to create an availabilities table then.
Google database normalization to get an idea why.
Take it as truth from one who has suffered such self-inflicted grief :-)