mysql基础命令

2019-11-11

基础操作命令:

启动服务命令:   以管理员身份运行cmd:输入:net start mysql81(mysql的名字) windwos
                systemctl  start mysql  linux  以服务启动
                systemctl  stop mysql  linux   以服务重启
停止服务:       以管理员身份运行cmd:net stop mysql81(mysql的名字)
                systemctl  stop mysql    以服务停止
连接数据库:     mysql -u root(用户名、默认为root) -p,然后需要输入密码:(安装时设置的)
退出登录:       quit或exit
查看版本:       select version();
显示当前时间:   select now();
远程连接:       mysql -h ip地址 -u 用户名 -p,输入对方的mysql密码
注册为windos服务(bin目录下):C:\mysql\bin\mysqld --install 服务名 --defaults-file=C:\my.ini(路径不要有空格,如果有将路径用双引号)
刷新权限:flush privileges;
连接远程数据库时,如果服务器端没有开启ssh,那么直接数据服务器的ip、数据库的账号、密码即;
    需要修改user表的host为%代码:
    > use mysql 
    > update user set host = '%' where user = 'root';
远程连接时。如果开启了ssh服务,还需要填写ssh的信息
sql语句注释 -- 注意:-- 要与语句中间有一个空格
msyql -u root -p -e 'create database bak_orderdb';//'可以在没有登录mysql时创建一个数据库

从数据中获取随机的值:rand()
   select * from user order by rand()  limit 3; 
正则匹配搜索: regexp 正则表达式  但是会降低效率
   select name,email from user where email regexp "@163.com"

查询包含指定sql方法名的全部方法名:? sql方法名
   ? fun%  可以获取更多的包含指定sql方法名的全部方法名

\G 美化查询出来的信息(使用它时不需要;结尾)

从自己身上查询数据插入到自己表中,成倍增加数据
    insert into stu(username,age,class_id) select username,age,class_id from stu;

查询出表的结构和注释
select  column_name,column_comment  from information_schema.columns where table_schema ='数据库名'  and table_name = '表名';

查询数据库中的所有表名
select table_name from information_schema.tables where table_schema='数据库名' and table_type='base table';

windows下mysql安装时的三个选项

数据库操作:

1、创建数据库: create database 数据库名 default character set utf8mb4 collate utf8mb4_general_ci

2、删除数据库: drop database 数据库名;

3、切换数据库:  use 数据库名;

4、查看选择的数据库:  select database();
5、查看所有的数据库: show databases;

数据表操作:

1、查看当前数据库中所有表(包括视图):show tables;
   show full tables; 会增加一个type列,显示出表的类型

2、创建表:create table 表名 (列及类型) ENGINE=INNODB  DEFAULT CHARSET=utf8mb4 comment '注释';
        说明:auto_increment表示自增长;primary key表示主键;not null表示不为空;default 0表示默认值;
        示例:create table student(id int auto_increment primary key,name varchar(20) not null,age int not null,gender bit default 1,address varchar(20),isDelete bit default 0);
3、删除表:drop table 表名;

4、查看表结构:desc 表名;

5、查看建表语句:show create table 表名;

6、重命名表名:rename table 原表名 to 新表名;
       
7、修改表:alter table 表名 add|change|drop|modify 列名 类型 默认值 comment '注释信息',add|change|drop 列名 类型 comment '注释信息';
        说明:# add添加;change变化;drop删除;modify变化
        示例:alter table 表名 add 列名 类型 默认值 注释 AFTER 指定某字段 #AFTER 表示增加在指定字段后面
        示例:alter table 表名 drop 列名
        示例:alter table 表名 modify 列名 新数据类型 新类型长度  新默认值  新注释
        示例:alter table 表名 change 列名 新列名 新数据类型 新类型长度  新默认值  新注释
8、修改字段
        设置默认值:alter table 表名 alter 字段名 set default 默认值;
        删除默认值:alter table 表名 alter 字段名 drop default;

数据操作:

1、增
   a、全列插入:insert into 表名 values(...);
      说明:主键列是自动曾长的,但是在全列插入时需要占位,通常使用0,插入成功后以实际数据为准
      示例:insert into student values(0,"tom",19,1,"北京",0);
   b、缺省插入:insert into 表名(列1,列2...) values(值1,值2...);
      示例:insert into student(name,age,address) values("lilei",23,"沈阳");
   c、同时插入多条数据:insert into 表名 values(...),(...),...
      示例: insert into student values(0,"haimeimei",18,0,"天津",0),(0,"poi",22,1,"上海",0),(0,"sunmou",22,0,"海南",0),(0,"刘某",20,0,"青海",0);

2、删:delete from 表名 where 条件;
   注意:如果没有条件是全部删除,慎用。
   示例:delete from student where id=4;
3、改:update 表名 set 列1=值1,列2=值2,... where 条件;
   注意:如果没有条件是整列全部都修改了,慎用
   示例:update student set age=30 where id=7;
4、查:查询表中的全部数据:select * from 表名;
   示例:select * from student;

数据查询:

1、基本语法:select * from 表名;

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

        select name as a,age from student;

2、消除重复行:在select后面列前使用distinct可以消除重复的行:select distinct gender from student;

3、条件查询:
        a、语法:select * from 表名 where 条件
        b、比较运算符:大于> 等于= 小于< 大于等于>= 小于等于<= 不等于!=,
            示例:查询id值大于3的所有数据:select * from student where id>3;
        c、逻辑运算符:与and 或or 非not
            示例:查询id值大于3的女同学:select * from student where id>3 and gender=0;
        d、模糊查询:like  %表示任意多个任意字符;_表示1个任意字符;
            示例:查询姓刘的:select * from student where name like "刘%";
        e、范围查询:
            1、in 表示在一个非连续的范围内,
                示例:查询编号是2,4,6的学生:select * from student where id in (2,4,6);
            2、between...and...表示在一个连续的范围内,
                示例:查询编号是2,6的学生:select * from student where id between 2 and 6;
        f、空判断:注意null与""是不同的;
            1、判断空是is null
                示例:查询没有地址的同学:select * from student where address is null;
            2、判断非空 is not null
                示例:查询有地址的同学:select * from student where address is not null;
        g、优先级:小括号,not,比较运算符,逻辑运算符,(and比or的优先级高,如果同时出现并希望先算or需要加括号来使用)

4、聚合:为了快速得到统计的数据,提供了5个聚合函数:
        a、count(*)   表示计算总行数,括号中可以写*和列名;
            示例:查询学生的总数: select count(*) from student;
        b、max(列)    表示求此列的最大值;
            示例:查询女生的编号最大值:select max(id) from student where gender=0;
        c、min(列)    表示求此列的最小值;
            示例:查询女生的编号最小值:select min(id) from student where gender=0;
        d、sum(列)    表示求此列的和;
            示例:查询女生的年龄和:select sum(age) from student where gender=0;
        e、avg(列)    表示求此列的平均值;
            示例:查询所有学生年龄的平均值:select avg(age) from student;

 5、分组:按照字段分组,表示此字段相同的数据会被放到一个集合中,分组后,只能查询出相同的数据列,对于有差距的数据列无法显示 在结果集中,可以对分组后的数据进行统计,做聚合运算。
        语法:分组查询:select 列1,列2,聚合... from 表名 group by 列1,列2,...
        示例:查询男女生总数:select gender,count(*) from student group by gender;
        分组后数据筛选:select 列1,列2,聚合... from 表名 group by 列1,列2,... having 列1,列2,...,聚合...
        示例:select gender,count(*) from student group by gender having gender;
        where 是对from后面的表进行筛选,属于对原始数据的修改,having是对group by的结果进行筛选

6、排序:select * from 表名 order by 列1 asc|desc,列2 asc|desc,...
        说明:
        a、将数据按照列1进行排序,如果某些列1的值相同,则按照列2进行排序,...
        b、默认按照从小到大的顺序排列
        c、asc升序
        d、desc降序
        示例:
        (1)按照年龄排序:select * from student order by age;
        (2)将没有删除的数据按照年龄排序:select * from student where isDelete=0 order by age;
        (3)将没有删除的数据按照年龄排序,当列1的某些值相同时,这些值将会按照列2进行排序:select * from student where isDelete=0 order by age desc,id desc;

7、分页:select * from 表名 limit start,count; start代表开始的下标,从0开始,count代表每页的数量
        示例:select * from student limit 0,3;
        按照条件查询后进行分页显示:select * from student where gender=1 limit 0,3;

数据表关联:

数据表关联:
    1、建表语句:
        a、创建班级表:create table class(id int auto_increment primary key,name varchar(20) not null,stuNum int not null);
        b、创建学生表并关联班级表的id:create table students(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));
    2、关联查询:查询学生的信息和班级的名字:
        a、表a inner join 表b,表a与表b匹配的行会出现在结果集中
            示例:select students.name,class.name from class inner join students on class.id=students.classid;
        b、表a left join 表b,表a与表b匹配的行会出现在结果集中,外加表a中独有的数据,未对应的数据使用null填充
            示例:select students.name,class.name from class left join students on class.id=students.classid;
        c、表a right join 表b,表a与表b匹配的行会出现在结果集中,外加表b中独有的数据,为对应的数据使用null填充
            示例:select students.name,class.name from class right join students on class.id=students.classid;

join连表查询时,如果两个表中的字段没有重复的时,可以不用加字段前面的表名

union关联会默认去重:会将联合起来表将重复的值去掉,使用union all将会获得全部的数据

select * from a,b where a.xx = b.xx; 可以查询出a表字段值与b表字段值相同的数据

等值连接 
#查询每个员工的job_title
SELECT e.last_name,e.job_id,job_title 
FROM employees e 
INNER JOIN jobs j 
ON e.job_id = j.job_id;

非等值连接 
#查询某个员工的工资等级
SELECT e.last_name,j.grade_level 
FROM employees e
INNER JOIN job_grades j 
ON e.salary BETWEEN j.lowest_sal AND j.highest_sal;

自连接
#查询某个员工及其对应的领导
SELECT e1.last_name 'employees',e2.last_name 'manager'
FROM employees e1
INNER JOIN employees e2 
ON e1.manager_id = e2.employee_id;

事务操作:

BEGIN 或 START TRANSACTION 显式地开启一个事务;

COMMIT 也可以使用 COMMIT WORK,不过二者是等价的。COMMIT 会提交事务,并使已对数据库进行的所有修改成为永久性的;

ROLLBACK 也可以使用 ROLLBACK WORK,不过二者是等价的。回滚会结束用户的事务,并撤销正在进行的所有未提交的修改;

root密码丢失

关闭mysql
进入mysql的bin目录
不使用密码进入mysql
方式1:mysqld.exe --skip-grant-tables & --user=mysql 后台启动服务端  
方式2:mysqld.exe --skip-grant-tables  --user=mysql 启动服务端
mysql -u root 进入mysql
flush privileges; 刷新权限
alter user 'root'@'localhost' identified by '456'; 修改密码
关闭mysqld进程
重启mysql服务

 

{/if}