一文轻松学会mysql数据库:ddl操作库表和dml语句增删改查表数据,全文特别详细,安装mysql数据请自行搜索,或者搜索我的头条文章有,,建议收藏+关注。不迷路,用的时候找得到。^_^
没有数据库之前,如果我们要进行数据存储,有几种方式:
我们可以使用java集合等方式将数据保存在内存中,但是数据不能持久化保存,断电/程序退出,数据就清除了我们还可以将数据保存在普通文件中,可以持久化保存,但是查找,增加,修改,删除数据比较麻烦,效率低所以我们需要一个既可以持久化保存数据又可以方便操作的地方来存储数据,这就是我们接下来要给大家介绍的数据库
数据库(databa,db):指长期保存在计算机的存储设备(硬盘)上,按照一定规则组织起来,可以被各种用户或应用共享的数据集合. 还是以文件的方式存在服务器的电脑上的。
说白了就是数据的仓库, 用来保存数据的.
数据库管理程序(dbms)可以管理多个数据库,一般开发人员会针对每一个应用创建一个数据库。为保存应用中实体的数据,一般会在数据库创建多个表,以保存程序中实体的数据。
数据库管理系统、数据库和表的关系如图所示:
我们把sql介绍完成了, 那下面就通过ddl操作数据库
create databa 数据库名 [character t 字符集][collate 校对规则] 注: []意思是可选的意思
字符集(chart):是一套符号和编码。
练习创建一个数据库(默认字符集)
create databa web16db;
创建一个day16_2的数据库,指定字符集为gbk(了解)
create databa web14_2 character t gbk;
show databas;
show create databa 数据库名;
查看web14_1这个数据库的定义show create databa web14_1;
drop databa 数据库名;
删除web14_2数据库drop databa web14_2;
alter databa 数据库名 character t 字符集;
修改web14_1这个数据库的字符集(gbk)alter databa web14_1 character t gbk;
注意:
是utf8,不是utf-8不是修改数据库名u 数据库名; //注意: 在创建表之前一定要指定数据库. u 数据库名
练习: 使用web14_1u web14_1;
查看正在使用的数据库lect databa();
我们第四章已经把数据库的crud讲解完了,下面我们就学习创建表
create table 表( 列 类型 [约束], 列 类型 [约束] ... );
约束种类:红眼病传染途径
not null: 非空 ; eg: urname varchar(40) not null urname这个列不能有null值unique:唯一约束, 后面的数据不能和前面重复; eg: cardno char(18) unique; cardno 列里面不可以有重复数据primary key;主键约束(非空+唯一); 一般用在表的id列上面. 一张表基本上都有id列的, id列作为唯一标识的 auto_increment: ==自动增长,必须是设置了primary key之后,才可以使用auto_increment==id int primary key auto_increment; id不需要我们自己维护了, 插入数据的时候直接插入null, 自动的增长进行填充进去, 避免重复了.注意:
先设置了primary key 再能设置auto_increment
只有当设置了auto_increment 才可以插入null 自己维护 否则插入null会报错
id列:
给id设置为int类型, 添加主键约束, 自动增长或者给id设置为字符串类型,添加主键约束, 不能设置自动增长create table student( id int primary key auto_increment, `name` varchar(40) not null, x int);
create table 表名( 列 类型 【约束】, 列 类型 【约束】);
类型数值:int bigint double 偶尔使用tinyint字符:char(n) 固定长度 varchar(n) 可变长度时间:date datetime timestamp约束not null 非空unique 唯一primary key 主键(非空+唯一)auto_increment 自动增长iid可以设置成int类型, 设置成primary key, 添加auto_increment 作为记录唯一标识注意:列名:见名识义 多个单词用_拼接列类型:tinyint int bigint double char varchar datetime timestamp列约束: not null:不能为空 primary key:主键,限制表中的每一行都是唯一的,主键所在的列不能为空也不可以重复 unique key:唯一约束 限制当前列可以为null(只能出现一个),但是不能重复 primary key == unique key + not null列属性: auto_increment:自增 用在主键列上,并且该列为整型 zerofill:零填充 用在整型类型列上 unsigned:无符号,最高位不表示正负 1.auto_increment需要和primary key 一起使用 2.unsigned、zerofill不能和primary key一起使用
我们把表创建好了, 下面就来介绍查看表
show tables;
desc student;
我们表创建好了, 如果要增加一列,要删除一列呢? 那下面就来讲解修改表
alter table student add grade varchar(20) not null;
给学生表的x字段改成字符串类型alter table student modify x varchar(10);
给学生表的grade字段修改成class字段alter table student change grade class varchar(20);
把class字段删除alter table student drop class;
把学生表修改成老师表(了解)rename table student to teacher
表创建好了, 我们还可以删除。 掌握表的删除
drop table teacher;
drop table 表名;
创建表create table tb_name(create table product( pid int primary key auto_increment, //只有设置了auto_increment id列才可以赋值为null pname varchar(40), price double, num int);
inrt into 表(列,列..) values(值,值..);
方式二: 插入所有的列注意: 如果没有插入了列设置了非空约束, 会报错的
inrt into 表 values(值,值....); eg:inrt into product values(null,'苹果电脑',18000.0,10);inrt into product values(null,'华为5g手机',30000,20);inrt into product values(null,'小米手机',1800,30);inrt into product values(null,'iphonex',8000,10);inrt into product values(null,'苹果电脑',8000,100);inrt into product values(null,'iphone7',6000,200);inrt into product values(null,'iphone6s',4000,1000);inrt into product values(null,'iphone6',3500,100);inrt into product values(null,'iphone5s',3000,100);inrt into product values(null,'方便面',4.5,1000);inrt into product values(null,'咖啡',11,200); inrt into product values(null,'矿泉水',3,500);
命令行插入中文数据报错:
关闭服务, net stop mysql在数据库软件的安装目录下面, 修改配置文件 my.ini中客户端的编码为gbk重新打开命令行,开启服务, net start mysqlinrt into 表名(字段列表) values(值列表)
插入所有的列
inrt into 表名 values(值,值,值....)
多行插入
inrt into 表名 values(值列表),(值列表),(值列表)...
注意:
插入特定的列没有赋值的列,系统自动赋为null(前提是当前列没有设置not null 约束)在插入指定列数据时,可以省略不插入的是一些(可以为空的列、设置默认值的列、设置auto_increment的列)。列名与列值的类型、个数、顺序要一一对应。值不要超出列定义的长度。eg:password varchar(12) abcd123456789插入的日期和字符串,使用单引号括起来。我们数据插入成功了, 还可以对已有的数据进行更新。
update 表 t 列 =值, 列 =值 [where 条件]
update product t price = 5000;
校花
将商品名是mac的价格修改为18000元update product t price = 18000 where name = 'mac';
将商品名是mac的价格修改为17000,数量修改为5update product t price = 17000,num = 5 where name = 'mac';
将商品名是方便面的商品的价格在原有基础上增加2元update product t price = price+2 where name = '方便面';
update tb_name t 列1=值1[,列2=值2...] where条件
注意实际工作中,where 条件必不可少,不可省略delete from 表 [where 条件] 注意: 删除数据用delete,不用truncate
类型删除表中名称为’mac’的记录
delete from product where pname = 'mac';
删除价格小于5001的商品记录
delete from product where price < 5001;
删除表中的所有记录
delete from product;
truncate table 表;
delete from 表 【where 条件】 注意:实际开发中,where条件一定不能少
delete 和truncate区别【面试题】 delete 删除表中的数据,表结构还在; 删除后的数据可以找回,一条一条的删除.truncate 删除是把表直接drop掉,然后再创建一个同样的新表。删除的数据不能找回。执行速度比delete快。工作里面的删除 物理删除: 真正的删除了, 数据不在, 使用delete就属于物理删除逻辑删除: 没有真正的删除, 数据还在. 搞一个标记, 其实逻辑删除是更新 eg: del_flag 0存在 1删除工作里面一般使用逻辑删除用得多 alter table product add del_flag tinyint(4) default 0 not null;我们上面讲解了对数据的增删改, 下面就来重点讲解数据的简单查询.
lect [*] [列名 ,列名] [列名 as 别名 ...] [distinct 字段] from 表名 [where 条件]
lect * form 表
查询商品表里面的所有的列lect * from product;
lect 列名,列名,列名... from 表
查询商品名字和价格lect pname, price from product;
lect distinct 字段名 from 表名; //要数据一模一样才能去重
去重查询商品的价格lect distinct price from product;
注意点: 去重针对某列, distinct前面不能先出现列名
lect 列名 as 别名 ,列名 from 表 //列别名 as可以不写lect 别名.* from 表 as 别名 //表别名(多表查询, 看主页另外一篇mysql高级)
查询商品名称和商品价格,商品价格通过别名‘价格’来显示lect pname , price as 价格 from product;
lect pname ,price+10 from product;
运算查询字段,字段之间是可以的字符串等类型可以做运算查询,但结果没有意义注意
lect ... from 表 where 条件 //取出表中的每条数据,满足条件的记录就返回,不满足条件的记录不返回
between…and… 区间查询eg: where price between 1000 and 3000 相当于 1000<=price<=3000
in(值,值..)-- 查询id为1,3,5,7的lect * from t_product where id = 1lect * from t_product where id = 3lect * from t_product where id = 5lect * from t_product where id = 7lect * from t_product where id in(1,3,5,7)
like 模糊查询 一般和_或者%一起使用_ 占一位% 占0或者n位name like '张%' --查询姓张的用户, 名字的字数没有限制name like '张_' --查询姓张的用户 并且名字是两个的字的
and 多条件同时满足where 条件1 and 条件2 and 条件3
or 任意条件满足where 条件1 or 条件2 or 条件3
lect * from product where price > 3000;
查询id=1的商品lect * from product where pid = 1;
查询id<>1的商品lect * from product where pid <> 1;
查询价格在3000到6000之间的商品lect * from product where price between 3000 and 6000;
查询id在1,5,7,15范围内的商品lect * from product where id = 1;lect * from product where id = 5;lect * from product where id = 7;lect * from product where id = 15;lect * from product where id in (1,5,7,15);
查询商品名以ipho开头的商品(iphone系列)lect * from product where pname like 'ipho%';
查询商品价格大于3000并且数量大于20的商品 (条件 and 条件 and…)l教师个人工作总结范文ect * from product where price > 3000 and num > 20;
查询id=1或者价格小于3000的商品lect * from product where pid = 1 or price < 3000;
lect [*]|[字段列表] from 表名 [where条件]lect * from tb_name;
条件where 条件between … and …inlike _|%有时候我们需要对查询出来的结果排序显示,那么就可以通过order by子句将查询出的结果进行排序。排序可以根据一个字段排,也可以根据多个字段排序,排序只是对查询的结果集排序,并不会影响表中数据的顺序。
# 创建学生表(有sid,学生姓名,学生性别,学生年龄,分数列,其中sid为主键自动增长)create table student( sid int primary key auto_increment, sname varchar(40), x varchar(10), age int, score double);inrt into student values(null,'zs','男',18,98.5);inrt into student values(null,'ls','女',18,96.5);inrt into student values(null,'ww','男',15,50.5);inrt into student values(null,'zl','女',20,98.5);inrt into student values(null,'tq','男',18,60.5);inrt into student values(null,'wb','男',38,98.5);inrt into student values(null,'小丽','男',18,100);inrt into student values(null,'小红','女',28,28);inrt into student values(null,'小强','男',21,95);
lect 字段名 from 表名 [where 条件] order by 字段名 [asc|desc]; //asc: 升序,默认值; desc: 降序
练习: 以分数降序查询所有的学生lect * from student order by score desc
lect 字段名 from 表名 where 字段=值 order by 字段名1 [asc|desc], 字段名2 [asc|desc];
练习: 以分数降序查询所有的学生, 如果分数一致,再以age降序lect * from student order by score desc, age desc
lect * from tb_name where条件 order by 排序要使用的列[asc|desc [,第二列...]]order by 列 asc/desc, 列 asc/desc;asc: 升序【默认值】desc: 降序
应用场景 商城里面 根据价格, 销量, 上架时间, 评论数… 社交里面 根据距离排序排序不改变表中数据的顺序,只是改变查询结果集的显示顺序之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是==纵向查询==,它是对一列的值进行计算,然后返回==一个结果值==。聚合函数会忽略空值null
聚合函数用于数学计算统计!
lect 聚合函数(列名) from 表名 [where 条件];
练习-- 求出学生表里面的最高分数lect max(score) from student-- 求出学生表里面的最低分数lect min(score) from student-- 求出学生表里面的分数的总和(忽略null值)lect sum(score) from student-- 求出学生表里面的平均分lect avg(score) from student-- 统计学生的总人数 (忽略null) lect count(sid) from studentlect count(*) from student
注意: 聚合函数会忽略空值null
我们发现对于null的记录不会统计,建议如果统计个数则不要使用有可能为null的列,但如果需要把null也统计进去呢?我们可以通过 ifnull(列名,默认值) 函数来解决这个问题. 如果列不为空,返回这列的值。如果为null,则返回默认值。
lect 聚合函数(列) from 表名;
2.聚合函数 用于数学统计:
max() 最大值min() 最小值sum() 求和avg() 平均值count() 统计数量:求一个表中的记录数,建议使用count(*)3.注意事项
聚合函数会忽略null值的分组查询是指使用 group by语句对查询信息进行分组
group by怎么分组的? 将分组字段结果中相同内容作为一组,如按性别将学生分成两组
==group by将分组字段结果中相同内容作为一组,并且返回每组的第一条数据,所以单独分组没什么用处。分组的目的就是为了统计,一般分组会跟聚合函数一起使用==
lect 字段1,字段2... from 表名 [where 条件] group by 列 [having 条件];
练习:根据性别分组, 统计每一组学生的总人数-- 根据性别分组, 统计每一组学生的总人数lect x, count(*) from student group by x
lect x, count(*) from student group by x having count(*) > 5
lect 分组列,分组统计结果 from 表 [where条件] group by 列 [having条件]
注意事项根据某一列进行分组, 将分组字段结果中相同内容作为一组; 有几组 返回的记录就有几条单独分组 没有意义, 返回每一组的第一条记录分组的目的一般为了做统计使用, 所以经常和聚合函数一起使用在分组里面, 如果lect后面的列没有出现在group by后面 展示这个组的这个列的第一个数据使用分组时,注意要进行查询显示的是分组列和分组统计结果。where和having的区别【面试】子名作用where 子句1) 对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,即先过滤再分组。2) where后面不可以使用聚合函数having字句1) having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,即先分组再过滤。2) having后面可以使用聚合函数limit是限制的意思,所以limit的作用就是限制查询记录的条数. 经常用来做分页查询
lect ... from .... limit a ,b.
limit a,b;a:起始行数,从0开始计数,如果省略,默认就是0; a=(当前页码-1)*b;b: 返回的行数练习eg: 分页查询学生, 每一页查询4条 b=4; a=(当前页码-1)*b;第一页: a=0, b=4;第二页: a=4, b=4;第三页: a=8, b=4;
limit a,b; a:从第几条数据开始查询, 从0开始计数 【a=(当前页码-1)*b】b: 一页查询的数量【固定的,自定义的】
淘宝不能正常显示
应用场景如果数据库里面的数据量特别大, 我们不建议一次查询出来. 为了提升性能和用户体验, 使用分页lect...from...where...group by...order by...limitlect...from...where...lect...from...where...order by...lect...from...where...limit...lect...from...where...order by...limit
mysql进阶:多表联合查询,外键约束,多表间关系,连接查询,内连接查询,隐式内连接,事务特性和隔离级别
本文发布于:2023-04-05 15:32:13,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1ca32ed9e77662888b4dae7e5c5d7f1f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:数据库的增删改查sql语句(sql语句增删改查的基本命令).doc
本文 PDF 下载地址:数据库的增删改查sql语句(sql语句增删改查的基本命令).pdf
留言与评论(共有 0 条评论) |