如何使用mySQL

mysql基本使用
2019年05月07日 09:16:12 终究是半圈 阅读数:6
mysql数据库
创建表

  • 数据类型

    • 整数类型 int
    • 浮点类型 double
    • 日期类型 date/timestamp
    • 字符串类型 char varchar
      • 可变varchar 不可变是char
      • 空间换时间
  • 首先足够用,其次尽量小

  • create table 表名(
    id int,
    name varchar(20)
    );

  • 查看创建表的语句

    • show create table 表名
  • 查看表的明细

    • desc 表名

删除表

  • drop table 表名
    1
    插入数据
  • 第一种:全表字段插入
    • insert into 表名 values (val1,val2...)
    • 插入的字段类型和顺序必须与表的字段类型与顺序保持一致,个数是针对全表字段的插入
  • 指定表字段的插入
    • insert into 表名 (字段1,字段2...) values (val1,val2...)
    • 插入的数据的顺序要与声明的字段名的顺序一致
    • 类型也要一致,个数也要一致

总结
1: 登录:

mysql -uroot -p
密码
2: 查看有哪些数据库

show databases;
3: 切换数据库

use 数据库名;
4: show tables;

5: 查看表字段明细

desc 表名;
6: 插入数据

insert into 表名 (字段1,…) values (值1…)
更新操作

UPDATE 表名 SET 字段1 = 表达式,[,字段2 = 表达式] [WHERE express布尔值]
update student set tmp = 20+1,sex = sex where 1=1;
删除操作

DELETE FROM 表名 [,WHERE exr布尔值]
delete from student where name = ‘马蓉’;
truncate和delete的区别

truncate table 表名
delete会一条一条的删 自增id保留
truncate先摧毁整张表,再创建一张和原来的表结构一模一样的表
truncate在效率上比delete高
truncate只能删除整表的数据,也就是格式化。
truncate会把自增id截断恢复为1
查询语句
DISTINCT 去重,尽量不要根据* 来去重
select distinct name,age from student;
查询语句可以指定多个字段查询,也可以使用* 查询
在企业中,使用SQL查询* 是会拉低查询效率的 ,建议少用* 查询
条件查询
select * from student where math > 66.6 or math < 66.6;

< <= >= = <> 大于、小于、大于(小于等于)、不等于
BETWEEN…AND 显示在某一区间的值
IN(set) 显示在in列表中的值,例:in(100,200)
LIKE ‘张pattern’ 模糊查询%
IS NULL 判断是否为空
AND 多个条件同时成立
OR 多个条件任一成立
NOT 不成立,例:WHERE NOT(salary>100)

模糊查询

  • 包含关select * from student where name like '%关%';
  • 以关开头select * from student where name like '关%';
  • 以关结尾select * from student where name like '%关';
    1
    2
    3
    判断为空
    select * from student where sex is not null;
    聚合函数
    统计数量
    count(星号)
    求和
    sum(math+english+chinese)
    平均值
    avg(math+english+chinese)
    找最值
    max(列)/min(列)/max(math+english+chinese)
    排序(order by)
    select * from student where 1= 1 order by chinese desc,math desc,english desc;
    分组(group by)
    如果聚合函数只有一行,需要对于不同的集合做聚合运算就加上分组
    select zu,count(*) from student group by zu;
    分页(limit)
    • select * from student limit 10,5;
    • limit offset(起始位置) count(显示多少个)
    • limit 使用的时候,offset从0开始
    • 豆瓣: offset (当前页-1)* 页显示数
      • count : 页显示数

猜你喜欢

转载自blog.csdn.net/weixin_44275692/article/details/89924881