- mysql 安装
- 连接
- 导入数据
- 导出数据
- crud
- 常用函数:count, avg, max, min, length
RDBMS 关系型数据库管理系统
图:
mysql 安装
linux
服务端
sudo apt-get install mysql-server
sudo service mysql start 启动数据库服务器
ps ajx|grep mysql 查看服务器是否启动
sudo service mysql stop
sudo service mysql restart
位置:/var/lib/mysql
mysql配置位置:/etc/mysql
mysql默认是存储在/var/lib/mysql目录下的
mysql> show variables like ‘%data%’;
命令行客户端
sudo apt-get install mysql-client
启动数据库
mysql -u root -pmysql 密码是mysql
命令行连接
mysql -uroot -p
回车后输入密码,当前设置的密码为mysql
退出数据库:
ctrl+d或
quit 或者 exit
windows
windows安装mysql:
https://blog.csdn.net/zhouzezhou/article/details/52446608
注意管理环境变量添加后才可以用net start mysql80,且管理员身份执行 (mysql80是安装时取的名字)
设置的是开机启动
navicat连接不上:ALTER USER ‘root‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘yourpassword‘
注意:1.可以不用引号,2.可能需要 flush privileges
连接
远程连接不上,常见处理:
- ping不通
- 该服务host仅localhost
- 有密码等权限问题
curd
关系型数据库核心元素
数据行(记录)
数据列(字段)
数据表(数据行的集合)
数据库(数据表的集合) ==== 整个Excel 文件就好比一个数据库,一个sheet 就 好比一个 数据表
常用数据类型如下:
整数:int,bit
小数:decimal
字符串:varchar,char
日期时间: date, time, datetime
枚举类型(enum)
约束:
-- auto_increment表示自动增长
-- not null 表示不能为空
-- primary key 表示主键
-- default 默认值
登录成功后,输入如下命令查看效果
查看版本:select version();
显示当前时间:select now();
- 数据库
查看所有数据库 show databases;
使用数据库 use 数据库名;
查看当前使用的数据库 select database();
创建数据库 create database 数据库名 charset=utf8; 等号可以不写
查看创建数据库的语句 show create database xxx;
删除数据库/表 drop database 数据库名; drop table 表名
删除行(表结构还在) Delete from 表名 where xxx ; delete from 表名 或 delete * from 表名
删除表内的数据(表结构还在) truncate table 表名 = delete from 表名
删除数据的速度,一般来说: drop> truncate > delete
使用场合:
当你不再需要该表时, 用 drop;
当你仍要保留该表,但要删除所有记录时, 用 truncate;
当你要删除部分记录时(always with a where clause), 用 delete
- 数据表
查看当前数据库中所有表 show tables;
创建表 (字段 ) create table 数据表名字 (字段1 类型 约束,字段2 类型 约束...);
查看表结构 desc 数据表的名字; 查看表的创建语句 show create table xxxx;
查询classes表中所有的数据 select * from xxxx;
修改表-添加字段 alter table 表名 add 列名 类型; # 列名不可少
修改表-修改字段:类型或约束 alter table 表名 modify 列名 类型及约束;
修改表-修改字段:重命名 alter table 表名 change 原名 新名 类型及约束;
修改表-删除字段
alter table 表名 drop 列名(字段);
删除表 drop table 表名
数据 记录 增删改查 (curd) 代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)
增加 记录(插入数据,行) insert into xxxx values(0, "mike", 18, 1.45, "保密", 2); >>>>配合 desc 使用, 对应table的字段 全部都要写 主键字段 可以用 0 null default 来占位
部分插入 insert into 表名(列1,...) values(值1,...);
insert into students(high) values(180.1);
多行插入
insert into students(name, high) values("李四", 180.1), ("王五", 1.22);
修改 update 表名 set 列1=值1,列2=值2... where 条件;
update students set age=18 where id=1;
删除
物理删除 delete from students where id=6;
逻辑删除 alter table students add is_delete bit default 0;
update students set is_delete=1 where id=1;
查询基本使用 select * from students;
一定条件的查询 select * from students where id>=1 and id<=4; ## 要有空格
查询指定列 select 列1,列2,... from 表名; (同样可以加where)
字段的顺序 列2 列1会对应 可以使用as为列或表指定别名
select name as 姓名, id as 学号 from students where id>=1 and id<=4;
select students.name, students.age from students; #
select s.name, s.age from students as s; ####
消除重复行
-- distinct 字段
-- 查询students表中所有的不重复的性别
select gender from students;
select distinct gender from students;
条件查询
比较运算符
select * from students where age>18;
select id,name,gender from students where age>18;
逻辑运算符
select * from students where age >=18 and age <= 28;
select * from students where age >=18 or height >= 180;
select * from students where not age<=18 and gender=2;
select * from students where not (age<=18 and gender=2);
模糊查询
-- like
-- % 替换0个或者多个
-- _ 替换1个
select * from students where name like "小%";
select * from students where name like "%小%";
select * from students where name like "";
select * from students where name like "%%";
范围查询
in (1, 3, 8)表示在一个非连续的范围内
select name,age from students where age in (18, 34);
select name,age from students where age not in (18, 34);
between ... and ...表示在一个连续的范围内
select name,age from students where age between 18 and 34;
select name,age from students where age not between 18 and 34;
失败的select * from students where age not (between 18 and 34);
空判断
select * from students where height is null;
判非空is not null
select * from students where height is not null;
排序
order by 字段 asc从小到大排列,即升序 ascend default;
desc从大到小排序,即降序 descend
select * from students where (age between 18 and 34) and gender=1 order by age;
order by 多个字段
select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc;
聚合函数
count()统计列数,count(字段)一样
select count() from students where gender=1;
select count() as "男性" from students where gender=1;
最大值 max 最小值 min 求和 sum 平均值 avg
select max(age) from students;
select sum(age)/count() from students;
select avg(age) from students;
四舍五入 round(123.23 , 1) 保留1位小数
select round(avg(age), 2) from students;
分组
select gender from students group by gender;
失败select * from students group by gender;
select gender, count() from students group by gender;
select gender, count() from students where gender=1 or gender=2 group by gender;
--group_concat(...)
select gender, group_concat(name) from students group by gender;
select gender, group_concat(name, id) from students group by gender;
select gender, group_concat(name, "%", id) from students group by gender;
--having
select gender, group_concat(age) from students group by gender having avg(age) > 30;
select gender, group_concat(name, age) from students group by gender having avg(age) > 30;
select gender, group_concat(name) from students group by gender having count(*) > 2;
分页
-- limit start, count 从第0行开始查 limit 3 ; >>>limit 0, 3 ;
select * from students limit 0, 3;
select * from students limit 4, 2;
select * from students where gender=2 order by height desc limit 0, 2;
连接查询
-- inner join ... on
-- select ... from 表A inner join 表B;
select * from students inner join classes on students.cls_id=classes.id;
select * from students inner join classes on students.cls_id=classes.id;
select s.name, c.name from students as s inner join classes as c on s.cls_id=c.id;
select s., c.name from students as s inner join classes as c on s.cls_id=c.id;
select c.name, s.from students as s inner join classes as c on s.cls_id=c.id order by c.id, s.id;
select * from students left join classes on students.cls_id=classes.id;
-- 查询没有对应班级信息的学生
select * from students left join classes on students.cls_id=classes.id where/having classes.id is null;
自关联
select * from areas where pid is null;
select count() from areas where pid is null;
select p.atitle,c.atitle from areas as p inner join areas as c on c.pid=p.aid where p.atitle="广东省";
子查询
select from students where age=(select max(age) from students);
-- 标量子查询
select * from students where height>(select avg(height) from students);
-- 列级子查询
select * from students where cls_id in (select id from classes);
事务四大特性(简称ACID)
事务是由一组SQL语句组成的逻辑处理单元, 银行转帐就是事务的一个典型例子。
原子性(Atomicity)
不被中断
一致性(Consistency)
总数一定
隔离性(Isolation)
对方看不到
持久性(Durability)
数据被保存
图形化界面客户端navicat
进入解压后的文件目录 运行 ./start_navicat
外键对查询没影响, 插入数据时影响效率
ctrl + shift + t 终端开标签
分组一般和聚合函数使用;
where having
order by
limit 最后
原文地址:https://www.cnblogs.com/bruspawn/p/10330982.html