MySQL 命令操作

Mysql进入与退出

 

进入数据库:mysql -uroot -p

退出: exit

 

 

库级操作语句

 

  1. 显示所有的表:show databases;
  2. 创建库: create database 库名; (重复创建会报错,可在库名前加上[if not exists])
  3. 删除库: drop database 库名;(如果不知道数据库是否存在,记得在库名前加[if exists])
  4. 进入数据库:use 库名;

表级操作语句

  1. 显示所有的表: show tables;
  2. 创建表: create table 表名;(重复创建会报错,可在表名前加上[if not exists])
  3. 显示创建表的信息: show create table 表名;
  4. 删除表:drop table 表名;

MysSQL表中数据的操作

插入数据(create) insert into values

 

  1. 指定字段插入: insert into 表名(表头)value(插入信息);
  2. 全字段插入:  insert into 表名value (插入整段信息);
  3. 多行插入: insert into 表名 value(插入信息),(插入信息),…;

 

查询数据 (read) select from where

  1. 指定字段查询: select 表头 from 表名;
  2. 2.      全字段查询:select * from 表名;(select查询关键字,  * 代表所有 )
  3. 带条件查询:select 表头1 from 表名 where 表头2  条件;

例:select name from student where age = 19;

        (查询学生表里年龄等于19的姓名)

                      select * from 表名 where 表头字段 条件;

                      例:select * from student where age = 19;

                             (查询学生表里等于19岁的全部字段)

修改数据(update)update set where

  1. 修改所有数据:update 表名 set 表头字段 = 条件;
  2. 修改多个数据:update 表名 set 表头字段 = 条件, 表头字段2 = 条件2;
  3. 修改满足条件的数据:update 表名 set 表头字段1 = 条件1  where 表头字段2 = 条件2;

例:update student set id = 5 where name =某某;

       (把学生表姓名为某某的id改成5)

删除数据 (delete)delete from where

 

  1. 删除表中所有数据: delete from 表头字段;
  2. 删除表中满足条件的数据:delete from 表头字段 where 表头字段 = 条件;

例:delete from student where name = 某某;

        (删除学生表里某某整行值)

MySQL数据类型:

数值类型、字符类型、时间日期类型

 

数值类型常用举例

 

字符类型常见举例

 

 

时间日期类型

 

案例

 

MySQL筛选条件

 

In null 例子

select * from student where id is null;

错误例子: select * from student where is = null;(这样是显示不了的,空值以这种方式查询是查不出来的)

in not null 例子

select * from student where id is not null;

与或非

and or not

 

and 例子:select * from student where id =5 and age = 9;

  • or  例子:select * from student where id =5 or age = 9;

 

 

 

 

 

 

 

 

排序(order by)

正序举例:select * from student order by age;

倒序举例:select * from student order by age desc;

限制(limit)

举例:select * from student limit 0,5;

(MySQL是 左闭右闭)

 

 

去重(distinct)

举例:select distinct * from student;

 

模糊查询

select * from 表名 where 表头字段 like ‘第一个字加%’;(查询任意多个字符 :%)

 

select * from 表名 where 表头字段 like ‘第一个字加__’(查询任意单个字符:__ ,后面几个字就带几个下划线_ )

注:也可查询任何位置关键字,如 名字中间字 或最后一个字。

 

范围查询

  1. 连续范围

select * from 表名 where 表头字段 >=范围1 and 范围2;

举例:

或、

select * from 表名 where 表头字段 between 范围1 and 范围2;

举例:

 

2.间隔范围

select * from 表名 where 表头字段 in (范围1, 范围2);

举例:

 

聚合函数

 ­­­统计个数:count()

select count(表头字段) from 表名;

 

求和:sum()

select sum(表头字段) from 表名;

 

 

最大值:max()

select max(表头字段) from 表名;

 

 

 

 

 

 

平均值:avg()

select avg(表头字段) from 表名;

 

 

最小值:min()

select min(表头字段) from 表名;

 

 

列出全部字段:gpoup_concat()

select group_concat(表头字段) from 表名;

 

group_concat 大部分都是跟分组进行功能的共同使用,单独使用会非常少,在分组的情况下,只能够出现分组字段和聚合字段,其他的字段没有意义,会报错!

 

select group_concat(表头字段1),group_concat(表头字段2) from 表名;

 

 

 

 

 

 

  

where条件查询举例

select name,avg(age) from student where id<=5 group by name;

 

意思是在student表里where条件查询id小于等于5的人名以及年龄平均值。

 

聚合函数分组:

select group_concat(表头字段) from student group by 表头字段

(这样可把重复的数据打印在同一行上)

 

 

可一次查询多个数值

 

聚合筛选 having

select 表头字段1,聚合函数(表头字段2) from 表名 group by 表头字段1 having 表头字段1 比较运算符 表头字段1的数值;

举例: select id,avg(age) from student group by id having id <= 2;

 

意思是利用having聚合筛选出student表里id小于等于2的平均年龄值的临时表,

注意表达格式!注意id出现的位置!

 

一个查询语句中同时包含了别名(as),聚合函数,where,having那么他们的执行顺序是

先是执行: where然后执行:聚合函数和别名最后执行:having

 

别名(as): 如果后面再用到聚合函数avg了可以进行聚合函数的赋值 ‘as 赋值名’

select id,avg(age) as aa from student group by id having id <= 2;

子查询

子查询相当于嵌套  将一个查询的结果留下来用于下次查询(select中嵌套select)

 举例

 

mysql

表结构修改

 1.  修改表名:alter table 表格文件名 rename to 新文件名;

­­­­

 

2  .修改字段名:alter table 表格文件名 change 字段名 新字段名;

­

 

3 . 修改字段类型:alter table 表格文件名 modify mid 新字段类型;

 

4 . 添加字段:alter table 表格文件名 add 新字段名 数据类型(20);

 

5 . 删除字段:alter table 表格文件名 drop 字段名;

 

 

原文地址:https://www.cnblogs.com/gd000-/p/10651839.html