当前位置: 动力学知识库 > 问答 > 编程问答 >

Database structures Phpadmin in MySQL CodeIgniter

问题描述:

Imagine we have a phpadmin database and we have a table for students and student_music and student_friend student_friend_music. Does this make sense performance wise to have four tables? or do you think we need to have three tables for students students_friends student_music and table for music. How does facebook stores friends relationship in it's database?

Other thing is that I designed the data base for both. I don't know if I see any difference but I think once the users increase one would beat other performance wise?

So my question concerns the performances of querying:

Is this better to have more tables or

Can we have duplicates.

Do you know any good book, tutorial or reference I can study to know about relational data bases in Php my admin and mysql.

Update:

Table Student can have many to many relation with itself.

Table Music has Many to Many with the student as well.

Student id Student_friend Music id STUDENT_MUSIC

A 1 1-3 YT 1 1 3

B 2 2-3 RU 2 2 3

C 3 PI 3 3 1

3 2

So I am using something called Data Mapper in code igniter which causing me a headache but this sounds like a structure I am thinking now.

网友答案:

Check out this link for a mysql introduction and the wikipedia article on relational databases. Read about tables, primary and foreign keys. Before worrying about performance you need to address the structure of your database.

Try (One-to-Many:A student can own many pieces of music and have one friend):

CREATE TABLE Student(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
    first_name VARCHAR(30),
    last_name VARCHAR(30),
    friend_id INT)

CREATE TABLE Music(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
    music_title VARCHAR(30),
    music_student_id INT)
    FOREIGN KEY (music_student_id) REFERENCES Student(id)
                  ON DELETE CASCADE

Or Try (Many-to-Many:Many students can own many pieces of music and have many friends):

CREATE TABLE Student(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
    first_name VARCHAR(30),
    last_name VARCHAR(30))
    FOREIGN KEY (id) 
    REFERENCES StudentMusic (Student_id)
    ON DELETE CASCADE

CREATE TABLE Music(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
    music_title VARCHAR(30),
    FOREIGN KEY (id) 
    REFERENCES StudentMusic (Music_id)
    ON DELETE CASCADE

CREATE TABLE StudentMusic (
    Student_id INT NOT NULL AUTO_INCREMENT, 
    Music_id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY  (Student_id,Music_id)

CREATE TABLE Friendships(
    student_A_id INT,
    student_B_id INT)
    PRIMARY KEY  (student_A_id,student_B_id)

Handling the data views of the relationships can be shown using a Select statement. In the One-to-Many design finding a Student's music uses the following query:

Select Student.first_name,Student.last_name,Music.music_title 
FROM Student 
LEFT JOIN Music on (Student.ID=Music.music_student_id)

Part of designing a database is figuring out what relationships you will need to query.

Also look into normalizing databases.

网友答案:

table suggestions:

table_students (contains students info, etc) table_music (music pref of studs with student id from table_students) table_friends (contains student id's from table students and flag value either friend or not friend)

分享给朋友:
您可能感兴趣的文章:
随机阅读: