Mysql数据库 | SQL语句解析『下篇』

个人主页:💗wei_shuo的个人主页
🏀 Hello World !🏀

文章目录
  • DML
    • 数据表记录的插入
    • 数据表记录的修改
    • 数据表记录的删除

  • DDL
    • alter 改
    • create 增
    • drop 删
  • TCL
  • DCL
  • 导入数据库
  • 数据处理函数
    • 单行处理函数
      • lower 转换小写
      • upper 转换大写
      • substr取子串
      • concat字符串拼接
      • length取长度
      • trim去空格
      • 首字母小写
      • str_to_date将字符串转化成日期
      • date_format格式化日期
      • format设置千分位
      • round四舍五入
      • rand()生成随机数
      • ifnull函数
      • case..when..then..when..then..else..end
  • 分组函数(多行处理函数)

DML

数据操作语言,凡是对数据进行增删改操作的语句都是DML

insert delete update

insert 增

delete 删

update 改

这个主要操作表中的data

数据表记录的插入

  • 插入完整记录

<code style="margin-left:0"><a href="https://www.mhzhuji.com/kwck/57"  class="lar_link lar_link_outgoing" data-linkid="219" data-postid="872"  title="数据库MySQL"  rel="nofollow" target="_blank" >mysql</a>> desc xs;
+----------+------------------+------+-----+---------+-------+
| Field    | Type             | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| 学号     | char(6)          | NO   | PRI | NULL    |       |
| 姓名     | char(8)          | NO   |     | NULL    |       |
| 专业名   | varchar(20)      | YES  |     | NULL    |       |
| 性别     | char(2)          | NO   |     | 男      |       |
| 出生日期 | date             | NO   |     | NULL    |       |
| 总学分   | tinyint unsigned | YES  |     | NULL    |       |
| 备注     | text             | YES  |     | NULL    |       |
+----------+------------------+------+-----+---------+-------+
7 rows in set (0.01 sec)

<a href="https://www.mhzhuji.com/kwck/57"  class="lar_link lar_link_outgoing" data-linkid="219" data-postid="872"  title="数据库MySQL"  rel="nofollow" target="_blank" >mysql</a>> insert into xs values('200201','张明','计算机应用技术','男','1998-8-5','9','学习委员');
Query OK, 1 row affected (0.01 sec)</code>
  • 插入数据就录的一部分

<code style="margin-left:0"><a href="https://www.mhzhuji.com/kwck/57"  class="lar_link lar_link_outgoing" data-linkid="219" data-postid="872"  title="数据库MySQL"  rel="nofollow" target="_blank" >mysql</a>> insert into xs(学号,姓名,性别,出生日期) values('200326','唐辉阳','男','2003-2-6');
Query OK, 1 row affected (0.01 sec)</code>

插入多行数据

<code style="margin-left:0">mysql> insert into xs(学号,姓名,性别,出生日期)
    -> values
    -> ('200336','李昌城','男','2003-6-3'),
    -> ('200327','徐斌','男','2003-3-5'),
    -> ('200308','郑明鑫','男','2003-4-5');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0</code>

数据表记录的修改

<code style="margin-left:0">mysql> update xs
    -> set 姓名='魏硕' where 学号='200201';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0</code>

数据表记录的删除

  • 使用delete语句删除数据

<code style="margin-left:0">mysql> delete from xs
    -> where 姓名='高远';
Query OK, 1 row affected (0.01 sec)</code>
  • 使用truncate语句删除数据
    (清空数据记录)

<code style="margin-left:0">mysql> select * from sss;
+-------+-------+---------+------+--------+
| uid   | uname | ugender | uedu | upor   |
+-------+-------+---------+------+--------+
| 11111 | tang  | n       | bk   |        |
| 22222 | zhang | n       | bk   | 副教授 |
+-------+-------+---------+------+--------+
2 rows in set (0.00 sec)

mysql> truncate sss;
Query OK, 0 rows affected (0.04 sec)

mysql> select * from sss;
Empty set (0.01 sec)</code>

DDL

数据定义语言,凡是带create,drop,alter的都是DDL

DDL主要操作的是表的结构,不是表中的数据

create 增

drop 删

alter 改

这个增删改和DML不同,主要是对表的结构进行操作

alter 改

  • 修改数据表名

<code style="margin-left:0">alter table cj rename 成绩表;</code>
  • 修改字段名和数据类型

语法格式:

<code style="margin-left:0">     alter  table  表名  change  旧字段名  新字段名  新数据类型;</code>

<code style="margin-left:0">alter table 信息管理学生表 change 专业名 zym varchar(15);</code>
  • 修改字段的数据类型

语法格式:

<code style="margin-left:0">     alter  table  表名  modify  字段名  新数据类型;</code>

<code style="margin-left:0">alter table 信息管理学生表 modify zy varchar(20);</code>
  • 添加字段

语法格式:

<code style="margin-left:0">     alter  table 表名  add  新字段名 新数据类型  [约束条件]  [first | after 已经存在的字段名];</code>

<code style="margin-left:0">alter table 信息管理学生表 add 年龄 varchar(3);</code>
  • 删除字段

语法格式:

<code style="margin-left:0">    alter  table  表名  drop  字段名;</code>

<code style="margin-left:0">alter  table  xs  drop  备注;</code>

create 增

  • 根据查询创建数据表

<code style="margin-left:0">create table 软件技术 select 学号,姓名,性别,出生日期,总学分,备注 from xs where 专业名='软件技术';</code>

drop 删

  • 删除表

<code style="margin-left:0">create table zyrs like xs;
drop table zyrs;</code>

TCL

事务控制语言

包括:

<code style="margin-left:0"> 事务提交:commit;

 事务回滚:rollback;</code>

DCL

数据控制语言

例如:授权grant、撤销权限revoke……

导入数据库

<code style="margin-left:0">mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| cjgl               |
| cjgl1              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tang               |
+--------------------+
7 rows in set (0.00 sec)

mysql> use cjgl1;
Database changed
mysql> source D:\Program Files\QQ files\cjgl-bak.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.04 sec)

Query OK, 0 rows affected (0.04 sec)

Query OK, 0 rows affected (0.05 sec)

Query OK, 0 rows affected (0.05 sec)

Query OK, 29 rows affected (0.05 sec)
Records: 29  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.06 sec)

Query OK, 0 rows affected (0.06 sec)

Query OK, 0 rows affected (0.06 sec)

Query OK, 0 rows affected (0.06 sec)

Query OK, 0 rows affected (0.06 sec)</code>

数据处理函数

数据处理函数又被称为单行处理函数

单行处理函数的特点:一个输入对应一个输出

和单行处理函数相对的是:多行处理函数(多行处理函数的特点:多个输入,对应一个输出!)

单行处理函数

  • lower 转换小写

<code style="margin-left:0">mysql> select lower(课程名) 课程名,开课学期 from kc where 课程号=212;
+--------------+----------+
| 课程名       | 开课学期 |
+--------------+----------+
| oracle<a href="https://www.mhzhuji.com/kwck/57"  class="lar_link lar_link_outgoing" data-linkid="219" data-postid="872"  title="数据库MySQL"  rel="nofollow" target="_blank" >数据库</a> |        2 |
+--------------+----------+
1 row in set (0.01 sec)

mysql> select 课程名,开课学期 from kc where 课程号=212;
+--------------+----------+
| 课程名       | 开课学期 |
+--------------+----------+
| ORACLE数据库 |        2 |
+--------------+----------+
1 row in set (0.00 sec)</code>
  • upper 转换大写

<code style="margin-left:0">mysql> select upper(课程名) 课程名,开课学期 from kc;
+--------------+----------+
| 课程名       | 开课学期 |
+--------------+----------+
| 计算机基础   |        1 |
| C语言        |        1 |
| 高等数学     |        3 |
| 数据结构     |        5 |
| 操作系统     |        6 |
| 计算机组装   |        4 |
| ORACLE数据库 |        2 |
| 计算机网络   |        1 |
| 软件工程     |        7 |
+--------------+----------+
9 rows in set (0.01 sec)

mysql> select 课程名,开课学期 from kc;
+--------------+----------+
| 课程名       | 开课学期 |
+--------------+----------+
| 计算机基础   |        1 |
| c语言        |        1 |
| 高等数学     |        3 |
| 数据结构     |        5 |
| 操作系统     |        6 |
| 计算机组装   |        4 |
| ORACLE数据库 |        2 |
| 计算机网络   |        1 |
| 软件工程     |        7 |
+--------------+----------+
9 rows in set (0.00 sec)</code>
  • substr取子串

<code style="margin-left:0">#模糊查询课程号中第二位为零的行
mysql> select 课程号 from kc where substr(课程号,2,1)='0';
+--------+                           从课程号中第2位开始,取1位
| 课程号 |
+--------+
| 102    |
| 209    |
| 208    |
| 101    |
| 301    |
| 302    |
| 206    |
+--------+
7 rows in set (0.00 sec)</code>
  • concat字符串拼接

<code style="margin-left:0">mysql> select concat('Hello','World') result;
+------------+
| result     |
+------------+
| HelloWorld |
+------------+
1 row in set (0.00 sec)</code>
  • length取长度

<code style="margin-left:0">mysql> select length('hhgjfgks') as result;
+--------+
| result |
+--------+
|      8 |
+--------+
1 row in set (0.00 sec)</code>
  • trim去空格

<code style="margin-left:0">mysql> select trim('   hkhhukhhyiyy  ') result;
+--------------+
| result       |
+--------------+
| hkhhukhhyiyy |
+--------------+
1 row in set (0.00 sec)</code>
  • 首字母小写

<code style="margin-left:0">mysql> select concat(lower(substr(课程名,1,1)),substr(课程名,2,length(课程名)-1)) as result from kc where 课程号='212';
+--------------+
| result       |
+--------------+
| oRACLE数据库 |
+--------------+
1 row in set (0.00 sec)

mysql> select 课程名 from kc where 课程号='212';
+--------------+
| 课程名       |
+--------------+
| ORACLE数据库 |
+--------------+
1 row in set (0.00 sec)</code>
  • str_to_date将字符串转化成日期
  • date_format格式化日期
  • format设置千分位
  • round四舍五入

<code style="margin-left:0">mysql> select round(3.6) as result;
+--------+
| result |
+--------+
|      4 |
+--------+
1 row in set (0.00 sec)
mysql> select round(3.764,2) as result;  //四舍五入保留两位小数
+--------+
| result |
+--------+
|   3.76 |
+--------+
1 row in set (0.00 sec)</code>
  • rand()生成随机数

<code style="margin-left:0">mysql> select round(rand(),1) 随机数 from kc;//生成0到1之间的随机数,保留一位小数
+--------+
| 随机数 |
+--------+
|    0.9 |
|      0 |
|    0.4 |
|    0.8 |
|    0.8 |
|    0.6 |
|    0.5 |
|    0.8 |
|    0.6 |
+--------+
9 rows in set (0.00 sec)
mysql> select round(rand()*100,0) result from kc;  //100以内随机数
+--------+
| result |
+--------+
|      6 |
|     76 |
|     60 |
|     76 |
|     97 |
|     58 |
|     98 |
|     19 |
|     98 |
+--------+
9 rows in set (0.00 sec)</code>
  • ifnull函数

可以将null转化成一个具体的值

注意:

null如果参与运算,最终结果一定是null。为了避免这一现象,需要ifnull函数。

ifnull函数用法:

ifnull(数据,被当做那个值)

如果“数据”为null是,那么这个数据结构当做那个值

<code style="margin-left:0">mysql> select 6 + ifnull(备注,8)  result from xs;
+--------+
| result |
+--------+
|     14 |
|     14 |
|     14 |
|     14 |
|     14 |
|     14 |
|     14 |
|     14 |
|      6 |
|     14 |
|      6 |
|     14 |
|      6 |
|     14 |
|     14 |
|     14 |
|     14 |
|     14 |
|     14 |
|     14 |
+--------+
20 rows in set (0.00 sec)</code>
  • case…when…then…when…then…else…end

<code style="margin-left:0">mysql> select 开课学期,学分 from kc;
+----------+------+
| 开课学期 | 学分 |
+----------+------+
|        1 |    3 |
|        1 |    3 |
|        3 |    3 |
|        5 |    6 |
|        6 |    3 |
|        4 |    4 |
|        2 |    5 |
|        1 |    4 |
|        7 |    2 |
+----------+------+
9 rows in set (0.00 sec)
mysql> select  开课学期,(case 开课学期 when '1' then 学分+1 when '2' then 学分+2 else 学分 end) new from kc;
+----------+------+
| 开课学期 | new  |
+----------+------+
|        1 |    4 |
|        1 |    4 |
|        3 |    3 |
|        5 |    6 |
|        6 |    3 |
|        4 |    4 |
|        2 |    7 |
|        1 |    5 |
|        7 |    2 |
+----------+------+
9 rows in set (0.00 sec)</code>

分组函数(多行处理函数)

多行处理函数的特点:输入多行,最终输出一行

count 计数

sum 求和

avg 平均值

max 最大值

min 最小值

未经允许不得转载:木盒主机 » Mysql数据库 | SQL语句解析『下篇』

赞 (0)

相关推荐

    暂无内容!