MySQL
2025-02-21 08:12:50 4 举报
AI智能生成
MySQL
作者其他创作
大纲/内容
建库建表
创建数据库
create database 数据库;
删除数据库
drop database 数据库;
删除存在的数据库
drop database if exists 数据库;
删除存在的数据库
drop database if exists 数据库;
使用数据库
use 数据库名;
建表语法
create table 表名(
字段列/列名 数据类型 约束,
字段列/列名 数据类型 约束,
字段列/列名 数据类型 约束
)
字段列/列名 数据类型 约束,
字段列/列名 数据类型 约束,
字段列/列名 数据类型 约束
)
常用的约束
主键
primary key 唯一非空,一个表只有一个
外键
foreign key 引用其他表的字段
非空
not null
唯一
unique
自增
auto_increment 整数类型
用户授权
查看系统用户
select user,host from mysql.user;
配置root的远程登录
1. 修改root的主机为%
update mysql.user set host = '%' where user = 'root';
2. 刷新权限
flush privileges;
update mysql.user set host = '%' where user = 'root';
2. 刷新权限
flush privileges;
新建用户
-- 创建新用户
create user 'zhangsan'@'localhost' identified with mysql_native_password by '123456';
create user 'zhangsan'@'localhost' identified with mysql_native_password by '123456';
用户授权
grant 权限列表 on 数据库.表 to 用户名@主机;
权限列表:insert、delete、update、select、create、drop、replication slave ..\ all
数据库.* *.*
权限列表:insert、delete、update、select、create、drop、replication slave ..\ all
数据库.* *.*
外键约束
外键约束是一种约束,实现引用完整性(保证当前表添加数据,在另一个表有对应数据)
特点:
添加数据,先主后从
删除数据,先从后主
特点:
添加数据,先主后从
删除数据,先从后主
语法
创建表时创建外键
create table 表(
列名 类型 ...,
constraint 外键名 foreign key (外键列) references 主表(主键)
);
创建表之后建外键
alter table 表
add constraint 外键名 foreign key (外键列) references 主表(主键);
-- 创建饭卡表
create table meal_card(
card_id int primary key auto_increment,
student_id int not null,
money float not null
);
-- 临时添加外键
alter table meal_card add constraint fk_student_id
foreign key (student_id) references student(id);
-- 删除外键
alter table meal_card drop foreign key fk_student_id;
-- 添加饭卡
insert into meal_card(student_id,money) values(1,100),(2,150),(3,200),(4,600);
-- 不能添加主表不存在的数据
insert into meal_card(student_id,money) values(111,100);
-- 如果主表的数据被从表引用,是不能删除的
delete from student where id = 1;
create table 表(
列名 类型 ...,
constraint 外键名 foreign key (外键列) references 主表(主键)
);
创建表之后建外键
alter table 表
add constraint 外键名 foreign key (外键列) references 主表(主键);
-- 创建饭卡表
create table meal_card(
card_id int primary key auto_increment,
student_id int not null,
money float not null
);
-- 临时添加外键
alter table meal_card add constraint fk_student_id
foreign key (student_id) references student(id);
-- 删除外键
alter table meal_card drop foreign key fk_student_id;
-- 添加饭卡
insert into meal_card(student_id,money) values(1,100),(2,150),(3,200),(4,600);
-- 不能添加主表不存在的数据
insert into meal_card(student_id,money) values(111,100);
-- 如果主表的数据被从表引用,是不能删除的
delete from student where id = 1;
连接查询
内连接
将两张表中相符的数据查询出来(交集)
外连接
分为左外连接和右外连接,左外连接将左表所有数据加两表相符的数据查询出来,右表不相符的数据补空值
语法
内连接
select 表1.列 + 表2.列 from 表1 [inner] join 表2 on 主表.主键 = 从表.外键;
select 表1.列 + 表2.列 from 表1 [inner] join 表2 on 主表.主键 = 从表.外键;
左外连接
select 表1.列 + 表2.列 from 表1 left join 表2 on 主表.主键 = 从表.外键;
select 表1.列 + 表2.列 from 表1 left join 表2 on 主表.主键 = 从表.外键;
常用的数据类型
int
varchar(n)
char(n)
float
double
decimal(n,m)
date
datetime
timestamp
增删改操作
DML 数据操作语言
插入语句
insert into 表(列,列,列...) values(值,值,值...);
删除语句
delete from 表 where 条件;
清空表
delete from 表;
-- 清理表,包括自增值
truncate table student;
-- 清理表,包括自增值
truncate table student;
更新语句
update 表 set 列 = 值,列 = 值.. where 条件;
基本的查询
查询所有字段和记录(生产环境中不建议使用*)
select 列名 from 表
给字段起别名
select 列名 as 别名 from 表
select 列名 别名 from 表
select id 编号,name 姓名,age 年龄 from student;
select 列名 别名 from 表
select id 编号,name 姓名,age 年龄 from student;
查询的条件
and or in between
模糊查询
select * from 表 where 列 like '通配符'
分页查询
select * from 表 limit 开始位置,长度;
查询排序
select * from 表 order by 列 asc | desc ;
分组查询
查询时按字段将数据分组,进行统计工作
统计可以由聚合函数实现
- count 求数量
- sum 求和
- avg 求平均值
- max 求最大值
- min 求最小值
统计可以由聚合函数实现
- count 求数量
- sum 求和
- avg 求平均值
- max 求最大值
- min 求最小值
-- 聚合函数
select count(*) 人数,sum(age) 年龄总和,avg(age) 平均年龄,max(age) 最大年龄,min(age) 最小年龄 from student;
select count(*) 人数,sum(age) 年龄总和,avg(age) 平均年龄,max(age) 最大年龄,min(age) 最小年龄 from student;
分组关键字 group by
select 聚合函数|分组列 from 表 group by 分组列;
having关键字 作用:在分组之后对分组结果进行筛选,必须出现在group by 之后
where --> group by --> having
where --> group by --> having
表之间的关系
MySQL属于关系型数据库,表和表之间用外键建立关系
关系分为
1. 一对多关系 (学生和成绩、部门和员工)
实现:创建外键
2. 一对一关系(学生和饭卡、人和身份证)
实现:创建外键,外键列创建唯一约束 unique
3. 多对多关系 (学生和课程)
实现:创建中间表包含两个表的外键
关系分为
1. 一对多关系 (学生和成绩、部门和员工)
实现:创建外键
2. 一对一关系(学生和饭卡、人和身份证)
实现:创建外键,外键列创建唯一约束 unique
3. 多对多关系 (学生和课程)
实现:创建中间表包含两个表的外键
0 条评论
下一页