当前位置:网站首页>Constraints and design of database
Constraints and design of database
2022-07-27 06:26:00 【Envy only mandarin ducks, not immortals】
Catalog
1、 Export of Database Constraints
(1) Non empty constraint not null
Two 、 A brief introduction to the design of the table
3、 ... and 、 Copy operation of data table
(1) Copy table structure and data to new table
(2) Only copy table structure to new table
(3) Copy the data from the old table to the new table
(6) Aggregate functions and group by Comprehensive use of
One 、 Database Constraints
1、 Export of Database Constraints
There are certain restrictions on the amount of energy that can be added to the value of a column , This means is called restraint , Like the data sheet created before , There are no constraints on the attributes of the table

You can see it , When inserting data , This man has no name , But there are achievements , This set of data is actually unreasonable , Therefore, we have to restrict the inserted data at some time
2、 The type of constraint
(1) Non empty constraint not null
Specify that this attribute cannot be empty , Columns with non empty constraints are not inserted when inserting data , You must have a default value or display insertion


How to add a non empty constraint after creating a table ?
alter table Table name change Property name Property name Attribute types not null;
At this time, the corresponding attribute in the table must be modified null Values can be modified only if they are all deleted or changed to non empty , To view non empty constraints desc Table name ;

(2) Unique constraint
The unique constraint of a table can have multiple
Unique constraint means that the corresponding field is unique , Can't repeat ,null Not included , View unique constraints with show keys from
Table name ;


How to modify a field as a unique constraint after creating a table
alter table Table name add unique( Property name );

(3) Default constraint
Table specifying default values , If this column of data is not inserted when inserting , Just use the default values

(4) Primary key constraint
There is only one table · Primary key prinary key=unique + not null
The column value of the primary key constraint cannot be duplicate , Not for null, Primary key constraints can be composed of multiple columns , It is called a federated primary key


How to add a primary key after creating a table , The premise is that there is no primary key in the current table
alter table Table name add primary key( Property name );

Delete primary key
alter table Table name drop primary key;

Composite primary key , The primary key must also satisfy the attribute 1 And attribute 2
Add a composite primary key after creating the table
alter table Table name add primary key( Property name 1, Property name 2);
Creating means adding a composite primary key

Since the primary key auto_increment
Because the primary key is not duplicate and empty , Generally speaking, the primary key is int Or fixed length char type , We can hand over the growth of the primary key to the database for automatic execution
① Inserts that can be displayed by the auto increment primary key null Or don't write , Will trigger the auto increment operation

② About auto increment after deleting auto increment PK , The auto increment primary key is at the current maximum +1 Place continues to increase

③ The display inserts a value into the auto increment primary key

(5) Foreign key constraints
Foreign keys are used to associate primary keys or unique keys of other tables
foreign key ( Field name ) references Main table ( Column )

student In the table class_id Associated with the class Tabular id attribute ,student Table when inserting data , Must now class In the table id There is corresponding data in the column , Only when student Of class_id insert data

You can also find , When error data insertion occurs , The primary key will still be self incremented .
When deleting the main table , It must be ensured that all the corresponding data from the table are deleted , Data in the main table can be deleted .
Two 、 A brief introduction to the design of the table
1、 Three paradigms

First normal form : Make sure that each column is atomic ( When designing tables , Each column cannot be broken down again )
Second normal form : All attributes in the current table are related to the primary key
Third normal form : The attributes in the table are directly related to the primary key rather than indirectly
3、 ... and 、 Copy operation of data table
(1) Copy table structure and data to new table
create table New table select * from Old table ;

Copy the data from the old table to the new table , The structure of the two tables is exactly the same
insert into New table select * from Old table ;
(2) Only copy table structure to new table
create table New table select * from Old table where 1=2;

create table New table like Old table ;

(3) Copy the data from the old table to the new table
Suppose the data organization of the new table is the same as that of the old table
insert into New table select * from Old table ;

Suppose the data structure of the new table is different from that of the old table , The fields correspond to each other
insert into New table ( Field 1, Field 2,…….)SELECT Field 1, Field 2,…… FROM Old table
Four 、 Aggregate functions
1、 Common aggregate functions
Aggregation function refers to the aggregation of data between rows , Column independent

(2)count function

count(*) Full table scan , Count the rows of the whole table
count( Any value ) It's the same thing , But faster
for example : Count the number of rows in the current table


count( attribute );

(3) sum function
see emp The total monthly salary of all employees in the table , You can also use as names


(4)max and min function
View the maximum and minimum wages of all employees
(5)avg function
Find the average monthly salary of all employees

(6) Aggregate functions and group by Comprehensive use of
Count the average salary of each position

Query the maximum and minimum wages for each position


After grouping, you must use having, That is to say, aggregate functions are used before where, Aggregate function followed by hanving
Calculate the average salary of different positions , Keep the average salary greater than 400 The record of , The execution of the following sentence occurs after the aggregate function , First calculate the average of , And then keep it

Calculate the average salary of the three positions , Get rid of Monkey King's salary , Keep the average salary >400 The record of

边栏推荐
- 网络故障排查:用户VLAN下用户无法收到组播报文故障(IGMP Snooping)
- Wireshark IP address domain name resolution
- Unity hub login no response
- Pzk learns data types, binary conversion, input and output, operators, branch statements, ifelse of C language
- Navigation related messages
- 七大排序详解
- Unityshader Gaussian blur
- 英语基础知识:定语使用规则下篇
- ROS话题名称设置
- 多线程的相关知识
猜你喜欢

shell脚本之函数调用

Allow or prohibit connecting to a non domain and a domain network at the same time

Wireshark graphical interface capture

Strategies for common locks in multithreading

5g network identity - detailed explanation of 5g Guti

Chapter for software testing

Ram of IP core

Li Kou daily question leetcode 513. find the value in the lower left corner of the tree

Briefly remember the top ten orders

wireshark IP地址域名解析
随机推荐
英语基础知识:修饰性的句子成分-上篇
ROS node name duplicate
Common SQL optimization methods
This is my blog
Remote sensing image recognition - making data sets
Knowledge supplement of multithreading
Programming learning records - Lesson 8 [array and design Gobang, minesweeping game]
How to choose the correct server backup method
Pzk learns string function of C language (1)
Learning records of programming -- Lesson 2 [first knowledge of C language]
多线程的相关知识
Basic concepts of software testing
Learning the operation environment needs to be equipped during software testing
网络故障排查:用户VLAN下用户无法收到组播报文故障(IGMP Snooping)
Summary of Internet simple protocol
Database commands
机器人导航
Pzk learns data types, binary conversion, input and output, operators, branch statements, ifelse of C language
shell脚本之函数调用
Index and transaction of database (emphasis)

