数据库原理与应用学习笔记

1.简单查询

SELECT USERNAME FROM STUDENT WHERE ID = 1;

2.between

SELECT USERNAME FROM STUDENT WHERE SCORE BETWEEN 60 AND 100;    //性能更优
等价于:SELECT USERNAME FROM STUDENT WHERE SCORE >= 60 AND SCORE <= 100;

3.like模糊查询

SELECT USERNAME FROM STUDENT WHERE USERNAME LIKE "_杰%";  //第二个字为杰的
SELECT USERNAME FROM STUDENT WHERE USERNAME LIKE "张\_%" ESCAPE '\';

4.IS NULL

SELECT USERNAME FROM STUDENT WHERE PHONE IS NULL;

5.IN集合查询

SELECT USERNAME FROM STUDENT WHERE ADDRESS IN ("北京","广州");    //性能更优
等价于SELECT USERNAME FROM STUDENT WHERE ADDRESS = "北京" OR ADDRESS = "广州";

6.DISTINCT

// DISTINCT短语的作用范围是所有目标列,SELECT子句中只出现一次。作用:消除结果集重复的元素。
SELECT DISTINCT ADDRESS FROM STUDENT;

7.INTO 查询结果存入新表

SELECT USERNAME,PHONE,ADDRESS INTO BJ_STUDENT FROM STUDENT WHERE ADDRESS = "北京";

8.ORDER BY

SELECT USERNAME FROM STUDENT ORDER BY AGE,SCORE DESC;  
//先按AGE升序排列,再按SCORE降序排列。(缺结果图)
Orderby.png

9.TOP

SELECT TOP 2 * FROM STUDENT ORDER BY SCORE;  
//分数最少的两个
SELECT TOP 2 WITH TIES * FROM STUDENT ORDER BY SCORE; 
//分数最少的两个集合

10.UNION 并

//把查询的结果合并起来并去掉重复的元组(要保留,必须用UNION ALL)
SELECT USERNAME FROM STUDENT WHERE ADDRESS = '北京' 
    -> UNION 
    -> SELECT USERNAME FROM STUDENT WHERE ADDRESS = '上海';
//等价于
SELECT DISTINCT USERNAME FROM STUDENT WHERE ADDRESS IN ('北京', '上海');

11.INTERSECT 交

SELECT * FROM STUDENT WHERE SCHOOL = 'CTGU' 
    -> INTERSECT 
    -> SELECT * FROM STUDENT WHERE SCORE > 100;
//等价于
SELECT * FROM STUDENT WHERE SCHOOL = 'CTGU' AND SCORE > 100;

12.EXCEPT 差

SELECT USERNAME FROM STUDENT WHERE ADDRESS = '北京'
    -> EXCEPT 
    -> SELECT USERNAME FROM STUDENT WHERE ADDRESS = '上海';

13.连接查询

SELECT USER.ID, USERNAME, ORDER_NAME, PRICE 
    -> FROM USER, USER_ORDER 
    -> WHERE USER.ID = USER_ORDER.USER_ID;

SELECT USER.ID, USERNAME, ORDER_NAME, PRICE 
    -> FROM USER, USER_ORDER 
    -> WHERE USER.ID = USER_ORDER.USER_ID 
    -> AND ADDRESS = '北京';

SELECT USER.ID, USERNAME,PRODUCE_NAME, PRICE, PCS, PHONE, ADDRESS 
    -> FROM USER, USER_PRODUCE, PRODUCE 
    -> WHERE USER.ID = USER_PRODUCE.USER_ID 
    -> AND PRODUCE.ID = USER_PRODUCE.PRODUCE_ID;

14.内连接

SELECT USER.ID, USERNAME, ADDRESS, PCS
    -> FROM USER INNER JOIN USER_PRODUCE
    -> ON USER.ID = USER_PRODUCE.USER_ID
    -> WHERE ADDRESS = '北京';

SELECT USER.ID, USERNAME, ADDRESS, PCS 
    -> FROM USER INNER JOIN USER_PRODUCE 
    -> ON USER.ID = USER_PRODUCE.USER_ID 
    -> INNER JOIN PRODUCE ON PRODUCE.ID = USER_PRODUCE.PRODUCE_ID 
    -> WHERE USERNAME = 'ct';

15.自连接

SELECT UA.* FROM USER UA, USER UB 
    -> WHERE UA.ADDRESS = UB.ADDRESS 
    -> AND UB.USERNAME = 'ct';

16.外连接

//左连接
SELECT USER.ID, USERNAME, ADDRESS, PCS FROM USER     
    -> LEFT JOIN USER_PRODUCE ON USER.ID = USER_PRODUCE.USER_ID;
//右连接
SELECT USER.ID, USERNAME, ADDRESS, PCS FROM USER     
    -> RIGHT JOIN USER_PRODUCE ON USER.ID = USER_PRODUCE.USER_ID;

17.分组及汇总查询

// COUNT
SELECT COUNT(DISTINCT ADDRESS) FROM USER;
// 统计起别名
SELECT COUNT(DISTINCT Address) AS AddressNumber FROM USER;
// SUM
SELECT COUNT(*) AS produceNumber, SUM(PCS) as SumNumber FROM USER_PRODUCE;
// GROUP BY
SELECT PRODUCE_ID, SUM(PCS) as SumAmount FROM USER_PRODUCE GROUP BY PRODUCE_ID;
//分组,统计
SELECT ADDRESS, COUNT(*) as TotalPerson FROM USER GROUP BY ADDRESS;
//分组,统计,max,min
SELECT PRODUCE_ID,COUNT(*) as CountNum, MAX(PCS) as max_PCS, MIN(PCS) as min_PCS FROM USER_PRODUCE GROUP BY PRODUCE_ID HAVING COUNT(*) > 0;
//mysql 居然不支持 GROUPING SETS
GROUPING SETS ?

18.子查询

SELECT * FROM USER WHERE ID = (SELECT USER_ID FROM USER_PRODUCE WHERE ID = 4);

SELECT * FROM USER WHERE ID IN (SELECT USER_ID FROM USER_PRODUCE WHERE PRODUCE_ID = 2);

//查询子查询中最好的记录,应该有优化方案
SELECT * FROM USER_PRODUCE WHERE USER_ID = 1 AND PRODUCE_ID = 2 AND PCS >= ALL (SELECT PCS FROM USER_PRODUCE WHERE USER_ID = 1 AND PRODUCE_ID = 2);
//优化
SELECT * FROM USER_PRODUCE WHERE ID = (SELECT MAX(PCS) FROM USER_PRODUCE WHERE USER_ID = 1 AND PRODUCE_ID = 2);
// ANY ALL 使用较少
// 内外层互相关嵌套查询
SELECT * FROM USER_PRODUCE A WHERE PCS = (SELECT MAX(PCS) FROM USER_PRODUCE B WHERE A.USER_ID = B.USER_ID AND A.PRODUCE_ID = B.PRODUCE_ID) ORDER BY ID;
数据库.png

19.EXISTS嵌套查询

SELECT * FROM USER_PRODUCE WHERE EXISTS (SELECT * FROM PRODUCE WHERE PRODUCE.ID = USER_PRODUCE.PRODUCE_ID AND PRODUCE_NAME = '橙汁');
//等价于
SELECT * FROM USER_PRODUCE WHERE PRODUCE_ID IN (SELECT ID FROM PRODUCE WHERE PRODUCE_NAME = '橙汁');
// NOT EXISTS
SELECT * FROM PRODUCE A WHERE NOT EXISTS (SELECT * FROM PRODUCE B WHERE B.PRICE > A.PRICE AND B.PRODUCE_NAME='橙汁');

20.子查询额外操作

// insert 子查询
insert into TargetTable(id, total) select produce_id, sum(pcs) from user_produce group by produce_id;
// update 子查询
update PRODUCE set PRICE = PRICE * 2 where id not in (select PRODUCE_ID from USER_PRODUCE group by PRODUCE_ID having sum(PCS)>=10);
// delete 子查询
delete from USER_PRODUCE where PRODUCE_ID in (select ID from PRODUCE where PRODUCE_NAME = '咖啡');

21.数据库设计三范式

1NF:如果关系模式R中每个属性值都是一个不可分解的数据项,则该关系模式满足第一范式。
2NF:满足1NF且所有非主属性都完全依赖与其主码。
3NF:数据不能存在传递关系,即每个属性都跟主键有直接关系而不是间接关系

22.新建数据库 utf-8

CREATE DATABASE mytest DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

23.创建用户并赋予用户权限

create user 'repl'@'192.168.1.%' identified by 'repl';
grant all privileges on mytest.* to 'repl'@'192.168.1.%' identified by 'repl';
grant all privileges on *.* to 'repl'@'192.168.1.%' identified by 'repl';
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容