剑谱第三页,女人真烦人 mysql增删改查,内左右连接 复习笔记
Posted 两掺豆腐脑丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑谱第三页,女人真烦人 mysql增删改查,内左右连接 复习笔记相关的知识,希望对你有一定的参考价值。
创建了两个案例数据库分别为 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;
以上是关于剑谱第三页,女人真烦人 mysql增删改查,内左右连接 复习笔记的主要内容,如果未能解决你的问题,请参考以下文章