1. Database Constraints1.1 IntroductionAs the name suggests, constraints in the database are restrictions on the data inserted into the database. The purpose of this is to ensure the validity and integrity of the data. This greatly improves the quality of data in the database, saves database space and the time for calling data. I have previously introduced the data types in MySQL. The data types can actually verify part of the data. The same goes for constraints, which can also verify some data. This will avoid any illegal data. 1.2 Constraint TypesCommon types of constraints are as follows:
1.3 not null If However, when doing some questionnaires, there are required items. In this case, these options cannot be empty, so you can use not null. Example: 1.4 unique If But in real life, for example, ID card numbers, phone numbers, etc. are actually unique values, so you can use Example: Notice: When a column has 1.5 default When we do not use Example: 1.6 primary key Example: Notice:
Supplement: Automatic increment - MySQL can automatically assign the next available number to each row, eliminating the need to manually assign a unique value when adding a row (which can be done manually). But it must be used when create table, for example:
1.7 foreign key A foreign key is For example, if you shop on Taobao, a database can have two data tables: the product table and the order table, and both tables can contain product numbers. The product number in the order table can only appear if it exists in the product table. Therefore, foreign keys can be used to constrain and prevent abnormal data from appearing. Example:
Solution: logical deletion (do not directly delete the database records, do not break the foreign key constraints) Introduce a new field in the product table to indicate whether the record is valid or invalid. The default value of this field can be set to 1, which means valid. If you want to delete this product, change this field to 0, and the product will be invalid. 1.8 check
Example: 2. Design of database tablesAfter we have mastered the basic database operation methods, if we are faced with the operation of building a table from scratch, we must have a good grasp of the entities we need and the relationships between them. There are four main types of relationships between entities:
2.1 One-to-one relationshipFor example, a student corresponds to a user identity of a system, and we can have two design methods: Put student information and user information into one table
Put students in table A and user information in table B. In table B, you can insert student IDs to associate students with users. A table:
Table B:
2.2 One-to-many relationshipFor example, there is generally a one-to-many relationship between a middle school student and his class. A student can only have one class, and a class can have multiple students. We can usually design two tables, table A for storing student information and table B for class information, and add classes to the student information to associate Table A:
Table B:
2.3 Many-to-many relationshipsFor example, there is a many-to-many relationship between college students and courses. A student may have multiple different courses, and a course may have many students to choose from. At this time, we design three tables, namely, table A for storing student information, table B for storing course information, and table C for connecting students with courses. Table A:
Table B:
C Table:
This is the end of this article about the constraints of MySQL database and the design principles of data tables. For more information about the constraints of MySQL database and data table design, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: HTML implements a fixed floating semi-transparent search box on mobile
>>: How to automatically import Vue components on demand
Automatic web page refresh: Add the following code...
Effect: CSS style: <style type="text/css&...
background Today, I was browsing CodePen and saw ...
Docker installation 1. Requirements: Linux kernel...
Hi, everyone; today is Double 12, have you done a...
1. Preparation 1.1 Download the tomcat compressed...
Today I will talk about a CSS special effect of h...
Note: There was no error in the project startup d...
Table of contents How to set cookies Disadvantage...
question: Recently, garbled data appeared when de...
1. Download Python 3 wget https://www.python.org/...
Use JS to implement object-oriented methods to ac...
Original link: https://vien.tech/article/138 Pref...
Windows: Support NTFS, FAT Linux supports file fo...
Table of contents 1. First, configure the main.js...