data-modeling

SQL - Should I use a junction table or not?

冷暖自知 提交于 2019-11-29 05:07:15
I am creating a new SQL Server 2008 database. I have two two tables that are related. The first table looks like this: BRANDS // table name BrandID // pk BrandName // varchar The second table looks like this: MODELS // table name ModelID // pk ModelDescription // varchar Every brand will have at least one model and every model will belong to only one brand. The question is, should I create a junction table like this BRANDS_MODELS // table name RecordID // pk BrandID ModelID Or should I modify the MODELS table to include the BrandID like this MODELS // table name BrandID // ModelID // pk

Best way to store a many-to-many relationship in MySQL?

﹥>﹥吖頭↗ 提交于 2019-11-29 05:03:55
Let's say I have a simple database with tables 'posts' and 'tags'. Posts can have many tags and tags can belong to many posts. What is the best way to structure the database? I thought of using a list/serialize: tags idx tag_id, str tag_name posts idx post_id, str title, list tag_ids OR having another table with the associations. Problem is using this I don't even know how to structure the query to pull the associated tag names when I get a post. posts idx post_id, str title post_tags fk post_id, fk tag_id I actually I don't like either of them. Is there a better way? The post_tags is the

How to put a complicated equation into a R formula?

旧城冷巷雨未停 提交于 2019-11-29 04:03:48
We have the diameter of trees as the predictor and tree height as the dependent variable. A number of different equations exist for this kind of data and we try to model some of them and compare the results. However, we we can't figure out how to correctly put one equation into the corresponding R formula format. The trees data set in R can be used as an example. data(trees) df <- trees df$h <- df$Height * 0.3048 #transform to metric system df$dbh <- (trees$Girth * 0.3048) / pi #transform tree girth to diameter First, the example of an equation that seems to work well: form1 <- h ~ I(dbh ^ -1)

Designing 1:1 and 1:m relationships in SQL Server

空扰寡人 提交于 2019-11-28 21:41:19
In SQL Server 2008, how does one design a 1:1 and 1:m relationship? Any relationship requires that the "parent" table (the one side) have a Primary (or unique) Key (PK), that uniquely identifies each row, and the "child" table (the other side) have a Foreign Key column or columns, that must be populated with values that are the same as some existing value[s] of the Primary Key in the parent table. If you want a one to many (1-M) relationship then the Foreign Key should be an ordinary attribute (column or columns) in the child table that can repeat (there can be many rows with the same value)

Managing hierarchies in SQL: MPTT/nested sets vs adjacency lists vs storing paths

天大地大妈咪最大 提交于 2019-11-28 21:40:47
问题 For a while now I've been wrestling with how best to handle hierarchies in SQL. Frustrated by the limitations of adjacency lists and the complexity of MPTT/nested sets, I began thinking about simply storing key paths instead, as a simple node_key/node_key/... string. I decided to compile the pros and cons of the three techniques: Number of calls required to create/delete/move a node: Adjacency = 1 MPTT = 3 Path = 1 (Replace old node path with new node path across all nodes that contain that

Star schema, normalized dimensions, denormalized hierarchy level keys

五迷三道 提交于 2019-11-28 21:32:59
Given the following star schema tables. fact, two dimensions, two measures. # geog_abb time_date amount value #1: AL 2013-03-26 55.57 9113.3898 #2: CO 2011-06-28 19.25 9846.6468 #3: MI 2012-05-15 94.87 4762.5398 #4: SC 2013-01-22 29.84 649.7681 #5: ND 2014-12-03 37.05 6419.0224 geography dimension, single hierarchy, 3 levels in hierarchy. # geog_abb geog_name geog_division_name geog_region_name #1: AK Alaska Pacific West #2: AL Alabama East South Central South #3: AR Arkansas West South Central South #4: AZ Arizona Mountain West #5: CA California Pacific West time dimension, two hierarchies, 4

SQL - Best practice for a Friendship table

╄→尐↘猪︶ㄣ 提交于 2019-11-28 20:50:57
Before you show me duplicates, please note that I've searched through the site an have found some examples but not quite specific to my problem :) What's the best way to create a Friendship table in SQL, but making sure that each row is unique in the sense that the same UserID and FriendID will never be alowed regardless of which column they belong to? I have this rough example CREATE TABLE [dbo].[Friendship]( [UserID] [uniqueidentifier] NOT NULL, [FriendID] [uniqueidentifier] NOT NULL, [FriendshipStatus] [int] NOT NULL ) And there are 2 foreign keys to the Users table, both from UserID and

Modeling a many-to-many relationship in ASP.NET MVC using LINQ to SQL

試著忘記壹切 提交于 2019-11-28 19:56:37
I've been reading and watching videos about MVC and I'm starting to get a little confused. The simple tutorials are far too simple and they seem to only involve a single database table. Whoopty doo! The advanced tutorials assume a lot of knowledge is present and I have trouble following those. Not to mention, there are 15 ways to model your database and nobody wants to explain why they do it this way or that way. So, I'm looking for a simple tutorial or explanation of what process you would go through to design a simple CRUD application that involves a many-to-many relationship, explained

How would I model data that is heirarchal and relational in a document-oriented database system like RavenDB?

。_饼干妹妹 提交于 2019-11-28 19:20:08
问题 Document oriented databases (particularly RavenDB) are really intriguing me, and I'm wanting to play around with them a bit. However as someone who is very used to relational mapping, I was trying to think of how to model data correctly in a document database. Say I have a CRM with the following entities in my C# application (leaving out unneeded properties): public class Company { public int Id { get; set; } public IList<Contact> Contacts { get; set; } public IList<Task> Tasks { get; set; }

MySQL friends table

空扰寡人 提交于 2019-11-28 18:52:21
I have a MySQL DB in which I store data about each user. I would like to add a list of friends for each user. Should I create a table of friends for each user in the DB or is there a better way? Assuming all your friends are also in the user table you will need a friends table which defines a simple one-to-many relationship - linking the users table back to itself. So User Table UserID int identity not null [other attribute fields] Friends Table UserIDLink1 int UserIDLink2 int [other attribute field] Where both UserIDLink1 and UserIDLink2 are foreign keys on the Users table. So for instance if