MYSQL Learning Three (many to many)

Source: Internet
Author: User

In my previous blog, I learned a little bit about the basics of database, and today we are learning the design of many-to-many databases in a database. What are the many-to-many data relationships in our daily lives? If you are still a student, the most likely thing to think about is the relationship between the teacher and the student. A teacher must have taught more than one student, and a student has listened to many teachers ' classes. If you are a salesman, the most likely thing to think about is the relationship between the order and the customer. In short, there is too much of a lot of relationships in life.

Instance:

In the following, we will understand the relationship between the database and many-to-many in relation to the teacher and students;


Hypothesis: The teacher's information is id,name,salary, the student's information is Id,name,grade (as shown:)


650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/89/B1/wKioL1gZ-KLyQKCgAAA0rEvyzqY836.png-wh_500x0-wm_3 -wmp_4-s_1336960193.png "title=" many-to-many. png "alt=" wkiol1gz-klyqkcgaaa0revyzqy836.png-wh_50 "/>

You can see the teacher's information form and the student's information table in the middle, so we think of creating two forms of teacher and student.

First create a table of teacher's information:


CREATE TABLE TEACHERS (

ID int PRIMARY KEY,

Nmae varchar (100),

SALARY Float (10,2)

);

Then create a table of student information:

CREATE TABLE STUDENTS (

ID int PRIMARY KEY,

NAME varchar (100),

GRADE varchar (1)

);

Finally create a table of relationship between teacher and student:

CREATE TABLE Teachers_students (

t_id int,

s_id int,

Primary KEY (T_ID,S_ID),

Constraint T_ID_FK foreign KEY (t_id) references TEACHERS (ID),

Constraint S_ID_FK foreign KEY (s_id) references STUDENTS (ID)

);


Note: The above MySQL statement can be executed directly, if you are like me is a small white, perhaps this hint for you to use;

Before the table is created. You should create the database, then use the database, and then create the table

So you should first log in to the database: Mysql-uroot-p ();

Then create the Database:create database Test;

Then use Database:use Test;

Finally, create the table.


Our table is created, and as a Java developer, the tables in the database have been identified, should we design our Java class?

Now we're going to start designing our Java class:

First we design our teacher class: The Teacher class first certainly includes the basic information of teacher, but how does the relationship with the student withdraw? If you're not sure, take a look at the denomination:

public class teacher{

Private Integer ID;

private String name;

Private Float salary;

Private list<student> Stus = new arraylist<student> ();

....

}

How is the student class designed? Of course, the same class as the teacher! First you have to have the basic information of the students, and then the relationship with the teacher reflects: so we can be designed as follows:

public class student{

Private Integer ID;

private String name;

private Character grade;

Private list<teacher> ts = new arraylist<teacher> ();

...

}



This is the design of a simple many-to-many database and the corresponding relationship between Java classes. Finally another one-to-one database, like our ID card and the relationship between people under normal circumstances is a person only one ID card. An ID card is only one person, if you really want to design a one-to-ones relationship table? I also don't know the specific application scenario. If I wanted to, could I just design a table? After all, as far as I know. The rate of a single-table query is still much faster? Although I have not used it. Some time ago listen to a colleague of our company said, our company's products in the process of upgrading, because the database of things too much, resulting in the database in the upgrade time is too long, and then through the database of experts on the database optimization, greatly shorten the upgrade time. Therefore, we should also take into account the efficiency of the database in the future design process of the database. I hope the great God will teach you a lot.

This article is from the "Learning Notes" blog, so be sure to keep this source http://7298246.blog.51cto.com/7288246/1868746

MYSQL Learning Three (many to many)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.