Hi everybody,
I am trying to make a EER diagram using mysql workbench to start with a database.
This is a vehicle parts database dat im trying to design. i have following five tables:
1. Make
2. Model
3. Year
4. Body
5. Trim
Now creating them with or without workbench is easy but im trying to make some business logic in it to avoid repetitions of data again and again by way of creating relationships and foreign keys.
Table Make have fields:
1. Id
2. name
Table Model have fields:
1. Id
2. name
3. make_id (foreign key)
Now from above i created a one to many relationship to between model and make table because a make have many models (e.g. honda may have civic and accord etc.) but a model will have only one make. That's y workbench passed the make id as foreign key into the table.
Table year have fields:
1. Id
2.year
Now dat each model may have many years and a year may have many models tells me dat i have to create a many to many relationship here. So i created many to many relationship between year table and model table. By doing that workbench created another table called year_has_model which has following fields:
1. model_id
2. model_make_id
3. year_id
Now i know that each model may have various years and a year may have various models and i can insert the values into it without any problem.
As i go further i have 2 tables left i.e. body and trim. Now a model in a year may have different bodies and a body in a year may have different trim levels.
This is really making me mad. im not getting how should i relate it to my tables. Wat is the best approach to solve this problem as speed is the most important thing and i dont wana repeat values.
If i create many to many relationship between body and year then body_has_year will not give me any info about the model and make and if i create many to many relationship between trim and body then wat about the years and how do i relate to it?
I am not able to solve this type of things. Please help me wat should i do?
Here is the generated sql code for quick reference please:
thanking you.
regards
I am trying to make a EER diagram using mysql workbench to start with a database.
This is a vehicle parts database dat im trying to design. i have following five tables:
1. Make
2. Model
3. Year
4. Body
5. Trim
Now creating them with or without workbench is easy but im trying to make some business logic in it to avoid repetitions of data again and again by way of creating relationships and foreign keys.
Table Make have fields:
1. Id
2. name
Table Model have fields:
1. Id
2. name
3. make_id (foreign key)
Now from above i created a one to many relationship to between model and make table because a make have many models (e.g. honda may have civic and accord etc.) but a model will have only one make. That's y workbench passed the make id as foreign key into the table.
Table year have fields:
1. Id
2.year
Now dat each model may have many years and a year may have many models tells me dat i have to create a many to many relationship here. So i created many to many relationship between year table and model table. By doing that workbench created another table called year_has_model which has following fields:
1. model_id
2. model_make_id
3. year_id
Now i know that each model may have various years and a year may have various models and i can insert the values into it without any problem.
As i go further i have 2 tables left i.e. body and trim. Now a model in a year may have different bodies and a body in a year may have different trim levels.
This is really making me mad. im not getting how should i relate it to my tables. Wat is the best approach to solve this problem as speed is the most important thing and i dont wana repeat values.
If i create many to many relationship between body and year then body_has_year will not give me any info about the model and make and if i create many to many relationship between trim and body then wat about the years and how do i relate to it?
I am not able to solve this type of things. Please help me wat should i do?
Here is the generated sql code for quick reference please:
Code:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`make`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`make` ;
CREATE TABLE IF NOT EXISTS `mydb`.`make` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(75) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`model`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`model` ;
CREATE TABLE IF NOT EXISTS `mydb`.`model` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(75) NULL ,
`make_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`id`, `make_id`) ,
CONSTRAINT `fk_model_make`
FOREIGN KEY (`make_id` )
REFERENCES `mydb`.`make` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `fk_model_make` ON `mydb`.`model` (`make_id` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`year`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`year` ;
CREATE TABLE IF NOT EXISTS `mydb`.`year` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`year` VARCHAR(10) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`body`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`body` ;
CREATE TABLE IF NOT EXISTS `mydb`.`body` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(75) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`trim`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`trim` ;
CREATE TABLE IF NOT EXISTS `mydb`.`trim` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(75) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`model_has_year`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`model_has_year` ;
CREATE TABLE IF NOT EXISTS `mydb`.`model_has_year` (
`model_id` INT UNSIGNED NOT NULL ,
`model_make_id` INT UNSIGNED NOT NULL ,
`year_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`model_id`, `model_make_id`, `year_id`) ,
CONSTRAINT `fk_model_has_year_model1`
FOREIGN KEY (`model_id` , `model_make_id` )
REFERENCES `mydb`.`model` (`id` , `make_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_model_has_year_year1`
FOREIGN KEY (`year_id` )
REFERENCES `mydb`.`year` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_model_has_year_model1` ON `mydb`.`model_has_year` (`model_id` ASC, `model_make_id` ASC) ;
CREATE INDEX `fk_model_has_year_year1` ON `mydb`.`model_has_year` (`year_id` ASC) ;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
regards
Comment