1. 安装好mysql数据库
启动mysql数据库
在cmd命令窗口启动,使用管理员权限打开cmd命令窗口,启动
net start mysql
2. 使用navicat进行数据库的操作
navicat是数据库可视化管理工具,需要解码,可以去b站找,也可通过下方链接
链接:https://pan.baidu.com/s/1DtfqX-GwnZL-thH_wJJgrw?pwd=6666
提取码:6666
具体连接数据库方法这里就不赘述了,可以去看一下navicat基本操作方法。
3. mysql基本语法
3.1 创建数据库
创建名为Tests的数据库;
create database Tests;
3.2 查询数据库
查询本地中的所有数据库
show databases;
3.3 选择数据库
具体要操作哪个数据库要进行选择,例如要操作Tests数据库
use Tests;
3.4 查询数据库
由于我新建的数据库,没有表所以为空
show tables;
3.5 创建数据表
创建数据表成功,创建数据表基本语法
create table 名字(列的名称 类型)
create table student(id int,name char(10),age int,sex char(5));
3.6 查询表结构
查询表结构是在选择数据库的基础上去做的
desc student;
3.7数据表添加列
语法结构 alter table 表名 add 要添加的列名 类型;
alter table student add height int(10);
3.8 数据表删除列
语法结构 alter table 表名 drop 列名;
alter table student drop age;
3.9 数据列改名
语法结构 alter table 表名 change column 要改的列名 改的名字 类型;
alter table student change column height high int(3);
3.10 数据列修改数据类型
语法 alter table 表名 modify column 修改的数据列 类型;
alter table student modify column high char(10);
3.11 修改数据表名
alter table student rename student_table;
3.12 插入数据项
3.12.1数据表中文编码错误以及插入数据
有时添加中文到数据表中会有编码错误第一行就是为了解决编码错误问题
ALTER TABLE student_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE student_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
insert into student_table(id,name,sex,height) value (001,"张三","男",160);
3.13 删除数据项
选择某一个属性为标准进行删除,下列为删除high为140的一行数据
delete from student where height=140;
3.14 更新数据项
对数据表中数据value数据进行重写
update student set height=180;
3.15删除表
drop table student;
3.16 删除数据库
drop database Tests;
4. 逻辑运算符
1.between 最小值 and 最大值
语法:select * from 表名 where 列名 between 最小值 and 最大值;
select * FROM student_table where height BETWEEN 170 and 190;
作用和使用><是一样的
select * FROM student_table where height>=170 and height<=190;
2.null
语法:select * from 表名 where 列名 is null;(判断这一列有空值)
select * from student_table where height is null;
3. in (取值范围)
语法:select * from 表名 where 列名 in (值1,值2,…)
select * from student_table where height in (170,190);
4.like 好像
通配符: % 代表任意字符 _ 一个下划线代替一个字符
语法:select * from 表名 where 列名 like ‘通配符 特征 通配符’;
select * from student_table where name like '张_';
5.as 为表名称或者列名称指定别名
select id as student_id from student_table where name like '张_';
6.union 合并两个或多个select语句结果集
select id from student_table as st where st.name like '张_' union select name from student_table where height=190;
五,排序
关键词 order by
排序规则:升序排序 asc (默认可省略) 降序排序 desc
语法:
单列 select * from 表名 order by 列名 asc或者 desc;
多列 select * from 表名 order by 列名 asc或者 desc,列名2 asc或者desc,…;
select * from student_table order by height asc,name desc;
四,去重
GROUP BY 和 DISTINCT 都是用于从数据库中选择唯一值的 SQL 子句
select distinct height from student_table;
select height,count(*) from student_table group by height;
待续…