在從mongodb到mysql的轉換中,您將在設計應用程序時錯過mongodb的靈活性。 我可以推薦你看看this book,你可以在那裏瞭解正常化。
您的例子沒有給出任何關於如果一對多(用戶工作只是在一個項目上)或許多關係到許多人(用戶工作在許多項目)的足夠信息。使用「用戶A」,「用戶B」等作爲用戶的標識符,我會這樣做,對於我所說的兩種認同:
1 to Many。 -
create table projects(Id integer PRIMARY KEY, Name varchar(20), Type varchar(20));
create table users (Id varchar(20) PRIMARY KEY, Project_Id integer, FOREIGN KEY fk_proj(Project_Id) REFERENCES projects(Id));
mysql> describe projects;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Id | int(11) | NO | PRI | NULL | |
| Name | varchar(20) | YES | | NULL | |
| Type | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> describe users;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Id | varchar(20) | NO | PRI | NULL | |
| Project_Id | int(11) | NO | | NULL | |
+------------+-------------+------+-----+---------+-------+
,用戶(Poject_Id)引用項目(編號)
我在ID中使用VARCHAR只是爲案件屬性。
你如何在MySQL中查詢? 選擇一個從下面
select p.Id, Name, Type, u.Id as Users from projects as p JOIN users as u on p.Id=u.Project_Id;
或者
select p.Id, Name, Type, u.Id as Users from projects as p, users as u where p.Id=u.Project_Id;
同樣的結果
+----+-------+-------+-------+
| Id | Name | Type | Users |
+----+-------+-------+-------+
| 1 | ProjA | TypeA | UserA |
| 1 | ProjA | TypeA | UserB |
| 1 | ProjA | TypeA | UserC |
| 2 | ProjB | TypeB | Userx |
| 2 | ProjB | TypeB | UserY |
| 2 | ProjB | TypeB | UserZ |
+----+-------+-------+-------+
許多到Many.-對於M到M的關係,而不是使用的項目標識users表中,您將創建一個包含兩個表主鍵(project.Id和users.Id)的單獨表,a nd使屬性的元組成爲該表的pk。
create table projects(Id integer PRIMARY KEY, Name varchar(20), Type varchar(20));
create table users (Id varchar(20) PRIMARY KEY);
create table projects_users(Id_Project integer not null, Id_User varchar(11) not null, PRIMARY KEY pk_projects_users(Id_Project, Id_User));
mysql> describe projects;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Id | int(11) | NO | PRI | NULL | |
| Name | varchar(20) | YES | | NULL | |
| Type | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> describe users;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Id | varchar(20) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> describe projects_users;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Id_Project | int(11) | NO | PRI | NULL | |
| Id_User | varchar(11) | NO | PRI | NULL | |
+------------+-------------+------+-----+---------+-------+
再次,選擇您要查詢的方式之一。
SELECT p.Id, Name, Type, pu.Id_User as User FROM projects as p join projects_users as pu ON p.Id=pu.Id_Project;
SELECT p.Id, Name, Type, pu.Id_User as User FROM projects as p, projects_users as pu WHERE p.Id=pu.Id_Project;
而結果
+----+-------+-------+-------+
| Id | Name | Type | User |
+----+-------+-------+-------+
| 1 | ProjA | TypeA | UserA |
| 1 | ProjA | TypeA | UserB |
| 1 | ProjA | TypeA | UserC |
| 1 | ProjA | TypeA | UserY |
| 2 | ProjA | TypeA | Userx |
| 2 | ProjA | TypeA | UserY |
| 2 | ProjA | TypeA | UserZ |
+----+-------+-------+-------+
正如你所看到的查詢關於MySQL從查詢有關MongoDB很大的不同。我希望我通過這個解釋讓我自己清楚。
如果一個用戶可以在多個項目上工作,這應該回答你的問題:http://stackoverflow.com/questions/2923809/many-to-many-relationships-examples –