引子:Excel 最简单的数据库

Mysql:属于大型数据库

 

 

一、基本命令:

  1. 启动服务:

说明: 以管理员身份运行cmd

                格式: net start 服务名称

实例: net start mysql

  1. 停止服务:

说明: 以管理员身份运行cmd

格式:net stop 服务名称

实例:net stop mysql

 

  1. 连接数据库:

格式:mysql -u 用户名 -p

实例:mysql -uroot -p

输入密码(安装时设置的)

 

  1. 退出登录(断开连接)

quit或者exit

 

  1. 查看版本型号 (连接后可以执行)

示例:select version();

  1. 显示当前时间(连接后可以执行)

示例:selcet now();

  1. 远程连接

格式:mysql -h ip地址 -u 用户名 -p

输入对方的mysql密码

 

 

 

 

二、数据库操作

  1. 创建数据库

格式:create database 数据库名 charset=utf8;

示例:create database lilei charset=utf8;

 

 

  1. 删除数据库

格式:drop database 数据库名

示例:drop database lilei;

 

 

 

 

  1. 切换数据库

格式:use 数据库名;

示例:use yeyu;

 

 

  1. 查看当前选择的数据库

Select database();

 

三、表操作

  1. 查看当前数据库中的所有表

Show tables;

  1. 创建表

格式:create table 表名(列及类型)

说明:auto_increment表明自增长 primary key 主键

not null表示字段不能为空

示例:create table student(id int auto_increment primary key

, name varchar(20) not null);

  1. 删除表

格式:drop table 表名;

示例:drop table student;

  1. 查看表结构

格式: desc 表名;

示例: desc students;

  1. 查看建表语句

格式:show create table 表名;

示例:show create table  student;

  1. 重命名表

格式:rename table 原表名 to 新表名;

示例:rename table car  to newCar;

  1. 修改表结构

格式:alter table 表名 add|change|drop 列名 类型;

示例:alter table newCar add is_delete bit default 0;                    

四、数据操作

a、全列插入:

格式:insert into 表名 values;

说明:主键列是自动增长,但是在全列插入时需要占位,通常

使用0,插入成功以后以实际数据为准

示例:insert into student values (

0,”tom2”,19,1,”北京”,0

);

b、缺省插入

格式:insert into 表名(列1,列2,…) value(值1,值2,…);

示例:insert in to student(name,age,address) value(“lilei”,19,”北京”);

c、同时插入多条数据

格式:insert into 表名 values(…),(…),…;

示例:insert into sutdent values(0,”tom”,19,1,”北京”,0

),(0,”tom”,19,1,”北京”,0

),(0,”tom”,19,1,”北京”,0

);

格式:delete from表名 where 条件;

实例:deltet from 表名 where id=4;

注意:没有条件是全部删除,慎用

格式:update 表名 set  列1=值1,列2=值2,…where 条件;

实例:update student set age=16 where id=1;

注意:如果没有条件全部列都修改,慎用

说明:查询表中的全部数据

格式: select *from 表名;

示例: select * from student;

五、查

  1. 基本语法

格式: select *from 表名;

说明:a、from关键字后面是表名,表示数据来源于这张表

      b、select 后面写表中的列名,如果是*表示在结果集中显示表中的所有                   列

      c、在select后面的列名部分,可以使用as为列名起别名,这个别名显                    示在结果集中

d、如果要查询多个列,之间使用多逗号分隔

实例:

select * from student;

select name,age,from student;

select name as a,age from student;

  1. 消除重复行

在select 后面列前面使用distinct可以消除重复的行

示例:

select gender from student;

select distinct gender from student;

  1. 条件查询

a、语法

select *from 表名 where 条件;

b、比较运算符

  

等于=

大于>

小于<

大于等于>=

小于等于<=

不等于 !=或<>

需求:查询id值大于8的所有数据

示例:select * from student where id>8 ;

 

c、逻辑运算符

and  并且

or     或者

not  非

需求:查询id大于7的女同学

示例: select * from student where id>7 and gender=0;

 

d、模糊查询

insert into sudent value(0,”习大大”,66,1,”北京”,0);

insert into sudent value(0,”习大大”,66,1,”北京”,0);

需求:查询姓习的同学

示例:select * from student where name like “习%”

 

like

% 表示任意多个任意字符

_ 表示任意字符

e、范围查询

in 表示在一个非连续的范围内容

between…and… 表示在一个连续的范围内

需求:查询编号是8,10,12的学生

示例:select * from student where id in (8,10,12);

需求:查询编号为6-8的学生

示例:select * from student where id between 6 and 8;

f、空判断

Insert into student(name,age) values(“赵乐”,15);

注意:null和””是不同的

判断空: is null

判断非空: is not null

需求:查询没有地址的同学

示例:select * from student where address is null;

 

g、优先级

小括号,not,比较运算符,逻辑运算符,

and 比or的优先级高 同时出现并希望先or,

需要结合括号来使用。

  1. 聚合

为了快速得到统计数据,提供了五个聚合函数

A  count(*) 表示计算总行数,括号中可以写*和列名

B  max(列) 表示求此列的最大值

C  min(列)  表示求此列的最小值

D  sum(列)  表示求此列的和

E  avg(列)  表示求此列的平值

需求:查询学生总数

示例:select count(*) from student;  #   *可以是id

 

需求:查询女生的编号最大值

示例:select max (id) from student where gender=0;

 

需求:查询女生的编号最小值

示例:select min (id) from student where gender=0;

 

 

需求:查询女生的年龄和

示例:select sum(age) from student where gender=0;

 

需求:查询所有学生年龄的平均值

示例:select avg(age) from student;

 

  1. 分组

按照字段分组,表示此自断相同的数据会被放在一个集合中。

分组后,只能查询出相同的数据列,对于有差异的数据列无法显示在结果         集中

可以对分组后的数据进行统计,做聚合运算

语法: select 列1 ,列2,聚合…from 表名group  by列1,列2,列3,…;

需求:查询男女生总数

示例:select gender,count(*) from student group  by gender ,age;

      select gender,count(*) from student group  by gender ;

 

分组后的数据筛选:select 列1 ,列2,聚合…from 表名group  by列1,列2,列3,…having 列1,…聚合…;

示例:select gender,count(*) from student group by gender having gender;

 

Where 和 having的区别:

Where是对from后面指定的表进行筛选,属于对原始数据的筛选

Having 是对group by 的结果进行筛选

 

 

 

  1. 排序

语法: select * from 表名 order by 列1

asc|desc,列2 asc|desc,…;

说明

  1. 将数据按照列1进行排序,如果某些列1的值相同,则按照列2进行排序
  2. 默认按照从小到大的顺序排序
  3. asc升序
  4. desc降序

需求:没有被删除的数据按年龄排序

示例:select * from student where is_delete =0 order by age;

  1. 分页

 语法: select * from 表名 limit start,count

 说明:start 索引从0 开始

 示例: select * from student limit 0,3;

select * from student limit 3,3;

 

六、关联 1对多

建表语句

1. Create table class (id int auto_increment primary key ,name varchar(20)not null ,stuNum int not null);

2. Create table student((id int auto_increment primary key ,name varchar(20)not null,gender bit default 1,classid int not null,foreign key(classid)references class(id) );

插入一些数据:

insert into class values(0,”1班”,55),(0,”2班”,50),(0,”3班”,60),(0,”4班”,80);

insert into students values (0,”tom”,1,1);

insert into students values (0,”lilei”,1,10)#没有此班级无法插入

insert into students values (0,”zhaole”,1,2);

 

 

 

关联查询

Select students.name,class.name from class inner join students on class.id = students.class.id;

分类

1. 表A inner join 表B

表A表B匹配的行会出现在结果集中

2. 表A left join 表B

表A和表B匹配的行会出现在结果集中,外加表A中独有的数据,未对应得数据用null填充

3. 表A right join 表B

表A和表B匹配的行会出现在结果集中,外加表B中独有的数据,未对应得数据用null填充、

 

 

 

高版本的mysql 和 低版本的 mysql 的加密方式 不一样 所以需要 修改加密方式

  • mysql -u root “p #进入数据库
  • ALTER USER ‘root’ @’localhost’ IDENTIFIED BY ‘你的mysq|密码’ PASSWORD EXPIRE NEVER;
  • #修改加密规则
  • ALTER USER ‘root’ @’localhost’ IDENTIFIED WITH mysql native_ password BY ‘你的mysq|
  • 密码’; #修改密码
  • FLUSH PRIVILEGES; #刷新数据

版权声明:本文为ln-xx原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ln-xx/p/12071617.html