zoukankan      html  css  js  c++  java
  • mysql基础语法

    
    

    一、库

     mysql -uroot -p #进入mysql
     show databases; #查看所有库
     quit, exit, ctrl +d #退出mysql

     

     

    二、表

    1.  insert into 表名 values( id int primary key) #增加内容
       alter table 表名 add 类型 约束; #添加类型 和约束
       alter table 表名 add 类型名 tinyint #增加布尔值 1 是true 2 是 false

       

    2.  delete from 表名 where 类型=类型(如‘张三’如 1) #删除某一内容 背
       alter table 表名 drop 列名 #删除列
       drop table 表名 #删除表
       alter table 子表名 drop foreign key 外键名称 #删除外键
    3.  alter table 旧表名 rename 新表名 #更换表名
       alter table 表名 modify 列名 类型 约束; #修改表的类型和约束
       update 表名 set 列名=新值 where (id=2) #修改内容
       update book set price=10 where id in(8,9,10); #where id in (可放多id)
       update 表名 set 列名 =‘ 男’where id in (1,2,3,4) #把新添加的列添加数据
       alter table 旧表名 rename 新表名 #修改表名
       alter table 表名 change 旧列名 新列名 类型 约束 #修改列名
       alter table 表名 modify 列名 新类型 (新参数 约束) #修改已有列类型

       

    4.  select now #查看时间
       select version() #查看版本号
       select * from 表名 ; #查看表中内容
       select * from 表名 where id between 3 and 8; #连续范围
       select round(avg(id),2) from book;; #保留两位小数
       select * from 表名 where 类型 * and *;(如 1 and 5 ) #连续范围查找
       select * from student where id in(1,3,5,7); #不连续范围
       select * from 表名 where 类型 like '内容%' and 类型 like '内容%'; #查关于莫类型 某名字
       select * from 表名 where 类型>'2006-0-06'; #比字符串大小
       select id(要显示的内容 若显示全部用*代替) from book order by id(按照什么排序) desc(倒序); 排序
       select sum(id) from book; #求和
       select avg(price) from book; #求平均值
       select count(id) from book; #获取总行数
       select * from book limit 4,2; #从哪行开始 查几排
       select * from score where degree between 60 and 80 order by degree desc; #排序查询范围
       select distinct 列名 from 表名 #去重单列

       

     

    1. 分组

       select publish from book group by publish; #分组对查询后的结果分
       select publish,group_concat(bookname) from book group by publish; #concat(连带某类次数)
       select publish,count(bookname) from book group by publish; #分组查询莫一分组数量
       select publish,count(bookname) from book group by publish having count(*)>1; #分组后用having添加条件
       select publish,count(bookname) from book group by publish with rollup; #总结后 再求和

      having 条件表达式:用来分组查询后指定一些条件来输出查询结果 having作用和where一样,但having只能用于group by

      group_concat(字段名)可以作为一个输出字段来使用, 表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合

      group by + with rollup

      with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和

       select gender,count(*) from students group by gender with rollup;
       +--------+----------+
       | gender | count(*) |
       +--------+----------+
       | 男 | 5 |
       | 女 | 7 |
       | 中性 | 1 |
       | 保密 | 1 |
       | NULL | 14 |
       +--------+----------+
       
       select gender,group_concat(age) from students group by gender with rollup;
       +--------+-------------------------------------------+
       | gender | group_concat(age) |
       +--------+-------------------------------------------+
       | 男 | 29,59,36,27,12 |
       | 女 | 18,18,38,18,25,12,34 |
       | 中性 | 33 |
       | 保密 | 28 |
       | NULL | 29,59,36,27,12,18,18,38,18,25,12,34,33,28 |
       +--------+-------------------------------------------+

       

    2. 外键

       给谁添加外键谁就是子表
       alter table 子表名 add foreign key(列名) references 主表(列名); #创建外键
       alter table 子表名 add foreign key (列名) references 主表(id)
       foreign key(列名) references 主表(id)
       foreign key(t_id) references book(id) ); #简单版
       show create table table 表名 #查看外键
       alter table 表名 drop foreign key 外键名 #取消外键约束

       

    3. 连接查询

      select * from 表1 inner join 表2 on 相同类型(表1.id=表2.pid) #连接查询
      select * from ares as a inner join ares as b on a.id=b.pid; #as 重命名
      create view vlia as select department.dname,employee.uname from department,employee where department.cid=employee.uid;
      select b.name,max(score) from students as a inner join class as b on a.id=b.id group by b.name;
      #连接查询多个列表 分组
    1. 分页

       

    没有结果就是最好的结果。 -Linux哲学
  • 相关阅读:
    linux安装python串口工具pyserial遇到不能成功导入的问题
    yum rpm apt-get wget 辨析
    系统时间不一致导致memcached的session不共享
    ERROR: transport error 202: bind failed: Address already in use
    The user specified as a definer ('root'@'%') does not exist
    IE比Chrome强的一个地方
    电脑突然死机,系统日志记录事件ID=6008
    mysql 查询多个id
    tomcat登陆WEB显示无权限问题&& tomcat无限循环启动问题
    MySql中把一个表的数据插入到另一个表中的实现代码
  • 原文地址:https://www.cnblogs.com/Stubbornlyn/p/13158632.html
Copyright © 2011-2022 走看看