Mysql基础知识复习

0x00 前言

重新好好学习渗透,从复习基础知识开始,基础真的很重要

0x01 表操作

0x011 创建表

语法:

create table 表名称(

​ 字段1 类型(长度) 约束,

​ 字段1 类型(长度) 约束,

​ 字段1 类型(长度) 约束

);

注意:

字符串数据的类型,长度是必须的

如果是int类型,则长度默认为11

0x012 数据类型

字符串型:

​ VARCHAR(用得较多):长度可变,节省空间

​ CHAR:长度固定,输入不够用空格补全

大数据类型(不常用):

​ BLOB:存储字节

​ TEXT

数值型:

​ TINYINT,SMALLINT,INT,BIGINT,FLOAT,DOUBLE

逻辑型:

​ BIT

在Java中是true或是false

在数据库中bit类型,1或0

日期型:

​ DATE:只包含日期 年月日

​ TIME: 只包含时分秒

​ DATATIME:年月日和时分秒

​ TIMESATMP:年月日时分秒,如果传入空值,默认获取当前系统的时间保存到字段中

0x013 表约束

唯一约束:unique

create table table1(
	phone char(111) unique
)

主键值:primary key

create table table1(
	phone char(111) primary key
)

0x014 练习:建表

create table employee(
    id int,
    name varchar(30),
    gender char(1),
    birthday date,
    entry_data date,
    job varchar(50),
    sal double,
    resume text
);

//查看建表信息:

show create table 表名;

0x015 操纵表命令

查看库里的表:show tables;

查看表的详细信息: desc 表名;

查看表创建信息: show create table 表名;

删除表: drop table 表名;

0x016 修改表

加一个字段
alter table employee add image varchar(50);
修改字段
alter table employee modify image varchar(40);
修改字段名称
alter table employee change 旧字段 新字段 数据类型长度 约束;
删除字段
alter table employee drop image;
该表名为user
rename table employee to user;

0x02 数据操作

0x021 向表中添加数据

insert into 表 (字段1,字段2,字段3) values (值1,值2,值3);

insert into 表 values (值1, 值2,值3,值4.。。); 向表中所有字段添加值

注意点:

插入的数据结构要和表中设定的相同

字符串和日期类型要用单引号括起来

insert into user values (1,'小明','男','2000-11-11','2010-11-11','cooker');

0x022 解决中文乱码

dos窗口默认的编码是gbk,mysql客户端和服务器都是utf-8

修改my.ini

[client]
port=3306
[mysql]
default-character-set=utf8

改成gbk

0x023 修改数据语句update

update 表名称 set 字段1=值1,字段2=值2 where 条件

(如果没有where则全改)

练习:

update user set job='coder';
update user set job='bear' where name='雄大';
update user set job='BEAR',gender='熊' where name='雄大';
update user set sal = sal + 1000 where id=5;

0x024 删除数据

delete from 表名 where 条件

delete from user where name='小明';

删除所有的数据:

delete from 表:

​ 支持事务的操作,一行一行的数据删

start transaction;
delete from user;
rollback;

truncate 表:

​ 不支持事务的操作,把整张表都删掉,创建一张新的表(空的)

0x025 查询数据

select * from 表;

select 字段1,字段2,字段3 from 表;

DISTINCT 去除重复的关键字

​ select DISTINCT english from stu;

可以对查询的列进行运算:

​ select username,english+10,math+10,chinese+10 from stu;

​ select username,(math+english+chinese) from stu;

查询语句中可以使用as的关键字,起别名

​ select username,(math+english+chinese) as t from stu where name=‘美美’;

​ select username,(math+english+chinese) as t from stu where (math+english+chinese) > 200

但是不可以这样写

​ select username,(math+english+chinese) as t from stu where t > 2;

0x0251 where子句

select * from stu where english where english = 100;
select * from stu where english where english in (80,90,97);

like 模糊查询
select * from stu where english where username like ‘张_’;
下划线为占位符,占一个字节
select * from stu where english where username like ‘%张%’;
百分号为通配符

​ select * from stu where english > 60 and english < 90;
特有的关键字between 包含大于等于和小于等于
​ select * from stu where english between 60 and 90;
​ select * from stu where english in (89,90);
​ select * from stu where english >60 and chinese < 90;

0x0252 order by

order by 字段 asc | desc;

adc 升序(默认)

desc 降序

放在sql语句的末尾

select username,(chinese+math+english) as t from stu order by t desc;

可以这样写 order by math desc,chinese desc;

先按照数学降序,再按语文降序

0x0253 聚集函数

统计某一列数据的函数

count:

求数量

select count(列名) from 表名

select count(math) from stu where math >80;

sum:

求和,只对数学数值有作用

avg:

求一个字段的平均

max(),min():

求最大值和最小值

0x026 分组查询

select * from stu 默认使用的分组查询,分成一组

关键字:group by 先分组,再操作查询

select count(*) from stu; 默认一组,统计一组人数

select count(*) from stu group by sex;分成两组,分别统计两组人数

先分组再过滤:having,只能放在group by后面:

//分组后总价大于100

select product,sum(price) from orders group by product having sum(price) > 100;

//商品价格大于100,然后要求总价大于100

select product,sum(price) from orders where price > 100 group by product having sum(price) > 100;

where后面不加聚合函数

0x027 约束

0x0271 唯一和非空约束

唯一的约束 unique

非空 not null

0x0272 单表的约束(主键)

某一字段为主键,满足特点:非空,唯一,被引用

声明主键:primary key

create table person(
	id int primary key,
	username varchar(30) 
);

主键的自动增长:

只能用int,bigint类型

通过关键字 auto_increment

create table person(
	id int primary key auto_increment,
	username varchar(30) 
);

0x0273 多表约束(外键)

外键约束的作用:

目的是保证表结构中数据的完整性,在多表间建立关系

设置外键:

目的:把dno字段设置成外键,映射到部门表主键上,外键字段的取值从部门主键取得值

create table emp(
	eid int primary key auto_increment,
	ename varchar(30),
	sal double,
	dno int,
	foreign key (dno) references dept (did)
)

设置外键dno,从dept表中的did字段取值

0x028 多表设计

0x0281 三种设计方式

一对一,一对多,多对多

一对多

需求场景:

例如:员工表 多方 -> 部门结构表 一方

​ 订单表 多方-> 用户表 一方

主表(一方) 从表(多方)

建表原则:在多方表中添加字段,把该字段作为外键,指向一方表的主键

多对多

订单表->商品表

用户与角色

建表原则:以用户表和角色表例子,需要建立3张表,其中有一张中间表,存储数据关系。

多对多可以拆分成2个一对多的场景

在这里插入图片描述

0x029 多表查询

0x0291 笛卡尔积

直接查询两个表时,会产生笛卡尔积,有重复数据产生

0x0292 内连接

普通内连接查询:

select * from dept inner join emp on did = dno;

inner join 内连接

后面的on,实现主外键关联

隐式内连接查询:

select * from dept,emp where did = dno;

where实现主外键关联条件

select * from dept d,emp e where d.did = e.dno;

省略了as,因为可能有重名字段所以大都这样写

select d.dname,e.ename from dept d,emp e where d.did = e.dno;

0x0293 外连接

左外连接:

select * from dept d left outer join emp e on d.did = e.dno;

outer关键字可省略不写

右外连接:

select * from dept d right join emp e on d.did = e.dno;

0x0294 区别

内连接和外连接的区别:

内连接查询的是两张表中交集的数据

左外连接查询的是左表中所有的数据+两张表主外键关联的数据

右外连接查询同理

0x030 子查询

通过多个select语句查询结果

把查询语句的结果当作另一个查询语句的条件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值