强哥MySQL学习笔记
数据库服务器:
1.数据库
2.数据表
数据表:
1.表结构(字段)
2.表数据(记录)
3.表索引(加快检索)
表引擎:
1.myisam
2.innodb
查看表字段
desc table;
删除数据库:
drop database jiuxian;
删除表:
drop table user;
表字段类型:
1.数值
int(11) 有符号
int(10) 无符号
float
2.字符串
char 255
varchar 65535, 建议255
text 65535
3.日期和时间
数值时间戳
数据字段属性:
1.unsigned
2.zerofill
3.auto_increment
4.null
5.
*建议:
所有字段都是not null,而默认值是null,为了节省空间。
char和varchar区别:
1.char最长255,varchar最长65535
2.char固定长度空间,varchar可变长空间
修改数据库字符集:
修改my.ini
[client]
default-character-set = utf8
[mysqld]
character-set-server = uft8
collation-server = utf8_general_ci
批量删除表:
drop table `t1`,`t2`,`t3`,'t4';
表索引:
1.主键索引 (主键索引已经包含唯一索引)
2.唯一索引
3.普通索引
desc select * from t1 where id=4; --查看语句执行情况
desc select * from t1 where id=4\G --以行打印结果(不能加;)
普通索引:
1.增
alter table t1 add index idx_name(name); --给name添加一个名字叫idx_name的索引
2.删
alter table t1 drop index idx_name;--删除索引
唯一索引:
1.增
alter table t1 add unique uni_name(name);--
2.删
alter table t1 drop index uni_name;
主键索引:
删:
alter table t1 modify id int unsigned not null;--先删除自增
alter table t1 drop primary key;--再删除索引
反引号的作用:
--一个表名如果是关键字
insert into where(id) values(1); --会报错
insert into `where`(id) values(1);--不会报错
修改数据表字段:
增
alter table t1 add sex tinyint not null;
alter table t1 add sex tinyint not null after name;--加一列sex放到name后面
alter table t1 add sex tinyint not null first;--加一列sex放到第一列
删:
alter table t1 drop sex;
改:
alter table t1 change name username varchar(30) not null;
mysql结构化查询语言
结构化查询语言sql包含4个部分:
1.DDL --数据定义语言,create,drop,alter
2.DML --数据操作语言,insert,update、delete
3.DQL --数据查询语言,select
4.DCL --数据控制语言,grant,commit,rollback
数据表操作:
1.sql条件
数学运算符 +,-,*,/
逻辑运算符 && || and or
比较运算符 = != >= <= > < <>
1.增 insert
2.删 delete
truncate user; --擦除user表内容
3.改 update
4.查 select
select * from user where id in(1,3,4);
select * from user where id between 3 and 5;
别名:
select id as i,username as u,password as p form user;
select id i,username u,password p form user;
distinct关键字使用:
去掉重复值:
select distinct age form user;
查询空值:
select * from where age is null;
like:
select * from user where username like "%linux%"; --查询含有linux的
正则查询:
select * from user where username regexp '$456'; --正则查询
排序:
升序,数字从小到大
select * from user order by id asc;
降序,数字从大到小
select * from user order by id desc;
mysql常用函数:
连接函数concat()
select concat('hello','world','!!!') hw; --输出helloworld!!!并建立别名hw
select concat(username,password) from user; --
随机数rand()
select * from user order by rand() limit 3;
统计个数count()
select count(*) from user; --*特指主键,但是和id不同。
求和sum()
select sum(id) from user;
平均值avg()
select avg(id) from user;
最大值max()
select max(id) from user;
最小值min()
select min(id) from user;
limit限定输出个数:
select * from user limit 3;
select * from user limit 7,3; --从7开始,截3个
group by 分组聚合:
select class_id,count(*) from user group by class_id;
select concat(class_id,'-','class') class,count(*) tot from user group by class_id;
多表查询:
表与表关系:
1.有关系
2.没关系
有关系:
1.合并
2.分离
#一个表一个主体
#两个表是一对一关系,则可以合并
1.普通多表查询
class 表:
create table class(
id int unsigned not null auto_increment,
name varchar not null,
primary key(id)
);
user 表:
create table user(
id int unsigned not null auto_increment,
username varchar not null,
password varchar not null,
primary key(id)
);
需求:请打印出每个学员的姓名和班级。
select * from user,class where user.class_id=class.id;
select user.username,class.name from user,class where user.class_id=class.id;
2.嵌套查询
select * from user where id in (select max(id) from user);
3.链接查询
左连接
把左边的表全部输出
需求:把所有班的总人数统计出来,前提是有些班没人。
select class.name,count(user.id) from class left join user on class.id=user.class_id group by class.id;
需求:把所有班的总人数统计出来,前提是有些班没人。注:人数为零则打印出"无";
select class.name,if(count(user.id),count(user.id),'wu') from class left join user on class.id=user.class_id group by class.id;
右连接:
把右边的表全部输出
select class.name,if(count(user.id),count(user.id),'wu') from user right join class on class.id=user.class_id group by class.id;
内连接:
完全等于普通多表查询,必须是符合条件的多个表的数据才会显示
select class.name,count(user.id) from class inner join user on class.id=user.class_id group by class.id;
需求:用户表,有一列是成绩,请统计这张表中及格人数和不及格人数.使用一条语句
答案1:
select (select count(*) from user where score>=60) yes,(select count(*) from user where score<60) no ;
答案2:
select sum(if(score>=60,1,0)) ok,sum(if(score<60,1,0)) nook from user;
having:
分组聚合之后筛选
select class_id from user group by class_id having class_id<=2;
强哥MySQL学习笔记的更多相关文章
- 强哥jQuery学习笔记
js对象: 1.js内置对象 2.js元素对象 3.jquery对象 js特效: 1.js元素对象 2.jQuery对象 jQuery学习: 1.核心函数 2.选择器 3.筛选 4.文档处理 5.属性 ...
- 强哥ThinkPHP学习笔记
TP框架:1.模板引擎2.MVC设计模式3.常用操作类 模板引擎和框架区别1.模板引擎只是框架中用来做php和html分离 MVC设计模式M model 数据模型V view 视图C control ...
- 强哥PHP学习笔记
1.php的代码,必须放在.php的文件中,php代码必须写在<?php ?>之间. 2.//单行注释 /* 多行注释 */ 3.默认首页index.php index.html inde ...
- 强哥memcache学习笔记
搭建memcache服务器:1.在内存中缓存数据2.数据形态以key->value memcache优点:1.快速缓存2.跨域登录memcache缺点:1.复杂的数据存取的操作2.不能永久保存数 ...
- 强哥JavaScript学习笔记
js文件放header头最后,js代码放body体最后 js语言定位: js是基于对象的语言 php.java是面向对象的语言 定义变量: var str="hello world" ...
- 强哥HTML学习笔记
html 浏览器的选择:1.火狐2.ie3.chrome4.mac5.opera 安装两款插件:1.firebug2.web develope html页面元素:1.doctype2.htmlhead ...
- 强哥CSS学习笔记
html嵌套css样式:1.外部(推荐)2.内部3.内联(不推荐) css优先级1.内联2.id选择器3.class选择器4.标签 css长度单位:1.px2.em (14px) css选择器:常用选 ...
- Mysql学习笔记(三)对表数据的增删改查。
正文内容. 这一部分是最简单的,也是最麻烦的.简单是因为其实只包括增删该插四个部分.大体上看,增加数据.删除数据.修改数据.查询数据都不麻烦啊,我们日常都是常用的.这个谁不会呢?以前在培训机构学mys ...
- MySQL学习笔记一
MySQL 学习笔记 一 一.数据库简单介绍 1. 按照数据库的发展时间顺序,主要出现了以下类型数据库系统: Ø 网状型数据库 Ø 层次型数据库 Ø 关系型数据库 Ø 面向对象数据库 上面4中数据库系 ...
随机推荐
- Activiti工作流学习笔记(四)——工作流引擎中责任链模式的建立与应用原理
原创/朱季谦 本文需要一定责任链模式的基础,主要分成三部分讲解: 一.简单理解责任链模式概念 二.Activiti工作流里责任链模式的建立 三.Activiti工作流里责任链模式的应用 一.简单理解责 ...
- ABP 适用性改造 - 添加 API 版本化支持
Overview 在前面的文章里有针对 abp 的项目模板进行简化,构建了一个精简的项目模板,在使用过程中,因为我们暴露的 api 需要包含版本信息,我们采取的方式是将 api 的版本号包含在资源的 ...
- Leedcode算法专题训练(链表)
1.发现两个链表的交点 160.两个链表的交集(容易) Leetcode /力扣 public class Solution { public ListNode getIntersectionNode ...
- L'ane Trotro(小驴托托) 67集法语字幕+11集无字幕 百度云
<小驴托托>(L'ane Trotro)是法国经典的少儿动画片,讲述了小驴托托的生活,它的对白浅显易懂,非常适合法语初学者培养语感以及学习日常表达. 百度云链接自取:https://pan ...
- Blog总结(前三次作业总结)
前三次作业总结 1.前言 (1)第一次题目集共有8道题目,难度较为简单,知识点为JAVA的一些编程基础知识点,如输入输出,选择,循环,一维数组等. (2)第二次题目集共有5道题目,难度较第一次题目集有 ...
- 1036 Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- 【yml】springboot 配置类 yml语法
参考:https://www.runoob.com/w3cnote/yaml-intro.html YAML 是 "YAML Ain't a Markup Language"(YA ...
- MySql 按日,按周,按月 分组 统计数据
知识关键词:DATE_FORMAT 按天统计: SELECT DATE_FORMAT(create_time,'%Y%m%d') days, COUNT(caseid) count FROM tc_c ...
- [CTF]当铺密码
[CTF]当铺密码 --------------------- 作者:adversity` 来源:CSDN 原文:https://blog.csdn.net/qq_40836553/articl ...
- Redis数据结构—链表与字典的结构
目录 Redis数据结构-链表与字典的结构 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 Redis字典的使用 Re ...