当前位置:网站首页>数据库的常用命令2
数据库的常用命令2
2022-07-27 16:08:00 【zh_Tnis】
1.字段的null与not null。
- null:表示字段可以为null
- not null:表示字段不可为null

2.自动编号(auto_increment),与主键(primary key)同时使用。
3.主键约束。
每张表都会存在主键,并且有且仅有一个主键,主键是这张表唯一标识,可以用来代表表中的所有数据,主键不可以重复,并且主键可以是多个字段,多个字段形成的主键称为联合主键。
一般情况下,我们创建的表中通常将ID这类型的字段设置为主键,每张表在Java对应一个实体类,主键自动是非空约束。
当我们创建表时,例如表中存在的字段为ID,并且将其设置为auto_increment primary key,这时候当我们往该表中添加记录时,不往表中自主加入ID的记录,那么我们的SQL会自动为其添加数据,每次添加记录的主键值(ID)按上层的记录往下走。

4.唯一约束。
用于保证表中的记录的唯一性,它可以不是主键,它可以是为null,并且每张表可以存在多个唯一约束。

5.默认约束。
在创建表中为字段加入默认约束的关键字后,如果往表中insert入记录,没有往默认约束的字段添加非指定的记录,那么系统自动为其添加默认的记录。

6.外键约束。
建立两张表上的约束,关键字为forergn key。一个为父表,一个为子表,它们具备着相同的搜索引擎,mysql的默认存储引擎是Innodb。外键列(子表)和参照列都必须存在相同的索引,如果没有的话那么mysql会自动为其创建索引。
创建父表:

创建子表:

添加父表字段的记录:

添加子表字段的记录:

上面的图示中,class表是参照列,也就是父表,student表是外键列,也就是子表。其中字段id是作为外键约束的关键字,往子表中添加入的id为1或者2,代表的是该学生的父表中对应哪一个班级的id,也就是suyi、lisi是班级2-101的学生,niuer、zhangsan是1-101的学生。
其中class对应student是多对一关系(one to many),student对应class是一对一关系(one to one)。
注意:当我们想删除记录的时候,还要考虑想删除的表与其他表是否存在联系,如果存在那么就不能直接删除父表中的记录,要先将子表中相关联的记录删除了,然后才能删除父表中的记录。
- 对一个数据列建立的约束,称列级约束
- 对多个数据列建立的约束,称表级约束
- 列级约束既可以在列定义时声明,也可以在列定义后声明,表级约束只能在列定义后声明
7.对已创建的表名进行更改。
rename table table_name to new_table_name;

8.修改表的结构。
- 在已有的字段后添加字段:alter table table_name add col_name not null after table_col_name;
- 在表中的第一个位置添加字段:alter table table_name add col_name not null first table_col_name;

9.添加多列。
- alter table table_name add (col_name1,col_name2);

10.删除表中的字段。
- alter table table_name drop table_col_name;

11.删除多列字段。
- alter table table_name drop table_col_name1,drop table_col_name2;

12.为无约束的表字段添加约束。
- alter table table_name add 约束关键字 (table_col_name);

13.给存在约束的表字段删除约束。
- 1:alter table table_name drop index table_col_name;
- 2:alter table table_name drop 约束关键字;

14.修改字段的类型。
alter table table_name MODIFY table_col_name 字段类型 first;

注意:如果修改的字段类型由大往小,并且表中的该字段存在数据时,那么就有可能造成数据的丢失。
15.修改表中字段的名称。
- alter table table_name change table_col_name new_col_name 字段类型;

16.更新表中字段的新记录。
- update table_name set table_col_name=table_col_name+新记录;

17.分页查询limit。
- select * from table_name limit m,n;
- m:从已有的表中下标为m的位置开始
- n:输出n条记录

边栏推荐
- 施耐德电气、欧莱雅等企业巨头如何开放式创新?DEMO WORLD世界创新峰会揭秘
- IDEA打包war包与war包位置
- What's the use of games| Game application value research case collection
- [learning notes] solutions to hot account problems
- 2022 safety officer-c certificate special operation certificate examination question bank and answers
- 图形界面编程
- Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps
- In the first week of June, risk control of e-shield business paid attention to 15 institutions such as New Oriental XRS, which were fined
- 查看端口PID及结束进程
- Es query limit 10000 data solutions
猜你喜欢

In the fourth week of July, Yidun business risk control focused on the supreme law to regulate app's forcible request for personal information

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

Application of knowing things and learning | correlation graph analysis in anti cheating business

VSS tip: search all checked out files (search checked out files according to users)

Code compliance: five reasons why developers use helix QAC
What are the safety risks of small games?
知物由学 | SO加固如何提升Android应用的安全性?
How to improve the security of Android applications?

解决Reids不能被其他IP访问

Dbeaver connection MySQL error: the server time zone value 'Ö Ð¹ ú±ê ×¼ ʱ ¼ ä‘ is unrecognized or represents more than
随机推荐
Code compliance: five reasons why developers use helix QAC
用slmgr命令激活正版Win7旗舰版系统
Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks
Salesforce File Share and Security
golang 等待一组goroutine完成,并带返回值(2)
Golang customize once. When error occurs, reset it for the second time
Localization within Communities
How to solve the error of ora-00955 when Oracle modifies the primary key
2022 safety officer-a certificate examination questions and online simulation examination
国巨斥资18亿美元收购竞争对手Kemet,交易或在明年下半年完成
Convolutional neural network -- SSD thesis translation
2022 safety officer-c certificate special operation certificate examination question bank and answers
多线程实现循环
Golang waits for a group of goroutines to complete with the return value (2)
Hutool digital computing
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)
ts学习笔记-interface
[learning notes] Lombok's @builder annotation
Error launching IDEA
面试常见问题一二