MySQL从入门到.....

207 阅读5分钟

数据库 database

  • 一行:一条记录
  • 一列:一个字段

对应

  • 一个表 对应java的一个类
  • 一个字段-> 类的一个属性(成员变量)
  • 一条记录-> 类的一个对象

登录数据库

mysql --host=cdb-e.cd.tencentcdb.com --user=root --password=0000000 --P10086

SQL语言的分类

  • DML数据库操作语言
    • 对表中数据进行增删改
    • insert delete update
  • DQL数据库查询语言
    • 对表中的数据进行查询
    • select from从 where条件
  • DDL数据库定义语言
    • 主要是对数据库,数据库表创建删除维护(不会操作数据)
    • create创建数据库/表 alter修改 drop删库 show查
  • DCL数据库操纵/控制语言
    • 控制数据库访问权限和创建用户

SQL语法

  • 分号结尾
  • 不区分大小写
  • 关键字建议大小
  • /**/ #(单行) -- (单行) 三种注释
  • 类型
    • 整数 int
    • 浮点型 double
    • 字符串型 varchar (10)可变的字符
    • 日期类型 date 时间日期 datetime

DDL 数据库

  • 创建数据库
create database 数据库名;
create database db297 charset utf8;

# 删
drop database db297;

# 查正在使用的数据库
select database();

DDL 数据库表

# ***重点***
create table 表名(
    字段名1 数据类型1(长度) [约束],
    字段名2 数据类型2(长度) [约束]
);

CREATE TABLE student3(
	sid INT PRIMARY KEY,
	sname VARCHAR(30) ,
	sage INT
); 

# 删除表
drop table 表名;

# 查看一个表的详细信息
desc 表名
show tables; -- 查看所有表(了解

# 修改表名
rename table 表名 to 新表名;

# 修改表中的列
## 增删改查
alter table 表名 add 列名 类型(长度) [约束];
alter table 表名 drop 列名; -- 删除列
#查
desc 表名
#改
# 可以修改列的名字 类型 约束
alter table 表名 change 旧列名 新列名 类型 约束;
alter table 表名 modify 列名 类型 约束;

DML 数据库操作语言

对表中的数据进行增删改

增:插入一条数据

insert into 表名 列名1,列名2 values (值1,值2...);
-- 列名选择一列或几列 后面的值和前面的列一一对应

INSERT INTO student (sid,sname) VALUES ('001','李四');
INSERT INTO student (sid,sname,sage) VALUES ('002','王五',18);
INSERT INTO student VALUES ('003','赵六',28); -- 插入所有字段 列可以省略

删:删除一条记录

delete from 表名; -- 删除表中所有记录
delete from 表名 where sid = '3';-- 删除1条记录 

改:修改表中的数据

update 表名 set 字段名 = 值;
update students set sage=20; -- 把所有记录都改了
update students set sage=20 where sname='王五';

DQL 查询数据库中的数据

select * from 表名; # 查询表中所有列的数据

# 查询指定列
select pname,price from product;

# 去重查询
select distinct price from product;

# 运算查询 所有结果都+10
select price+10 from product; -- 列名也变成+10
select price+10 as 打折价格 from product; 

条件

  • 大小 > < <= >= = <>不等于 !=
  • 区间 between ... and ... (值 日期)
  • 在范围内 in in(100,200)
  • like 模糊查询 '张%'
    • %表示0个或者多个任意字符
    • _表示一个字符
  • is NULL / IS NOT NULL
  • and or not
# 带有条件的查询 where

# 查询为空的商品  不能用 =NULL
select * from product where category_od is null;
# 判断不为空
select * from product where category_od is not null;

约束

  • 主键约束 primary key 必须唯一,而且不能为空

  • 自动增长阅读 auto_increment 被自动增长约束,值可以不用管,

CREATE TABLE users(
	uid INT PRIMARY KEY AUTO_INCREMENT,
	uname VARCHAR(32),
	upass VARCHAR(32)
);

INSERT INTO users(uname,upass) VALUES('老王','12345')

删除表中所有数据

delete from tbname; -- 删除表中所有数据 但是自动增长不会重置为1

truncate table tbname -- 删除表的所有数据 并且重置自动增长
# truncate 摧毁表 然后重建

解决乱码问题

  • 临时 set names gbk;

排序查询

关键字: order by

  • 从大到小 DESC
  • 从小到大[默认] ASC 升序
select name,price from product order by price desc;

聚合查询

纵向个数查询

特点: 是单一值

  • count(会自动忽略空值)
select count(*) from product; -- 有几条记录
  • max 求最大值
  • min 求最小值
  • avg 求平均值
  • sum 求和

分组查询

把查询数据分为几个组

//先分组 再聚合

只能写聚合函数和分组的列名

关键字: group by 狗肉铺白

select count(*) from product group by category_id;

select count(*) from product group by category_id having count(*) > 1;

select category_id,count(*) from product group by category_id having count(*) > 1;

where写在基本查询后面
having卸载分组查询后面
where后面不能写count sum等聚合函数

分页查询

只查询记录这种的一部分 关键字 limit 数值1(从0开始的下标),数值2(查询的数量)

select * from product limit 0,5;

查询的公式:
一页n条

1页 limit 0,n
第2页 limit n,n
第100页 limit (100-1)n,n
第m页 limit (m-1)n,n

SQL约束

  • 实体完整性:对数据行约束,比如主键约束PRIMARY KEY,唯一约束unique
  • 域完整性:对数据列约束,比如数据类型 默认约束default 非空约束not null
  • 引用完整性:外键约束(多表的关系)
  • 自动增长约束auto_increment
create table books(
    bid int primary key auto_increment,
    bname varchar(50);    
);

多表

  • 一对多
    • 学生和成绩
    • 分类和商品
    • 客户和订单
    • 部门和员工
  • 多对多(3张表)
    • 学生和教师
    • 商品和订单
    • 学生和课程
  • 一对一
-- 外键约束
alter table 中间表 add constraint 取个名字_fk
foreign key(列) references 左边表(主键)

alter table 中间表 add constraint 取个名字_fk
foreign key(列) references 右边表(主键)

多表查询:

  1. 交叉查询(笛卡尔积) select * from 表1,表2 (没有意义)
    总字段=表1字段+表2字段
    总记录数=表1记录数×表2记录

  2. 内连接查询 inner join

主键=外键

  • 隐式内连接查询(不写inner join)
    select * from 主表,从表 where 主表.主键 = 从表.外键
  • 显示内连接查询
    select * from 主表 innerr join 从表 on 主表.主键 = 从表.外键
  1. 外连接查询 outer join

左外连接以左表为准,左边必须都有,没有的补空

  • 左外连接 left outer join
    select * from 主表 left outer join 从表 on 主表.主键 = 从表.外键
  • 右外连接 right outer join select * from 主表 left outer join 从表 on 主表.主键 = 从表.外键

子查询

-- 子查询
select * from products where category_id = (select cid from categgory where cname = '化妆品');

-- 内连接
select p.* from products p,category c where p.category_id = c.cid AND c.neme = '化妆品';