Implementing many, many relationships in mysql

I have two tables

1. Rectangle (rectId, xPos, yPos, height, width)

2. Scale (scaleId, someothercols ...)

Now each column of the rectangle can be mapped to a scale from 0 to 1.

ie xPos can scale, yPos can scale, etc. Thus, in the general case, one rectangle can have more than one scale.

Also, all of the above columns may also not have a reference to scale, in which case they will have a static value ie xPos = 50, yPos = 60, and so on.

You can also reference a scale to more than one rectangle.

What is the best way to implement this in sql

What I thought has a Rectangle_scale join table with rect_column exg attribute:

Rectangle_scale

| rectScaleId | rectId      | scaleId      | rect_col(string)|
|:----------- |------------:|:------------:|---------------- | 
| 1           |      1      |     2        |  Xpos           |
| 2           |      1      |     3        |  Ypos           | 
| 3           |      2      |     2        |  Height         | 

? rectScaleId, rectId scaleId

, - , , Laravel 5.2 , , , Laravel eloquent, .

+4
2

, , .

, , rectange scale.

:

  • ,

, :

create table rectangle (
  rectID int primary key auto_increment,
  xPos int,
  yPos int, 
  height int,
  width int)ENGINE = Innodb;

create table scale (
  scaleID int primary key auto_increment,
  descr text )ENGINE = Innodb;

create table rect_scale (
  scale int not null,
  rect int ,
  rect_col varchar(100),
  primary key (scale,rect),
  foreign key(scale) references scale(scaleID),
  foreign key(rect) references rectangle(rectID)     
)ENGINE = Innodb;

SQLFiddle:

+1

, xPos ​​yPos , :

CONSTRAINT xpos_scale_fk FOREIGN KEY(xpos) REFERENCES scale(scaleId) 
CONSTRAINT ypos_scale_fk FOREIGN KEY(ypos) REFERENCES scale(scaleId) 

:

, i.e xPos = 50, yPos = 60 .

, , , xpos ypos - . , .

, ( ):

rectangle
rect_id, xpos, ypos, height, width
1, 0, 1, 2, 2

scale
scale_id, units, values
0, pixels, 50
1, pixels, 100
2, meters, 2

, , .

0

Source: https://habr.com/ru/post/1658253/


All Articles