Pergunta

I have the following issue. We are currently working on a system for a shuttle service company. Now, part of the entities in the database for this system include numerous lookup tables (such as vehicle_type, employee_status, etc), as well as some other tables, such as vehicle and vehicle_service log.

Now the issue we as a team are having is that we cannot agree on what the logical relationship cardinalities between entities should be. The two main problem relationships include the tables defined as follows:

CREATE TABLE IF NOT EXISTS `user_type` (
  `type_id` tinyint(4) NOT NULL AUTO_INCREMENT,
  `description` varchar(200) NOT NULL,
  PRIMARY KEY (`type_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='Store the user types - employee
 or consultant' AUTO_INCREMENT=1 ;

which is linked to

CREATE TABLE IF NOT EXISTS `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `user_type` tinyint(4) NOT NULL,
  PRIMARY KEY (`user_id`),
  KEY `user_type` (`user_type`),
  KEY `username` (`username`),
  KEY `login` (`username`,`password`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='Table used when logging in 
to check access level, type of user, etc. ' AUTO_INCREMENT=1 ;

The user table includes other irrelevant data. So the issue here is that I say (because MySQL Workbench reverse engineered it that way and it makes more sense) that the relationship should be 1-many, while another team member says that it should be 0-many (because some records may exist in the user_type table that aren't used in the user table)

The other table relationship we are having words about are defined as follows:

CREATE TABLE IF NOT EXISTS `vehicle` (
  `vehicle_id` int(11) NOT NULL AUTO_INCREMENT,
  `registration_number` varchar(10) NOT NULL,
  PRIMARY KEY (`vehicle_id`),
  UNIQUE KEY `registration_number` (`registration_number`),
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='Actual vehicle information'
 AUTO_INCREMENT=1 ;

Again, with some other columns not relative to the question. This links with

CREATE TABLE IF NOT EXISTS `service_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vehicle_id` int(11) NOT NULL,
  `description` text NOT NULL,
  `date` date NOT NULL,
  `cost` double NOT NULL,
  PRIMARY KEY (`id`),
  KEY `vehicle_id` (`vehicle_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Store records of all services 
to vehicles' AUTO_INCREMENT=1 ;

Should this be 1-many or 0-many because a vehicle may not yet go in for a service? According to me it should be 1-many, but I don't know if this works logically.

We are all very confused about this whole logical modelling thing, so any help would be much appreciated!

I figured it would be easier for me to create the DB first and then reverse engineer it to a physical model, but never though about logical.

Foi útil?

Solução

Zero to many if it is optional. Say for example a Sales Rep would have a zero or many customer. Why is that? Because if there is a new sales rep then it would mean he/she has no customer to begin with unless of course he/she assume the accounts of a resigned Sales Rep.

On the other hand one or many is mandatory. For example a an Order which has order date and customer who ordered should have at least one record on Order Detail table. Let's say a customer ordered a tablet last 04/22/2013 then he/she would have:

 Order table
 ----------------------------------------
 Orderid.  OrderDate. Customermnum
 ----------------------------------------
  1.       04/22/2013 101

 Order detail table
 ----------------------------------------
 Orderid.  Productid.  Qty.   quotedprice
 ----------------------------------------
  1.       T101       1       500

So, in your case User to UserType is 1 to 0 or many beacause a user type may have not been used by any user yet.

Now, vehicle to service It is also 1 to 0 or many since a vehicle may not necessarily have a service done yet.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top