mysql数据库对象关系映射_MySQL
bitsCN.com
mysql数据库对象关系映射
1.对“对象关系映射”的理解
a.对象:可以理解为java中的类
b.关系:可以理解为数据库的表
c.映射:这是因为java中的数据的类型与数据库的数据类型是有些不同的,如何将java中的类型的数据用数据库中的数据类型来存储起来,这就映射。
同时,还有当java中的两个对象之间是有关系的,那么在存储在数据库中时,也需要用一种方式来体现出这种存在的关系,而不能是两个
不相关的两张表,这也需要映射出来这种关系。
2.表与表之间的关系类型可以分为以下四种:
多对一,一对多,多对多,一对一
3.分别对这四处种关系进行示例说明
a.多对一(一对多和多对一,其实是看的角度不同,实际上可以看成同一种)员工与部门之间的关系
有以下两个java对象:
对象类:
[java] Department int id; String name; Set employees=new HashSet(); Employee int id; String name; Department dept;
员工与部门,这两者是典的多对一的关系,多个员工可以属于同一个部门,一个部门中可以有多个员工。
注意:在实际开发时,如果要设计一个员工管理系统,那么在进行对象实体类的设计时,就是按照上面
设计方式来设计员工类和部门类。在员工 类中有一个Department dept属性,而没有直接用int dept_no,
因为样更加符面象对象的设计思想。
对应的数据库的表为:
[sql] create department( id int primary key, name varchar(20) ); [sql] create employee( id int primary key, name varchar(20), dept_id int, constraint dept_id_FK foreign key(dept_id) references department(id) );
注:两个表之间的关系,多对一,用外键来体现。一般外键的应该放在多的一方,在这里也就是放在employee员工表中。
b.多对多的关系 学生与老师之间的关系
java类对象:
[java] Teacher int id; String name; Set students=new HashSet(); Student int id; String name; Set teachers=new HashSet();
对应的数据库中的表:
[sql] create table teacher( id int primary key, name varchar(20) ); create table student( id int primary key, name varchar(20) );
注:对于多对多的关系,应该创建第三张表来保存两张表的映射关系
[sql] create table student_teacher( student_id int, teacher_id int, constraint student_teacher_PK primary key(student_id,teacher_id), constraint student_id_FK foreign key(student_id) references student(id), constraint teacher_id_FK foreign key(teacher_id) references teacher(id) );
第三张中的主键是以前两张表的主键作为复合主键,并分别以两张表的主键作为外键。
c.一对一的关系 人与身份证之间的关系
java类对象:
[java] Person int id; String name; IdCard ic; IdCard int id; String address;
对应的数据库表为:
[sql] create table person( id int primary key, name varchar(20) ); create table idcard( id int primary key, address varchar(40), constraint id_FK foreign key(id) references person(id) );
注:对于一对一关系的两张表,分主表与从表,从表的存在必须依赖于主
表,主表可以不依赖于从表。从表的设计上其主键字段同时也是外键字段。
备注:有时在设计表时,会特意把两张表合在一张表中,虽然会造成
数据冗余,但是却可以不因为联表查询而造成查询性能有所降低。这是用
空间来换时间的做法。
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Steps to automatically back up MySQL data using Navicat: Install and connect to the MySQL server. Create a backup task, specifying the backup source, file location, and name. Configure backup options, including backup type, frequency, and retention time. Set up an automatic backup plan, enable automatic backup, set time and frequency. Preview the backup settings and perform the backup. Monitor backup progress and history.
