您好,登錄后才能下訂單哦!
SQL: 指結(jié)構(gòu)化查詢語言,全稱是 Structured Query Language。 SQL語言包括三種主要程序設(shè)計(jì)語言類別的語句:數(shù)據(jù)定義語言(DDL),數(shù)據(jù)操作語言(DML)及數(shù)據(jù)控制語言(DCL)。 DCL:數(shù)據(jù)庫控制語言,Data Control Language, 用來設(shè)置或更改數(shù)據(jù)庫用戶或角色權(quán)限的語句,【grant、remove,deny】 DML:數(shù)據(jù)操縱語言,Data Manipulation Language ,命令使用戶能夠操作已有數(shù)據(jù)庫中的數(shù)據(jù)的計(jì)算機(jī)語言 【Insert、delete、update】,處理表數(shù)據(jù) DDL:數(shù)據(jù)庫模式定義語言,Data Definition Language ,是用于描述數(shù)據(jù)庫中要存儲的現(xiàn)實(shí)世界實(shí)體的語言【create,add,alter,drop】處理表結(jié)構(gòu) DQL:數(shù)據(jù)查詢語言,Data Query Language SELECT ,【select】
----------------------------------表結(jié)構(gòu)---------------------------------------------------- --學(xué)生表 Student(編號student_code、姓名student_name、年齡student_age、性別student_sex) --課程表 Course(課程編號course_code、課程名稱course_name、教師編號teacher_code) --成績表 Score(學(xué)生編號student_code、課程編號course_code、成績score) --教師表 Teacher(教師編號teacher_code、姓名teacher_name)
創(chuàng)建表:
CREATE TABLE Student ( `student_code` varchar(255) NOT NULL COMMENT '學(xué)號' , `student_name` varchar(255) NULL COMMENT '學(xué)生姓名' , `student_sex` char(1) NULL COMMENT '性別' , `student_age` varchar(255) NULL COMMENT '年齡' , `create_by` varchar(255) NULL COMMENT '創(chuàng)建人' , `create_date` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '創(chuàng)建日期' , `update_by` varchar(255) NULL COMMENT '更新人' , `updtae_date` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間' , PRIMARY KEY (`student_code`) );
CREATE TABLE Course ( `course_code` varchar(255) NOT NULL COMMENT '課程號' , `course_name` varchar(255) NULL COMMENT '課程姓名' , `teacher_code` varchar(255) NULL COMMENT '教師編碼' , `create_by` varchar(255) NULL COMMENT '創(chuàng)建人' , `create_date` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '創(chuàng)建日期' , `update_by` varchar(255) NULL COMMENT '更新人' , `updtae_date` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間' , PRIMARY KEY (`course_code`) );
CREATE TABLE Score ( `student_code` varchar(255) NOT NULL COMMENT '學(xué)號' , `course_code` varchar(255) NULL COMMENT '課程編碼' , `score` int(100) NULL COMMENT '成績' );
CREATE TABLE Teacher ( `teacher_code` varchar(255) NOT NULL COMMENT '教師編號' , `teacher_name` varchar(255) NULL COMMENT '教師姓名' , `create_by` varchar(255) NULL COMMENT '創(chuàng)建人' , `create_date` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '創(chuàng)建日期' , `update_by` varchar(255) NULL COMMENT '更新人' , `updtae_date` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間' , PRIMARY KEY (`teacher_code`) );
插入數(shù)據(jù):
---插入數(shù)據(jù)-- INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709001', 'A001', '男', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709002', 'A002', '女', '22', 'system', '2017-01-03 15:08:38', 'system', '2017-01-03 15:08:38'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709003', 'A003', '女', '22', 'system', '2017-01-03 15:08:38', 'system', '2017-01-03 15:08:38'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709004', 'A004', '男', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709005', 'A005', '男', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709006', 'A006', '女', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709007', 'A007', '男', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709008', 'A008', '女', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709009', 'A009', '男', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709010', 'A010', '女', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709011', 'A011', '男', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); INSERT INTO `student` (`student_code`, `student_name`, `student_sex`, `student_age`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('201709012', 'A012', '男', '22', 'system', '2017-01-03 15:08:23', 'system', '2017-01-03 15:08:23'); ---插入數(shù)據(jù)-- INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('Chinese', '語文', 'T001', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('Math', '數(shù)學(xué)', 'T002', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('English', '英語', 'T003', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('Physics', '物理', 'T004', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('Political', '政治', 'T005', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('Organism', '生物', 'T006', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('Geography', '地理', 'T007', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('History', '歷史', 'T008', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); INSERT INTO `course` (`course_code`, `course_name`, `teacher_code`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('Chemistry', '化學(xué)', 'T009', 'system', '2017-01-03 15:19:18', 'system', '2017-01-03 15:19:32'); ---插入數(shù)據(jù)-- INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709001', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709002', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709003', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709004', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709005', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709006', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709007', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709008', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709009', 'Political', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'Chemistry', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'Chinese', '90'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'English', '97'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'Geography', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'History', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'Math', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'Organism', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'Physics', '99'); INSERT INTO `score` (`student_code`, `course_code`, `score`) VALUES ('201709010', 'Political', '99'); ---插入數(shù)據(jù)-- INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T001', '王羲之', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16'); INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T002', '趙匡胤', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16'); INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T003', '趙明', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16'); INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T004', '燕十三郎', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16'); INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T005', '龍吟', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16'); INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T006', '方言', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16'); INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T007', '彭松', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16'); INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T008', '唐宋', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16'); INSERT INTO `teacher` (`teacher_code`, `teacher_name`, `create_by`, `create_date`, `update_by`, `updtae_date`) VALUES ('T009', '張三豐', 'system', '2017-01-03 15:42:06', 'system', '2017-01-03 15:42:16');
場景測試:
-- 1.查詢“A”課程比“B”課程成績高的所有學(xué)生的學(xué)號 SELECT student.student_code AS "StudentCode", student.student_name AS "StudentName" FROM Student student WHERE ( SELECT score1.score FROM Score score1 WHERE score1.student_code = student.student_code AND score1.course_code = 'Chemistry' -- A課程 ) > ( SELECT score2.score FROM Score score2 WHERE score2.student_code = student.student_code AND score2.course_code = 'Chinese' -- B課程 ); --2.查詢平均成績大于90分的同學(xué)的學(xué)號和平均成績 SELECT score.student_code AS "StudentCode", AVG(score.score) AS " AVGScore" FROM Score score GROUP BY score.score HAVING AVG(score.score) > 90 --3.查詢所有同學(xué)的學(xué)號、姓名、選課數(shù)、總成績; SELECT student.student_code AS "StudentCode",--學(xué)號 student.student_name AS "StudentName",--姓名 COUNT(score.course_code) AS "SelectCourse",--選課數(shù) SUM(score.score) AS "SumScore"--總成績 FROM Student student LEFT JOIN Score score ON score.student_code = student.student_code GROUP BY student.student_code, student.student_name; --4、查詢姓“張”的老師的個(gè)數(shù); SELECT Count(*) FROM Teacher teacher WHERE teacher.teacher_name LIKE '張%'; --5.查詢沒有學(xué)過“張三豐”老師課的同學(xué)的學(xué)號、姓名; SELECT student.student_code AS "StudentCode", student.student_name AS "StudentName" FROM Student student WHERE student.student_code NOT IN ( SELECT DISTINCT (score.student_code) FROM Score score, Course course, Teacher teacher WHERE score.course_code = course.course_code AND teacher.teacher_code = course.course_code AND teacher.teacher_name = '張三豐' );
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。