日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

【MySQL数据库】一天学完MySQL笔记——纯SQL文档版

發布時間:2024/2/28 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【MySQL数据库】一天学完MySQL笔记——纯SQL文档版 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

查看建表語句

show create table `表名`

全部筆記

-- Notepad++快捷鍵: -- CTRL D復制一行 -- CTRL L刪除一行-- Eclipse快捷鍵: -- ALT ↑↓移動一行-- 單個主鍵約束 主鍵不能為空 不能重復 create table user(id int primary key,name varchar(20) );-- 顯示所有表 show tables;-- 顯示表結構 describe user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)-- 插入 insert into user values(1, "魔鬼");-- 查詢 select * from user;-- 聯合主鍵 只要聯合的主鍵值加起來不重復就可以 任何不能為空 create table user2(id int,name varchar(20),password varchar(20),primary key (id,name) );-- 插入 insert into user2 values(1, "大傻子", 123456); insert into user2 values(2, "大傻子", 123456); insert into user2 values(2, "大鯊魚", 123456);select * from user2;-- 自增約束 create table user3(id int primary key auto_increment,name varchar(20) );insert into user3(name) values("大鯊魚"); insert into user3(id, name) values(9, "大鯊魚"); insert into user3(name) values("大鯊魚"); insert into user3(name) values("大鯊魚"); insert into user3(name) values("大鯊魚"); insert into user3(name) values("大鯊魚");-- 忘記創建主鍵,如何添加 create table user4(id int,name varchar(20) );-- 修改為主鍵 alter table user4 add primary key(id); -- 方法1 alter table user4 modify id int primary key; -- 方法2-- 刪除主鍵 alter table user4 drop primary key;-- 唯一約束 字段的值不能重復 create table user5(id int,name varchar(20),unique(name) -- 這里 );create table user6(id int unique, -- 也可以這里name varchar(20) );alter table user5 add unique(name); -- 如果創建表時忘記添加 可以這樣修改 desc user5;insert into user5 values(1,"張三"); insert into user5 values(2,"張三"); ERROR 1062 (23000): Duplicate entry '張三' for key 'name'create table user7(id int,name varchar(20),unique(id,name) -- 組合不重復 ); insert into user7 values(1,"張三"); insert into user7 values(2,"張三");-- 刪除唯一約束 alter table user7 drop index id;-- 添加唯一約束 alter table user7 modify name varchar(20) unique;-- 刪除表中所有數據 delete from user7;-- 非空約束 create table user8(id int,name varchar(20) not null ); insert into user8 values(5, "張三");-- 默認約束 設置默認值 create table user10(id int,name varchar(20),age int default 10 ); insert into user10(id) values(1);-- 外鍵約束 涉及到兩個表:主表、副表 -- 1) 副表的內容要參照主表,若主表不存在值,則副表不能使用 -- 2) 主表中的記錄被副表引用,不可以被刪除 create table classes(id int primary key,name varchar(20) ); create table students(id int primary key,name varchar(20),class_id int,foreign key(class_id) references classes(id) ); desc students;insert into classes values(1,"一班"); insert into classes values(2,"二班"); insert into classes values(3,"三班"); insert into classes values(4,"四班");insert into students values(1,"小明",2); insert into students values(4,"老高",3);insert into students values(2,"老裴",5); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`today`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))-- 數據庫的三大設計范式 -- 1)第一范式 1NF-- 數據表中的所有字段都是不可分割的原子值 create table students2(id int primary key,name varchar(20),address varchar(30) );insert into students2 values(1,"高淇","中國北京海淀區200號"); insert into students2 values(2,"裴新","中國北京朝陽區50號"); insert into students2 values(3,"哥","山東青島88號");-- 字段值還可以繼續拆分,不滿足第一范式 +----+------+---------------------+ | id | name | address | +----+------+---------------------+ | 1 | 高淇 | 中國北京海淀區200| | 2 | 裴新 | 中國北京朝陽區50| | 3 || 山東青島88| +----+------+---------------------+create table students3(id int primary key,name varchar(20),country varchar(30),province varchar(30),streetNum varchar(30) ); insert into students3 values(1,"高淇","中國","北京","200號"); insert into students3 values(2,"裴新","中國","上海","800號"); insert into students3 values(3,"哥","中國","江西","6號"); -- 字段值不能更詳細劃分,滿足第一范式 +----+------+---------+----------+-----------+ | id | name | country | province | streetNum | +----+------+---------+----------+-----------+ | 1 | 高淇 | 中國 | 北京 | 200| | 2 | 裴新 | 中國 | 上海 | 800| | 3 || 中國 | 江西 | 6| +----+------+---------+----------+-----------+-- 2) 第二范式 -- 滿足第一范式前提下,除主鍵外每一列必須完全依賴主鍵-- 不滿足第二范式 create table mylist(product_id int,product_name varchar(20),costumer_id int,costumer_name varchar(20),primary key(product_id,costumer_id) ); +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | product_id | int(11) | NO | PRI | NULL | | | product_name | varchar(20) | YES | | NULL | | | costumer_id | int(11) | NO | PRI | NULL | | | costumer_name | varchar(20) | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+-- 滿足第二范式 create table user_order(order_id int primary key,product_id int,costumer_id int );create table product(id int primary key,name varchar(20) );create table costumer(id int primary key,name varchar(20) ); desc mylist; desc user_order; desc product; desc costumer; +-------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------+------+-----+---------+-------+ | order_id | int(11) | NO | PRI | NULL | | | product_id | int(11) | YES | | NULL | | | costumer_id | int(11) | YES | | NULL | | +-------------+---------+------+-----+---------+-------++-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------++-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+-- 3)第三范式 -- 除了主鍵列之外,其他列之間不能有傳遞依賴關系-- 不滿足第三范式 create table user_order(order_id int primary key,product_id int,costumer_id intcustomer_phone varchar(15) -- phone應該放在costumer表中 );-- 查詢練習 -- 創建數據庫 CREATE DATABASE select_test; -- 切換數據庫 USE select_test;-- 創建學生表 CREATE TABLE student (no VARCHAR(20) PRIMARY KEY, -- 學號name VARCHAR(20) NOT NULL, -- 姓名sex VARCHAR(10) NOT NULL, -- 性別birthday DATE, -- 生日class VARCHAR(20) -- 所在班級 );-- 創建教師表 CREATE TABLE teacher (no VARCHAR(20) PRIMARY KEY, -- 教師號name VARCHAR(20) NOT NULL, -- 姓名sex VARCHAR(10) NOT NULL, -- 性別birthday DATE, -- 生日profession VARCHAR(20) NOT NULL, -- 職稱department VARCHAR(20) NOT NULL -- 部門 );-- 創建課程表 CREATE TABLE course (no VARCHAR(20) PRIMARY KEY, -- 課程號name VARCHAR(20) NOT NULL, -- 課程名t_no VARCHAR(20) NOT NULL, -- 教師號FOREIGN KEY(t_no) REFERENCES teacher(no)-- 外鍵關聯教師號 );-- 成績表 CREATE TABLE score (s_no VARCHAR(20) NOT NULL, -- 學號c_no VARCHAR(20) NOT NULL, -- 課程號degree DECIMAL, -- 成績FOREIGN KEY(s_no) REFERENCES student(no),-- 外鍵綁定學號FOREIGN KEY(c_no) REFERENCES course(no), -- 外鍵綁定課程號PRIMARY KEY(s_no, c_no) -- 聯合主鍵:學號、課程號 );-- 查看所有表 SHOW TABLES;-- 添加學生表數據 INSERT INTO student VALUES('101', '曾華', '男', '1977-09-01', '95033'); INSERT INTO student VALUES('102', '匡明', '男', '1975-10-02', '95031'); INSERT INTO student VALUES('103', '王麗', '女', '1976-01-23', '95033'); INSERT INTO student VALUES('104', '李軍', '男', '1976-02-20', '95033'); INSERT INTO student VALUES('105', '王芳', '女', '1975-02-10', '95031'); INSERT INTO student VALUES('106', '陸軍', '男', '1974-06-03', '95031'); INSERT INTO student VALUES('107', '王尼瑪', '男', '1976-02-20', '95033'); INSERT INTO student VALUES('108', '張全蛋', '男', '1975-02-10', '95031'); INSERT INTO student VALUES('109', '趙鐵柱', '男', '1974-06-03', '95031');-- 添加教師表數據 INSERT INTO teacher VALUES('804', '李誠', '男', '1958-12-02', '副教授', '計算機系'); INSERT INTO teacher VALUES('856', '張旭', '男', '1969-03-12', '講師', '電子工程系'); INSERT INTO teacher VALUES('825', '王萍', '女', '1972-05-05', '助教', '計算機系'); INSERT INTO teacher VALUES('831', '劉冰', '女', '1977-08-14', '助教', '電子工程系');-- 添加課程表數據 INSERT INTO course VALUES('3-105', '計算機導論', '825'); INSERT INTO course VALUES('3-245', '操作系統', '804'); INSERT INTO course VALUES('6-166', '數字電路', '856'); INSERT INTO course VALUES('9-888', '高等數學', '831');-- 添加添加成績表數據 INSERT INTO score VALUES('101', '6-166', '88'); INSERT INTO score VALUES('103', '3-105', '92'); INSERT INTO score VALUES('103', '3-245', '86'); INSERT INTO score VALUES('103', '6-166', '85'); INSERT INTO score VALUES('105', '3-105', '88'); INSERT INTO score VALUES('105', '3-245', '75'); INSERT INTO score VALUES('105', '6-166', '79'); INSERT INTO score VALUES('109', '3-105', '76'); INSERT INTO score VALUES('109', '3-245', '68'); INSERT INTO score VALUES('109', '6-166', '81');-- 1、查詢表中所有記錄 SELECT * FROM course; +-------+------------+------+ | no | name | t_no | +-------+------------+------+ | 3-105 | 計算機導論 | 825 | | 3-245 | 操作系統 | 804 | | 6-166 | 數字電路 | 856 | | 9-888 | 高等數學 | 831 | +-------+------------+------+ SELECT * FROM score; +------+-------+--------+ | s_no | c_no | degree | +------+-------+--------+ | 103 | 3-105 | 92 | | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 105 | 3-105 | 88 | | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | | 109 | 6-166 | 81 | +------+-------+--------+ SELECT * FROM student; +-----+--------+-----+------------+-------+ | no | name | sex | birthday | class | +-----+--------+-----+------------+-------+ | 101 | 曾華 || 1977-09-01 | 95033 | | 102 | 匡明 || 1975-10-02 | 95031 | | 103 | 王麗 || 1976-01-23 | 95033 | | 104 | 李軍 || 1976-02-20 | 95033 | | 105 | 王芳 || 1975-02-10 | 95031 | | 106 | 陸軍 || 1974-06-03 | 95031 | | 107 | 王尼瑪 || 1976-02-20 | 95033 | | 108 | 張全蛋 || 1975-02-10 | 95031 | | 109 | 趙鐵柱 || 1974-06-03 | 95031 | +-----+--------+-----+------------+-------+ SELECT * FROM teacher; +-----+------+-----+------------+------------+------------+ | no | name | sex | birthday | profession | department | +-----+------+-----+------------+------------+------------+ | 804 | 李誠 || 1958-12-02 | 副教授 | 計算機系 | | 825 | 王萍 || 1972-05-05 | 助教 | 計算機系 | | 831 | 劉冰 || 1977-08-14 | 助教 | 電子工程系 | | 856 | 張旭 || 1969-03-12 | 講師 | 電子工程系 | +-----+------+-----+------------+------------+------------+-- 2、查詢 student 表中 name、sex、class 列 SELECT name, sex, class FROM student;-- 3、查詢 teacher 表中不重復的 department 列 SELECT distinct department FROM teacher;-- 4、查詢 score 表中成績在60-80之間的所有記錄 select * from score where degree between 60 and 80; select * from score where degree>60 and degree<80; -- 直接運算符比較-- 5、查詢 score 表中成績為 85,86 的記錄 select * from score where degree in(85,86,87,88);-- 6、查詢 student 表中 "95301" 班或性別為 "女" 的記錄 select * from student where class = "95031" or sex = "女";-- 7、class 降序查詢 student 表種所有記錄 select * from student order by class; -- 默認升序 select * from student order by class asc; -- 升序 select * from student order by class desc; -- 降序-- 8、以 c_no 升序、degree 降序查詢 score 表中所有記錄 select * from score order by c_no asc, degree desc;-- 9、查詢"95031"班的學生人數 select count(*) from student where class="95031";-- 10、查詢score表中最高分學生的學號和課程號 select s_no,c_no,degree from score where degree=(select max(degree) from score); --子查詢 select s_no,c_no,degree from score order by degree desc limit 0,1; --排序:你不能確定有幾個最高分-- 11、查詢每門課的平均成績 select avg(degree) from score where c_no="3-105"; -- 一門課 select c_no, avg(degree) from score group by c_no; -- 每門課-- 12、查詢score表中至少有兩名學生選修并以3開頭的課程的平均分數 select c_no,avg(degree),count(*) from score group by c_no; select c_no,avg(degree),count(*) from score group by c_no having count(c_no)>=2 and c_no like '3%';-- 13、查詢分數大于70,小于90的sno列 select s_no,degree from score where degree>70 and degree<90;select s_no,degree from score where degree between 70 and 90;-- 14、查詢所有學生的 student.name score.c_no score.degree 列 -- 如果名稱不重復 點之前的表名可以省略 select student.no,score.s_no,student.name, score.c_no, score.degree from student, score where student.no = score.s_no;-- 15、查詢所有學生的 sno(學號) cname(課程名) degree(成績) 列 -- 通過觀察發現 聯查的原理是:先單查,再聯合篩選 -- 最后查詢結果數量 是跟著最長的結果degree表走的 -- 三表聯查 不理解的話 先把where去掉 輸出一下 select student.name,student.no,course.name,score.degree from student, course, score where student.no = score.s_no and course.no = score.c_no; -- 兩表聯查 答案 select score.c_no,course.name,score.degree from course,score where score.c_no = course.no;-- 16、查詢所有學生的sname學生名 cname課程名 degree課程成績 select student.name,course.name,score.degree from student, course, score where student.no = score.s_no and course.no = score.c_no; -- 取別名 select student.name as 姓名,course.name as 課程,score.degree as 成績 from student, course, score where student.no = score.s_no and course.no = score.c_no;-- 17、查詢“95031”班學生每門課的平均分:先查什么,再查什么 select * from student where class="95031"; -- 先思考條件select course.name as 課程名稱, course.no as 課程編號, avg(degree) as 平均分 from course, score where score.s_no in(select no from student where class="95031") -- 條件篩選 and course.no = score.c_no group by score.c_no; +------------+----------+---------+ | 課程名稱 | 課程編號 | 平均分 | +------------+----------+---------+ | 計算機導論 | 3-105 | 82.0000 | | 操作系統 | 3-245 | 71.5000 | | 數字電路 | 6-166 | 80.0000 | +------------+----------+---------+-- group by 的原理: 先收集前一句的結果 再按要求篩選 select * from score; select * from course; select * from course,score; select * from course,score where course.no = score.c_no; -- where 用于聯合兩表(取交集) select * from course,score where course.no = score.c_no group by score.c_no; -- group by用于去除重復select * from course,score where course.no = score.c_no -- where,group by順序不能反 group by score.c_no; -- 18、查詢選了"3-105"課程的同學的所有信息 select * from student where no in(select s_no from score where c_no = "3-105" group by s_no);-- 19、查詢成績高于[學號為"109"、課程號為"3-105"的學生的成績]的所有記錄 select * from score where degree > (select degree from score where s_no=109 and c_no="3-105"); -- 先找到學號為"109"、課程號為"3-105"的學生的成績 select degree from score where s_no=109 and c_no="3-105";-- 20、查詢[與學號為108、101的同學同年出生]的所有學生的學號、姓名和生日 select * from student where year(birthday) in(select year(birthday) from student where no in(101, 108)); -- 先查詢[學號為108、101的同學]的出生年份 select year(birthday) from student where no in(101, 108);-- 21、查詢[“張旭”教師任課的]學生成績 select * from student; select * from course; -- 查:張旭的教師號 select no from teacher where name = "張旭"; -- 查:張旭教師號對應的課程 select no from course where t_no in (select no from teacher where name = "張旭"); -- 查:上了這些課的學生成績 select * from score where c_no in (select no from course where t_no in (select no from teacher where name = "張旭"));-- 22、查詢選修某課程的同學的人數多于5人的教師姓名 -- 給我感覺 select count(*)是在group by之后運算的,select c_no 是在group by之前運算的? INSERT INTO score VALUES('101', '3-105', '90'); INSERT INTO score VALUES('102', '3-105', '91'); INSERT INTO score VALUES('104', '3-105', '89'); -- 查:人數多于5人的課程編號 select c_no from score group by c_no having count(*)>5; -- 查:老師 通過course銜接 select * from teacher where no in(select t_no from course where no in (select c_no from score group by c_no having count(*)>5));-- 23、查詢95033班和95031班全體學生記錄 INSERT INTO student VALUES('110', '張飛', '男', '1974-06-03', '95038');-- 24、 查詢[存在有85分以上成績]的課程的課程編號 select distinct c_no from score where degree > 85;-- 25、查詢“計算機系”教師所教課程的成績表 select * from score where c_no in(select no from course where t_no in(select no from teacher where department ="計算機系"));-- 26、查詢[“計算機系”與"電子工程系"不同職稱的教師]的姓名和職稱 -- union求并集 select * from teacher; +-----+------+-----+------------+------------+------------+ | no | name | sex | birthday | profession | department | +-----+------+-----+------------+------------+------------+ | 804 | 李誠 || 1958-12-02 | 副教授 | 計算機系 | | 825 | 王萍 || 1972-05-05 | 助教 | 計算機系 | | 831 | 劉冰 || 1977-08-14 | 助教 | 電子工程系 | | 856 | 張旭 || 1969-03-12 | 講師 | 電子工程系 | +-----+------+-----+------------+------------+------------+select * from teacher where department="計算機系" and profession not in (select profession from teacher where department="電子工程系") union select * from teacher where department="電子工程系" and profession not in (select profession from teacher where department="計算機系"); +-----+------+-----+------------+------------+------------+ | no | name | sex | birthday | profession | department | +-----+------+-----+------------+------------+------------+ | 804 | 李誠 || 1958-12-02 | 副教授 | 計算機系 | | 856 | 張旭 || 1969-03-12 | 講師 | 電子工程系 | +-----+------+-----+------------+------------+------------+-- 27、查詢[選了編號為“3-105”的課程]且[成績至少高于一個選了編號為"3-245"的課程同學]的[課程no、學生no、成績],并按照degree從高到低排序 -- any表示至少一個 select * from score where c_no="3-105" and degree > any (select degree from score where c_no="3-245") order by degree desc;-- 28、()查詢[選了編號為“3-105”的課程]且[成績高于所有選了編號為"3-245"的課程同學]的[課程no、學生no、成績],并按照degree從高到低排序 -- all表示所有 select * from score where c_no="3-105" and degree > all (select degree from score where c_no="3-245") order by degree desc;-- 29、查詢所有教師和同學的name、sex和birthday -- union取并集 只要列數相同即可 與列的數據類型無關 select name,sex,birthday from teacher union select name,sex,birthday from student;-- 30、查詢所有“女”教師和“女”同學的name、sex和birthday select name,sex,birthday from teacher where sex="女" union select name,sex,birthday from student where sex="女";-- 31、查詢成績低于該課程平均成績的同學的成績表 select * from score;--把表分成兩個,取別名[a] [b] +------+-------+--------+ +------+-------+--------+ | s_no | c_no | degree | | s_no | c_no | degree | +------+-------+--------+ +------+-------+--------+ | 101 | 3-105 | 90 | | 101 | 3-105 | 90 | | 101 | 6-166 | 88 | | 101 | 6-166 | 88 | | 102 | 3-105 | 91 | | 102 | 3-105 | 91 | | 103 | 3-105 | 92 | | 103 | 3-105 | 92 | | 103 | 3-245 | 86 | | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 103 | 6-166 | 85 | | 104 | 3-105 | 89 | | 104 | 3-105 | 89 | | 105 | 3-105 | 88 | | 105 | 3-105 | 88 | | 105 | 3-245 | 75 | | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | | 109 | 3-245 | 68 | | 109 | 6-166 | 81 | | 109 | 6-166 | 81 | +------+-------+--------+ +------+-------+--------+ -- 像是遍歷求平均 這個score a,score b巧妙! select * from score a where degree < (select avg(degree) from score b where a.c_no = b.c_no); -- 這個題使用group by沒法做-- 32、查詢所有[任課]教師的name和Department INSERT INTO score VALUES('109', '9-888', '80');-- 所有 課程編號 select distinct c_no from score; -- 根據 課程編號 找 教師編號 select t_no from course where no in (select distinct c_no from score); -- 根據 教師編號 找 教師所有信息-- 33、查詢至少有2名男生的班號 select * from student where sex="男" group by class having count(*)>1;-- 33、查詢student表中不姓王的同學記錄 -- 模糊查詢 select * from student where name not like "王%"; -- 34、查詢student表中每個學生的姓名和年齡 select year(now()); select name,year(now())-year(birthday) as "年齡" from student;-- 34、查詢student表中最大最小birthday select birthday from student order by birthday; -- 最大最小 select max(birthday) as "最大", min(birthday) as "最小" from student; +------------+------------+ | 最大 | 最小 | +------------+------------+ | 1977-09-01 | 1974-06-03 | +------------+------------+-- 35、以[1班號]和[2年齡]從大到小的順序查詢student表中的全部數據 select * from student order by class desc,birthday;-- 38、查詢“男”教師及其鎖上的課程 select * from teacher where sex="男"; select * from course where t_no in(select no from teacher where sex="男");-- 39、查詢最高分同學的學號、課程編號、成績 -- 我的解答:題意看錯了。查詢了最高分同學的學號、班號、成績。 select student.no,student.class,score.degree from student,score where score.s_no=student.no order by degree desc limit 0,1; --標準解答 select * from score where degree = (select max(degree) from score);-- 40.查詢和“李軍”同性別的所有同學的姓名 select name from student where sex=(select sex from student where name="李軍");-- 41、查詢查詢和“李軍”同性別、同班的所有同學的姓名 select name from student where sex = (select sex from student where name="李軍") and class = (select class from student where name="李軍");-- 42、查詢所有選修“計算機導論”課程的“男”同學成績表 -- 兩個條件:計算機導論、男 select * from score where c_no = (select no from course where name="計算機導論") -- 計算機導論 and s_no in (select no from student where sex = "男"); -- 男-- 43、假設使用如下面命令建立了一個grade表: create table grade(low int(3),upp int(3),grade char(1) ); insert into grade values (90,100,'A'); insert into grade values (80,89,'B'); insert into grade values (70,79,'C'); insert into grade values (60,69,'D'); insert into grade values (0,59,'E'); +------+------+-------+ | low | upp | grade | +------+------+-------+ | 90 | 100 | A | | 80 | 89 | B | | 70 | 79 | C | | 60 | 69 | D | | 0 | 59 | E | +------+------+-------+ --查詢所有同學的s_no、c_no、grade列 select s_no,c_no,grade from score,grade where degree between low and upp; +------+-------+-------+ | s_no | c_no | grade | +------+-------+-------+ | 101 | 3-105 | A | | 101 | 6-166 | B | | 102 | 3-105 | A | | 103 | 3-105 | A | | 103 | 3-245 | B | | 103 | 6-166 | B | | 104 | 3-105 | B | | 105 | 3-105 | B | | 105 | 3-245 | C | | 105 | 6-166 | C | | 109 | 3-105 | C | | 109 | 3-245 | D | | 109 | 6-166 | B | | 109 | 9-888 | B | +------+-------+-------+--連接查詢-- 內連接 -- inner join 或者 join-- 外連接 -- 1)左連接 left join 或者 left outer join -- 2)右鏈接 right join 或者 right outer join -- 3)完全外連接 full lion 或者 full outer join CREATE DATABASE testJoin; CREATE TABLE person (id INT, --持有卡號name VARCHAR(20), --人姓名cardId INT --卡種類 );CREATE TABLE card (id INT, --卡種類name VARCHAR(20) --卡名稱 );INSERT INTO card VALUES (1, '飯卡'), (2, '建行卡'), (3, '農行卡'), (4, '工商卡'), (5, '郵政卡'); INSERT INTO person VALUES (1, '張三', 1), (2, '李四', 3), (3, '王五', 6); SELECT * FROM card; SELECT * FROM person;-- 1)inner join 內聯查詢:兩張表中的數據,通過某個字段相等,查詢出相關記錄數據 select * from person inner join card on person.cardId = card.id; select * from person join card on person.cardId = card.id; -- 省略inner-- 2)left join 左外連接 -- 把左邊表中所有數據取出來,而右邊表中的數據,如果有就顯示出來,如果沒有就顯示null select * from person left outer join card on person.cardId = card.id; select * from person left join card on person.cardId = card.id; -- 省略outer +------+------+--------+------+--------+ | id | name | cardId | id | name | +------+------+--------+------+--------+ | 1 | 張三 | 1 | 1 | 飯卡 | | 2 | 李四 | 3 | 3 | 農行卡 | | 3 | 王五 | 6 | NULL | NULL | +------+------+--------+------+--------+-- 3)right join 右外連接 -- 把右邊表中所有數據取出來,而左邊表中的數據,如果有就顯示出來,如果沒有就顯示null select * from person right outer join card on person.cardId = card.id; select * from person right join card on person.cardId = card.id; -- 省略outer +------+------+--------+------+--------+ | id | name | cardId | id | name | +------+------+--------+------+--------+ | 1 | 張三 | 1 | 1 | 飯卡 | | 2 | 李四 | 3 | 3 | 農行卡 | | NULL | NULL | NULL | 2 | 建行卡 | | NULL | NULL | NULL | 4 | 工商卡 | | NULL | NULL | NULL | 5 | 郵政卡 | +------+------+--------+------+--------+-- 4)full join 全外連接 -- mysql不支持full join select * from person full join card on person.cardId = card.id;-- unoin 實現full join select * from person left join card on person.cardId = card.id union select * from person right join card on person.cardId = card.id; +------+------+--------+------+--------+ | id | name | cardId | id | name | +------+------+--------+------+--------+ | 1 | 張三 | 1 | 1 | 飯卡 | | 2 | 李四 | 3 | 3 | 農行卡 | | 3 | 王五 | 6 | NULL | NULL | | NULL | NULL | NULL | 2 | 建行卡 | | NULL | NULL | NULL | 4 | 工商卡 | | NULL | NULL | NULL | 5 | 郵政卡 | +------+------+--------+------+--------+-- 事務 -- 在 MySQL 中,事務其實是一個最小的不可分割的工作單元。事務能夠保證一個業務的完整性。 -- 比如銀行轉賬: -- a -> -100 UPDATE user set money = money - 100 WHERE name = 'a'; -- b -> +100 UPDATE user set money = money + 100 WHERE name = 'b'; -- 如果只有一條執行成功,另一條執行不成功,會出現數據前后不一致 -- 多條sql語句可能會有同時成功的要求(要么同時成功,要么同事失敗)-- mysql如何控制事務? -- 1)mysql默認開啟事務 -- 查詢事務的自動提交狀態 SELECT @@AUTOCOMMIT; +--------------+ | @@AUTOCOMMIT | +--------------+ | 1 | +--------------+ -- 自動提交的作用:當我們執行一條 SQL 語句的時候,其產生的效果就會立即體現出來,且不能回滾CREATE DATABASE bank;USE bank;CREATE TABLE user (id INT PRIMARY KEY,name VARCHAR(20),money INT );INSERT INTO user VALUES (1, 'a', 1000);SELECT * FROM user; +----+------+-------+ | id | name | money | +----+------+-------+ | 1 | a | 1000 | +----+------+-------+ -- 可以看到,在執行插入語句后數據立刻生效,原因是 MySQL 中的事務自動將它提交到了數據庫中 -- 那么所謂回滾的意思就是,撤銷執行過的所有 SQL 語句,使其回滾到最后一次提交數據時的狀態 -- 在 MySQL 中使用 ROLLBACK 執行回滾: -- 回滾到最后一次提交 ROLLBACK; -- 不能回滾 沒效果 SELECT * FROM user; +----+------+-------+ | id | name | money | +----+------+-------+ | 1 | a | 1000 | +----+------+-------+-- 由于所有執行過的 SQL 語句都已經被提交過了,所以數據并沒有發生回滾。那如何讓數據可以發生回滾? -- 關閉自動提交 SET AUTOCOMMIT = 0;-- 查詢自動提交狀態 SELECT @@AUTOCOMMIT;-- 將自動提交關閉后,測試數據回滾: INSERT INTO user VALUES (2, 'b', 1000);-- 關閉 AUTOCOMMIT 后,數據的變化是在一張虛擬的臨時數據表中展示, -- 發生變化的數據并沒有真正插入到數據表中。 SELECT * FROM user;-- 由于數據還沒有真正提交,可以使用回滾 ROLLBACK; +----+------+-------+ | id | name | money | +----+------+-------+ | 1 | a | 1000 | +----+------+-------+-- 那如何將虛擬的數據真正提交到數據庫中?使用 COMMIT INSERT INTO user VALUES (2, 'b', 1000);-- 手動提交數據(持久性),將數據真正提交到數據庫中,執行后不能再回滾提交過的數據。 COMMIT;-- 提交后測試回滾 ROLLBACK;-- 再次查詢(回滾無效了) SELECT * FROM user; +----+------+-------+ | id | name | money | +----+------+-------+ | 1 | a | 1000 | | 2 | b | 1000 | +----+------+-------+-- 事務總結--查看自動提交狀態:1自動 2手動SELECT @@AUTOCOMMIT --設置手動提交:SET AUTOCOMMIT = 0 --手動提交(AUTOCOMMIT = 0)COMMIT;--事務回滾(AUTOCOMMIT = 0)ROLLBACK; -- 開啟自動提交 SET AUTOCOMMIT = 1;-- 手動開啟一個事務 begin; -- 這樣 start transaction; -- 或者這樣-- 示例1 begin; UPDATE user set money = money - 100 WHERE name = 'a'; UPDATE user set money = money + 100 WHERE name = 'b'; rollback; -- 成功-- 示例2 start transaction; UPDATE user set money = money - 100 WHERE name = 'a'; UPDATE user set money = money + 100 WHERE name = 'b'; ROLLBACK; -- 成功--讓事務生效 commit;-- 總結:事務的四大特征:-- A 原子性:事務是最小的單位,不可以再分割;-- C 一致性:要求同一事務中的 SQL 語句,必須保證同時成功或者失敗;-- I 隔離性:事務1 和 事務2 之間是具有隔離性的;-- D 持久性:事務一旦結束 ( COMMIT ) ,就不可以再返回了 ( ROLLBACK )

總結

以上是生活随笔為你收集整理的【MySQL数据库】一天学完MySQL笔记——纯SQL文档版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。