Database preparation

Preface

In fact, the student performance management system I built is very simple, with only the most basic operations of adding, deleting, modifying and checking. But lately I've been struggling with those methods to do it. Should you use Qt's .ui file to edit the interface or type pure code by yourself? Should you use object-oriented programming or process-oriented programming? Should you use a ternary connection to create a table or three binary connections to create a table. A choice phobia sufferer's nightmare. Finally, I finally decided to edit the graphical interface in a pure code-oriented process, and then create tables in the database based on the ER diagram. Finally, I decided to use three binary connections. Although it is more troublesome, what I want is to be proficient in the use of databases.

Start the topic

Our system uses a database. Students who have studied databases should all know that we must first draw an ER diagram, and then we can use the ER diagram to decompose which tables we need to create, what attributes each table has, etc. Today we mainly talk about how to create tables based on ER diagrams. here is the image.

Binary connection

These are the three binary connections I am talking about. Each rectangle represents an entity; each diamond represents a connection; and each ellipse represents an attribute. These are the most basic knowledge of databases. One entity is one table; one relationship is one table. As shown in the figure, this ER diagram requires up to 6 tables. Of course, this is the case with the most tables.

Next, we will introduce the situations that can be merged. In 1-to-many, the contact can be merged into the party with many; in many-to-many, the contact can be merged into any party; in 1-to-1, merging is not possible. As shown in the figure above, one teacher can teach multiple courses, and one course can be taught by multiple teachers; one student can choose multiple teachers, and one teacher can teach multiple students; one course can be studied by multiple students, A student can also study multiple courses. Therefore, there is a many-to-many relationship between each entity, so we can merge the relationship into any entity without any problem. Therefore, this ER diagram has up to six tables and at least three tables. When merging, we need to add the primary key of the entity that is not merged at the other end of the relationship to the entity to be merged, and then create the table .

Then, let’s take a look at the ternary connection

The principle is exactly the same as mentioned above, so this ER diagram only needs four tables at most, and at least three tables. Each of these two ER diagrams has its own advantages and disadvantages, but it is obvious that the ternary connection is simpler. If you are afraid of trouble, you can choose to create a database based on the ternary connection.

Create a database using MySql

Next we have to type the code again. Of course, the premise is that you have installed the database. It doesn't matter which database it is, because SQL is universal. Without further ado, let’s go straight to the picture

Is the MySQL service turned on?

The first thing is to log in to MySql. Of course, the premise is that you have installed the MySql database and turned on this service. Here's how to check if it's open.

Right-click the mouse and click Start. Of course, there is an easier way to start it, but the premise is that you have to know the name of your service. For example, mine is called MySQL80. Start cmd as administrator, and then execute the following line of commands to start the service.

net start 服务的名称

 Log in to MySQL

Open the cmd command line and enter -u username -p password. If you add a carriage return after -p here, the password will become an asterisk and will not be displayed directly. If you do not add it, the password will be displayed directly.

Create database and view

Enter the command to create the database

create database 数据库名称;

View all database commands

show databases;

Remember to add a semicolon. Results as shown below

These are the two databases I created

Work with databases and operate on tables

Use database commands

use 数据库名称;

Then create the required tables in the database

create table 表名(列名 数据类型 是否为空 是否位主键......);

Check whether the table is created successfully

show tables;

 View data in table

select * from 表名;

The effect is as follows

 

It's empty because I haven't inserted any data.

Only for learning, please do not use it for illegal purposes.

Guess you like

Origin blog.csdn.net/qq_64241302/article/details/132763054