• MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法


    花了将近一周的时间学习了数据库的基本用法,今天总结一下。

    一、一些感悟

    数据库的定义:数据库是“按照数据结构来组织、存储和管理数据的仓库”。是一个长期存储在计算机内的、有组织的、可共享的、统一管理的大量数据的集合。

    人类文明的象征之一,就是人类会高效的记录、传递、使用信息,其实不止人类,自然界的很多动植物也会传递信息,例如动物的嚎叫、气味,植物的信息素等等,都是传递信息的方式。但人类不同于动植物的信息使用的方式就是,人类发明了可以长久保留信息的方法–文字。

    而自十九世纪麦克斯韦发现电磁波以来,人类传播信息的速度发生了前所未有的改变,而到1946年,英国剑桥大学数学实验室的莫里斯·威尔克斯教授和他的团队以EDVAC为蓝本,设计和建造EDSAC,1949年5月6日正式运行,是世界上第一台实际运行的存储程序式电子计算机。

    这样,电子时代的“纸”、“笔”、“信鸽”,都具备了,新时代的信息大门经过一代代人的努力,终于向全人类敞开了。

    二、数据库中的语言

    总的来说数据库的定义就是“按照数据结构来组织、存储和管理数据的仓库”。是一个长期存储在计算机内的、有组织的、可共享的、统一管理的大量数据的集合。

    而我学习的mysql是众多数据库中的一种,就初学者而言,还挺好用的。

    1.分类

    数据库的语句分为四大种,分别是:

    (1)DDL(数据定义语言)

    定义和管理数据对象:如数据库,数据表等命令:CREATE、DROP、ALTER

    DDL命令主要负责数据库的创建、删除、修改

    (2)DML(数据操作语言)

    用于操作数据库对象中所包含的数据命令:INSERT、UPDATE、DELETE

    主要负责数据库中数据表中内容的新增、修改、删除,注意区别和DDL的区别

    (3)DQL(数据查询语言)

    用于查询数据库数据命令:SELECT

    (4)DCL(数据控制语言)

    用来管理数据库的语言,包括管理权限及数据更改命令:GRANT、COMMIT、ROLLBACK

    今天重点来整理归纳一下DDL、DML、DQL三种语言

    2.DDL语句操作数据库

    注意:SQL语法不区分大小写

    –每一句结束的时候都要用一个分号;

    #显示所有的库
    show databases;
    
    #创建一个库
    --create database库名;
    
    create database haha;
    
    #删除一个库
    --drop database库名;
    
    drop database haha;
    
    #使用库
    --use库名;
    
    use haha;
    
    
    # 查看库中所有的表
    show tables;
    
    #建表
    create table表名(
    
    字段名类型属性,
    
    字段名类型属性,
    
    ......
    
    字段名类型属性
    
    );
    
    use haha;
    
    create table tab_teacher(
    
    tea_name varchar(10),
    
    tea_sex char(1),
    
    tea_birthday datetime,
    
    tea_money decimal(20,1)
    
    );
    
    show tables;
    
    #查看表结构
    desc tab_teacher;
    
    show create table tab_teacher;
    
    --`反引号让关键词失效
    
    CREATE TABLE`tab_teacher`(
    
    `tea_name`varchar(10)DEFAULT NULL,
    
    `tea_sex`char(1)DEFAULT NULL,
    
    `tea_birthday`datetime DEFAULT NULL,
    
    `tea_money`decimal(20,1)DEFAULT NULL
    
    )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
    
    COLLATE=utf8mb4_0900_ai_ci
    
    #改变字段的属性
    --alter table表名modify字段名要修改的属性;
    alter table tab_teacher modify tea_money decimal(3,2);
    --alter table表名change旧的字段名新的字段名要修改的属性;
    alter table tab_teacher change tea_money gongzi decimal(30,2);
    #添加字段
    --alter table表名add字段名类型属性;
    alter table tab_teacher add tea_address varchar(200)default'中国';
    #删除字段(危险操作)
    --alter table表名drop字段名;
    alter table tab_teacher drop tea_birthday;
    #修改表名
    --alter table旧表名rename as新表名;
    alter table tab_teacher rename as t;
    #删除表(危险操作)
    drop table t;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    3.DML语句操作数据库

    #新增
    --语法
    --insert into表名(字段名,字段名...字段名)values(值,值...值);
    --日期用字符串的形式表示
    insert into student(sid,sname,birthday,ssex,classid)
    values(9,'张三','1999-1-1','男',3);
    insert into student(sid,ssex,classid)values(11,'男',2);
    --让主键自增
    insert into student(sname)values("s张三");
    insert into student values(default,'罗老师','1970-6-1','男',2);
    insert into student values(null,'法外狂徒','1970-6-1','男',2);
    --一次性插入多条数据
    insert into student(sname,ssex)
    values('辛弃疾','男'),('陆游','男'),('李清照','女');
    --不常用的新增方式
    --表都要存在
    create table stu1(
    	xingming varchar(10),
    	ssex varchar(2)
    )
    --insert into select
    insert into stu1 select sname,ssex from student
    --新建表的时候插入数据
    --新表不能存在
    create table newstu select sname,birthday,ssex from student
    #修改
    --语法
    --update表名set字段名=值,字段名=值...where子句
    update stu1 set xingming='赵雷'
    update newstu set ssex='女'where sname='李清照'
    --范围
    update student set ssex='女',classid=10 where sid>=10 and sid<=15;
    --between小数据and大数据
    update student set ssex='呵呵',classid=20 where sid between 10 and 15;
    #删除
    --delete from表名where子句
    delete from stu1;
    delete from student where sname='老薛'
    delete from student
    --清空表
    truncate表名
    truncate student;
    --delete truncate drop区别
    --mysql8新特性
    CREATE TABLE jsltab(
    id INT PRIMARY KEY AUTO_INCREMENT,
    a INT,
    b INT,
    c INT GENERATED ALWAYS AS(a+b)VIRTUAL
    )
    insert into jsltab(a,b)values(100,200)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    4.DQL语句操作数据库

    #查询
    select * from student;
    select sid,sname,birthday,ssex,classid from student;
    
    select sname,ssex from student;
    
    -- 字段起别名
    
    select sname as '姓名', 
    	birthday '生日',ssex 性别 from student
    
    
    -- 去除重复 distinct 
    select distinct ssex,classid,sid from student;
    
    
    -- 带条件的查询  WHERE 子句
    
    select * from student 
    	where ssex = '男' and classid = 1
    
    -- sid 是在 3-7范围内的学生
    
    -- 生日 大于 1990-1-1 的学生
    select * from student where birthday < '1990-1-1';
    
    -- 模糊查询 like
    insert into student(sname) 
    values('陈与义'),('义'),('陈义');
    
    -- 查询李字有关的数据
    -- 模糊符号 % 任意多的任意字符
    select * from student where sname like '%李%';
    -- 姓薛的人
    select * from student where sname like '李%';
    
    -- 模糊符号_ 一个任意字符
    select * from student where sname like '李__';
    
    
    -- 学生编号是 2,5,6,8,9,20,300,4000
    -- in 在特定的范围内查找
    select * from student  
    	where sid in (2,5,6,8,9,20,300,4000)
    
    
    -- 没有生日的学生  is 是对null的判断
    select * from student where birthday is null;
    
    select * from student where birthday is not null;
    
    
    #聚合函数
    
    count(字段) -- 统计个数
    
    -- 数字
    avg(字段) -- 平均值
    sum(字段) -- 总和
    max(字段) -- 最大值
    min(字段) -- 最小值
    
    -- count 统计个数
    
    select count(*) from student where ssex = '男'
    
    select count(sname) from student where ssex = '男'
    
    select count(1) from student where ssex = '男'
    
    select count('a') from student where ssex = '男'
    
    -- count() 不统计null
    select count(birthday) from student;
    
    
    -- avg 平均值
    -- 所有学生的平均分
    select avg(score) from sc;
    
    -- sum 总成绩
    select sum(score) from sc;
    
    
    select count(*) 次数, sum(score) 总成绩, 
    		avg(score) 平均分, max(score) 最高分,
    		min(score) 最低分 from sc 
    
    
    #分组
     group by 字段
    -- 
    select count(1) from student where ssex = '男'
    
    select count(1) from student where ssex = '女'
    
    
    select ssex,count(sid) from student group by ssex
    
    -- 每个班有多少学生
    select classid,count(sid) from student group by classid
    
    -- sc  每个学生的平均分
    
    select sid,avg(score) 平均分, sum(score) 总成绩,
    	max(score) 最高分, min(score) 最低分, count(*) 次数
     from sc group by sid;
    
    -- where 是对聚合(分组)前的每一条数据的筛选
    -- having 是对聚合(分组)后的每一条数据的筛选
    -- having 不能单独出现,必须要有group by
    select sid,avg(score) from sc
     group by sid having avg(score) > 80
    
    -- 
    select sid,avg(score) 
    from sc where score < 60 group by sid
    
    -- 排序 order by 
    -- 升序 ASC
    -- 降序 DESC
    select * from student order by sid 
    -- 多排序规则按照 order by 后面的字段 先写先排后写后排
    select * from sc order by score desc, sid desc
    
    
    # 分页 limit
    
    -- 一页看3个学生
    -- 												(页码-1)*步长,步长
    select * from student limit 6, 3
    
    
    -- 成绩表中及格的成绩的总成绩是第二名的学生编号和平均分
    
    select sid,avg(score)	from sc 
    where score >= 60 group by sid 
    order by sum(score) desc limit 1,1
    
    
    #多表联查
    -- 非等值联查
    -- 笛卡尔积
    select * from student, class
    
    -- 等值联查
    -- 内联数据
    select *
    from student, class 
    where student.classid = class.classid
    
    -- 学过张三老师课程的学生
    
    select student.* from student,sc,course,teacher 
    where student.sid = sc.sid and sc.cid = course.cid 
    and course.tid = teacher.tid and tname = '张三'
    
    
    -- inner join  on
    select * from student 
    inner join class 
    		on student.classid = class.classid
    inner join sc 
    		on student.sid = sc.sid
    inner join course 
    		on course.cid = sc.cid
    
    
    #外联查询
    -- 	left join  on  左外联
    select * from student left join class 
    on student.classid = class.classid
    
    -- 	right join on  右外联
    select * from class right join student 
    on student.classid = class.classid
    
    
    -- 
    select * from student left join class 
    on student.classid = class.classid
    where class.classid is null
    union
    select * from student right join class 
    on student.classid = class.classid
    where student.sid is null
    
    
    -- 学校中所有人的名字
    union 会去除重复的数据(DISTINCT 一致)
    union all 不会去重
    
    select sname ,ssex 性别,classid 其他 from student
    union 
    select tname 名字,tsex,tmoney from teacher
    
    
    
    select * from student left join class 
    on student.classid = class.classid
    union all
    select * from student right join class 
    on student.classid = class.classid
    #子查询
    -- where 子查询
    
    -- 查询id最大的一个学生
    select * from student order by sid desc limit 1;
    
    -- 查询id最大的一个学生(子查询)
    select * from student
     where sid = (select max(sid) from student)
    
    -- 每个班id最大的学生编号
    select * from student where sid in(
    select max(sid) from student group by classid);
    
    -- 没学过张三老师课程的学生
    select * from student where sid not in(
    select student.sid from student 
    left join sc on student.sid = sc.sid
    left join course on sc.cid = course.cid 
    left join teacher on course.tid = teacher.tid 
    where tname = '张三')
    
    
    -- from 子查询
    
    -- 查询大于5人的班级名称和人数
    
    select classname,count(*) from student 
    left join class on student.classid = class.classid
    group by class.classid 
    
    -- from 子查询
    select classname,a 人数 from class 
    left join 
    	(select classid, count(*) a 
    	from student group by classid)t1
    on class.classid = t1.classid
    
    
    -- 每个班的平均成绩,班级名称和平均成绩
    
    select classname, avg(score) from class 
    left join student on class.classid = student.classid
    left join sc on student.sid = sc.sid
    group by class.classid
    
    select * from class 
    left join (
    	select classid,avg(score) from sc 
    		left join student on sc.sid = student.sid
    group by classid 
    )t1 on class.classid = t1.classid
    
    
    -- 
    select * from teacher where exists (
    select * from student where ssex = '你猜猜')
    
    --  any/some  all 
    
    -- 查询出一班成绩比二班最低成绩高的学生
    select *  from student 
    inner join sc on student.sid = sc.sid
    where classid = 1 and score > (
    select min(score) from student 
    inner join sc on student.sid = sc.sid
    where classid = 2
    )
    
    
    select score from student 
    inner join sc on student.sid = sc.sid
    where classid = 2
    
    
    
    select *  from student 
    inner join sc on student.sid = sc.sid
    where classid = 1  and (score > 70.0 or score > 60.0 or 
    score > 80.0 or score > 50.0 or score > 30 or score > 20 
    or score > 31 or score > 34)
    
    
    select *  from student 
    inner join sc on student.sid = sc.sid
    where classid = 1  and  score > any(select score from student 
    inner join sc on student.sid = sc.sid
    where classid = 2)
    
    -- 查询出一班成绩比二班最高成绩高的学生
    select *  from student 
    inner join sc on student.sid = sc.sid
    where classid = 1 and score > (
    select max(score) from student 
    inner join sc on student.sid = sc.sid
    where classid = 2
    )
    
    select *  from student 
    inner join sc on student.sid = sc.sid
    where classid = 1  and (score > 70.0 and 
    score > 60.0 and 
    score > 80.0 and score > 50.0 and 
    score > 30 and score > 20 
    and score > 31 and score > 34)
    
    
    
    select *  from student 
    inner join sc on student.sid = sc.sid
    where classid = 1  and  score > all(select score from student 
    inner join sc on student.sid = sc.sid
    where classid = 2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316

    三、结语

    接下来以及目前我在学用java调用数据库进行操作,学完这段时间就可以做项目了。怎么说呢,学的我想下去陪我的祖师爷。哥们大学专业学的是信息与计算科学,祖师爷叫克劳德·艾尔伍德·香农,是信息论创始人。

    如果是计算机专业的祖师爷是冯 ·诺依曼和图灵的话,那我的祖师爷应该就是克劳德·香农先生了,他是一位奇才,精通数学与电气工程,然后创立了信息论,所以有了我们这个专业,有机会想专门为他写一篇,敬请期待。他提出的信息熵的概念,在现如今的很多领域都有应用,间接的推动了 计算机科学的革命。艾伦·图灵、冯·诺依曼和_克劳德·香农_无疑是_计算机_科学与技术的三位奠基人,说起来我以后要从事的程序员这个职业,也离不开祖师爷的贡献。

    后面要学的东西还很多,想想就头疼,这段时间状态有点不对,得调整一下。

    心有所向,日复一日,必有精进。

  • 相关阅读:
    【21天学习挑战赛—经典算法】希尔排序
    电力系统IEEE14节点系统同步模型(Simulink)
    ubuntu16.04下thrift安装及遇到的问题
    PyQT5 普通按钮互斥选中
    ThingsBoard教程(二三):在规则链中计算二个设备的温度差
    NodeJS技巧:在循环中管理异步函数的执行次数
    Java网络编程
    重写muduo网络库开发环境和前期准备
    Splashtop 的卓越安全性获得 ISO 27001 认证
    Linux下自动删除过期备份和自动异地备份的脚本
  • 原文地址:https://blog.csdn.net/m0_67390788/article/details/126037186