mysql 知識(shí)復(fù)習(xí)

linux (mysql安裝) :sudo apt-get install mysql-server mysql-client
mysql 修改密碼: mysqladmin -u root -p password 123456
mysql重要知識(shí):增刪該查,事務(wù),索引,存儲(chǔ)過程
root 用戶是最高權(quán)限
常見的字符集:GB2312,GBK
事務(wù):要么完全執(zhí)行,要么完全不執(zhí)行 
事務(wù)語句 : 1.開啟:begin; 2.提交:commit; 3.回滾:rollback;
原子性:在一個(gè)事物的所有操作中 要么全部執(zhí)行,要么全部不執(zhí)行,
一致性:在事務(wù)開始之前和事務(wù)結(jié)束以后,數(shù)據(jù)庫的完整性沒有被破壞,
隔離性:防止多個(gè)事務(wù)并發(fā)執(zhí)行時(shí)由于交叉執(zhí)行而導(dǎo)致數(shù)據(jù)的不一致
 持久性:事務(wù)處理結(jié)束后,對(duì)數(shù)據(jù)的修改就是永久的,即便系統(tǒng)故障也不會(huì)丟失。

索引:

SQL:結(jié)構(gòu)化查詢語言
sql功能分類
DDL: 數(shù)據(jù)定義語言 用來定義數(shù)據(jù)庫對(duì)象:創(chuàng)建庫、表、列等
DML: 數(shù)據(jù)操作語言 用來操作數(shù)據(jù)表中的記錄
DQL: 數(shù)據(jù)查詢語言 用來查詢數(shù)據(jù)
DCL: 數(shù)據(jù)控制語言 用來定義訪問權(quán)限和安全級(jí)別

MySQL支持多種類型
數(shù)值類型:int 整數(shù),double 浮點(diǎn)數(shù)
字符串類型:char , varchar , text 文本
日期和時(shí)間類型:date 年月日 yyyy-MM-dd ,time 時(shí)分秒 hh:mm:ss ,datetime 年月日 時(shí)分秒 yyyy-MM-dd hh:mm:ss

DDL 數(shù)據(jù)定義語言
給表添加一列:alter table 表名 add 列名 數(shù)據(jù)類型;   alter table students add age int;
刪除一列:alter table students drop age;
修改表的數(shù)據(jù)類型:alter table students modify id bigint;
修改表名:alter table 原始表名 TO 新表名;  alter table students to teacher;
查看表的創(chuàng)建細(xì)節(jié):show create table students;
修改表的字符集gbk:alter table students charset=gbk
修改表的列名:alter table students change name newname varchar(20);
修改teacher表的存儲(chǔ)引擎更改為MyISAM類型 : alter table teacher engine = MyISAM

DML 數(shù)據(jù)操作語言 (對(duì)表中的數(shù)據(jù)進(jìn)行增、刪、改操作)
增,插入數(shù)據(jù) insert into (注意:我們插入的數(shù)據(jù)順序必須要跟列對(duì)應(yīng))
1.完全插入:例如:insert into 表名 values( 108401,' 小甜甜 ', 20,1,' 1245678999 ');
2.選擇插入:例如:insert into 表名(userid,name,age) values( 10000,' 花花 ',19);
3.多行插入:例如:insert into 表名(userid,name) values(19999,' 葡萄 '),(18888,‘ 辣椒 ’);
將一個(gè)表復(fù)制到另一個(gè)表中 insert into 新表 (列名,列名...) select 列名,列名... from 原表 ;

更新 update
更新一條數(shù)據(jù):update 表名 set 列名1=列值1, 列名2=列值2... where 列名=值;例如:update students set name='倪好' where id=1;
把倪好的年齡在原來基礎(chǔ)上+1歲:例如:update students set age=age+1 where id = 7;
把年齡這個(gè)字段更新為18歲:update students set age=18;
把姓名為花花的學(xué)生分?jǐn)?shù)修改為90:update students set score=90 where name='花花';

刪除 delete
刪除一條數(shù)據(jù):delete from 表名 where 列名=值;例如:delete from students where id=3;
刪除所有(盡量不要用):delete from 表名;例如:delete from students;
刪除truncate:例如:truncate 表名;例如:truncate students;

DQL 數(shù)據(jù)查詢語言 用來查詢數(shù)據(jù)
查詢 select
聚合函數(shù)
count() | count(1) :計(jì)算所有行; select count() from students where age>11;
avg() :計(jì)算列的平均值; select avg(age) from students;
max() :計(jì)算列的最大值; select max(age) from students;
min() :計(jì)算列的最小值; select min(age) from students;
sum() :求和 ,計(jì)算列的總和; select sum(age) from students;
模糊查詢:關(guān)鍵字 like  通配符 _ :任意一個(gè)字母,通配符 %?。喝我?~n個(gè)字母
select * from students where name like '_花'; select * from students where name like '花%';
去重: 關(guān)鍵字distinct select distinct name from students;
分組查詢:group by   根據(jù)名字分組 select name from students group by name;
使用group_concat(字段名) 來放置每一組的某字段的值的集合
根據(jù)性別分組,查詢哪個(gè)名字為男性,哪個(gè)姓名為女性:select gender,group_concat(name) from students group by gender
查詢每個(gè)部門的部門名稱以及每個(gè)部門工資大于1500的人數(shù)
select job,group_concat(salary),count(*) from emp where salary > 1500 group by job;
去重和分組的區(qū)別:去重是把相同的數(shù)據(jù)去除掉,分組是把相同的數(shù)據(jù)劃成一組
group by + having having作用和where一樣,但是having只能用于 group by where 在分組之前使用,having在分組之后使用,ha cving 可以使用聚合函數(shù),where 不可以使用
使用having 查詢工資總和大于9000的部門名稱:select job,group_concat(salary),sum(salary) from emp group by job having sum(salary)>5000;
使用where 查詢工資大于9000的部門名稱:select job,group_concat(salary),sum(salary) from emp where salary>9000 group by job;
書寫順序
select --> from --> where --> group by --> having --> order by --> limit

合并結(jié)果集union與union_all?。骸『喜⒔Y(jié)果集就是把兩個(gè)SELECT語句的查詢結(jié)果合并到一起展示
注意事項(xiàng):被合并的兩個(gè)結(jié)果集:列數(shù),列類型必須相同
union 合并時(shí)去除重復(fù)記錄:select * from 表1 union select * from 表2;
union all 合并時(shí)不去除重復(fù)記錄:select * from 表1 union all select * from 表2;
笛卡爾集
什么是笛卡爾集:同時(shí)查詢兩個(gè)表(使用一個(gè)SQL語句),出現(xiàn)的結(jié)果就是笛卡爾集結(jié)果
例如:select * from students,score;mjkysql 知識(shí)復(fù)習(xí) ?!?br> 解決笛卡爾集現(xiàn)象:查詢時(shí)主鍵和外鍵保持一致,
99寫法:select * from students st,score sc where st.id=sc.sid;
內(nèi)連接寫法:select * from students st inner join score sc on st.id=sc.sid;
查詢成績(jī)大于70并且性別為女的學(xué)生的所有信息:select * from students st inner join score sc on st.id=sc.sid where sc.score>70 and st.gender='女';

內(nèi)連接:( 關(guān)鍵字 inner join)
              select * from students st inner join score sc on st.id=sc.sid;
左鏈接:( 關(guān)鍵字 left join )左表中數(shù)據(jù)的全部顯示出來,右表中只顯示符合條件的數(shù)據(jù)
               select * from students st left join score sc on st.id=sc.sid;
右鏈接:( 關(guān)鍵字 right join )右表中的數(shù)據(jù)全部顯示出來,左表中只顯示符合條件的數(shù)據(jù)
                select * from score sc right join students st on st.id=sc.sid;          

多表查詢:
               99寫法:select * from students st,score sc,course co where st.id=sc.sid and co.id=sc.cid;
               內(nèi)連接:select * from students st inner join score sc on st.id=sc.sid inner join course co on co.id=sc.cid;

子查詢:
            什么是子查詢?
                    一個(gè)select語句中包含另外一個(gè)完整的select語句或者說兩個(gè)以上select,那么就是子查詢語句了
            子查詢出現(xiàn)的位置~
                    where后,把select查詢出的結(jié)果當(dāng)做另外一個(gè)select的條件值
                    from后,把查詢出的結(jié)果當(dāng)作一個(gè)新表  然后另起一個(gè)新名字   例如:as cc
                      
                    where 后面:查詢與項(xiàng)羽同一個(gè)部門人員工
                                          select ename from emp where deptno = (select deptno from emp where ename='項(xiàng)羽');
                    from 后面:查詢30號(hào)以內(nèi)大于2000的薪水的人(放在from,是把這個(gè)語句當(dāng)成一個(gè)表)
                                          select ename from (select ename,deptno,salary from emp where deptno=30) as cc where 
                                          cc.salary>2000;

1.排序 order by   asc 升序 desc 降序
select * from emp order by age; 按照年齡排序(默認(rèn)為升序)
select * from emp order by age asc; 按照年齡排序升序
select * from emp order by age desc; 按照年齡排序降序
2.條件查詢 and
select * from emp where name='花花' and age=11; 查詢emp表中 名字為花花并且年齡為11的所有信息(兩個(gè)條件同時(shí)滿足)
3.條件查詢 or
select * from emp where name='花花' or age=11; 查詢emp表中 名字為花花或者年齡為11的所有信息(滿足其中一個(gè)條件即可)
4.集合方式查詢 in   (in 相當(dāng)于or)
select * from emp age in (11,21); 查詢emp 表中年齡為11或者21 的所有信息
5.區(qū)間查詢 between ... and ... 
select * from emp where age between 11 and 21; 查詢emp表中年齡在11至21之間的所有信息 (還可以用 >= <=)
相當(dāng)于
select * from emp where age >= 11 and age <= 21; 查詢emp表中年齡大于等于11并且年齡小魚等于21的所有信息

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • -- 1基礎(chǔ)查詢 -- 1.1查詢所有列 SELECT * FROM stu; -- 1.2在stu表中查詢指定列...
    龍傲天閱讀 824評(píng)論 0 0
  • 條件查詢就是在查詢時(shí)給出WHERE子句,在WHERE子句中可以使用如下運(yùn)算符及關(guān)鍵字: =、!=、<>、<、<=、...
    淡泊年華閱讀 637評(píng)論 0 2
  • 一、SQL速成 結(jié)構(gòu)查詢語言(SQL)是用于查詢關(guān)系數(shù)據(jù)庫的標(biāo)準(zhǔn)語言,它包括若干關(guān)鍵字和一致的語法,便于數(shù)據(jù)庫元件...
    shadow雨軒閱讀 585評(píng)論 0 3
  • 1. 查詢20號(hào)部門的所有員工信息: select * from emp where deptno = 20; 2...
    AAnna珠閱讀 3,924評(píng)論 0 2
  • 1.安裝go語言環(huán)境(windows和linux) 2.環(huán)境變量配置path環(huán)境(例如C:\Go\bin) 3.添...
    smallThree1閱讀 448評(píng)論 0 0

友情鏈接更多精彩內(nèi)容