Mysql

数据库简介

一、什么是数据库(database,简称DB)
狭义:存储数据的仓库
广义:对数据进行存储和管理的软件及数据本身称数据库
二、数据库的组成
1、库–>表–>数据(代表一对多,例:一个库含有多个表)
(从使用者观点看,数据库主要由文档(documents),记录(records),和字段(fields)三个层次构成)
三、为什么学习数据库
1、在测试过程中要编写或使用SQL语句,查询或监控底层数据sh
四、如何连接数据库(Cmd命令窗口连接mysql命令大全)
1、客户端(Navicat)(网上教程)
2、命令连接。 mysql -h(数据库ip地址)-u用户名 -P端口号 -P密码
例:mysql -uroot -p
b1、创建库 表 数据
a、create database 数据库名字(英文); 创建数据库
b、show databases; 展示库
c、use 数据库名字; 使用数据库
d、drop database 库名; 删除库
库名一经创建不可修改
e、create table 表的名字(英文)(字段名,数据类型【约束】); 创建表
f、show tables; 展示表
g、desc 表名; 查看表结构
h、exit 退出库
i、drop table 表名;删除表(删除表则表中数据一起删除)
j、insert into 表名()values(字段值,字段值2….);插入数据
k、insert into 表名(字段名,字段名…) values (字段值,字段值2….); 插入数据(保持顺序一样)
l、insert into 表名()values(字段值,字段值2….),(字段值,字段值2….);插入多条数据数据
m、select * from 表名;查询所有数据
n、select * from 表名 where 字段名=字段值(name like ‘张三’)
查找表中符合条件的数据
o、给表新增一列
alter table 表名 add 字段名 数据类型
eg:alter table test001 add newyear year;
p、修改更新数据
update 表名 set 字段名 =新值 where表达式
eg:update test011 set newyear=‘2019’
q、重命名一个表名
rename table 旧表名 to 新表名
Alter table 旧表名 to新表名;
r、修改字段类型
Alter table 表名 modify column 字段名 新类型

Alter table new011 modify column newyear date

s、修改列
Alter table 表名 change 旧字段名 新字段名 新字段类型
eg:Alter table new011 change newyear name varchar(30);

t、删除数据(只删数据,保存表名)
Delete from 表名 where 条件表达式

u、show create table 表名;查看建表语句
alter table 表名 convert to character set utf8; 为表叫上utf8 注释中文
v、删除列:alter table 表名 drop column 列名;

w、外键
在这里插入图片描述在这里插入图片描述

第八天
数据库

一、数据类型
数据类型分数值型、字符型
数值型分整型、浮点型两种
整型分有符号 无符号两种
tinyint(微小整型)
smallint(小整型)
mediumint(中整型)
int(整型)
bigint(大整型)
浮点型
单精度浮点型float
双精度浮点型double

1、整型:tinyint(m) unsigned zerofill
a、m代表数值长度,不影响取值范围
b、unsigned(无符号),影响取值范围,不影响数值长度
无符号:0–255
有符号:-128—127
c、zerofill:0填充,与m配合使用,当输入数值的长度小于m时,系统会用0自动填充
例:create table test001(age tinyint(3) unsigned zerofill);
2、浮点型
a、float(m,z)(单精度):
a1、m代表数值总长度,z代表小数点位数(整数位数=m-z)
a2、缺点:丢失精度(四舍五入)
a3、取值范围:-3.4E+38~3.4E+38(3.4乘于10的38次方)
例:float (5,2)数值总长度5位,小数点2位,整数位数3位
insert into test001(hight float(5,2)) values(183.429);
显示数值:183.43
超过定义的小数位数时,系统自动对数值进行四舍五入
超过定义的整数位数时,系统自动报错或显示最大值(缺少精度)

b、double(双精度):用法与float相同
不同之处:double比float精度要高,占的字符比float要高

二、字符型
1、char(m)
a、m代表字符长度,当输入的字符长度小于m时,系统会在字符后自动用空格填充,当输出时,系统会自动把空格过滤掉
b、 若输入的字符后含有空格时,数据不会原样输出,输出时系统会自动把空格过滤掉
c、char 空间利用率低,数据读取速度较快
2、varchar(m)
a、m代表字符长度,当输入的字符长度小于m时,系统会在字符后不会自动用空格填充
b、若输入的字符后含有空格时,输出时系统会原样输出
c、varchar空间利用率高,读取速度慢
例:create table test009(name char(5))charset utf8;
create table test009(name varchar(5))charset utf8;
insert into test009() values('张三 ');
insert into test009() values('张三 ');
Select * from test009 where name like '张三 ’
Select * from test009 where name like '张三 ’
Char:查到张三 分几个单位用几个,没有的用空格代替。 空间利用率低
Varchar:张三 (含有空格)。 用几个占几个单位,多余单位可供别的使用 空间利用率高
三、时间日期
1、date 000-01-01/9999-12-31
2、time ‘-838:59:59’/‘838:59:59’
3、datetime 1000-01-01 00:00:00/9999-12-31 23:59:59
4、year 1901/2155

四、约束
1、主键约束(PRI主键意思,只能有一个)
primary key
特点:不能为空且不能重复
create table student(id tinyint(4) zerofill primary key,name char(10),age tinyint(3) unsigned)charset utf8/gbk(gbk只解释中文);
2、唯一约束。unique
特点:不重复
create table student(id tinyint(4) zerofill primary key,name char(10)unique,age tinyint(3) unsigned)charset utf8;
3、默认约束
default
特点:若不输默认值,则使用规定的默认值
create table student(id tinyint(4) zerofill primary key,name char(10)unique,色系char(5)default ‘男’)charset utf8;
4、非空约束
not null
特点:不能为空
create table student(id tinyint(4) zerofill primary key,name char(10)unique,色系char(5)default ‘男’,age tinyint(3) unsigned not null)charset utf8;
5外键约束
foreign key (字段名) references 表名 (字段名)
Create table score(stuid tinyint(4)zerofill)

where条件表达式
1、精确查询
= > < >= <= !=
2、模糊查询
Like/not like
通配符 %:匹配0~n个字节
_:匹配一个字节
3、集合运算
In /not in (注释:有限的集合,一一列举出来)
4、非空运算
Is Null /is not Null
5、区间运算
Between…and…
6、逻辑运算
And/or
And or同时出现,先计算and后计算or
排序
Select * from 表名order by 字段名1,字段名2【asc/desc】;
默认升序
asc:升序
desc:降序
【】代表可选
eg:select * from 表名 where 条件 order by 字段名;
分组
Select * from 表名group by 字段名1,字段名2;

聚合函数
含义:求值函数
1、Count 求总个数
Count (字段名)
Select count(字段名) from 表名 group by 字段名;
2、sum(字段名)求和
Select sum(字段名) from 表名 group by 字段名;
4、avg(字段名)求平均数
Select avg(字段名) from 表名 group by 字段名;
eg:Select bj.avg(nl) from xsb group by bj;
5、max(字段名)求最大值
Select max(字段名) from 表名 group by 字段名;
eg:Select bj.max(nl) from xsb where bj=‘1班’ group by bj;
6、min(字段名)求最小值
Select min(字段名) from 表名 group by 字段名;
eg:Select bj.min(nl) from xsb where bj=‘1班’ group by bj;

先where 后group by 最后order by
eg:Select bj.min(nl) from xsb where bj=‘1班’ group by bj order by…

关键字

1、having条件表达式

where和聚合函数不能同时使用 使用having

先执行where 在执行group。在执行order 最后having执行语句
Select * from cjb where kch=001 group by xh order by cj having avg(cj)>60;

Select * from xsb having nl= min(nl);也可以使用

2、Distinct 去重,去掉重复的
eg:select distinct jg from xsb ;(distinct jg 之间没有点)
多表查询
嵌套查询
一个sql语句嵌套在另一个sql语句中
Select * from 表名 where A.x =/in (sql)
嵌套特点:逻辑关系复杂,(对逻辑要去较高)
查询效率高,记录永不增长

eg:select cj from cjb where xh in (select xh from xsb where bj =‘2班’ and xm = ‘张三’)and kch=(select kch from kcb where kcm = ‘数据库技术’);

等值连接

表名.字段名
eg:select cj from cjb,xsb,kcb where cjb.xh= xsb.xh and kcb.kch=cjb.kch and bj=‘3班’ and xm=‘张三’ and kcm=‘数据库技术’;

内连接

inner jion

Select * from A inner jion B on A.x =B.x (inner jion C on 条件)

外连接

外连接分左外连接(左连接) ,右外连接(右连接)

左连接:Select * from A left jion B on A.x =B.x (left jion C on 条件)
显示1。2 全部消息,没有的显示null
左连接:以左表为准,左表向右表匹配,匹配不上的,左表正常显示,右边信息显示为null

右连接:Select * from A right jion B on A.x =B.x (right jion C on 条件)
显示2。3全部消息,没有的显示null
右连接:以右表为准,右表向左表匹配,匹配不上的,右表正常显示,左边信息显示为null

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值