1. Constraint concepts and classification1.1 The concept of constraints: Limit the data in the table to ensure the correctness, validity and integrity of the data 1.2 Constraint Classification
2. Adding and removing five constraints2.1 Six ways to add constraints1. Add a non-empty constraint when creating a table ( Added as a column-level constraint)
2. Add a non-null constraint when creating a table ( Added as table-level constraint)
3. When modifying the column name
4. When modifying the data type of a column
5. You can add constraints when adding new columns
6. Foreign key specific
2.2 Three ways to delete constraints1. No constraints when modifying column names
2. No constraints when modifying the data type of a column
3. Use drop to delete specific constraints
Note: View the names of the constraints in the table:
2.3 Add and delete methods corresponding to the five major constraints (serial numbers correspond to 2.1 and 2.2)1. Non-null constraint ( not null)
2. Unique constraint ( unique)
3. Default constraints ( default)
4. Primary Key ( primary key
5. Foreign Key ( foreign key)
Let's take a look at an employee table (the fields are employee number, name, age, department, and department location): We can find that there are redundant parts, that is, the R&D department corresponds to Guangzhou, and the sales department corresponds to Shenzhen. There is no need to write them so many times. In fact, they can be divided into two tables to prevent redundancy. The first table stores employee information, and the other stores department information table 1 (employee table employee): Table 2 (department table): The ids of both tables here are primary keys But there is a problem here, that is, we can delete any row in the department table, such as the row with id=1. Once deleted, the employees with dep_id=1 in table 1 will not be able to find their department information, so we can use foreign keys to solve this problem. We can specify that dep_id is a foreign key when creating the table, and associate it with the primary key id of department Statement format (although this is the second method, that is, adding a foreign key with table-level constraints, the syntax is different):
For this example, you can write:
After adding the foreign key, we cannot delete any row in the department table. Also, we cannot fill in dep_id with any number other than 1 or 2 when adding new employee information in employee. 2.4 Summary of creating constraintsWhen creating a table, adding column-level constraints only supports: default, not empty, primary key, unique. When adding table-level constraints only supports: primary key, unique, foreign key. 2.5 The difference between primary key and unique keyThere can be at most one primary key and multiple unique keys. Two columns are allowed to form a primary key and a unique key. 3. Auto-increment column3.1 ConceptIf a column is of numeric type, use auto_increment to achieve automatic value growth (usually used with the primary key) 3.2 Add a primary key constraint when creating a table and complete the example of primary key self-incrementFormat:
For example:
If we have a data in the table at this time; When we insert data again, we don't give the student number.
When I checked the table again, the student number was automatically added to our table based on the auto-increment value: 3.3 Adding and Removing Self-Growth1. Add (the above adding at the time of creation is one kind)
2. Delete
3.4 Setting the auto-increment step size The starting value of the auto-increment is 1, and the starting value of the step length is 1 The above is the detailed content of the MySQL learning of the five major constraints of database tables for beginners. For more information about the five major constraints of MySQl, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Practical operation of using any font in a web page with demonstration
>>: How to set focus on HTML elements
I like to pay attention to some news on weekdays a...
Table of contents Preface MySQL master-slave repl...
In Node.js, a .js file is a complete scope (modul...
Summary: Configure nginx reverse proxy jira and i...
Table of contents Preface Motivation for Fragment...
XHTML defines three document type declarations. T...
Without further ado, here are the renderings. The...
This article example shares the specific code for...
effect: The GROUP_CONCAT function can concatenate...
Win10 system locally installed MySQL8.0.20, perso...
Table of contents Preface 1. insert ignore into 2...
After pressing Enter on the webpage, the form is a...
Set Tomcat to automatically start the service: I ...
Deleting a table is not very common, especially f...
Copy code The code is as follows: <BODY> //...