当前位置:网站首页>集合集合!!快来复习--mysql增删改查,内、左右连接 复习笔记
集合集合!!快来复习--mysql增删改查,内、左右连接 复习笔记
2022-06-25 23:53:00 【两掺豆腐脑丶】
创建了两个案例数据库分别为 MyBingSchool、employee_tbl
逻辑很清晰,SQL使用简单。多多复习
– 创建MyBingSchool 设置字符集为utf8(万国码)
create database MyBingSchool CHARSET=utf8;
– 进入数据库
use MyBingSchool;
– 删除表(结构和数据)
drop table Student;
– 学生信息表
create table Student(
-- 主键 学生编号 varchar不能使用default
s_id varchar(20) COMMENT '学生编号',
s_name varchar(20) UNIQUE NOT NULL COMMENT '学生姓名' ,
s_sex varchar(10) NOT NULL COMMENT '学生性别' ,
s_brith varchar(20) NOT NULL COMMENT '学生出生年月' ,
primary key(s_id)
)ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
– 查询表数据
SELECT * FROM Student;
select * from Cuosre;
select * from Sorce;
select * from Teacher;
– 向Student表插入数据
insert into Student VALUES('01','张三','男','1996-01-24');
insert into Student VALUES('02','王二','男','1998-09-01');
insert into Student VALUES('03','李四','男','1978-03-13');
insert into Student VALUES('04','赵雷','男','1996-04-24');
insert into Student VALUES('06','雷子','女','2000-04-24');
insert into Student VALUES(DEFAULT,'小花','女','1996-05-24');
– VARCHAR 插入时也不能使用default
– 修改插入数据
UPDATE Student set s_id='05' where s_name='小花';
– 删除编号为06的数据
DELETE from Student WHERE s_id='06';
– 课程表
drop table Cuosre;
create table Cuosre(
c_id VARCHAR(20) COMMENT '课程编号',
c_name varchar(20) UNIQUE NOT NULL COMMENT '课程名',
t_id varchar(20) NOT NULL COMMENT '教师表id外键',
PRIMARY key (c_id),
CONSTRAINT fk_Cuosre_Teacher foreign key (t_id) references Teacher(t_id)
)ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
select * from Cuosre;
select * from Sorce;
select * from Teacher;
insert into Cuosre VALUES('01','高数一','01');
insert into Cuosre VALUES('02','英语','02');
insert into Cuosre VALUES('03','政治','03');
– 教师表
drop table Teacher;
create table Teacher(
t_id VARCHAR(20) primary key comment '教师编号',
t_name VARCHAR(20) UNIQUE not null comment '教师名'
)ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
select * from Sorce;
select * from Teacher;
insert into Teacher VALUES('01','孙风');
insert into Teacher VALUES('02','李云');
insert into Teacher VALUES('03','周梅');
insert into Teacher VALUES('04','吴兰');
– 成绩表
drop table Sorce;
create table Sorce(
s_id VARCHAR(20) comment '成绩表编号',
c_id VARCHAR(20) comment '外键、课程表编号',
-- 组合主键
s_sroce int(3),
-- 设置组合主键
primary key (s_id,c_id),
-- 单外键 注意指定于指定的表对应
CONSTRAINT fk_Sorce_Cuosre foreign key (c_id) references Cuosre(c_id)
)ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
select * from Sorce;
insert into Sorce VALUES('01','01','99');
insert into Sorce VALUES('01','02','89');
insert into Sorce VALUES('01','03','99');
insert into Sorce VALUES('02','01','89');
insert into Sorce VALUES('02','02','60');
insert into Sorce VALUES('02','03','50');
insert into Sorce VALUES('03','01','100');
insert into Sorce VALUES('03','02','90');
insert into Sorce VALUES('03','03','70');
insert into Sorce VALUES('04','01','79');
insert into Sorce VALUES('04','02','80');
insert into Sorce VALUES('04','03','50');
– GROUP BY学习表数据
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `employee_tbl`
-- employee_tbl 表的结构
-- ----------------------------
DROP TABLE IF EXISTS `employee_tbl`;
CREATE TABLE `employee_tbl` (
`id` int(11) NOT NULL,
`name` char(10) NOT NULL DEFAULT '',
`date` datetime NOT NULL,
`singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT '登录次数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `employee_tbl`
-- employee_tbl 表的数据记录
-- ----------------------------
BEGIN;
INSERT INTO `employee_tbl` VALUES ('1', '小明', '2016-04-22 15:25:33', '1'), ('2', '小王', '2016-04-20 15:25:47', '3'), ('3', '小丽', '2016-04-19 15:26:02', '2'), ('4', '小王', '2016-04-07 15:26:14', '4'), ('5', '小明', '2016-04-11 15:26:40', '4'), ('6', '小明', '2016-04-04 15:26:54', '2');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
set names utf8;
SELECT * FROM employee_tbl;
– 给字段列起别名 fild1 as ‘’
SELECT name,COUNT(*) as '总数' from employee_tbl GROUP BY name DESC;
– WITH ROLLUP 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)。
SELECT name ,sum(singin) as '歌曲' FROM employee_tbl GROUP BY name with ROLLUP;
– coalesce 来设置一个可以取代 NUll 的名称
SELECT coalesce(name,'总数') ,sum(singin) as singin_count FROM employee_tbl GROUP BY name with ROLLUP;
SELECT * FROM Student;
select * from Cuosre;
select * from Sorce;
select * from Teacher;
– INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。等价于where
SELECT c_id as '编号' ,c_name as '课程名' , t_name as '老师' from Cuosre INNER join Teacher ON Cuosre.t_id=Teacher.t_id;
– INNER JOIN等价于where
SELECT c_id as '编号' ,c_name as '课程名' , t_name as '老师' from Cuosre , Teacher WHERE Cuosre.t_id=Teacher.t_id;
– LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
SELECT c_id as '编号' ,c_name as '课程名' , t_name as '老师' from Cuosre LEFT join Teacher ON Cuosre.t_id=Teacher.t_id ;
– RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
SELECT c_id as '编号' ,c_name as '课程名' , t_name as '老师' from Cuosre RIGHT join Teacher ON Cuosre.t_id=Teacher.t_id;
边栏推荐
- Making 3D romantic cool photo album [source code attached]
- 同花顺上登录股票账户是安全的吗?同花顺上是如何开股票账户的
- 使用Gin框架运行Demo时报错“ listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden”
- 【花雕体验】11 上手ESP32C3
- Unknown device ID does not appear on the STM32 st-link utility connection! Causes and Solutions
- ADC acquisition noise and comparison between RMS filter and Kalman filter
- 黑盒测试 — 测试用例 之 判定表法看这一篇就够了
- LabVIEW开发监控聚变实验脉冲电源
- ETCD数据库源码分析——集群通信初始化
- 经纬度 多点 获取中心点 已解决
猜你喜欢

SPI protocol

Freertos+stm32l+esp8266+mqtt protocol transmits temperature and humidity data to Tencent cloud IOT platform

The kth largest element in the array

C#使用MySql进行操作

Template engine - FreeMarker first experience

《产品思维30讲》精华及感想

LabVIEW开发监控聚变实验脉冲电源

Redis之Strings命令

Music spectrum display toy -- implementation and application of FFT in stm32

远程增量同步神器rsync
随机推荐
Laravel基础课 路由和MVC——路由
数字电路——加法器
mysql错误代码2003的解决办法
halcon之区域:多种区域(Region)生成(4)
Comment promouvoir efficacement les produits
Endnote IEEE Transactions on industrial electronics/tie/tpel reference format template
From query database performance optimization to redis cache - talk about cache penetration, avalanche and breakdown
Computer network knowledge summary (interview)
Installation and startup of redis
Etcd database source code analysis -- inter cluster network layer server interface
Using redis database as cache in Django
Tools - API document generation tool
About the use of hc-12 radio frequency module
Discrete Mathematics - 01 mathematical logic
Zhihuijia - full furniture function
Recognize map
在线小工具分享(不定时更新,当前数量:2)
Laravel basic course routing and MVC - controller
idea配置
Web information collection, naked runners on the Internet