MySQL 第一部分

1.mysql简介:数据库管理系统,现在在oracle旗下
2.mysql客户端:1)查看数据库 show databases;
2)进入数据库 use test;
3)查看数据表 show tables;
4)查看表结构 desc user;
5)退出登录,断开连接 exit;quit;\q
3.建库、建表 1)建库 drop database if exists db1;
create databases db1 charset utf8;
2)建表 drop table if exists sallary;
sallary(id int,name varchar(20),gender char(1));
4.数据类型 1)数字 unsigned 无符号,只有正数
zerofill 配合显示位数,如果不足,补0
tinyint\smallint\int int ,在查询结果中,不足三位按三位显示,]
bigint float double[运算不准确] decimal(4,2),总共4位。小数两位
2)字符串 1)char char(6) 超出6个,根据数据库设置,可能出错、截断
定长字符串,存储访问效率高
字符串长度不足、会补空格
最长255个字符
2)varchar(6)
变长字符串,存储访问效率低于char
最长不超过65535字节,超过255字节,使用text
3)text 长文本类型,最长65535字节
3) date 年月日
time 时分秒
datetime 年月日时分秒
timestamp 时间戳,与datatime存储相同的数据,最大表示2038年
在插入、修改数据时可自动更改为系统当前时间
5.sql 结构化查询语言
DDL 数据定义语言(建库建表)
DML 数据操作(增删改)
DQL 数据查询
6.中文数据 set names gbk 把客户端编码告诉服务器
7.insert 插入数据
insert into sal valuea(6,‘李四’,‘男’);
insert into sal (id,name)values(4,‘王五’);
8.update 更新、修改数据
update sal set name=‘张思’,age=‘15’ where id=5;
9.delete 删除数据
delete from sal where id=8;
delete from sal where id>8;
10.select 查询数据
select * from sal 查询所有
select id ,name from sal; 查询指定字段
11.where子句 设置过滤条件

= 等值过滤 like% 通配多个
<> 不等过滤 like_ 通配单个
****> >= < <= and 并且
between 小 and 大 or 或者
in (2,3,4) 在指定的值中取值
is null is not null
12.distinct 去除重复值
select distinct a from 去除a字段重复值
13.order by 排序
order by a 按a字段升序排序
order by a ,b 按a字段升序排序,再按b字段升序排序
asc 升序(默认)
desc 降序
14.查询执行顺序
1)select 字段
2)from
3)where 过滤
4)选取字段
5)order by 排序
15.单引号 字符串内容中的单引号,用两个单引号转义
16.函数
1)字符串函数 char_length(字符串) 字符数
length(字符串) 字节数
left(字符串, length) 获得左侧字符
substring(字符串, start, length) 截取字符串
instr(字符串, 子串) 查找子串位置
concat(s1,s2,s3…) 字符串连接
lpad(字符串,8,’
’) 左侧填充
*2)数字函数 ceil(数字) 向上取整到个位
floor(数字) 向下取整到个位
round(数字, 2) 四舍五入到小数点2位
truncate(数字, 2) 舍弃到小数点2位
rand() 随机数[0, 1)
rand() 随机数[0, 1)
3)日期函数
curdate() 当前日期
curtime() 当前时间
now() 当前日期时间
extract(字段 from 日期) 抽取指定字段的值
date_add(日期, interval 字段 值) 在指定字段上加一个值
datediff(日期1, 日期2) 两个日期之间相差的天数

           4)null值函数    ifnull(a,b)
                           a不是null  返回a
                           a是null    返回b

**17.**多行函数、聚合函数 sum()和 avg()平均 max()最大 min() 最小
count()行数、多行函数不能和其他普通字段一起使用
18
. group by 子句,分组求多行函数 [按指定字段中相同的值进行分组]
19.having 用来对多行函数结果进行过滤
having 和 where 作用相同,都是条件过滤
where 过滤普通条件,最早执行
having 过滤多行函数结果,分组,求完多行函数后,才执行
having 跟在 group by 后面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值