MySQL基础入门
MySQL 基础入门笔记涵盖 DDL 数据定义、DML 数据操纵与 DQL 数据查询三大核心板块。一、连接 MySQL安装好 MySQL 后打开命令行终端输入以下命令连接数据库mysql-u root-p-u指定用户名root 是超级管理员-p表示需要输入密码连接成功后会出现mysql提示符。退出输入exit;二、数据库与表的基本操作DDLDDLData Definition Language用于定义和管理数据库结构库、表、字段。2.1 数据库操作操作SQL 语句说明查看所有库show databases;列出当前 MySQL 中所有数据库进入库use 数据库名;切换到指定数据库创建库create database 数据库名;新建一个数据库删除库drop database 数据库名;危险操作删除整个库2.2 表操作操作SQL 语句说明查看当前库所有表show tables;列出当前库下的所有表创建表create table 表名 (字段定义...);必须定义字段结构删除表drop table 表名;危险操作表和数据一起删除2.3 常用数据类型类型说明示例int整数int,int unsignedvarchar(n)变长字符串最多 n 个字符varchar(40)decimal(m,n)定点小数m 总长度n 小数位decimal(10,2)float/double浮点数不精确float2.4 字段约束约束说明primary key主键唯一标识一条记录unsigned无符号非负auto_increment自动递增常用于主键not null不允许为空default默认值2.5 建表综合示例createtabletest1(pidintunsignedprimarykeyauto_increment,pnamevarchar(40),pricedecimal(10,2),numint);字段说明字段含义类型与约束pid商品主键 id无符号整数自动递增pname商品名称最长 40 个字符price商品价格总长 10 位小数保留 2 位num商品库存数量整数三、数据增删改DMLDMLData Manipulation Language用于操作表中的数据插入、更新、删除。3.1 插入数据 insert向表中添加新记录insertinto表名(字段1,字段2,...)values(值1,值2,...);示例insertintoproducts(name,price,stock)values(平板电脑,2999.00,50);insertintocustomers(name,phone,city)values(小红,13999999999,杭州);3.2 更新数据 update修改表中已有的记录。一定要加 where 条件否则会更新整张表。update表名set字段1新值1,字段2新值2where条件;示例-- 将 id1 的商品价格改为 5499updateproductssetprice5499whereid1;-- 将无线鼠标的库存增加 50updateproductssetstockstock50wherename无线鼠标;危险操作提醒如果update不加where条件会修改表中所有行的该字段值3.3 删除数据 delete从表中删除记录。同样一定要加 where 条件deletefrom表名where条件;示例-- 删除 id10 的商品name 为 null 的那条deletefromproductswhereid10;-- 删除所有库存为 0 的商品谨慎deletefromproductswherestock0;** 危险操作提醒**delete from 表名不加 where 会清空整张表delete vs drop 区别delete删除数据表结构还在drop删除整张表结构和数据都没了四、数据查询DQLDQLData Query Language用于从表中查询数据核心是select语句。4.1 比较运算符运算符含义示例大于where num 10小于where num 10大于等于where num 10小于等于where num 10等于where num 10!或不等于where num ! 10select*fromtest.test1wherenum10;select*fromtest.test1wherenum!10;4.2 null 值判断注意null 不能使用或!判断必须用is null/is not null。select*fromtest.test1wherepnameisnull;select*fromtest.test1wherepnameisnotnull;4.3 范围查找运算符含义示例between x and y在[x, y]闭区间内where pid between 2 and 8not between x and y不在[x, y]区间内where pid not between 2 and 8select*fromtest.test1wherepidbetween2and8;select*fromtest.test1wherepidnotbetween2and8;4.4 集合查询-- 匹配集合内任意一个值select*fromtest.test1wherepidin(1,3,9);-- 不在集合内select*fromtest.test1wherepidnotin(1,3,9);4.5 模糊查询like通配符%代表任意数量的任意字符。模式含义示例x%以 x 开头where pname like 苹%%x以 x 结尾where pname like %机%x%包含 xwhere pname like %手%select*fromtest.test1wherepnamelike%手%;4.6 排序对查询结果按指定字段排序-- 升序asc 可省略默认升序select*fromtest.test2orderbynumasc;-- 降序desc 不可省略select*fromtest.test2orderbynumdesc;4.7 聚合函数对整张表的数据做统计计算每类只返回一条统计结果。函数作用示例max(字段)获取最大值select max(num) from test.test2;min(字段)获取最小值select min(num) from test.test2;avg(字段)计算平均值忽略 nullselect avg(num) from test.test2;sum(字段)计算总和select sum(num) from test.test2;count(字段)统计非空行数select count(num) from test.test2;count(*)统计总行数含 nullselect count(*) from test.test2;selectmax(num)as最大值fromtest.test2;selectmin(num)as最小值fromtest.test2;selectavg(num)as平均值fromtest.test2;selectsum(num)as总和fromtest.test2;selectcount(num)as非空数量fromtest.test2;selectcount(*)as总行数fromtest.test2;count(字段)vscount(*)区别count(字段)— 只统计该字段有值非 null的行count(*)— 统计全部行数哪怕某字段为 null 也会计入4.8 多表关联join关联两张及以上数据表通过匹配条件合并多表数据。三种关联方式对比关联类型关键字行为内关联inner join只保留匹配成功的数据两边不匹配的全部舍弃左关联left join左表数据全部保留右表无匹配补 null右关联right join右表数据全部保留左表无匹配补 null-- 内关联只展示能匹配上的数据select*fromtest.test1asainnerjointest.test2asbona.pidb.pid;-- 左关联左表 test1 全部保留select*fromtest.test1asaleftjointest.test2asbona.pidb.pid;-- 右关联右表 test2 全部保留select*fromtest.test1asarightjointest.test2asbona.pidb.pid;小提示as关键字用于给表起别名可以省略直接写test1 a。4.9 分组与过滤group by / having将查询数据按指定字段归类相同字段值的行合并为同一组。常搭配聚合函数实现分组维度的统计。group by— 按指定字段分组-- 统计每个城市的顾客数selectcity,count(*)as人数fromcustomersgroupbycity;having— 对分组后的结果做条件过滤。where在分组前筛选行having在分组后筛选组where无法替代having关键字过滤时机能否用聚合函数where分组前过滤行不能having分组后过滤组可以-- 筛选顾客数大于 1 的城市having 过滤组selectcity,count(*)as人数fromcustomersgroupbycityhavingcount(*)1;执行顺序where→group by→having→order by4.10 时间函数用于处理日期、时间类型的数据支持时间获取、字段提取、日期运算、差值计算等。常用时间数据类型类型说明示例date年月日2002-12-17time时分秒12:30:45datetime年月日时分秒2002-12-17 12:30:45获取当前时间selectnow();-- 当前日期时间2002-12-17 12:30:45selectsysdate();-- 当前日期时间同 nowselectcurdate();-- 当前日期2002-12-17selectcurrent_date;-- 同上selectcurtime();-- 当前时间12:30:45selectcurrent_time;-- 同上时间提取函数函数作用示例year(日期)提取年year(2002-12-17)→ 2002month(日期)提取月month(2002-12-17)→ 12day(日期)提取日day(2002-12-17)→ 17hour(时间)提取小时hour(12:30:45)→ 12minute(时间)提取分钟minute(12:30:45)→ 30second(时间)提取秒second(12:30:45)→ 45quarter(日期)提取季度1-4quarter(2002-12-17)→ 4week(日期)当年第几周week(2002-12-17)dayofyear(日期)当年第几天dayofyear(2002-12-17)selectyear(2002-12-17),month(2002-12-17),day(2002-12-17);selecthour(12:30:45),minute(12:30:45),second(12:30:45);selectquarter(2002-12-17),week(2002-12-17),dayofyear(2002-12-17);-- 业务示例查询订单的下单月份selectid,total,month(order_date)as下单月份fromorders;日期加减-- 增加date_add(基准日期, interval 数值 单位)selectdate_add(2002-12-17,interval1day);-- 2002-12-18selectdate_add(2002-12-17,interval1month);-- 2003-01-17selectdate_add(2002-12-17,interval1year);-- 2003-12-17-- 减少date_sub(基准日期, interval 数值 单位)selectdate_sub(2002-12-17,interval1day);-- 2002-12-16selectdate_sub(2002-12-17,interval1month);-- 2002-11-17selectdate_sub(2002-12-17,interval1year);-- 2001-12-17时间差计算-- 天数差datediff(前日期, 后日期)selectdatediff(2006-12-17,2002-12-17);-- 1461天-- 单位时间差timestampdiff(单位, 后日期, 前日期)selecttimestampdiff(year,2002-12-17,2006-12-17);-- 4年selecttimestampdiff(month,2002-12-17,2006-12-17);-- 48月selecttimestampdiff(day,2002-12-17,2006-12-17);-- 1461天格式化日期-- date_format(日期, 格式)%Y四位年 %m两位月 %d两位日selectdate_format(order_date,%Y年%m月%d日)as下单日期fromorders;4.11 子查询子查询是嵌套在 SQL 语句中的查询先执行内层结果给外层用-- 标量子查询查询价格高于平均价的商品select*fromproductswhereprice(selectavg(price)fromproducts);-- 子查询 in有下过单的顾客select*fromcustomerswhereidin(selectdistinctcustomer_idfromorders);五、总结分类核心操作关键字/语法连接登录/退出mysql -u root -p/exit;DDL库操作create database/drop database/use/show databasesDDL表操作create table/drop table/show tablesDML插入insert into ... values ...DML更新update ... set ... where ...DML删除delete from ... where ...DQL基础查询select ... from ... where ...DQL条件!is nullbetweeninlikeDQL排序order by ... asc/descDQL分组过滤group by ... having ...DQL聚合max()min()avg()sum()count()DQL日期时间函数now()year()month()date_add()datediff()等DQL子查询嵌套selectDQL多表inner joinleft joinright join ... on ...此为MySQL 基础篇适合零基础入门和基础复习。