提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
第一次使用csdn记录
提示:以下是本篇文章正文内容,下面案例可供参考
#一、安装MySQL学习安装使用,已安装好了
#二、使用步骤 #1.使用代码如下(示例)
win+R 进入命令提示符,输入cmd:进入小黑框
输入cd /d MySQL下的bin路径,进入该目录下
cd /d D:MySQLmysql-8.0.28-winx64bin
启动MySQL服务 (结束即为另一条)
net start mysql net stop mysql
输入命令后 ,输密码进入MySQL
mysql -u root -p
如上,进入MySQL数据库服务,即可对数据库进行操作。
#2.MySQL基础操作只需要一些基础操作,特意记录一下
#2.1DDL(数据定义语言) 对数据库操作
查询所有数据库
show databases;
查询当前所用的数据库
select database();
创建一个数据库
create database test_sql;
删除数据库
drop database test_sql;
使用想用的数据库(后面的是数据库名字)
use mysql;
查询当前数据库所有表
show tables;
创建表
create table testtable( id int comment '序号', age int comment '年龄', name varchar(20) comment '名字' );
显示表结构
desc testtable;
查询指定表的建表语句
show create table testtable;
往指定表里加入内容
alter table testtable add sexy varchar(1) comment '性别';
修改表中的数据类型
alter table testtable modify name varchar(15) comment '姓名';
修改表中名字
alter table testtable change name username varchar(15) comment '用户名';
删除字段
alter table testtable drop sexy;
修改表名
alter table testtable rename to newtable;
删除表
drop table newtable;#2.2 DML(数据操作语言)
对表中数据进行操作
对指定字段插入数据
insert into testtable (id,sexy) values (1,'男');
对全部字段插入数据
insert into testtable values (1,18,'小明','男');
批量添加数据
指定字段
insert into testtable (id,name) values (2,'叶银松'),(3,'程思齐'),(4,'孔德鹏');
全部字段
insert into testtable values (1,25,'叶银松','男'),(2,25,'倪超','男'),(3,25,'孔德鹏','男'),(4,26,'王昊东','男'),(5,25,'程思齐','男');
修改数据
update testtable set name='叶银松' where age=18;
删除数据
delete from testtable where age=18;#2.3DQL(数据查询语言) 查询表中数据
部分:
select id,age from testtable;
查询所有数据:
select * from testtable;
设置别名:
select id as uid from testtable;
select name uname from testtable;
去除重复记录:
select distinct id from testtable;
条件查询
select * from testtable where id>2;
模糊查询:
select * from testtable where name like '%鹏';
聚合查询
select max(id) from testtable where sexy='男';
分组查询
select sexy from testtable where id>1 group by sexy;
select sexy from testtable where id>1 group by sexy having sexy=’男’;
感觉分组查询最复杂,其中where和have有区别
where 和 having 的区别:
- 执行时机不同:where是分组之前进行过滤,不满足where条件不参与分组;having是分组后对结果进行过滤。
- 判断条件不同:where不能对聚合函数进行判断,而having可以。
- -- 年龄小于45,并根据工作地址分组,获取员工数量大于等于3的工作地址
-
select workaddress, count(*) address_count from employee where age < 45 group by workaddress having address_count >= 3;
排序查询
asc:升序(默认)
select name from testtable order by age asc;
desc:降序
select name from testtable order by id desc;
分页查询
数据不够,只能看一页,关键词是limit
后面第一个参数是起始索引,第二个是查询记录数;
从0开始,起始索引 = (查询页码 - 1) * 每页显示记录数
select * from testtable limit 0,10;
重要!!!!!!
FROM -> WHERe -> GROUP BY -> SELECT -> ORDER BY -> LIMIT
#2.4 DCL(和用户打交道)创建用户
在当前主机上:localhost
任意主机:%
create user 'sq'@'localhost' identified by '123456';
修改密码
alter user 'sq'@'localhost' identified with mysql_native_password by '111111';
删除用户:
drop user 'sq'@'localhost';
查询用户权限
show grants for 'sq'@'localhost';
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单记录了mysql的使用,和常见的数据库的基础操作,让自己有个印象,作为教程是不够的。



