数据库基本语句复习

一、创建一个数据库

DROP TABLE IF  EXISTS student;
CREATE TABLE student(
	自动编号 TINYINT,
	学号 INT,
	姓名 VARCHAR(20),
	课程编号 varchar(4),
	课程 VARCHAR(20),
	课程分数 tinyint
);
DROP TABLE IF  EXISTS tb_lemon_score;
CREATE TABLE tb_lemon_score(
	sname VARCHAR(20),
	course VARCHAR(20),
	score tinyint
);

这里注意sql语句每一句结束都用分号隔开。数字少的用tinyint,多的用int,尽可能用varchar不用char。

二、添加数据

INSERT student VALUES
(1,2005001,'张三','0001','数学',69),
(2,2005002,'李四','0001','数学',89),
(3,2005001,'张三','0001','数学',69),
(4,2005001,'张三','0001','数学',69);
INSERT tb_lemon_score VALUES
('张三','语文',81),
('张三','数学',75),
('李四','语文',76),
('李四','数学',90),
('王五','语文',81),
('王五','数学',100),
('王五','英语',90);

三、查询tb_lemon_score表中所有科目都大于80分的人名字。

select sname from tb_lemon_score group by sname having min(score)>80

注意这里用having,记忆方法:遇到groupby首先想到having。

四、输出student 表中除了编号以外不重复的数据。

select distinct 学号,姓名,课程编号, 课程, 课程分数 from student 

五、对student 和tb_lemon_score两张表按照姓名和sname 进行内、左、右、全连接、笛卡尔积。

#内连接
select * from student inner join tb_lemon_score on student.姓名=tb_lemon_score.sname 
#左连接
select * from student left join tb_lemon_score on student.姓名=tb_lemon_score.sname 
#右连接
select * from student right join tb_lemon_score on student.姓名=tb_lemon_score.sname 
#全连接
select * from student full join tb_lemon_score on student.姓名=tb_lemon_score.sname 
#笛卡尔积(交叉连接)
select * from student inner join tb_lemon_score 
#或者
#select * from student cross join tb_lemon_score 

各种连接的理论理解:理论

猜你喜欢

转载自blog.csdn.net/weixin_39529891/article/details/104596227