相关免费学习推荐:mysql视频教程
文章目录
一、sql是什么?分类:二、关于数据库crud操作1.操作表list:2.对表内数据进行操作:a.查询b.where条件:三、查询1. 排序查询2. 聚合函数(列的计算)3. 分组查询4. 排序查询四、约束1.非空约束:not null2.唯一约束实例操作:3.主键约束:primary key4.自动增长:auto_increment五、总结错误实例一、sql是什么?structured query language:结构化查询语言
分类:1) ddl(data definition language)数据定义语言
用来定义数据库对象:数据库,表,列等。关键字:create, drop,alter 等
2) dml(data manipulation language)数据操作语言
用来对数据库中表的数据进行增删改。关键字:insert, delete, update 等
3) dql(data query language)数据查询语言
用来查询数据库中表的记录(数据)。关键字:select, where 等
4) dcl(data control language)数据控制语言(了解)
用来定义数据库的访问权限和安全级别,及创建用户。关键字:grant, revoke 等
二、关于数据库crud操作#createcreate database hzyc;create database if not exists hzyc98 character set gbk;#retrieveshow databases;show create database hzyc98;#updatealter database hzyc98 character set gbk;#deletedrop database hzyc98;drop database if exists hzyc98; #查看当前使用的数据库select database();show tables;use hzyc98
1.操作表list:表名/表头为:zoomlist
#查show tables; -- show tables_in_hzyc98desc zoomlist;#增create table zoomlist ( name varchar(30), age int, id int, height double(5,1))#删drop table if exists zoomlist;#改alter table zoomlist rename to newzoomlist;alter table zoomlist character set gbk;alter table zoomlist add name varchar(20);#加列alter table zoomlist change age newage int;alter table zoomlist modify age char(8);alter table zoomlist drop name;/*设置类型:*/ - int、double(5,1)、varchar(20) - date #yyyy-mm-dd - datetime #yyyy-mm-dd hh:mm:ss - timestamp#时间戳 yyyy-mm-dd hh:mm:ss
2.对表内数据进行操作:#除了数字,其他都需要引号来赋值insert into zoomlist (name, age, id, height) value('美洲豹',5,'20201207',3.2);insert into zoomlist ('美洲豹',5,'20201207',3.2);#删除delete from zoomlist where [条件];delete from zoomlist;truncate table zoomlist;#修改update zoomlist set name = '大笨象' age = 12 where address = '深圳';update zoomlist set address = '深圳';
a.查询#查询#尽量不要用 * 先desc一下表里面有啥,然后在决定展示什么东西。select * from zoomlist; select name,age from zoomlist; --只显示某个列,方便查看!select distinct name from zoomlist; --去除结果中[完全重复]的select name,score1,score2,scroe1+scroe2 from zoomlist;--as:自定义名字展示,也可以不写asselect name,scroe1+ifnull(scroe2,0) 总分 from zoomlist; --ifnull遇到没有值的直接给赋值为0select name,score1,score2,scroe1+ifnull(scroe2,0) as 总分 --显示表头from zoomlist,peoplelist; --从zoomlist、peoplelist里面获取
b.where条件:
* > 、< 、<= 、>= 、= 、!=、<>--不等号* and、or、not --关键字比&&、||、!好用推荐* between...and --范围内都符合就行* in( 集合) --特定值的范围* like:模糊查询(1)_:单个任意字符;(2)%:多个任意字符* is null例子:select name, age from student where age between 12 and 20;select name, age from student where age in (12,14,16,18);select name, age from student where name like '%牛%'; --查名字里面包含了牛的学生select name, age from student where name is not null; -- 查询学生:名字空的不查
三、查询1. 排序查询select * from employee order by age;select * from employee order by age asc; --升序select * from employee order by age desc; --降序select * from employee order by age desc height desc; --第一个一样的时候,才会用第二个方法排序(age降序,身高降序)
2. 聚合函数(列的计算)排除了null数据,并且有null的数据就不参与计算,不会报错!
count:统计个数min、max、sum、avg:求值select count(*) from student;select count(ifnull(age,20)) from student; select count(age) from student;--如果没有就不记录select count(id) from student; --我们一般选用主键来统计个数select max(age) from student;select min(age) from student;select sum(age) from student;select avg(age) from student;
3. 分组查询group by 之后就是两个不同的组别了,他们不能再去查看一个独立的个体了。
分组之后查询的字段:分组字段、聚合函数。where和having的区别? where在分组前限定,having在分组之后限定;where不符合条件的不参与分组,having不符合条件不会显示;只有having可以后跟聚合函数判断。select sex,count(name) from employee group by sex having count(name)<6;select sex,count(name) from employee where name = '张四' group by sex ;
4. 排序查询limit是一个mysql的方言,用于分页
select * from student limit 0,5; -- 第1页,从0索引开始,读5个数据select * from student limit 7,10; -- 第2页,从7索引开始(第8个数据),读10个数据
四、约束约束:主键约束:primary key非空约束:not null唯一约束:unique外键约束:foreign key1.非空约束:not null-- 建表时添加非空约束: create table employee( name char(30), sex char(8) not null ) alter table employee modify sex char(8) not null; --添加非空约束 alter table employee modify sex char(8); --破除非空约束
2.唯一约束只可以有一个null值,不能再多了;
删除约束只可以用drop index来删除unique约束
-- 建表时添加唯一约束: create table employee( name char(30), sex char(8), score int unique --分数要唯一 ) --添加唯一约束alter table employee modify name char(8) unique; --破除唯一约束-- alter table employee modify sex char(8); 不可用--破除name身上的unique约束用drop index除去索引alter table employee drop index name;
实例操作:
3.主键约束:primary key一个表只有一个primary key,非空且唯一
做记录的唯一标识,相当于index
-- 建表时添加主键约束: create table employee( id int primary key, --给id加上主键约束 name char(30), ) --添加唯一约束alter table employee modify id int primary key; --破除唯一约束-- alter table employee modify id int; 不可用!--破除id身上的primary key约束只能用drop primary keyalter table employee drop primary key;
4.自动增长:auto_increment只对数值有用,而且一般可以放给主键做自动增长
-- 建表时添加auto_increment: create table employee( id int auto_increment, --给id加上auto_increment name char(30), ) --添加auto_increment,自动从1开始alter table employee modify id int auto_increment;--设置初值alter table employee auto_increment = 100; --破除auto_incrementalter table employee modify id int;
五、总结 我们学习了sql是什么,做了一个简单的入门,也列举了一些mysql的基本操作,还有查询、约束是怎么一回事。
但是我也是刚刚才接触mysql,所以基本的操作手还比较生,要多学多练多去实践才能出真知。
在之后我们还会学习到mysql的多重关系、多表查询、事务(还不太清楚是什么)、jdbc各个语句、数据库连接池druid、jdbctemplate……还有好多东西要学,但是现在的任务还是在于把基本的东西梳理好,把基础巩固了才是硬道理!!!
错误实例:如添加数据的时候不写列名,那必须给所有列值,不然报错
以上就是5分钟学会mysql基本操作的详细内容。
