![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
#4
| |||
| |||
|
#5
| |||
| |||
|
|
Consider you have two tables - for example, "user" and "url". Now there are several attributes for each m:n relation that can be set independently; for example, "visited", "bookmarked" and "open on startup". Some of these are only set for few of the m:n relations, others can be set for most of them. Most are boolean, but a few might carry data as well. I see two basic ways of modeling this, both with advantages and disadvantages. 1.) Build a standard m:n link table and add the attributes as additional columns. - Pro: Read queries stay simple - Con: Write operations always have to consider that the row might not exist yet and needs to be inserted first (using triggers or "on duplicate key" constructs) 2.) For each attribute, add a separate m:n table. For boolean attributes, an existing row means that the attribute is set. For non-boolean attributes, add an extra data row. - Pro: Clean separation of attributes, update operations are straightforward - Con: Queries require more complex queries or extra views; possibly reduced performance because more joins are required? Which way of modeling do you prefer? Why, and/or in which situation? |
#6
| |||
| |||
|
|
Consider you have two tables - for example, "user" and "url". Now there are several attributes for each m:n relation that can be set independently; for example, "visited", "bookmarked" and "open on startup". Some of these are only set for few of the m:n relations, others can be set for most of them. Most are boolean, but a few might carry data as well. I see two basic ways of modeling this, both with advantages and disadvantages. 1.) Build a standard m:n link table and add the attributes as additional columns. - Pro: Read queries stay simple - Con: Write operations always have to consider that the row might not exist yet and needs to be inserted first (using triggers or "on duplicate key" constructs) 2.) For each attribute, add a separate m:n table. For boolean attributes, an existing row means that the attribute is set. For non-boolean attributes, add an extra data row. - Pro: Clean separation of attributes, update operations are straightforward - Con: Queries require more complex queries or extra views; possibly reduced performance because more joins are required? Which way of modeling do you prefer? Why, and/or in which situation? |
#7
| |||
| |||
|
|
Consider you have two tables - for example, "user" and "url". Now there are several attributes for each m:n relation that can be set independently; for example, "visited", "bookmarked" and "open on startup". Some of these are only set for few of the m:n relations, others can be set for most of them. Most are boolean, but a few might carry data as well. I see two basic ways of modeling this, both with advantages and disadvantages. 1.) Build a standard m:n link table and add the attributes as additional columns. - Pro: Read queries stay simple - Con: Write operations always have to consider that the row might not exist yet and needs to be inserted first (using triggers or "on duplicate key" constructs) 2.) For each attribute, add a separate m:n table. For boolean attributes, an existing row means that the attribute is set. For non-boolean attributes, add an extra data row. - Pro: Clean separation of attributes, update operations are straightforward - Con: Queries require more complex queries or extra views; possibly reduced performance because more joins are required? Which way of modeling do you prefer? Why, and/or in which situation? |
#8
| |||
| |||
|
|
CREATE TABLE Users (user_id CHAR(255) NOT NULL PRIMARY KEY, ..); CREATE TABLE URLs (url CHAR(255) NOT NULL PRIMARY KEY, ..); CREATE TABLE <<table name here (user_id CHAR(255) NOT NULL REFERENCES Users, url CHAR(255) NOT NULL REFERENCES URLs, url_status VARCHAR (20) NOT NULL CHECK url_status IN ('visited', 'bookmarked' , 'open on startup', ..), PRIMARY KEY (user_id, url, url_status)); |
#9
| |||
| |||
|
|
CREATE TABLE Users (user_id CHAR(255) NOT NULL PRIMARY KEY, ..); CREATE TABLE URLs (url CHAR(255) NOT NULL PRIMARY KEY, ..); CREATE TABLE <<table name here (user_id CHAR(255) NOT NULL REFERENCES Users, url CHAR(255) NOT NULL REFERENCES URLs, url_status VARCHAR (20) NOT NULL CHECK url_status IN ('visited', 'bookmarked' , 'open on startup', ..), PRIMARY KEY (user_id, url, url_status)); |
#10
| |||
| |||
|
|
CREATE TABLE Users (user_id CHAR(255) NOT NULL PRIMARY KEY, ..); CREATE TABLE URLs (url CHAR(255) NOT NULL PRIMARY KEY, ..); CREATE TABLE <<table name here (user_id CHAR(255) NOT NULL REFERENCES Users, url CHAR(255) NOT NULL REFERENCES URLs, url_status VARCHAR (20) NOT NULL CHECK url_status IN ('visited', 'bookmarked' , 'open on startup', ..), PRIMARY KEY (user_id, url, url_status)); |
![]() |
| Thread Tools | |
| Display Modes | |
| |