当前位置:网站首页>Common commands of database 2
Common commands of database 2
2022-07-27 18:24:00 【zh_ Tnis】
1. Field null And not null.
- null: The expression field can be null
- not null: Indicates that the field cannot be null

2. auto number (auto_increment), And primary key (primary key) Use at the same time .
3. Primary key constraint .
Each table will have a primary key , And there is only one primary key , The primary key is the unique identifier of this table , It can be used to represent all the data in the table , The primary key cannot be repeated , And the primary key can be multiple fields , A primary key formed by multiple fields is called a federated primary key .
In general , The tables we create usually contain ID This type of field is set as primary key , Each table is on Java Corresponding to an entity class , The primary key is automatically a non empty constraint .
When we create a table , For example, the fields in the table are ID, And set it to auto_increment primary key, At this time, when we add records to the table , Don't add to the table by yourself ID The record of , So our SQL Data will be automatically added , The primary key value of each record is added (ID) Go down according to the record of the upper layer .

4. Unique constraint .
It is used to ensure the uniqueness of records in the table , It can not be a primary key , It can be for null, And each table can have multiple unique constraints .

5. Default constraint .
After adding the keyword of the default constraint to the field in the create table , If you go to the table insert Record , No non specified records are added to the fields of the default constraint , Then the system automatically adds a default record .

6. Foreign key constraints .
Create constraints on two tables , Keyword is forergn key. One is the father table , One is a sub table , They have the same search engine ,mysql The default storage engine for is Innodb. Foreign key column ( Sub table ) And reference columns must have the same index , If not, then mysql It will be indexed automatically .
Create the parent table :

Create child tables :

Add the record of the parent table field :

Add records of sub table fields :

In the picture above ,class A table is a reference column , That is, the parent table ,student The table is a foreign key column , That is, sub table . Which field id Is a keyword used as a foreign key constraint , Add to the sub table id by 1 perhaps 2, It represents the corresponding class in the student's parent table id, That is to say suyi、lisi It's a class 2-101 Of the students ,niuer、zhangsan yes 1-101 Of the students .
among class Corresponding student It's a many to one relationship (one to many),student Corresponding class It's a one-on-one relationship (one to one).
Be careful : When we want to delete records , Also consider whether the table you want to delete is related to other tables , If it exists, the records in the parent table cannot be deleted directly , First delete the associated records in the sub table , Then you can delete the records in the parent table .
- Constraints on a data column , Called column level constraint
- Constraints on multiple data columns , Table level constraints
- Column level constraints can be declared at the time of column definition , You can also declare... After the column definition , Table level constraints can only be declared after a column definition
7. Change the name of the created table .
rename table table_name to new_table_name;

8. Modify the structure of the table .
- Add a field after the existing field :alter table table_name add col_name not null after table_col_name;
- Add a field at the first position in the table :alter table table_name add col_name not null first table_col_name;

9. Add multiple columns .
- alter table table_name add (col_name1,col_name2);

10. Delete the fields in the table .
- alter table table_name drop table_col_name;

11. Delete multi column fields .
- alter table table_name drop table_col_name1,drop table_col_name2;

12. Add constraints to unconstrained table fields .
- alter table table_name add Constraint keywords (table_col_name);

13. Delete constraints for table fields with constraints .
- 1:alter table table_name drop index table_col_name;
- 2:alter table table_name drop Constraint keywords ;

14. Change the type of field .
alter table table_name MODIFY table_col_name Field type first;

Be careful : If the modified field type changes from large to small , And there is data in this field in the table , Then it may cause data loss .
15. Modify the name of the field in the table .
- alter table table_name change table_col_name new_col_name Field type ;

16. Update the new record of the field in the table .
- update table_name set table_col_name=table_col_name+ New record ;

17. Paging query limit.
- select * from table_name limit m,n;
- m: Subscript from the existing table as m The position begins
- n: Output n Bar record

边栏推荐
- Salesforce runs all test classes and gets coverage reports
- Six relationships of classes -- the difference between dependency and Association
- GIS数据漫谈(五)— 地理坐标系统
- 2. 改变颜色空间及颜色检测
- Hutool string utility class
- Deep learning: GCN diagram classification case
- MySQL solves the problem of insert failure caused by duplicate unique indexes
- Find redundant duplicate records in the table, delete and retain the minimum one
- 数据库的常用命令1
- Hackers use lasers to attack, and voice assistants can be activated from a distance of 100 meters
猜你喜欢

WPF makes login interface

Introduction to ef framework

Set up SSO based on SAML 2.0 in salesforce and enable JIT user provisioning (between SF orgs / between SF org and experience cloud / other IDPs)

二叉树概念

Deep learning: Gan optimization method dcgan case

2. Change color space and color detection

Six relationships of classes -- the difference between dependency and Association

深度学习:GCN(图卷积神经网络)理论学习总结
![[MIT 6.S081] Lab 10: mmap](/img/5d/a59a6f723518553b9232bc09991075.png)
[MIT 6.S081] Lab 10: mmap

What every Salesforce developer should know about Dates and Times in Apex
随机推荐
zabbix6.0的安装部署
Bug records using distributed framework WCF
Salesforce runs all test classes and gets coverage reports
Software installation related
华为Mate30 Pro 5G拆解:自研芯片占比过半,美系芯片依然存在!
Together with Samsung, vivo will promote exynos980 dual-mode 5g mobile phone!
Deep learning: installation package records
嘉楠耘智已完成预路演,预计11月20日登陆纳斯达克
Year end summary template
OEM "made in the United States", domestic security equipment has been installed on the U.S. aircraft carrier!
Guoju spent $1.8 billion to acquire its competitor KEMET, and the transaction may be completed in the second half of next year
超实用!阿里P9私藏的Kubernetes学习笔记,看完直呼NB
Deep learning - VIDEO behavior recognition: paper reading - two stream revolutionary networks for action recognition in videos
发布自己的npm组件库
动态链表4单向循环链表(LoopSingle实现)
【学习笔记】lombok的@Builder注解
深度学习:GAN案例练习-minst手写数字
The end of another era!
Set up SSO based on SAML 2.0 in salesforce and enable JIT user provisioning (between SF orgs / between SF org and experience cloud / other IDPs)
Buffer的只读模式