本文共 5008 字,大约阅读时间需要 16 分钟。
1. SQL 语句可以单行或多行书写,以分号结尾。
2. 可使用空格和缩进来增强语句的可读性。
3. MySQL 数据库的 SQL 语句不区分大小写,关键字建议使用大写。
4. 3 种注释
① 单行注释: – 注释内容 或 # 注释内容(mysql 特有)
② 多行注释: /* 注释 */
DDL语言:全面数据定义语言(Data Define Language),是用来定义和管理数据对象,如数据库,数据表等。DDL命令有CREATE(创建)、DROP(删除)、ALTER(修改)。
下面用代码给大家举例子:
-- SQL语法不区分大小写-- 每一句结束的时候都要用一个分号;# 库的操作-- 显示所有的库show databases;-- 创建一个库-- create database 库名;create database ku;-- 删除一个库-- drop database 库名;drop database ku;-- 使用库-- use 库名;use ku;# 表的操作-- 查看库中所有的表show tables;-- 建表create table 表名( 字段名 类型 属性, 字段名 类型 属性, .... 字段名 类型 属性);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
二、DML(数据操作语言)
DML语言:数据操作语言(Data Manipulation Language),是用于操作数据库对象中所包含的数据。DML命令有INSERT(增加)、DELETE(删除)、UPDATE(修改)。
下面用代码给大家举例子:
# 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("王桑");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 = '老王';-- 清空表truncate 表名truncate student;
三、DQL(数据查询语言)
DQL语言:数据查询语言(Data Query Language),是用于查询数据库数据。DQL命令有SELECT(查询)。
下面用代码给大家举例子:
# DQL-- 查询-- 查询表所有行和列的数据(得到的是一张虚拟表)-- select * from 表名;select * from student;-- 查询指定字段-- select 字段名1,字段名2... from 表名;select sid,sname,birthday,ssex,classid from student;-- 字段起别名-- select 旧字段名 as '新字段名';select sname as '姓名', birthday '生日',ssex 性别 from student;-- 去除重复 distinct-- select distinct 字段名... from 表名; select distinct ssex,classid,sid from student;-- 带条件的查询 WHERE 子句select * from student where ssex = '男' and classid = 1;-- 生日 大于 1990-1-1 的学生select * from student where birthday < '1990-1-1';-- 模糊查询 likeinsert 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;# 分组-- 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;
四、聚合函数
语法:之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个结果值。聚合函数会忽略空值 NULL。
– 统计个数 count(字段)/字段可以写*、常量、任意字段名/count不统计数据为null的个数。
– 统计平均值 avg(字段)
– 统计最大值 max(字段)
– 统计最小值 min(字段)
– 统计总和 sum(字段)
eg: select count(*) 总个数, sum(score) 总成绩, avg(score) 平均分, max(score) 最高分, min(score) 最低分 from sc;
# 聚合函数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() 不统计nullselect 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;
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
转载地址:http://npdfk.baihongyu.com/