0

Im having trouble relating tables from one another.
Here is what I want to create based on my understanding.

I have an organization that can request a service, he can select from different services (Service1, Service2, OtherServices) The Service1-2 are fixed and they have their own specific column names (this means that when an organization request Service1 or 2, they have to fill up a form), while the OtherServices are the services with no forms. So if the adminadds a new service, it will automatically be a service with no forms, therefore, will reflect on the OtherServices table.

All TrackingNo is auto-increment The ServiceID from each Services are 1 is 1-to-1 with Table Service and the RequestID from every services are foreign key to table Request_Service

This is what my table looks like

  • Did you look into Composite keys? Here is a link http://stackoverflow.com/questions/5835978/how-to-properly-create-composite-primary-keys-mysql – Abdullah Khan Nov 20 '16 at 06:43

1 Answers1

0

In this situation I would go for document centric storage of your services instead of forcing them into a relational data structure. The data is not naturally relational, so the whole system will be easier to write and much more efficient if you design it this way.

By document centric what I mean is that when a service is requested, the application uses a document template to present a form to the user, then captures the users input as a single document, not multiple columns of data or multiple tables.

There are many choices for document format, two of the most popular are JSON and XML. If you need help with pros and cons etc I think this should be a separate question.

To store your documents you can just use the file system, or a BigTable. You could even store documents as a single column in a relational database but I would urge you away from this option.

Your database schema would look something like this:

/*!40101 SET NAMES utf8mb4 */;

CREATE DATABASE IF NOT EXISTS `services` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
USE `services`;

CREATE TABLE IF NOT EXISTS `organization` (
  `organization_id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`organization_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `request_service` (
  `request_id` int(11) NOT NULL AUTO_INCREMENT,
  `organization_id` int(11) NOT NULL,
  `service_id` int(11) NOT NULL,
  `service_type` int(11) NOT NULL,
  `document` LONGTEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
  `tracking_no` int(11) DEFAULT NULL,
  PRIMARY KEY (`request_id`),
  KEY `FK_request_service_service` (`service_id`),
  KEY `FK_request_service_organization` (`organization_id`),
  CONSTRAINT `FK_request_service_organization` FOREIGN KEY (`organization_id`) REFERENCES `organization` (`organization_id`),
  CONSTRAINT `FK_request_service_service` FOREIGN KEY (`service_id`) REFERENCES `service` (`service_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `requirement` (
  `requirement_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `requirement_type` int(11) NOT NULL,
  PRIMARY KEY (`requirement_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `restriction` (
  `restriction_id` int(11) NOT NULL AUTO_INCREMENT,
  `service_id` int(11) NOT NULL,
  `is_allowed` bit(1) NOT NULL,
  `for_active_only` bit(1) NOT NULL,
  PRIMARY KEY (`restriction_id`),
  KEY `FK_service_restrictions_service` (`service_id`),
  CONSTRAINT `FK_service_restrictions_service` FOREIGN KEY (`service_id`) REFERENCES `service` (`service_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `service` (
  `service_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `requirement_id` int(11) NOT NULL,
  PRIMARY KEY (`service_id`),
  KEY `FK_service_list_of_requirement` (`requirement_id`),
  CONSTRAINT `FK_service_list_of_requirement` FOREIGN KEY (`requirement_id`) REFERENCES `requirement` (`requirement_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
bikeman868
  • 2,236
  • 23
  • 30
  • I know, the table i did is unlikely. That's why i think something is wrong. anyway, i just recently discovered document-oriented databases, but the problem is I can only use mysql (from phpmyadmin). So i think my other options would be jusst store the documents in a single coumn using JSON or even using the file system. – Nitche x Kris Nov 20 '16 at 12:34
  • If you follow my advice then you can remove the three awkward tables from your design and simply join service to service request. This will simplify your design enormously and also make it much easier to add new kinds of service in the future. – bikeman868 Nov 20 '16 at 21:10
  • if i removethe 3 awkward tables (the service1,2 and otherservices) how will i reflect which service the organization make, depending on his input? using the solution you stated? :) – Nitche x Kris Nov 21 '16 at 16:35
  • or can i just modify the table services adding column ServiceTableName, then remove the relationship of services to the 3 awkward tables, then specify the table name in the column ServiceTableName? then crete a new table everytime i add a new serevice? Ex: 101 - My Service Name - MyTableName (ex:Tbl_Service1) – Nitche x Kris Nov 21 '16 at 16:39
  • Just add a ServiceType column. Please do not add a new table each time you introduce a new service, this will bloat your whole solution and reduce performance for no benefit. – bikeman868 Nov 21 '16 at 16:53
  • how will i do this in a schema though? using the serviceType column, where would i dd this column? – Nitche x Kris Nov 22 '16 at 01:43
  • Service type is clearly an attribute of a service request – bikeman868 Nov 22 '16 at 02:21
  • Where would I connect the Tbl_Services1-2 now? Sorry I have so many questions. Im kinda having a little trouble catching up. I did something like this. if its right...Tbl_Request_Service (PK_RequestID, FK_OrgID,ServiceType, Request_Status) the ServiceType is connected to the tbl_Service (ServiceType) ? but where will i connect the Tbl-Services1-2? – Nitche x Kris Nov 22 '16 at 05:06
  • The whole point is that you don't need Tbl-Services1-2, and having them makes everything much more complicated. Why have a complicated solution when a simple solution is available. I have edited my answer to include a database schema for you to look at. – bikeman868 Nov 22 '16 at 05:57