MySQL8.0对数据库增删改查的基本操作

MySQL8.0对数据库增删改查的基本操作

文章目录

前言

MySQL 中我们最常用的增删改查,对应SQL语句就是 insert 、delete、update、select,这种操作数据的语句,又叫Data Manipulation Statements(数据操作语句)。

一、对默认数据库的说明

先登录数据库然后执行show命令,查看数据库的命令是 show databases

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> 

从查询出来的表可以看到,一共有三个表,都是系统默认创建

1.MySQL数据库

MySQL 系统自带的核心数据库,它存储了MySQL的用户账户和权限信息,一些存储过程、事件的定 义信息,一些运行过程中产生的日志信息,一些帮助信息以及时区信息等

1.1.information_schema数据库

information_schema也是MySQL系统自带的数据库,主要保存MySQL数据库服务器的系统信息,比如数据库的名称、数据表的名称、字段名称、存取权限、数据文件、所在的文件夹和系统使用的文件夹,还包括有哪些表,哪些视图,哪些触发器,列,索引。

这些信息并不是真实的用户数据,而是一些 描述性信息,有时候也称之为 元数据 。在系统数据库 info rmation_schema 中提供了一些以
innodb_sys 开头的表,用于表示内部系统表。

1.2.performance_schema 库

performance [pəˈfɔ:məns] 性能

"performance_schema"是MySQL系统自带的数据库,主要保存MySQL服务器运行过程中的一些状态信息,可以用来监控MySQL的各类性能指标。包括统计最近执行了哪些语句,在执行过程的每个阶段都 花费了多长时间,内存的使用情况等信息。

这个库是从 MySQL5.5之后开始增加的。

1.3.sys 库

这个库是MySQL5.7版本之后增加的。

"sys"数据库是MySQL系统自带的数据库,主要作用是通过视图的形式把information_schema和performance_schema结合起来以一种更容易被理解的方式展示MySQL数据库服务器的各类性能指标,帮助系统管理和开发人员监控 MySQL的技术性能。

2.SQL语句

DDL语句 数据定义语言,用来管理表格

create 创建

alter 修改

drop 删除

创建表   create table 表名(字段名1 数据类型 约束条件,字段名2 数据类型 约束条件,...);
 
create table student(id int(20),name char(40),age int);
 
添加,修改,删除表字段
 
删除字段 alter table 表名 drop 字段;
alter table student drop age;
 
查看表结构 show columns from 表名;
show columns from student;
 
添加字段 alter table 表名 add 字段 数据类型 约束条件;
alter table student add age int not null;
 
修改字段 alter table 表名 modify字段名 数据类型 约束条件;
alter table student modify age int null;
 
删除表   drop table 表名;
drop table student;

DML语句 数据操纵语言,用来管理数据

insert 添加/插入

update 修改

delete 删除

select 查询

添加数据   insert into 表名 values (字段值,字段值,字段值...);
insert into student values (1,’张三’,18); 
添加多行数据   insert into 表名 values (字段值,字段值,字段值...),(字段值,字段值,字段值...),(字段值,字段值,字段值...);
 
修改数据   update 表名 set 字段=新值 where 条件
update student set id=2 where name=’张三’;
 
删除数据   delete from student where 条件
delete from student where id=1

DCL语句 数据控制语言,用来管理权限

授权
grant 权限 on 库.表 to '用户' @'IP' identified by '密码';

取消授权
revoke 权限 on 库.表 from '用户' @'IP';

查看某个的用户权限
use mysql;
select * from user where user='用户名称'\G;

查看所有用户权限
select * from user;

二. MySQL数据库的操作命令

1.查看当前有哪些数据库 show databases;

语法:show databases;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

1:information_schema这数据库保存了MySQL服务器所有数据库的信息。如数据库名,数据库的表,表栏的数据类型不访问权限等。

2:performance_schema 这是MySQL5.5新增的一个性能优化的引擎:命名PERFORMANCE_SCHEMA
#主要用于收集数据库服务器性能参数。MySQL用户是不能创建存储引擎为PERFORMANCE_SCHEMA的表
3:mysql库是系统库,里面保存有账户信息,权限信息等。

1.1.以行的方式显示

show databases \G #以行的方式显示

mysql> show databases \G;
*************************** 1. row ***************************
Database: information_schema
*************************** 2. row ***************************
Database: mysql
*************************** 3. row ***************************
Database: performance_schema
*************************** 4. row ***************************
Database: sys
4 rows in set (0.00 sec)

ERROR: 
No query specified	#未指定查询

2.创建数据库 create

创建数据库注意事项:

  • 1.在文件系统中,MySQL的数据存储区将以目录方式表示MySQL数据库。因此,上面命令中的数据库名字必须与操作系统的约束的目录名字一致。例如不允许文件和目录名中有,/,:,*,?,”,<,>,|这些符号,在MySQL数据库名字中这些字母会被自动删除。<遵从目录的约束>
  • 2.数据库的名字不能超过64个字符,包含特殊字符的名字或者是全部由数字或保留字组成的名字必须用单引号``包起来。
  • 3.数据库不能重名。
语法:create database 数据库名;
mysql> create database student;
Query OK, 1 row affected (0.00 sec)

mysql> create database lilinbo;
Query OK, 1 row affected (0.01 sec)

mysql> create database xinghai;
Query OK, 1 row affected (0.06 sec)

数据库名字也是有自己的规范的,比如当数据库有横线的时候会报错:

mysql> create database lilinbo-grow;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-' at line 1

对于这种因为名字出现的错误,我们可以使用 反引号来解决:

mysql> create database `lilinbo-grow`;
Query OK, 1 row affected (0.01 sec)

反引号是为了区分MYSQL的保留字与普通字符而引入的符号。

比如create是一个保留字:

mysql> create database `create`;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| create             |
| information_schema |
| lilinbo            |
| lilinbo-grow       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

如果不用反引号,MYSQL将把create视为保留字而导致出错,所以,有MYSQL保留字作为字段的,必须加上反引号来区分。

引号一般用在字段的值,如果字段值是字符或字符串,则要加引号,如:select=‘字段值’

不加反引号建的数据库或表不能包含MYSQL保留字,否则出错。

Linux中一切皆为文件,那创建的lilinbo数据库在哪里呢?

[root@localhost ~]# find / -name lilinbo
/usr/local/mysql/data/lilinbo

可以看到这个目录下都是我们创建的数据库和系统创建的数据库:
在这里插入图片描述
暂时我们先知道创建的文件都在这里就可以了,后面看看这些表结构和数据。

查看创建表执行了哪些命令:

mysql> show create database student \G;	#查看创建库执行了哪些命令
*************************** 1. row ***************************
       Database: student
Create Database: CREATE DATABASE `student` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */
1 row in set (0.00 sec)

ERROR: 
No query specified

3.选中某个数据库 use

我们要选择某个数据库,可以使用use语句,use语句会选择一个数据库成为当前的数据库,之后的操作就在选中的数据库里操作。

语法:use 数据库名;
mysql> use lilinbo;	#切换到某个库
Database changed
mysql> show tables;
Empty set (0.00 sec)

可以使用select查看当前所在的位置:

mysql> select database();
+------------+
| database() |
+------------+
| student    |
+------------+
1 row in set (0.00 sec)

mysql> select database();
+------------+
| database() |
+------------+
| lilinbo    |
+------------+
1 row in set (0.00 sec)

如果什么数据库也没有选择,默认显示的是NULL,Null意味着没有选择数据库;

4.删除数据库 drop

删除数据一定要慎重, 这里我们可以使用drop命令删除数据库,比如刚刚的mufeng-grow,我们来把它删除:

语法:drop database 数据库名;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| create             |
| information_schema |
| lilinbo            |
| lilinbo-grow       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> drop database lilinbo;
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| create             |
| information_schema |
| lilinbo-grow       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

可以看到删除的时候没有任何提示,直接就删除了,所以删除数据库一定要慎重!

5.修改数据库名字

MySQL 之前提供了一个 rename database db_old to db_new 的命令来直接对数据库改名,可能由于实现的功能不完备(比如,这条命令可能是一个超大的事务,或者是由于之前的表很多还是 MyISAM 等),后来的版本直接取消了这条命令。

1.mysqldump导入导出的过程中改名

用 mysqldump 工具,在旧库导出再往新库导入(最原始、最慢、最容易想到)的方法:旧库 yttdb_old 导出(包含的对象:表、视图、触发器、事件、存储过程、存储函数)

这种方法适合数据量小的时候用。

2.改整库的表名

利用 MySQL 更改表名的方法来批量把旧库的所有表依次遍历,改名为新库的表。

可以写个脚本批量改,适合数据量大的时候用。

三. 其它操作方法

1.刚刚登录MySQL的时候查看自己默认所在的位置

mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

mysql> 

此时默认为空;

2.在命令行选择默认的数据库

[root@localhost ~]# mysql -uroot -p123456 lilinbo-grow #指定数据库名
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select database();#登陆后,就已经在默认数据库里了
+--------------+
| database()   |
+--------------+
| lilinbo-grow |
+--------------+
1 row in set (0.00 sec)

在这里插入图片描述

四、数据库表的增删改查

1.创建数据库表 create

从MySQL8.0开始,全是innoDB表

语法:create tables 表名

语法:create table 表名 (字段名 类型, 字段名 类型, 字段名 类型); #在当前数据库中创建数据表

mysql> create table xinghai(id int );	#int代表整数类型
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+-------------------+
| Tables_in_xinghai |
+-------------------+
| xinghai           |
+-------------------+
1 row in set (0.00 sec)

#创建表,写入数据
mysql> create table test(id int,名字 char(10),年龄 int,性别 char(1));
Query OK, 0 rows affected (0.01 sec)

mysql> desc test;
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id     | int(11)  | YES  |     | NULL    |       |
| 名字   | char(10) | YES  |     | NULL    |       |
| 年龄   | int(11)  | YES  |     | NULL    |       |
| 性别   | char(1)  | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

2.显示当前或指定数据库中指定数据表的结构(字段)信息 desc

语法:desc 表格名 #显示当前或指定数据库中指定数据表的结构(字段)信息

mysql> desc xinghai;	#显示当前或指定数据库中指定数据表的结构(字段)信息
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.05 sec)

3.修改数据表的名称 alter

语法:alter table 表名 rename 新表名; #修改数据表的名称

语法:alter table 表名 drop 字段; #删除字段

语法:alter table 表名 change 原字段名 新字段名 新字段类型; #修改字段名

语法:alter table 表名 modify 要修改的字段名 要修改的类型; #修改字段的类型

语法:alter table 表名 add 字段名 字段类型; #在表中增加字段
mysql> alter table students add sex enum(‘M’,‘W’);

mysql> alter table xinghai rename to xxhf;
Query OK, 0 rows affected (0.04 sec)

+-------------------+
| Tables_in_xinghai |
+-------------------+
| xxhf              |
+-------------------+
1 row in set (0.00 sec)

mysql> desc xxhf;;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> alter table xxhf rename xinghai;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+-------------------+
| Tables_in_xinghai |
+-------------------+
| xinghai           |
+-------------------+
1 row in set (0.00 sec)

4.向数据表中插入新的记录 insert

语法:insert into 表名(字段1,字段2,……) values(字段1的值, 字段2的值,……); #向数据表中插入新的记录

#创建表,写入数据
mysql> create table test(id int,名字 char(10),年龄 int,性别 char(1));
Query OK, 0 rows affected (0.01 sec)

mysql> desc test;
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id     | int(11)  | YES  |     | NULL    |       |
| 名字   | char(10) | YES  |     | NULL    |       |
| 年龄   | int(11)  | YES  |     | NULL    |       |
| 性别   | char(1)  | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

#插入数据
mysql> insert into test values (1,'张三',18,'男');
Query OK, 1 row affected (0.00 sec)

#只给id和名字插入数据
mysql> insert into test(id,名字) values(2,'李四');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |   NULL | NULL   |
+------+--------+--------+--------+
2 rows in set (0.00 sec)

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |   NULL | NULL   |
|    3 | NULL   |   NULL | NULL   |
|    4 | NULL   |   NULL | NULL   |
|    5 | NULL   |   NULL | NULL   |
+------+--------+--------+--------+
5 rows in set (0.00 sec)

mysql> insert into test(id,年龄) values (3,4),(4,5),(5,6);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |   NULL | NULL   |
|    3 | NULL   |   NULL | NULL   |
|    4 | NULL   |   NULL | NULL   |
|    5 | NULL   |   NULL | NULL   |
|    6 | NULL   |   NULL | NULL   |
|    3 | NULL   |      4 | NULL   |
|    4 | NULL   |      5 | NULL   |
|    5 | NULL   |      6 | NULL   |
+------+--------+--------+--------+
9 rows in set (0.00 sec)

5.删除表格内容 delete

语法:delete from 表名; #将当前数据库表中记录清空

语法:delete from 表名 where 条件表达式; #删除一部分数据

语法:delete from 表名 where 条件1 and 条件2; #删除满足条件1和条件2的数据

#删除一部分数据
mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |   NULL | NULL   |
|    3 | NULL   |   NULL | NULL   |
|    4 | NULL   |   NULL | NULL   |
|    5 | NULL   |   NULL | NULL   |
|    6 | NULL   |   NULL | NULL   |
|    3 | NULL   |      4 | NULL   |
|    4 | NULL   |      5 | NULL   |
|    5 | NULL   |      6 | NULL   |
+------+--------+--------+--------+
9 rows in set (0.00 sec)

mysql> delete from test where 年龄=4;
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |   NULL | NULL   |
|    3 | NULL   |   NULL | NULL   |
|    4 | NULL   |   NULL | NULL   |
|    5 | NULL   |   NULL | NULL   |
|    6 | NULL   |   NULL | NULL   |
|    4 | NULL   |      5 | NULL   |
|    5 | NULL   |      6 | NULL   |
+------+--------+--------+--------+
8 rows in set (0.00 sec)

#删除满足条件的行
mysql> delete from test where id=4 and 年龄=5;
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |   NULL | NULL   |
|    3 | NULL   |   NULL | NULL   |
|    4 | NULL   |   NULL | NULL   |
|    5 | NULL   |   NULL | NULL   |
|    6 | NULL   |   NULL | NULL   |
|    5 | NULL   |      6 | NULL   |
+------+--------+--------+--------+
7 rows in set (0.00 sec)

6.修改、更新数据表中的记录 update

语法:update 表名 set 字段名=新数据 where 条件表达式; #修改、更新数据表中的记录

mysql> update test set id=7 where 年龄=6;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |   NULL | NULL   |
|    3 | NULL   |   NULL | NULL   |
|    4 | NULL   |   NULL | NULL   |
|    5 | NULL   |   NULL | NULL   |
|    6 | NULL   |   NULL | NULL   |
|    7 | NULL   |      6 | NULL   |
+------+--------+--------+--------+
7 rows in set (0.01 sec)

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |     20 | 男     |
|    3 | 老王   |     21 | 男     |
|    4 | 小莉   |   NULL | NULL   |
+------+--------+--------+--------+
4 rows in set (0.00 sec)

mysql> update test set 年龄=20,性别='女' where id=4 and 名字='小莉';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |     20 | 男     |
|    3 | 老王   |     21 | 男     |
|    4 | 小莉   |     20 | 女     |
+------+--------+--------+--------+
4 rows in set (0.00 sec)

7.数据表中查找符合条件的记录 select

语法:select * from 表名*

语法:select 字段名1,字段名2……from 表名 where 条件表达式; #从数据表中查找符合条件的记录

mysql> select * from test;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
|    2 | 李四   |   NULL | NULL   |
|    3 | NULL   |   NULL | NULL   |
|    4 | NULL   |   NULL | NULL   |
|    5 | NULL   |   NULL | NULL   |
|    6 | NULL   |   NULL | NULL   |
|    7 | NULL   |      6 | NULL   |
+------+--------+--------+--------+
7 rows in set (0.00 sec)

mysql> select id,年龄 from test;
+------+--------+
| id   | 年龄   |
+------+--------+
|    1 |     18 |
|    2 |   NULL |
|    3 |   NULL |
|    4 |   NULL |
|    5 |   NULL |
|    6 |   NULL |
|    7 |      6 |
+------+--------+
7 rows in set (0.00 sec)

mysql> select id=7 from test where 年龄=6;
+------+
| id=7 |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> select * from test where id>3;
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    4 | NULL   |   NULL | NULL   |
|    5 | NULL   |   NULL | NULL   |
|    6 | NULL   |   NULL | NULL   |
|    7 | NULL   |      6 | NULL   |
+------+--------+--------+--------+
4 rows in set (0.00 sec)

mysql> select * from test where 性别='男';
+------+--------+--------+--------+
| id   | 名字   | 年龄   | 性别   |
+------+--------+--------+--------+
|    1 | 张三   |     18 | 男     |
+------+--------+--------+--------+
1 row in set (0.00 sec)

8.在数据表中删除指定范围的记录 between…and…

语法:between…and… #在数据表中删除指定范围的记录

五、MySQL数据类型

数值型:整数型,浮点数(小数型)

日期时间型:年,年月日,时分秒,年月日时分秒

字符串型:文本类型字符串,二进制类型字符串

1.数值类型

在这里插入图片描述

1.1.整数型

整数型 + -

类型 存储需求 有符号取值范围 无符号取值范围 备注
tinyint 很小的整数 1字节 -128~127 0~255 2的八次方
smallint 小的整数 2字节 -32768~32767 0~65535 2的十六次方
mediumint 中等大小 3字节 -8399608~8399607 0~2^24-1 2的二十四次方
int 普通的 4字节 -2147483648~2147483647 0~2^32-1 2的三十二次方
bigint 超大的 8字节 0~2^64-1 2的六十四次方

案例1:

mysql> create table test2 (id tinyint);
#int(2)中(2)表示显示宽度,也就是显示几位数,但是并不严格,即使写入的数值超过了这个宽度只要值不超过数据类型的取值范围就可以正常写入和显示
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-------------------+
| Tables_in_xinghai |
+-------------------+
| test              |
| test2             |
| xinghai           |
+-------------------+
3 rows in set (0.00 sec)

mysql> insert into test2 values (127);
Query OK, 1 row affected (0.00 sec)
#id的数值是2的八次方范围:-128~127

mysql> desc test2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | tinyint(4) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

案例2:

mysql> create table test3 (a tinyint,b smallint,c mediuminnt,d int,e bigint);
Query OK, 0 rows affected (0.01 sec)

mysql> desc test3;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| a     | tinyint(4)   | YES  |     | NULL    |       |
| b     | smallint(6)  | YES  |     | NULL    |       |
| c     | mediumint(9) | YES  |     | NULL    |       |
| d     | int(11)      | YES  |     | NULL    |       |
| e     | bigint(20)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
#括号内的数字解释
()代表里边数据的长度,按照位数计算长度(比如127是三位数),可以修改长度
(4)含义:里边数据的长度最多能写四位,不能超出这个长度
(6)含义:最多能写六位,不能超出这个长度

1.2.小数型(浮点数)

float

float(x,y):单精度浮点数,占4字节存储,x为精度表示整数和小数总的位数,y为标度表示可以有几位小数,x的有效位是6~7

案例1:
mysql> create table t3(a float(5,2));#5代表数字整个的长度(包括小数点后的数字),第二位代表小数的长度,第一个要比第二个大
Query OK, 0 rows affected (0.00 sec)

mysql> desc t3;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| a     | float(5,2) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t3 values(111.222);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t3 values(333.4444);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t3 values(555.666);
Query OK, 1 row affected (0.01 sec)

mysql> select * from t3;
+--------+
| a      |
+--------+
| 111.22 |
| 333.44 |
| 555.67 |
+--------+
3 rows in set (0.00 sec)

mysql> create table t4(id float (6.3));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t4 values(11.23456);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 values(3456.7890);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 values(22222.1111);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 values (112323.45678);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t4;
+---------+
| id      |
+---------+
| 11.2346 |
| 3456.79 |
| 22222.1 |
|  112323 |
+---------+
4 rows in set (0.00 sec)

2.字符串类型

在这里插入图片描述
文本类型字符串:受字符集限制,对于uft8字符集来说,数字,字母,符号都占1字节,汉字占3字节,unicode字符集定义字符都占两字节

char(M):固定长度文本字符串,M表示显示宽度,表示最多可以显示M个字符,M的取值范围是0-255,如果写入的字符个数小于M的值,那么会以空格进行填充至M的数量,查询时会把填充的空格删除在显示,如果写入的字符个数大于M,那么多出的部分会被截取

varchar(M):不定长度文本字符串,M是显示宽度,表示最多可以显示M个字符,M取值范围是1-21844,如果写入的字符个数小于M那么不会进行空格的补充,如果写入的字符个数大于M那么多出的字符会被截取,或者报错,varchar会额外占用1~2字节的大小去记录写入的实际的字符长度,字符个数<=255,使用1字节,否则使用2字节

类型 支持存储的大小 备注
tinytext 非常小的文本字符串 最大支持255字节 2^8-1
text 小的文本字符串 最大支持65535字节 2^16-1
mediumtext 中等大小的文本字符串 最大支持2^24-1字节
longtext 大的文本字符串 最大支持2^32-1字节

案例1:

mysql> create table t5(name char(5),n varchar(5));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t5 values ('1234a','123ab');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t5;
+-------+-------+
| name  | n     |
+-------+-------+
| 1234a | 123ab |
+-------+-------+
1 row in set (0.00 sec)

3.日期时间型

在这里插入图片描述

3.1.year:年

格式一:以四位字符串格式表示,范围’1901’-‘2155’

格式二:以四位数字表示,范围1901-2155

格式三:以两位字符串格式表示,范围’00’-‘99’

‘00’-‘69’表示’2000’-‘2069’

‘70’-‘99’表示’1970’-‘1999’

格式四:以两位数字表示,范围00-99

00-69表示2000-2069

70-99表示1970-1999

案例1:

mysql> create table t6 (a year);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t6 values ('2022'),(2022),('20');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t6;
+------+
| a    |
+------+
| 2022 |
| 2022 |
| 2020 |
+------+
3 rows in set (0.00 sec)

3.2.time:时间

格式一:‘HH:MM:SS’,HH表示小时,MM表示分钟,SS表示秒钟

格式二:‘HHMMSS’

格式三:‘D HH:MM:SS’,D表示天数,会计算成对应的小时与HH的值相加,D取值范围为-34~34

时间的取值范围为-838:59:59-838:59:59

案例:

mysql> create table t7(a time);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t7 values ('20:22:26'),('202226'),('122:22:26');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t7;
+----------+
| a        |
+----------+
| 20:22:26 |
| 20:22:26 |
| 46:22:26 |
+----------+
3 rows in set (0.00 sec)

3.3.date:日期

格式一:‘YYYY-MM-DD’,YYYY表示年,MM表示月份,DD表示天,取值范围’1000-01-01~9999-12-03’

格式二:‘YY-MM-DD’,YY表示年,MM表示月,DD表示天,YY取值范围’00’-‘99’,‘00’-‘69’表示’2000’-‘2069’,‘70’-‘99’表示’1970’-‘1999’

格式三:YYMMDD

案例:

mysql> create table t9(a date);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t9 values('2023-03-22'),('23-03-22'),(3230322);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t9;
+------------+
| a          |
+------------+
| 2023-03-22 |
| 2023-03-22 |
| 2023-03-22 |
+------------+
3 rows in set (0.00 sec)

3.4.datetime:日期与时间

格式一:‘YYYY-MM-DD HH:MM:SS’,取值范围:‘1010-01-01 00:00:00’~‘9999-12:31 99:99:99’

格式二:‘YY-MM-DD HH:MM:SS’,YY取值范围’00’-‘99’,‘00’-‘69’表示’2000’-‘2069’,‘70’-‘99’表示’1970’-‘1999’

格式三:‘YYYYMMDDHHMMSS’,‘YYMMDDHHMMSS’

案例:

mysql> create table t8(a datetime);
Query OK, 0 rows affected (0.06 sec)

mysql> insert into t8 values('2023-03-22 22:15:55'),('23-03-22 22:15:55'),(20230322221555);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t8;
+---------------------+
| a                   |
+---------------------+
| 2023-03-22 22:15:55 |
| 2023-03-22 22:15:55 |
| 2023-03-22 22:15:55 |
+---------------------+
3 rows in set (0.00 sec)

六、MySQL约束条件

在这里插入图片描述
在这里插入图片描述

1.非空约束 not null

非空约束:not null 表示某列不能存储NULL值

如果指定某个字段具有非空约束条件,那么该字段的值不能为空

格式:

字段 数据类型 not null

案列:创建带有非空约束的表

mysql> create table 非空(id int,name varchar(10) not null));
Query OK, 0 rows affected (0.04 sec)

mysql> desc 非空;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(10) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into 非空 values (1,'a');
Query OK, 1 row affected (0.00 sec)

mysql> insert into 非空(name) values ('b');
Query OK, 1 row affected (0.00 sec)

mysql> select * from 非空;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
| NULL | b    |
+------+------+
2 rows in set (0.00 sec)

2.唯一性约束 unique

unique:要求必须是唯一的,但null除外.

如果字段设置了唯一性约束条件,那么该字段的值必须唯一。

格式:在数据类型后添加字段 数据类型 unique所有字段定义完成后添加 unique(字段)

案例:

mysql> create table 唯一 (id int unique,name varchar(10));;
Query OK, 0 rows affected (0.01 sec)

mysql> desc 唯一;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | UNI | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into 唯一 values(1,'a');
Query OK, 1 row affected (0.00 sec)

mysql> insert into 唯一 values(2,'b');
Query OK, 1 row affected (0.00 sec)

mysql> select * from  唯一;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
|    2 | b    |
+------+------+
2 rows in set (0.00 sec)

3.主键约束 primary key

主键约束=not null + unique(非空且唯一),确保某列(或两个列多列的结合)有唯一标识,有助于更容易更快速地找到表中地一个特定的记录。

如果字段设置了主键约束,那么该字段的值不能为空且值唯一,可以定义联合主键。

格式:直接添加到数据类型之后,字段 数据类型 primary key 定义完所有字段添加,#如果要定义联合主键,那么需要使用该格式创建。

案例:创建带有主键约束的表

mysql> create table 主键约束 (id int primary key,name varchar(10) unique,score int not null);
Query OK, 0 rows affected (0.01 sec)

mysql> desc 主键约束;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(10) | YES  | UNI | NULL    |       |
| score | int(11)     | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

4.外键约束 foregin key

用来建立两张数据表之间的联系,一张表可以对一个字段设置外键,也可以对多个字段设置外键,添加外键的表示属于子表,跟存在外键的表有联系的表属于父表,要求子表与父表之间存在关系的字段的数据类型必须一致,且父表的字段得是主键。

外键约束:保证一个表中的数据匹配另一个表中的值的参照完整性。

格式:constraint 外键约束名 foreign key(子表字段) references 父表名 主键

案例:创建带有外键约束的表


猜你喜欢

转载自blog.csdn.net/m0_67159981/article/details/130132102