美文网首页
mysql一些基础知识

mysql一些基础知识

作者: 基本密码宋 | 来源:发表于2018-02-07 23:13 被阅读19次

    安装mysql看 https://www.jianshu.com/p/5983a85ee10e 这个哥们的。
    我是windows上安装的

    打开终端

    mysql -uroot -p 打开。由于我是通过命令行安装的数据库 所以我的mysql是没有密码的。有的时候回打开失败 net start MySQL用此命令
    记住是 管理员的身份下运行cmd
    关闭数据库的时候net stop MySQL
    下面开始进行数据库的基本操作

    查看里面有哪些库

    show databases;

    删除某个库

    drop database 数据库的名字

    创建库

    create database 数据库的名字 charset=utf8(指定字符集)

    使用某个数据库

    use 数据库的名字

    查看当前使用的数据库

    select database();

    查看库中的表

    show tables

    万事俱备 开始创建表

      create table student(表的名字)(
      id int auto_increment primary key not null,
      name varchar(10) not null,
      gender bit default 1,
      birthday datetime);
    

    下面进行解释翻译下

     创建表 student(
     id      int类型的   自增  主键   不能为空,
     名字     varchar类型的(最大是10长度) 不能为空,
     性别     bit类型   默认值是 1(0,1其中的一个),
     生日     datetime类型
    

    查看表的结构

    desc student(表的名字)

    修改表

    alter table 表名 add|change|drop 列名 类型
    例子
    alter table student change isDelete(列的名字)bit not null default 0

    物理删除表

    drop table 表的名字

    修改表的名字

    rename table 原表名 to 新表名

    插入数据

    insert into students(表名) values(列名...)
    下面是demo
    insert into students values(0,'名字',1,'1992-01-01',0)

    (全列)插入表中的所有数据

    select * from students(表名)

    (缺省插入)插入数据 部分列插入

    insert into students (列的名字 列的约束可以为null) values(列的值)

    (缺省插入)插入多条数据 部分列插入

    insert into students (列的名字) values(‘名字1’),('名字2')

    修改表中数据

    update students(表名) set age=18 where id =1

    物理删除表中的一行

    delete from students where id =1 # 删除学生表中 id是1的数据

    查询数据 并 去除重复数据

    select distinct 列名 from student;

    查询数据 where 提交查询

    select * from student where name!='名字' #查询students表中 name不是‘名字的人’

    逻辑运算符

    and or not

    select * from students where id>3 and gender=1 #查询学生的id>3并且性别为女

    模糊查询 %表示任意多个字符 _表示一个字符

    select * from students where name like '宋%' # 在students表中查询名字为宋开头的任意符合要求的行
    select * from students where name like '宋_' # 在students表中查询名字为宋开头,后面只有一个字符的任意符合要求的行

    范围查询

    in 在某个范围内操作 select * from students where id in(1,2,3);

    between...and... select * from students where id between 1 and 10 #查询id从1到10之间的

    null select * from students where age is not null ; #查询学生表中age不是null的所有数据。

    聚合函数

    count() 数量求和

    select count(*) from students ;#查询学生表中所有的数量
    select count(*) from students where id>2 #查询学生表中id>2的所有数量

    min() 求里面最小的

    select min(id) from students ;#查询学生表中 id 最小的 结果出来的事 id

    max() 求里面最大的

    select max(id) from students ;#查询学生表中 id 最大的 结果出来的事 id

    sum() 求和

    select sum(id) from students ;#查询学生表中id的和

    avg() 求平均值

    select sum(id) from students ;#查询学生表中id的的平均值

    分组

    grop by 将相同的值归为一组

    select gender , count(*) where students group by gender;#查询学生表中,用gender进行分组。并计算数量,聚合到一起。

    hvaing where 是对原始数据集进行筛选。having是对分组后的数据进行筛选

    select count(*),gender from students group by gender having gender =0

    1、查询学生表 用gender进行分组。并用count进行统计数量。
    2、分组完成后,对数据进行筛选,筛选出gender=0 的。进行显示。
    当然可以使用as 别名 -->
    select count(*) ,gender as sex from students group by gender having sex =0

    排序

    order by asc (正序 默认) desc 倒序

    select *from students order by id asc|desc

    分页

    limit start,count; start-开始的角标(0开始) count-一共取多少位

    select * from students limit 0,2;

    相关文章

      网友评论

          本文标题:mysql一些基础知识

          本文链接:https://www.haomeiwen.com/subject/aqczzxtx.html