0

I currently busy with a project that I want to do for my second year at college... The project is all about users and contacts, each user can have many contacts and many contacts can belong to many users, since I see it as a many to many relationship I want to create a user_contact table. The relationship between a user and a contact can either be a friend,work college or family member..the relationship can also be either passive or active state.. I also want to keep track of who last edited the record of a contact and on what date by a certain user

The system will be used internally in a certain company and the system is all about showing the relationship that users have with contacts outside the company for example john is searching for pam that works for cola cola and he sees that phil that works with him also knows pam as a friend and the relationship is in a active state..

My current table structure:

//User table

user_id | username|password|fname|lname|email

//Contact_table
contact_id|fname|lname|contactnumber|email|lastedited|editdate

//User_contact table
id|user_id|contact_id|relationship_type|relationship_state

Since I'm new to the mysql environment my question is I'm I going the right way about it...Any advise will be very helpful..

And how do I link the user id from user table to the user id in the user_contact table and the same for the contact id ??

4

2 回答 2

0

版本的联系人应该是一个单独的表,以避免部分依赖。此外,最好把人对人,所以你不需要联系人表如果你要使用 InnoDB,你应该建立外键约束来将联系人和版本表中的 person_id 链接到 person 表。像这样的东西:

CREATE TABLE editions (
edit_id INT AUTO_INCREMENT PRIMARY KEY,
contact_id INT,
editor_id INT,
editdate DATE,
FOREIGN KEY (contact_id) REFERENCES contacts(contact_id),
FOREIGN KEY (editor_id) REFERENCES users(person_id)
);

CREATE TABLE contacts (
person_id INT,
contact_id INT,
relationship_type ENUM("friend", "work", "family"),
relationship_state CHAR(1),
FOREIGN KEY (person_id) REFERENCES persons(person_id),
FOREIGN KEY (contact_id) REFERENCES persons(person_id)
);

CREATE TABLE persons (
person_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(15) UNIQUE NOT NULL,
password VARCHAR(15) NOT NULL,
fname VARCHAR(15),
lname VARCHAR(25),
email VARCHAR(50) UNIQUE NOT NULL,
contactnumber VARCHAR(15)
);
于 2013-11-03T11:15:36.147 回答
0

您要创建的“链接”称为外键,可以在您的语句中CREATE TABLE声明。您可能还想idUser_Contact表中删除冗余并将关系本身声明为主键。例如,

CREATE TABLE User_Contact(
  user_id INT NOT NULL REFERENCES `User`(user_id),
  contact_id INT NOT NULL REFERENCES `Contact`(contact_id),
  -- other columns
  PRIMARY KEY (user_id, contact_id)
);  

这告诉DBMSuser_id来自User_ContactUser在 MySQL 中,这些约束是否真正被强制执行取决于您选择的存储引擎InnoDB强制执行它们;MyIASM没有。

于 2013-11-03T11:16:25.027 回答