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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

持续更新,mysql的复习强化路

發(fā)布時(shí)間:2024/3/13 数据库 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 持续更新,mysql的复习强化路 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)庫與表的基本操作

  • 了解結(jié)構(gòu)化查詢語言SQL
    sql是結(jié)構(gòu)化查詢語言,是一門標(biāo)準(zhǔn)的計(jì)算機(jī)語言,用于訪問和操作數(shù)據(jù)庫,其主能包括數(shù)據(jù)定義、數(shù)據(jù)操縱、數(shù)據(jù)查詢和數(shù)據(jù)控制
    sql已稱為RDBMS的標(biāo)準(zhǔn)語言,單不同的RDBMS使用的SQL版本有一些差異。

    按功能用途可以將sql語言分為4類: ddl、dml、dql、dcl

    • DDL(數(shù)據(jù)定義語言) :用于數(shù)據(jù)庫、表視圖等的建立、刪除包括CREATE、ALTER、DROP
    • DML(數(shù)據(jù)操縱語言):用于添加、刪除和修改數(shù)據(jù)表中的記錄包括: INSERT、DELETE、UPDATE
    • DCL(數(shù)據(jù)控制語言):包括數(shù)據(jù)庫對象的權(quán)限管理和事務(wù)管理包括:COMMIT、ROLLBACK、GRANT
    • DQL(數(shù)據(jù)查詢語言):查詢是數(shù)據(jù)庫的基本功能包括: SELECT
  • 掌握數(shù)據(jù)庫相關(guān)操作
    DDL之?dāng)?shù)據(jù)庫
    創(chuàng)建數(shù)據(jù)庫

  • CREATE DATABASE 數(shù)據(jù)庫名

    刪除數(shù)據(jù)庫

    DROP DATABASE 數(shù)據(jù)庫名

    展示數(shù)據(jù)庫

    show databases;

    查看自己當(dāng)前在哪個(gè)數(shù)據(jù)庫下工作

    select database();
  • 了解存儲引擎engine
    存儲引擎,就是如何存儲數(shù)據(jù)、如何更新數(shù)據(jù)、如何查詢數(shù)據(jù)、如何為存儲的數(shù)據(jù)建立索引等一系列技術(shù)的實(shí)現(xiàn)方法
    查看mysql支持的存儲引擎
  • show engines; 存儲引擎描述
    MyISAM擁有較快的插入、查詢速度,但不支持事務(wù)
    InnoDB支持ACID事務(wù),支持行級鎖,支持外鍵;MySQL 5.5版本之后默認(rèn)存儲引擎
    MRG_MYISAM將一組結(jié)構(gòu)相同的MyISAM表聚合成一個(gè)整體,再進(jìn)行增刪改查操作。
    Memory所有數(shù)據(jù)存儲再內(nèi)存中,響應(yīng)快,MySQL重啟時(shí)數(shù)據(jù)會全部丟失
    Archive歸檔,且有壓縮機(jī)制,適用于歷史數(shù)據(jù)歸檔
    CSV邏輯上由逗號分隔數(shù)據(jù),會為每張表創(chuàng)建一個(gè).csv文件。
  • 熟悉MySQL的數(shù)據(jù)類型
    在創(chuàng)建數(shù)據(jù)表時(shí)、準(zhǔn)確的定義字段的數(shù)據(jù)類型比較重要
  • 數(shù)值類型

    類型所占字節(jié)數(shù)說明
    tinyint1小整數(shù)值,如狀態(tài)
    smallint2大整數(shù)值
    mediumint3大整數(shù)值
    int4大整數(shù)值
    bigint8極大整數(shù)值
    float4單精度浮點(diǎn)數(shù)值
    double8雙精度浮點(diǎn)數(shù)值
    decimalmax(D+, M+)含小數(shù)值,例如金額

    日期和時(shí)間類型

    類型所占字節(jié)數(shù)說明
    date3YYYY-MM-DD
    time3HH:MM:SS
    year1YYYY
    datetime8YYYY-MM-DD HH:MM:SS
    timestamp8YYYYMMDDHHMMSS

    字符串類型

    類型所占字節(jié)數(shù)說明
    char0~255定長字段串
    varchar0~65535變長字符串
    text0~65535長文本數(shù)據(jù)
    blob二進(jìn)制形式文本數(shù)據(jù)
  • 熟悉MySQL的建表語法
    在DDL中對數(shù)據(jù)表的操作主要有3種: 創(chuàng)建、修改、刪除
    創(chuàng)建數(shù)據(jù)表,需要定義的信息主要包括: 表名、字段名、字段類型
  • mysql的建表語法

    create [temporary] table [if not exists] table_name [(create_definition,...)] [table_options][select_statement]

    temporary:臨時(shí)表,會話結(jié)束自動(dòng)消失
    create_definition:定義表種各列屬性
    table_options:表配置,如存儲引擎、字符集
    select_statement: 通過select語句建表

  • 掌握數(shù)據(jù)表的相關(guān)操作
    示例建表
  • create table contacts( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), phone VARCHAR(20) ) ENGINE = InnoDB DEFAULT CHARSET=utf8;

    查看表

    show tables;

    查看表結(jié)構(gòu)

    desc contacts;

    增加表結(jié)構(gòu)

    alter table contacts add sex char(1);

    修改表結(jié)構(gòu)

    alter table contacts modify sex int;

    刪除表結(jié)構(gòu)

    alter table contacts drop column sex;

    刪除表

    drop table contacts;
  • 掌握如何向表中插入數(shù)據(jù)
    INSERT插入單條數(shù)據(jù)
  • INSERT INTO table_name(field1,field2,...,fieldN) VALUES (value1,value2,...,valueN);

    INSERT插入多條數(shù)據(jù)

    INSERT INTO table_name(field1,field2,...,fieldN) VALUES (valueA1,valueA2,...,valueAN),(valueB1,valueB2,...,valueBN);

    注意事項(xiàng):

    • 如果字段是字符型,值必須適用單引號或者雙引號,如"value";如果值本身就有引號,需要轉(zhuǎn)義
    • 如果所有列都要添加數(shù)據(jù),insert into 語句可以不指定列,即INSERT INTO table_name VALUES (valueA1,valueA2,…,valueAN);
  • 掌握如何修改(更新)表中的數(shù)據(jù)
    updata語法:
  • UPDATE table_name SET field1=newValue1,field2=newValue2[WHERE Clause]

    注意事項(xiàng):

    • 可以同時(shí)更新一個(gè)或多個(gè)字段
    • 可以通過where子句來指定更新范圍,如果不帶where,則更新數(shù)據(jù)表中所有記錄。
    updata contacts set phone='12345678901' where name = "張三"
  • 掌握如何刪除表中的數(shù)據(jù)
    delete from table_name [WHERE Clause]
    注意事項(xiàng):
    • 可以通過where子句來指定刪除范圍,如果不帶where,則刪除數(shù)據(jù)表中所有記錄

    示例總結(jié):
    建表

    create table contacts( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), sex tinyint default 1, phone VARCHAR(20) ) ENGINE = InnoDB DEFAULT CHARSET=utf8;

    插數(shù)據(jù)

    insert into contacts(name,sex,phone) values("張三",1,"13200000001") insert into contacts(name,sex,phone) values("xiong\'s",1,"13200000002") // 有默認(rèn)值的字段,可以不賦值也行的哈~ insert into contacts(name,sex,phone) values("李四",1,"13200000003"),("王五",1,"13200000004");

    修改(更新表)

    update contacts set sex = 2;updata contacts set sex = 1 where name="張三";updata contacts set sex = 2, phone="13200000005" where name="張三";

    刪除表內(nèi)容

    delete from contacts where id =4; delete from contacts;
  • 了解什么是數(shù)據(jù)的完整性(準(zhǔn)確性、正確性)
    數(shù)據(jù)完整性是指存儲在數(shù)據(jù)庫中的數(shù)據(jù),應(yīng)該保持一致性和可靠性
    關(guān)系模型允許定義三類數(shù)據(jù)約束,他們是事提完整性、參照完整性、以及用戶定義的完整性約束、其中前兩種完整性約束由關(guān)系數(shù)據(jù)庫系統(tǒng)自動(dòng)支持
    • 實(shí)體完整性: 實(shí)體就是現(xiàn)實(shí)世界中的某個(gè)對象,RDBMS中一行代表一個(gè)實(shí)體,實(shí)體完整性就是保證每一個(gè)實(shí)體都能被區(qū)別
    • 域完整性: 域完整性主要是對列的輸入有要求,通過限制列的數(shù)據(jù)類型、格式或值的范圍來實(shí)現(xiàn)
    • 參照完整性: 主要是表與表之間的關(guān)系、可以通過外鍵來實(shí)現(xiàn)
    • 用戶自定義完整性: 借助存儲過程和觸發(fā)器實(shí)現(xiàn)
  • 掌握如何保證數(shù)據(jù)完整性(重點(diǎn))
  • 實(shí)體完整性: 要求每張表都有唯一標(biāo)識符,每張表中的主鍵字段不能為空且不能重復(fù)
    域完整性: 針對某一具體關(guān)系數(shù)據(jù)庫條件,保證表中某些列不能輸入無效值
    約束方法: 限制數(shù)據(jù)類型、檢查約束、默認(rèn)值、費(fèi)控約束
    參照完整性: 要求關(guān)系中不允許引用不存在的實(shí)體
    用戶自定義完整性: 反應(yīng)某一具體應(yīng)用所涉及的數(shù)據(jù)必須滿足語義要求。

    唯一性約束
    在Mysql中可以適用關(guān)鍵字UNIQUE實(shí)現(xiàn)字段的唯一性約束,從而保證實(shí)體完整性

    • unique意味著任何兩條數(shù)據(jù)的同一個(gè)字段不能有相同值
    • 一個(gè)表中可以有多個(gè)unique約束
    create table person( id int not null auto_increment primary key comment "主鍵id", name varchar(30) comment "姓名", id_number varchar(18) unique comment "身份證號" );

    外鍵約束
    外鍵(FOREIGN KEY)約束定義了表之間的一致性關(guān)系,用于強(qiáng)制參照完整性。外鍵約束定義了對同一個(gè)表或者其他表的列的引用,這些列具有PRIMARY KEY 或者 UNIQUE約束。

    # 學(xué)生表 create table stu( stu_no int not null primary key comment "學(xué)號", stu_name varchar(30) comment '姓名' ); # 成績表 create table sc(id int not null auto_increment primary key comment "主鍵id",stu_no int not null comment "學(xué)號",course varchar(30) comment "課程",grade int comment "成績",foreign key(stu_no) references stu(stu_no) );

    數(shù)據(jù)庫與表的基本操作

  • 了解select的完整語法
  • select column_name1,column_name2 from table_name [where where_condition] [group by {col_name | expr | position},...[with rollup]] [having where_condition] [ORDER BY {col_name | expr |position} [ASC | DESC],...[WITH ROLLUP]] [LIMIT {[offset,] row_count | row_count OFFSET offset}]
  • 掌握適用select進(jìn)行簡單查詢(重點(diǎn))
    查詢所有
  • select * from person

    條件查詢

    select name, id_number from person select name,id_num from from person where name="張三";

    工具用法 (科普):

    select 8*9;
  • 熟練掌握where子句各類運(yùn)算符的適用
    在sql中,insert、updata、delete和select后面都能帶where子句,用于插入、刪除、修改或查詢指定條件的記錄
  • sql語句中適用where子句用法

    select column_name from table_name where column_nam 運(yùn)算符 value 運(yùn)算符描述
    =等于
    <>或!=不等于
    >大于
    <小于
    >=大于等于
    >=小于等于
    between and選取介于兩個(gè)值之間的數(shù)據(jù)范圍;在Mysql中,相當(dāng)于>=并且<=
  • 熟練掌握多條件查詢and、or的適用
    在where子句中,適用and、or可以把兩個(gè)或多個(gè)過濾條件結(jié)合起來
  • and、or運(yùn)算符語法

    select column_name from table_name where condition1 and condition2 or condition3 運(yùn)算符描述
    and表示左右兩邊的條件同時(shí)成立
    or表示左右兩邊只要有一個(gè)條件成立

    案例演示:

    use mydb;create table employee(id int not null auto_increment primary key,name varchar(30) comment "姓名",sex varchar(1) comment "性別",salary int comment "薪資(元)" );insert into employee(name,sex,salary) values("張三",'男',5500); insert into employee(name,sex,salary) values("李潔",'女',4500); insert into employee(name,sex,salary) values("李小梅",'女',4200); insert into employee(name,sex,salary) values("歐陽輝",'男',7500); insert into employee(name,sex,salary) values("李芳",'女',8500); insert into employee(name,sex,salary) values("張江",'男',6800); insert into employee(name,sex,salary) values("李四",'男',12000); insert into employee(name,sex,salary) values("王五",'男',3500); insert into employee(name,sex,salary) values("馬小龍",'男',6000); insert into employee(name,sex,salary) values("龍五",'男',8000); insert into employee(name,sex,salary) values("馮小芳",'女',10000); insert into employee(name,sex,salary) values("馬小花",'女',4000);# 單條件查詢男性工資 select * from employee where sex = "男"; select * from employee where sex != "女"; select * from employee where sex <> "女"; select * from employee where salary >= 10000; select * from employee where salary between 10000 and 12000;# 多條件,性別為男并且薪資大于等于10000 select * from employee where sex = "男" and salary >=10000;# 多條件,男性員工中,大于10000 或者 小于4000 select * from employee where sex = "男" and salary <=4000 or salary >=10000; # 以上錯(cuò)誤寫法 or作用域它左右兩邊 select * from employee where sex = "男" and (salary <=4000 or salary >=10000);
  • 掌握運(yùn)算符in 的使用
    運(yùn)算符in允許我們在where子句中過濾某個(gè)字段的多個(gè)值
  • where 子句中使用in語法

    select column_name from table_name where column_name in (value1,value2,...)
  • 掌握運(yùn)算符like的使用
  • where子句使用like語法

    select column_name from table_name where column_name like "%value%"

    說明:
    - like子句中的%類似于正則表達(dá)式中的*,匹配任意0個(gè)或多個(gè)字符
    - like子句中的_匹配任意單個(gè)字符
    - like子句如果沒有%和_就相當(dāng)于運(yùn)算符=的效果

    示例代碼:

    # 選擇id是1或者2或者3的 select * from emplyee where id=1 or id=2 or id=3; select * from emplyee where id in(1,2,3); #篩選張三 select * from emplyee where name like "張三"; # 篩選姓李的用戶 select * from emplyee where name like "李%"; # 篩選名字芳結(jié)尾的 select * from emplyee where name like "%芳";
  • 了解Mysql內(nèi)置函數(shù)
    我們通常說的Mysql函數(shù)指的是Mysql數(shù)據(jù)庫提供的內(nèi)置函數(shù),包括數(shù)學(xué)函數(shù),字符串函數(shù),日期和時(shí)間函數(shù),聚合函數(shù)條件判斷函數(shù)等,這些內(nèi)置函數(shù)可以幫助用戶更加方便地處理表中的數(shù)據(jù),簡化用戶操作
  • 函數(shù)描述
    數(shù)學(xué)函數(shù)ABS、SQRT、MOD SIN COS TAN COT等
    字符串函數(shù)length lower upper trim substring
    日期時(shí)間函數(shù)now curdate curtime sysdate data_format yera month week
    聚合函數(shù)count sum avg min max
    條件判斷函數(shù)if ifnull case when 等
    系統(tǒng)信息函數(shù)version database user等
    加密函數(shù)md5
  • 掌握常用函數(shù)的用法(重點(diǎn))
  • 函數(shù)now()

    應(yīng)用場景:
    在實(shí)際應(yīng)用中,大多數(shù)業(yè)務(wù)表都會帶一個(gè)創(chuàng)建時(shí)間,create_time字段,用于記錄每一條數(shù)據(jù)的產(chǎn)生時(shí)間,在向表中插入數(shù)據(jù)時(shí),就可以在insert語句中使用now()函數(shù)

    inser into user(id,name,create_time) values(1,"zhangsan",now());

    date_format()
    應(yīng)用場景:
    在實(shí)際應(yīng)用中,一般會按標(biāo)準(zhǔn)格式存儲日期/時(shí)間,如:2020-05-26 20:41:16. 在實(shí)際的查詢中,又可能有其他的格式要求,這時(shí)候就需要采用date_format()函數(shù)進(jìn)行格式轉(zhuǎn)換。
    select name,date_format(birthday,'%Y%m%d') from user;
    聚合函數(shù)
    聚合函數(shù)是對一組值進(jìn)行計(jì)算,并返回單個(gè)值。
    Mysql常用的聚合函數(shù)有5個(gè),分別count、sum、avg、min和max.

    函數(shù) | 描述 --- | --- count | 返回復(fù)核條件的記錄總數(shù) sum | 返回指定列的總和,忽略空值 avg | 返回指定列平局值,忽略空值 min | 返回指定列的最小值, 忽略空值 max | 返回指定列的最大值,忽略空值

    示例代碼:

    # 男性數(shù)據(jù)有多少條 select count(*) from employee where sex = '男'; # 員工表里面的總薪資 select sum(salary) from employee; # 最低薪水 select min(salary) from employee; # 平均薪資 select avg(salary) from employee;

    如果有一條數(shù)據(jù)的工資為NULL它會自動(dòng)把這條數(shù)據(jù)整體過濾掉。

    ifnull()

    函數(shù)ifnull()用于處理NULL值
    ifnull(v1,v2)如果V1的值不為NULL,則返回v1否則返回V2.

    示例代碼:

    insert into employee(name,sex,salary) values("張熊","男",null);

    case when

    case when 是流程控制語句,可以在sql語句中使用case when來獲取更加準(zhǔn)確和直接的結(jié)果,sql中的case when類似于編程語句中的if else 或者switch

    case [col_name] when [value1] then [result1]...else [default] end case when [expr] then [result1]...else[default] end

    select id,name,case sexwhen '男' then 'F'when '女' then 'M'else ''end as sex, salary from employee;
  • 掌握排序的應(yīng)用場景以及order by的使用
    我們已經(jīng)掌握使用select語句結(jié)合where查詢條件獲取需要的數(shù)據(jù),但是在實(shí)際應(yīng)用中,還會遇到下面這類需求,又該如何解決?
    • 學(xué)生按升高從高到低進(jìn)行排序
    • 雙十一交易量排行榜
    • 博客中的文章按先后順序顯示
      在sql中,使用order by對查詢結(jié)果集進(jìn)行排序,可以按照一列或多列進(jìn)行排序

    order by語法

    select column_name1,column_name2 from table_name1,table_name2 order by column_name,column_nam [asc|desc]

    說明: asc表示按升序排列,desc表示按降序排列
    默認(rèn)情況按升序排列

    示例sql:

    select * from employee order by salary desc; # 按兩個(gè)字段進(jìn)行排序 select * from employee order by sex,salary desc; # 以上就是說 先按性別排序,就會先顯示女再顯示男,然后再按薪水排序,整體出現(xiàn)的情況,先按薪資從大到小顯示女員工信息,再按薪資從大到小顯示男員工薪資。
  • 掌握分頁的應(yīng)用場景以及l(fā)imit的使用
    在select語句中使用limit子句來約束要返回的記錄數(shù),通常使用limit實(shí)現(xiàn)分頁
  • limit語法

    select column_name1,column_name2 from table_name1,table_name2 limit [offset,]row_count

    說明: offset指定要返回的第一行的偏移量,第一行的偏移量是0,而不是1.row_count指定要返回的最大行數(shù)。這個(gè)offset對于新手來講,這個(gè)偏移量的說法很不友好我感覺,反正我第一次沒理解出來,就是從第幾條數(shù)據(jù)開始讀,讀多少條。 第一頁開始每頁顯示10條 limit 0,10

    示例sql:

    # 獲取前3條數(shù)據(jù) select * from employee limit 3; # 分頁每頁顯示5條,顯示第一頁 select * from employee limit 0,5; # 分頁每頁顯示5條,顯示第二頁 select * from employee limit 5,5; # 分頁顯示每頁顯示5條,顯示第三頁 select * fromm employee limit 10,5;
  • 掌握group by 的應(yīng)用場景及使用
    我們已經(jīng)掌握使用select語句結(jié)合where查詢條件獲取需要的數(shù)據(jù),但在實(shí)際的應(yīng)用中,還會遇到下main這類需求,又該如何解決:
    • 公司想知道每個(gè)部門有多少名員工
    • 班主任想統(tǒng)計(jì)各科第一名的成績
    • 某門店想掌握男、女性會員的人數(shù)及平局年齡
      從字面上理解,group by表示根據(jù)某種規(guī)則對數(shù)據(jù)進(jìn)行分組,它必須配合聚合函數(shù)進(jìn)行使用,對數(shù)據(jù)進(jìn)行分組之后需要進(jìn)行count、sum、avg、max和min等聚合運(yùn)算

    group by語法

    select column_name,aggregate_function(column_nam) from table_name group by column_name
  • aggregate_function表示聚合函數(shù)。

  • group by可以對一列或多列進(jìn)行分組。

  • 掌握having的應(yīng)用場景及使用
    在sql中增加having子句的原因是,where關(guān)鍵字無法與聚合函數(shù)一起使用,having子句可以對分組后的各組數(shù)據(jù)進(jìn)行篩選

  • having語法

    select column_name,aggregate_function(column_name) from table_name where column_name operator value group by column_name having aggregate_function(column_name) operator value

    示例sql:

    # 把原有的employee表刪除了,創(chuàng)建這個(gè)表 create table employee(id int not null auto_increment primary key,name varchar(30) comment "姓名",sex varchar(1) comment "性別",salary int comment "薪資(元)",dept varchar(30) comment "部門" ); insert into employee(name,sex,salary,dept) values("張三", "男", 5500, "部門A"); insert into employee(name,sex,salary,dept) values("李潔", "女", 4500, "部門C"); insert into employee(name,sex,salary,dept) values("李小梅", "女", 4200, "部門A"); insert into employee(name,sex,salary,dept) values("歐陽輝", "男", 7500, "部門C"); insert into employee(name,sex,salary,dept) values("李芳", "女", 8500, "部門A"); insert into employee(name,sex,salary,dept) values("張江", "男", 6800, "部門A"); insert into employee(name,sex,salary,dept) values("李四", "男", 12000, "部門B"); insert into employee(name,sex,salary,dept) values("王五", "男", 3500, "部門B"); insert into employee(name,sex,salary,dept) values("馬小龍", "男", 6000, "部門A"); insert into employee(name,sex,salary,dept) values("龍五", "男", 8000, "部門B"); insert into employee(name,sex,salary,dept) values("馮小芳", "女", 10000, "部門C"); insert into employee(name,sex,salary,dept) values("馬小花", "女", 4000, "部門B"); insert into employee(name,sex,salary,dept) values("張熊", "男", 8800, "部門A");# 男性員工和女性員工的數(shù)量 select sex,count(*) from employee group by sex; # 統(tǒng)計(jì)每個(gè)部門的人數(shù) select dept,count(*) from employee group by dept; # 知道每個(gè)工資薪水總和 select dept,sum(salary) from employee group by dept; # 每個(gè)部門薪資最高的 select dept,max(salary) from employee group by dept; # 每個(gè)部門薪資最低 select dept,min(salary) from employee group by dept; # 人數(shù)小于4個(gè)人的部門 select dept,count(*) from employee group by dept having count(*)<5;
  • 屬性group_concat的應(yīng)用場景
    應(yīng)用場景:
    • 使用group by可以分組統(tǒng)計(jì)每個(gè)部門有多少員工。加入,除了統(tǒng)計(jì)每個(gè)部門的員工數(shù)量之外,還想知道具體是哪些員工(員工列表),又該怎么實(shí)現(xiàn)呢?
  • 掌握group_concat的使用
  • group_concat

    group_concat配合group by一起使用,用于將某一列的值按指定的分隔符進(jìn)行拼接,mysql默認(rèn)的分隔符是逗號

    select dept,group_concat(name) from employee group by dept;
  • 掌握distinct的用法
    distinct用于在查詢中返回列的唯一不同值(去重復(fù)),支持單列或多列。在實(shí)際的應(yīng)用中,表中的某一列含有重復(fù)值是很常見的,如employ表的dept列.如果在查詢數(shù)據(jù)時(shí),希望得到某列的所有不同值,可以使用distinct
  • #distinct語法 select distinct column_name,column_nam from table_name;

    sql示例:

    # 性別這列去重 select distinct sex from employee;create table footprint(id int not null auto_increment primary key,username varchar(30) comment '用戶名',city varchar(30) comment "城市",visit_date varchar(10) comment "到訪日期" );insert into footprint(username, city, visit_date) values("liufeng", "貴陽", "2019-12-05"); insert into footprint(username, city, visit_date) values("liufeng", "貴陽", "2020-01-15"); insert into footprint(username, city, visit_date) values("liufeng", "北京", "2018-10-10"); insert into footprint(username, city, visit_date) values("zhangsan", "上海", "2020-01-01"); insert into footprint(username, city, visit_date) values("zhangsan", "上海", "2020-02-02"); insert into footprint(username, city, visit_date) values("lisi", "拉薩", "2016-12-20");# 有多少用戶在footprint留下足跡 select distinct username from footprint; # 所有的用戶達(dá)到過哪些城市 select dictinct city from footprint; # 每一個(gè)用戶去過哪些地方 select distinct username,city from footprint;
  • 熟悉表連接的幾種方式
    表連接(join) 是在多個(gè)表之間通過一定的連接條件,使表之間發(fā)生關(guān)聯(lián),進(jìn)而能從多個(gè)表之間獲取數(shù)據(jù)
  • 表連接語法

    select table1.column,table2.column from table1,table2 where table1.column1 = table2.column2;
  • 熟悉幾種表連接的區(qū)別
    • 內(nèi)連接 join 或inner join
    • 外連接:左連接 left join,右連接 right join 全連接 full join
    • 自然連接:同一張表內(nèi)的連接
    連接類型定義例子
    內(nèi)連接至連接匹配的行select A.c1,B.c2 from A join B on A.c3 = B.c3
    左連接包含左表的全部行(不管右表是否存在與之匹配的行), 以及右表中遍布匹配的行select A.c1,B.c2 from A left join B on A.c3 = B.c3
    右連接包含右表的全部行(不管左表是否存在與之匹配的行), 以及左表中遍布匹配的行select A.c1,B.c2 from A right join B on A.c3 = B.c3
    全連接包含左右兩個(gè)表的全部行(不管在另一個(gè)表中是否存在與之匹配的行)select A.c1,B.c2 from A full join B on A.c3 = B.c3

    全連接: mysql里面無

  • 掌握多表連接查詢
  • # 創(chuàng)建兩張表 drop table if exists score; drop table if exists student;create table student( stu_no varchar(20) not null primary key comment"學(xué)號", name varchar(30) comment "姓名", address varchar(150) comment '地址' ); insert into student(stu_no,name,address) values('2016001','張三','貴州貴陽'); insert into student(stu_no,name,address) values('2016002','李芳','陜西興平'); insert into student(stu_no,name,address) values('2016003','張曉燕','江西南昌');create table score( id int not null auto_increment primary key, course varchar(50) comment '科目', stu_no varchar(20) comment "學(xué)號", score int comment '分?jǐn)?shù)', foreign key(stu_no) references student(stu_no) );insert into score(course,stu_no,score) values('計(jì)算機(jī)','2016001',99); insert into score(course,stu_no,score) values('離散數(shù)學(xué)','2016001',85); insert into score(course,stu_no,score) values('計(jì)算機(jī)','2016002',78);# 表的數(shù)據(jù)特性,有個(gè)叫張曉燕 2016003的沒成績# 內(nèi)連接(交集) select A.stu_no,A.name,B.course,B.score from student A join score B on(A.stu_no = B.stu_no); select A.stu_no,A.name,B.course,B.score from student A inner join score B on(A.stu_no = B.stu_no); select A.stu_no,A.name,B.course,B.score from student A where A.stu_no = B.stu_no;# 左連接 select A.stu_no,A.name,B.course,B.score from student A left join score B on(A.stu_no = B.stu_no);
  • 什么是自連接
    自連接時(shí)一種特殊的表連接,它是指互相連接的表在物理上同為一張表,但是邏輯上時(shí)多張表。自連接通常用于表中的數(shù)據(jù)有層次結(jié)構(gòu),如區(qū)域表、菜單表、商品分類等。
  • 自連接語法

    select a.column,b.column from table a,table b where a.column=b.column;

    示例sql:

    drop table if exists area; drop table if exists area;create table area( id int not null auto_increment primary key comment '區(qū)域id', pid int not null comment '父id(0-省份)', name varchar(30) comment '區(qū)域名稱' );insert into area(id,pid,name) values(1,0,'貴州省'); insert into area(id,pid,name) values(2,1,'貴陽'); insert into area(id,pid,name) values(3,1,'遵義'); insert into area(id,pid,name) values(4,0,'廣東省'); insert into area(id,pid,name) values(5,4,'廣州'); insert into area(id,pid,name) values(6,4,'深圳');# 查出所有城市 select * from area where pid<>0; # 查出這些城市的父親id select A.id,A.name,B.name ad provinceName from area A, area B where A.pid = B.id and A.pid<>0;
  • 掌握子查詢in的使用
    之前的課程中,我們已經(jīng)學(xué)習(xí)過運(yùn)算符IN,它允許我們在where子句中過濾某個(gè)字段的多個(gè)值。
  • where子句使用in語法

    select column_name from table_name where column_name IN(value1,value2,...);

    如果運(yùn)算符in后面的值式來源于某個(gè)查詢結(jié)果,并非是指定幾個(gè)值,這時(shí)就需要用到子查詢。子查詢又稱為內(nèi)部查詢或嵌套查詢,即在sql查詢的where子句中嵌套查詢語句

    子查詢in語法

    select column_name from table_name where column_name IN(select column_name from table_name [where] );
  • 掌握子查詢exists的使用
    exists是子查詢中用于測試內(nèi)部查詢是否返回任何行的布爾運(yùn)算符。將主查詢的數(shù)據(jù)放到子查詢中做條件驗(yàn)證,根據(jù)驗(yàn)證結(jié)果(TRUE 或FALSE)來決定主查詢的數(shù)據(jù)結(jié)果是否保留
  • where子句使用exists語法

    select column_name1 from table_name1 where exists (select * from table_name2 where condition);

    示例sql:

    # 看下student表數(shù)據(jù) select * from score;# 要用in做一個(gè)子查詢 查詢所有選修了課程的學(xué)生 select A.* from student A where A.stu_no in(select B.stu_no from score B);# 查詢選修了離散數(shù)學(xué)的學(xué)生 select A.* from student A where A.stu_no in (select B.stu_no from score B where B.course='離散數(shù)學(xué)')# 要用exists做一個(gè)子查詢 查詢所有選修了課程的學(xué)生 select A.* from student A where exists(select * from score B where A.stu_no = B.stu_no); # 這個(gè)exists說一點(diǎn)個(gè)人的理解,先看子查詢里面的東西,也就是說子查詢查詢出來了一個(gè)表,這個(gè)時(shí)候就是說,A表查詢出來的數(shù)據(jù),在查詢出來的這個(gè)表中存在就能輸出出來,不存在就不能輸出。

    總結(jié)

    以上是生活随笔為你收集整理的持续更新,mysql的复习强化路的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。