当前位置:网站首页>肖sir__面试就业课___数据库
肖sir__面试就业课___数据库
2022-08-03 03:33:00 【多测师肖sir】
数据库:
ddl 数据库定义语言(对表结构的操作)
dml 数据库操作语言(增删改查对表数据)
一、概括
1、单表:
(1)增 insert into values
(2)删 delete (delete < truncat <drop)
(3)改 update set
(4)查 select
语句: like % 模糊查询、limit 限制行数 、 order by desc 或asc 排序、group by having 分组、
聚合函数:sum 、count 、max、min、avg 、distinct、
2、多表:
总结:
一、多表:
普通内连接:select * from 表1 inner join 表2 on 表 1.关联字段=表2.关联字段 (关联的数据显示,不关联的不显示)
隐藏内连接:select * from 表1,表2 where 表 1.关联字段=表2.关联字段 (关联的数据显示,不关联的不显示)
左连接:select * from 表1 left join 表2 on 表 1.关联字段=表2.关联字段 (左表全部显示,右表关联的数据关联,不关联的数据以null显示)
右连接 :select * from 表1 right join 表2 on 表 1.关联字段=表2.关联字( 右表全部显示,左表关联的数据关联,不关联的数据以null显示)
左独有数据:select * from 表1 left join 表2 on 表 1.关联字段=表2.关联字段 where 表2中的字段 is null
右独有数据:select * from 表1 right join 表2 on 表 1.关联字段=表2.关联字段 where 表1 中的字段 isnull
全外连接:union
(1)左独有+右独有+内连接
(2)左连接+右独有
(3)右连接+左独有
二、
三表内连接合并:
方法一:三表的隐藏内连接
格式:
SELECT * FROM 表1 as 别名, 表2 as 别名, 表3 sc as 别名 where 表1.关联字段1=表3.关联字段3 and 表2.关联字段2= 表3.关联字段3
案例:SELECT * FROM student as a,course as b,sc as c where a.stu_no=c.stu_no and b.c_no=c.c_no
方法二:三表普通内连接
SELECT * FROM 表1 as 别名 inner join 表3 as 别名 on 表1.关联字段1=表3.关联字段3 inner join 表2 as 别名 on 表2.关联字段2= 表3.关联字段3
案例SELECT * FROM student as a inner join sc as c on a.stu_no=c.stu_no INNER JOIN course as b on b.c_no=c.c_no
方法三:三表左连接
格式:
SELECT * FROM 表1 as 别名 left join 表3 as 别名 on 表1.关联字段1=表3.关联字段3 left join 表2 as 别名 on 表2.关联字段2= 表3.关联字段3
案例:SELECT * FROM student as a left join sc as c on a.stu_no=c.stu_no LEFT JOIN course as b on b.c_no=c.c_no
方法四:三表右连接
格式:SELECT * FROM 表1 as 别名 right join 表3 as 别名 on 表1.关联字段1=表3.关联字段3 right join 表3 as 别名 on 表2.关联字段2= 表3.关联字段3
案例:SELECT * FROM student as a right join sc as c on a.stu_no=c.stu_no right JOIN course as b on b.c_no=c.c_no
方法五:先合两个表在和合一个表的方法
SELECT * FROM (SELECT c_no FROM student as a,sc as c where a.stu_no=c.stu_no ) s INNER JOIN course as b where s.c_no=b.c_no
3、视图
(1)优点:1、查询复杂的语句更方便 2、更安圈全、保密字段
(2)创建视图:create view 视图名 as (sql语句)
4、索引
(1)索引优点:1、查询速度(group by ,order by) 2、保证数据的唯一性、3、实现表与表的参照性
(2)主键索引(parmary key)、唯一索引(unique)、普通索引 (index)
(3)建立索引的方式:1、建表 时创建 2、 alter table 添加字段或修改字段
5、外键
(1)让一张表记录的数据不要太过于冗余,
(2)优点:保持数据的一致性
(3)foreign key 外键
一、建表时添加外键
create table 表名 (字段 字符类型(字符宽度) primary key ,(字段2 字段类型2(字符宽度3),constraint 外键名 foreign key(子表字段) references 父表(父表的字段))engine=innodb default charset=utf8;
二、建表后添加外键
格式:alter table 表名 add constraint 外键名 foreign key(子表字段) references 父表( 父表字段)
6、存储
(1)造数据
(2)存储格式:
delimiter // 分隔符/定格符
create procedure 存储过程名( in/out/inout)参数
begin —开始
sql语句 ; —执行的语句
end —结束
// 分隔符
call 存储过程名称() 调用一个存储过程
(3)案例:造数
delimiter //
drop table if exists money;
drop procedure if exists pro4;
create procedure pro4(in x int)
begin
declare i int default 0;# 变量名称i int 数据类型 默认为0
drop table if exists money;
create table money(id int primary key auto_increment,money int(10));
while(i<x)DO
insert into money(money) values(100);
set i=i+1;
end while;
select * from money;
end
//
call pro4(20)
边栏推荐
- 【数据分析】基于MATLAB实现SVDD决策边界可视化
- els 消除行
- 再讲Promise
- 【obs】启动推流失败 : Output.StartStreamFailed 调用流程
- sql问题,如何能做到先声明表的名称,例如product202201,表示2022年一月份的货物表,再在声明过的表中查找,下面的代码运行时有错误显示找不到表table_name,请问改如何进行修改
- 移植RT-Thread编译报错thumb conditional instruction should be in IT block
- 【无标题】2022-7-24
- Auto.js Pro 计算脚本运行时间
- 有大佬知道 使用flinksql是 同步的日期字段为null的话怎么处理吗
- MySQL【约束】
猜你喜欢
随机推荐
ClickHouse—入门
一文了解SAP IBP是什么?
道通转债,微芯转债,博22转债上市价格预测
Auto.js Pro 编写第一个脚本hello world
MATLAB(5)绘图
【数据分析】基于MATLAB实现SVDD决策边界可视化
compose 位移视图
第三方支付--分账对接
mysql8默认密码丢失,如何更改密码详细步骤??
Sentinel vs Hystrix 限流对比,到底怎么选?
ClickHouse卸载、重安装
DPDK mlx5 驱动使用报错
什么是数据标注? 数据标注公司主要做什么?
金仓数据库 MySQL 至 KingbaseES 迁移最佳实践(3. MySQL 数据库移植实战)
Jincang Database Pro*C Migration Guide ( 5. Program Development Example)
ClickHouse—高级
金仓数据库 Pro*C 迁移指南( 4. KingbaseES 的 Pro*C 迁移指南)
阿里面试官:聊聊如何格式化Instant
高等代数_证明_不同特征值的特征向量线性无关
ClickHouse - Getting Started








