建表1:
CREATE TABLE student(
Sno char(15) NOT NULL,
Sname char(20) NULL,
Ssex char(2) NULL,
Sage smallint NULL,
Sdept char(20) NULL
)
INSERT student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('2012000', '李一平', '男', '18', 'IS')
INSERT student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('2012111', '张立', '男', '19', 'IS')
INSERT student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('2012222', '刘晨', '女', '19', 'CS')
INSERT student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('2012333', '王敏', '女', '18', 'MA')
INSERT student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('2012444', '李勇', '男', '20', 'CS')
INSERT student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('201255555', '张琴', '女', '19', 'CS')
INSERT student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('201266666', '王方', '女', '20', 'MA')
INSERT student (Sno, Sname, Ssex, Sage, Sdept) VALUES ('201277777', '黄林林', '男', '21', 'IS')
建表2:
CREATE TABLE Course (
Cno char(4) NOT NULL,
Cname char(40) NULL,
Cpno char(4) NULL,
Ccredit smallint NULL,
Sno nchar(10) NULL
)
INSERT Course (Cno, Cname, Cpno, Ccredit, Sno) VALUES ('1','数据库','5','4','2012000')
INSERT Course (Cno, Cname, Cpno, Ccredit, Sno) VALUES ('2','数学', NULL, '2', '2012111 ')
INSERT Course (Cno, Cname, Cpno, Ccredit, Sno) VALUES ('3','信息系统','1', '4', '2012222 ')
INSERT Course (Cno, Cname, Cpno, Ccredit, Sno) VALUES ('4','操作系统', '6', '3', '2012333 ')
INSERT Course (Cno, Cname, Cpno, Ccredit, Sno) VALUES ('5','数据结构', '7', '4', '2012444 ')
INSERT Course (Cno, Cname, Cpno, Ccredit, Sno) VALUES ('6','数据处理', NULL, '2', '201277777 ')
INSERT Course (Cno, Cname, Cpno, Ccredit, Sno) VALUES ('7','C语言 ', '6', '4', '201288888 ')
INSERT Course (Cno, Cname, Cpno, Ccredit, Sno) VALUES ('8','语文', '8', '8', '201299999 ')
建表3:
CREATE TABLE SC (
Sno nchar(10) NULL,
Cno char(4) NOT NULL,
Grade char(4) NOT NULL,
)
INSERT SC(Sno, Cno, Grade) VALUES ('2012000', '1', '60')
INSERT SC(Sno, Cno, Grade) VALUES ('2012111', '2', '70')
INSERT SC(Sno, Cno, Grade) VALUES ('2012222', '2', '70')
INSERT SC(Sno, Cno, Grade) VALUES ('2012333', '2', '70')
INSERT SC(Sno, Cno, Grade) VALUES ('2012444', '2', '70')
INSERT SC(Sno, Cno, Grade) VALUES ('2012555', '2', '70')
右连接 RIGHT JOIN
语法:SELECT 字段,* FROM 表名1 right join 表名2 on 表名1.字段=表名2.字段
right join 表名3 on 表名1.字段=表名3.字段
例子:SELECT * FROM student
RIGHT join Course on student.Sno=Course.Sno
三个表 右连接
从几个表中可以看出 最后关联的表很大程度上决定数据的情况,没有左关联那么直观,所以开发上个人建议使用左连接,具体情况,看公司怎么个统一
*****************************************************************************************************************
右连接与左连接对比图