學(xué)習(xí)記錄之?dāng)?shù)據(jù)庫(kù)(SQL)
數(shù)據(jù)庫(kù):
????學(xué)習(xí)如何對(duì)數(shù)據(jù)進(jìn)行增刪改查操作
SQL:
????Structured Query Language 結(jié)構(gòu)化查詢語(yǔ)言, 此語(yǔ)言是用于程序員和數(shù)據(jù)庫(kù)軟件進(jìn)行交流的語(yǔ)言
DBMS:
????DataBaseManagementSystem: 數(shù)據(jù)庫(kù)管理系統(tǒng)(俗稱(chēng)數(shù)據(jù)庫(kù)軟件)?
????常見(jiàn)的數(shù)據(jù)庫(kù)管理系統(tǒng)(DBMS)
????????MySQL: ?Oracle公司產(chǎn)品, ?MySQL08年被Sun公司收購(gòu), Sun公司09年被Oracle收購(gòu). ? ?開(kāi)源產(chǎn)品 ? ?市占率第一??
????????Oracle: ? ? Oracle公司產(chǎn)品, ?閉源產(chǎn)品, 性能最強(qiáng) ?價(jià)格最貴 ?市占率第二??
????????SQLServer: 微軟公司產(chǎn)品, ? 閉源產(chǎn)品, 市占率第三
????????DB2: IBM公司產(chǎn)品 ,閉源產(chǎn)品??
????????SQLite: 輕量級(jí)數(shù)據(jù), 安裝包只有幾十k ,只具備基礎(chǔ)的增刪改查功能??
如何連接數(shù)據(jù)庫(kù)執(zhí)行SQL語(yǔ)句:
執(zhí)行SQL語(yǔ)句之前需要先和數(shù)據(jù)庫(kù)軟件進(jìn)行鏈接
????1.從開(kāi)始菜單中找到MariaDB或MySQL文件夾 展開(kāi)后找到 MySQL Client 然后輸入密碼回車(chē)
????2.如果使用的Linux或蘋(píng)果系統(tǒng) ?打開(kāi)終端 然后輸入 mysql -uroot -p ?回車(chē) 然后輸入密碼回車(chē)
????3.顯示下圖內(nèi)容 代表成功

????4.退出命令 ?exit;
數(shù)據(jù)庫(kù)中如何保存數(shù)據(jù):
????數(shù)據(jù)庫(kù)中以表為單位保存數(shù)據(jù),表是存在于數(shù)據(jù)庫(kù)之中, 保存數(shù)據(jù)則需要先建庫(kù)再建表
SQL語(yǔ)句格式要求:
????1.以;結(jié)尾
????2.不區(qū)分大小寫(xiě)
????3.可以包含空格和換行?
數(shù)據(jù)庫(kù)相關(guān)SQL:
??查詢所有數(shù)據(jù)庫(kù):??show databases;? ??
? 創(chuàng)建數(shù)據(jù)庫(kù):create database 數(shù)據(jù)庫(kù)名 charset=utf8/gbk;
??舉例:
????create database db1;
????create database db2 charset=utf8;
????查看數(shù)據(jù)庫(kù)信息:?show create database 數(shù)據(jù)庫(kù)名;
舉例:
???show create database db1;
刪除數(shù)據(jù)庫(kù):?drop database 數(shù)據(jù)庫(kù)名;
舉例:
???drop database db2;
使用數(shù)據(jù)庫(kù):執(zhí)行表相關(guān)和數(shù)據(jù)相關(guān)的SQL語(yǔ)句之前必須使用了某一個(gè)數(shù)據(jù)庫(kù) 否則會(huì)報(bào)錯(cuò),格式: ?use 數(shù)據(jù)庫(kù)名;
舉例:
???use db1;
表相關(guān)的SQL語(yǔ)句:執(zhí)行表相關(guān)的SQL語(yǔ)句必須提前使用了某個(gè)數(shù)據(jù)庫(kù),否則會(huì)報(bào)錯(cuò)?

查詢所有表:show tables;
創(chuàng)建表:create table 表名(字段1名 類(lèi)型,字段2名 類(lèi)型)charset=utf8/gbk;
舉例:
???create table person(name varchar(50), age int);?
? ?create table hero(name varchar(50),money int,type varchar(10))charset=utf8;
查看表信息:show create table 表名; show create table student;
刪除表:drop table 表名; drop table student;?
修改表名:rename table 原名 to 新名;?rename table person to p;
查看表字段:??desc 表名;desc = description 描述??desc p;
添加表字段:?
????最后面添加格式: ? alter table 表名 add 字段名 類(lèi)型;
????最前面添加格式: alter table 表名 add 字段名 類(lèi)型 first;
????在xxx字段的后面添加格式: alter table 表名 add 字段名 類(lèi)型 after xxx;
????舉例:
???????alter table emp add age int;
???????alter table emp add id int first;
???????alter table emp add gender varchar(5) after name;
刪除表字段:alter table 表名 drop 字段名;
舉例:
???alter table emp drop gender;
修改表字段:alter table 表名 change 原名 新名 ?新類(lèi)型;
舉例:
??alter table emp change name age int;
數(shù)據(jù)相關(guān)SQL
????執(zhí)行數(shù)據(jù)相關(guān)的SQL 必須已經(jīng)使用了某一個(gè)數(shù)據(jù)庫(kù)并且已經(jīng)創(chuàng)建了保存數(shù)據(jù)的表
往表中插入數(shù)據(jù)(增):
????全表插入格式:insert into 表名 values(值1,值2,....);
????????insert into person values("Tom",18);
????指定字段插入格式: insert into 表名(字段1名,字段2名) values(值1,值2);
????????insert into person(name) values("Jerry");
????批量插入數(shù)據(jù):
????????insert into person values("lilei",28),("hanmeimei",28);
????????insert into person(name) values("aa"),("bb"),("cc");
????插入中文數(shù)據(jù):
???????insert into person values("劉德華",50);
????如果執(zhí)行上面SQL語(yǔ)句報(bào)如下錯(cuò)誤:?

執(zhí)行 set names gbk; ?可以解決?

查詢數(shù)據(jù):select 字段信息 from 表名 where 條件;
?舉例:
? ??select name from person;
????select name,age from person;
????select * from person;
????select name from person where age=28;
????select * from person where name='劉德華';
修改數(shù)據(jù):update 表名 set 字段1名=值,字段2名=值 where 條件;
舉例:
????update person set name="杰瑞" where name='Jerry';
????update person set name="張學(xué)友",age=30 where name="劉德華";
????update person set age=50 where name="aa";
????update person set age=88 where age=28;
????update person set age=10 where age is null;
刪除數(shù)據(jù):delete from 表名 where 條件;
舉例:
? ? delete from person where age=88;
????delete from person where name="張學(xué)友";
????delete from person where age>10;
????delete from person;
數(shù)據(jù)類(lèi)型:
????整數(shù): int(m) 和 bigint(m) , bigint等效java中的long, ?m代表顯示長(zhǎng)度,舉例m=5,存18 ?查詢到 00018 ? , 需要補(bǔ)零的話必須使用zerofill關(guān)鍵字
????create table t1(age int(5) zerofill);
????insert into t1 values(18);
????select * from t1;
浮點(diǎn)數(shù): double(m,d) ?m代表總長(zhǎng)度,d代表小數(shù)長(zhǎng)度 ? 54.432 ?m=5 d=3
????create table t2(price double(5,3));
????insert into t2 values(45.2312312123);
????insert into t2 values(455.231); ? //報(bào)錯(cuò)
字符串
????char(m): 固定長(zhǎng)度字符串 , m=5 ?存abc ?占5個(gè)字符長(zhǎng)度 , 應(yīng)用場(chǎng)景: 當(dāng)存儲(chǔ)長(zhǎng)度固定時(shí),比如存儲(chǔ)性別char(1) ?, 最大字符長(zhǎng)度255
????varchar(m):可變長(zhǎng)度字符串, m=5 存abc 占3個(gè)字符長(zhǎng)度, 最大值65535但是建議保存255以內(nèi)長(zhǎng)度的數(shù)據(jù)
????text(m):可變長(zhǎng)度字符串, 最大值65535 建議保存長(zhǎng)度大于255的數(shù)據(jù)
日期
????date: 只能保存年月日
????time: 只能保存時(shí)分秒
????datetime: 保存年月日時(shí)分秒, 最大值9999-12-31, 默認(rèn)值為null
????timestamp(時(shí)間戳:保存1970年1月1日到現(xiàn)在的毫秒數(shù)):保存年月日時(shí)分秒, 最大值2038-1-19 , 默認(rèn)值為當(dāng)前系統(tǒng)時(shí)間(當(dāng)賦值為null時(shí)觸發(fā)默認(rèn)值)
????create table t_date(t1 date,t2 time,t3 datetime,t4 timestamp);
????insert into t_date values("2022-06-09",null,null,null);
????insert into t_date values(null,"16:16:20","2011-11-22 10:20:30",null);
????select * from t_date;
主鍵約束 primary key
????約束: 創(chuàng)建表時(shí)給表字段添加的限制條件
????主鍵: 表示數(shù)據(jù)唯一性的字段稱(chēng)為主鍵
????主鍵約束: 限制主鍵的值唯一且非空
舉例:
????create table t5(id int primary key,name varchar(30));
????insert into t5 values(1,"a");
????insert into t5 values(2,"b");
????insert into t5 values(2,"c"); ? ?//重復(fù)報(bào)錯(cuò)
????insert into t5 values(null,"d"); ? ? ?//不能為null 報(bào)錯(cuò)??
主鍵約束+自增 ?primary key auto_increment
????當(dāng)字段設(shè)置為自增后 插入null值時(shí)會(huì)觸發(fā)自增
? ? ?自增規(guī)則: 從歷史最大值+1
舉例:
????create table t6(id int primary key auto_increment,name varchar(50));
????insert into t6 values(null,"a");
????insert into t6 values(null,"b");
????insert into t6 values(10,"c");
????insert into t6 values(null,"d");
????delete from t6 where id>=10;
????insert into t6 values(null,"e");
數(shù)據(jù)庫(kù)SQL語(yǔ)句的分類(lèi)
????DDL: 數(shù)據(jù)定義語(yǔ)言, 包括數(shù)據(jù)庫(kù)相關(guān)和表相關(guān)的SQL,truncate table 表名; ?刪除表并創(chuàng)建新表
????DML: 數(shù)據(jù)操作語(yǔ)言,包括:增刪改查相關(guān)的SQL語(yǔ)句
????DQL: 數(shù)據(jù)查詢語(yǔ)言,包括:select查詢相關(guān)
????TCL: 事務(wù)控制語(yǔ)言,包含和事務(wù)相關(guān)的SQL語(yǔ)句
????DCL: 數(shù)據(jù)控制語(yǔ)言, 包含創(chuàng)建用戶以及用戶權(quán)限分配相關(guān)SQL?
導(dǎo)入*.sql批處理文件?
? ? 1.壓縮文件復(fù)制到某個(gè)盤(pán)的根目錄下面并解壓到當(dāng)前文件夾
????2.在命令行客戶端中 執(zhí)行source F:/emp.sql;
????????執(zhí)行show tables;
????????如果經(jīng)過(guò)測(cè)試出現(xiàn)中文亂碼問(wèn)題 執(zhí)行 ?set names utf8; ? ? 或 set names gbk;
比較運(yùn)算符 > < >= <= = ?!=和<>
????1.查詢工資大于等于3000的員工姓名,工資和工作
????select name,sal,job from emp where sal>=3000;
????2.查詢1號(hào)部門(mén)的員工姓名,工資,部門(mén)id
????select name,sal,dept_id from emp where dept_id=1;
????3.查詢程序員的名字和工資
????select name,sal from emp where job="程序員";
????4.查詢員工表中不是銷(xiāo)售的員工姓名和工作(兩種寫(xiě)法)
????select name,job from emp where job!="銷(xiāo)售";
????select name,job from emp where job<>"銷(xiāo)售";
and 和 or 和 not
????and: 等效java中的&&, ?需要多個(gè)條件同時(shí)滿足時(shí)使用
????or: 等效java中的|| , ?多個(gè)條件滿足一個(gè)時(shí)使用
????1.查詢1號(hào)部門(mén)工資高于2000的員工信息
????select * from emp where dept_id=1 and sal>2000;
????2.查詢3號(hào)部門(mén)或工資等于5000的員工信息
????select * from emp where dept_id=3 or sal=5000;
????3.查詢出CEO和項(xiàng)目經(jīng)理的名字
????select name from emp where job="CEO" or job="項(xiàng)目經(jīng)理";
????4.查詢有獎(jiǎng)金的銷(xiāo)售名字和獎(jiǎng)金
????select name,comm from emp where comm>0 and job="銷(xiāo)售";
is null 和 is not null
????1.查詢有上級(jí)領(lǐng)導(dǎo)的員工姓名和領(lǐng)導(dǎo)id
????select name,manager from emp where manager is not null;
????2.查詢沒(méi)有上級(jí)領(lǐng)導(dǎo)的員工姓名和領(lǐng)導(dǎo)id
????select name,manager from emp where manager is null;
between x and y兩者之間(包含x和y)
????1.查詢工資在2000到3000之間的員工信息
????select * from emp where sal>=2000 and sal<=3000;
????select * from emp where sal between 2000 and 3000;
????2.查詢工資在2000到3000之外的員工信息
????select * from emp where sal not between 2000 and 3000;
in(x,y,z)
????當(dāng)查詢某個(gè)字段的值等于多個(gè)值的時(shí)候使用
????1.查詢工資等于1500,3000和5000的員工姓名和工資
????select name,sal from emp where sal=1500 or sal=3000 or sal=5000;
????select name,sal from emp where sal in(1500,3000,5000);
????2.查詢工作不是銷(xiāo)售和程序員的信息
????select ?* from emp where job not in("銷(xiāo)售","程序員");
distinct去重
去掉重復(fù)的數(shù)據(jù)
????1.查詢員工表中出現(xiàn)了哪幾種不同的工作
????select distinct job from emp;
????2.查詢員工表中出現(xiàn)了哪幾個(gè)部門(mén)的id
????select distinct dept_id from emp;
模糊查詢like
????%: 代表0或多個(gè)未知字符
????_:代表1個(gè)未知字符
????舉例:
????以x開(kāi)頭 ? ? ? x%
????以x結(jié)尾 ? ? ? %x
????包含x ? ? ? ? ? %x%
????第二個(gè)字符是x ? ? ? _x%
????以x開(kāi)頭以y結(jié)尾 ? ?x%y
????第三個(gè)是x倒數(shù)第二個(gè)是y ? ? ? __x%y_
排序 ?order by
???格式: order by 字段名 asc升序(默認(rèn))/desc降序;
????1.查詢員工姓名和工資,按照工資升序排序
????select name,sal from emp order by sal;
????select name,sal from emp order by sal desc;
????2.查詢工資高于2000的員工姓名和工資, 按照工資降序排序
????select name,sal from emp where sal>2000 order by sal desc;
????3.查詢每個(gè)員工的姓名,工資和部門(mén)id 按照部門(mén)id升序排序,如果部門(mén)id一致則按照工資降序
????select name,sal,dept_id from emp order by dept_id,sal desc;
分頁(yè)查詢
????格式: limit 跳過(guò)的條數(shù),請(qǐng)求的條數(shù)(每頁(yè)的條數(shù))跳過(guò)的條數(shù)= (請(qǐng)求的頁(yè)數(shù)-1)*每頁(yè)的條數(shù)
舉例:
????查詢第一頁(yè)的5條數(shù)據(jù)(1-5) ? limit 0,5
????查詢第2頁(yè)的5條數(shù)據(jù)(6-10) ? limit 5,5
????查詢第5頁(yè)的5條數(shù)據(jù) ? ? ? ? ? ? limit 20,5
????查詢第8頁(yè)的10條數(shù)據(jù) ? ? ? ?limit ?70,10
????查詢第7頁(yè)的8條數(shù)據(jù) ? ? ? ? ?limit ?48,8
別名
????select name as "名字" from emp;
????select name "名字" from emp;
????select name 名字 from emp;
聚合函數(shù)
通過(guò)聚合函數(shù)可以對(duì)查詢的多條數(shù)據(jù)進(jìn)行統(tǒng)計(jì)查詢
有哪些統(tǒng)計(jì)方式?
平均值avg
最大值max
最小值min
求和sum
計(jì)數(shù)count
平均值avg()
查詢1號(hào)部門(mén)的平均工資
select avg(sal) from emp where dept_id=1;
最大值max()
查詢1號(hào)部門(mén)的最高工資
select max(sal) from emp where dept_id=1;
最小值min()
查詢1號(hào)部門(mén)的最低工資
select min(sal) from emp where dept_id=1;
求和sum()
查詢程序員的工資總和
select sum(sal) from emp where job="程序員";
計(jì)數(shù)count(*)
查詢員工表的人數(shù)
select count(*) from emp;
分組查詢group by
可以將某個(gè)字段相同值的數(shù)據(jù)劃分為一組,然后以組為單位進(jìn)行統(tǒng)計(jì)查詢
如果題目中出現(xiàn)每個(gè)或每種這樣的關(guān)鍵字 并且查詢的內(nèi)容為聚合函數(shù)的結(jié)果
1.查詢每個(gè)部門(mén)的平均工資
select dept_id,avg(sal) from emp group by dept_id;
2.查詢每個(gè)部門(mén)的最高工資
select dept_id,max(sal) from emp group by dept_id;
3.查詢每種工作的平均工資
select job,avg(sal) from emp group by job;
4.查詢每個(gè)部門(mén)工資高于2000的人數(shù)
select dept_id,count(*) from emp where sal>2000 group by dept_id;
5.查詢每種工作的最低工資
select job,min(sal) from emp group by job;
6.查詢1號(hào)部門(mén)和2號(hào)部門(mén)的人數(shù)
select dept_id,count(*) from emp where dept_id in(1,2) group by dept_id;
7.查詢平均工資最高的部門(mén)id和平均工資
select dept_id,avg(sal) from emp group by dept_id order by avg(sal) desc limit 0,1;
having關(guān)鍵字:
where后面只能寫(xiě)普通字段條件,不能寫(xiě)聚合函數(shù)條件
having后面專(zhuān)門(mén)寫(xiě)聚合函數(shù)條件,而且需要和group by分組查詢結(jié)合使用,寫(xiě)在group by 的后面
1.查詢每個(gè)部門(mén)的平均工資,要求平均工資大于2000
select dept_id,avg(sal) from emp group by dept_id having avg(sal)>2000;
select dept_id,avg(sal) a from emp group by dept_id having a>2000;
2.查詢每種工作的人數(shù),只查詢?nèi)藬?shù)大于1的
select job,count(*) c from emp group by job having c>1;
3.查詢每個(gè)部門(mén)的工資總和,只查詢有領(lǐng)導(dǎo)的員工, 并且要求工資總和大于5400.
select dept_id,sum(sal) s from emp where manager is not null group by dept_id having s>5400;
4.查詢每個(gè)部門(mén)的平均工資, 只查詢工資在1000到3000之間的,并且過(guò)濾掉平均工資低于2000的
select dept_id,avg(sal) a from emp where sal between 1000 and 3000 group by dept_id having a>=2000;
各種查詢相關(guān)的關(guān)鍵字順序:select 字段信息 from 表名 where 普通字段條件 group by 分組字段名 having 聚合函數(shù)條件 order by 排序字段名 limit 跳過(guò)條數(shù),請(qǐng)求條數(shù).
子查詢(嵌套查詢)
????可以將一條SQL語(yǔ)句查詢的結(jié)果當(dāng)做另外一條SQL語(yǔ)句條件的值?
1.查詢工資大于2號(hào)部門(mén)平均工資的員工信息
select avg(sal) ?from emp where dept_id=2;
select * from emp where sal>(select avg(sal) ?from emp where dept_id=2);
2.查詢工資高于程序員最高工資的員工信息
select max(sal) from emp where job="程序員";
select * from emp where sal>(select max(sal) from emp where job="程序員");
3.查詢工資最高的員工信息
select * from emp where sal=(select max(sal) from emp);
4.查詢和孫悟空相同工作的員工信息
select * from emp where job=(select job from emp where name="孫悟空") and name!="孫悟空";
5.查詢工資最低的員工的同事們的信息(指同一部門(mén))
????得到最低工資
????select min(sal) from emp;
????通過(guò)最低工資得到拿最低工資人的部門(mén)id
????select dept_id from emp where sal=(select min(sal) from emp);
????通過(guò)部門(mén)id查詢員工
????select * from emp where dept_id=(select dept_id from emp where sal=(select min(sal) from emp)) and sal!=(select min(sal) from emp);
可以對(duì)查詢的字段進(jìn)行+-*/運(yùn)算
1.查詢每個(gè)員工的姓名,工資和年終獎(jiǎng)(年終獎(jiǎng)=5個(gè)月的工資)
select name,sal,sal*5 年終獎(jiǎng) from emp;
2.給3號(hào)部門(mén)所有員工漲薪5塊錢(qián)?
update emp set sal=sal+5 where dept_id=3;
3.查詢孫悟空的部門(mén)信息(用到dept部門(mén)表)
select dept_id from emp where name="孫悟空";
select * from dept where id=(select dept_id from emp where name="孫悟空");
關(guān)聯(lián)關(guān)系:
????指創(chuàng)建表時(shí),表和表之間存在的業(yè)務(wù)關(guān)系
????有哪幾種關(guān)系:
一對(duì)一:有AB兩張表,A表中的一條數(shù)據(jù)對(duì)應(yīng)B表中的一條數(shù)據(jù), 同時(shí)B表中的一條數(shù)據(jù)也對(duì)應(yīng)A表中的一條數(shù)據(jù)

一對(duì)多:有AB兩張表, A表中的一條數(shù)據(jù)對(duì)應(yīng)B表中的多條數(shù)據(jù), 同時(shí)B表中的一條數(shù)據(jù)對(duì)應(yīng)A表中的一條數(shù)據(jù)

多對(duì)多:有AB兩張表, A表中的一條數(shù)據(jù)對(duì)應(yīng)B表中的多條數(shù)據(jù), 同時(shí)B表中的一條數(shù)據(jù)也對(duì)應(yīng)A表中的多條數(shù)據(jù)

如果兩張表之間存在關(guān)聯(lián)關(guān)系,如何建立關(guān)系:
一對(duì)一: 可以在兩張關(guān)系表中任何一個(gè)表里面添加建立關(guān)系的字段,指向另外一張表的主鍵
一對(duì)多: 在一對(duì)多的兩張表里面的表示"多"的表中添加建立關(guān)系的字段指向另外一張表的主鍵
多對(duì)多:需要?jiǎng)?chuàng)建一個(gè)單獨(dú)的關(guān)系表,關(guān)系表中兩個(gè)字段分別指向另外兩張表的主鍵
關(guān)聯(lián)查詢:
????同時(shí)查詢多張表數(shù)據(jù)的查詢方式稱(chēng)為關(guān)聯(lián)查詢
????關(guān)聯(lián)查詢包括: 等值鏈接, 內(nèi)連接和外連接
關(guān)聯(lián)查詢之等值鏈接:
格式: select * from A,B where 關(guān)聯(lián)關(guān)系 and 其它條件
????查詢每個(gè)員工的姓名和對(duì)應(yīng)的部門(mén)名
????select e.name,d.name from emp e,dept d where e.dept_id=d.id;????
????查詢工資高于2000的員工姓名,工資和部門(mén)信息
????select e.name,e.sal,d.* from emp e,dept d where e.dept_id=d.id and sal>2000;
關(guān)聯(lián)查詢之內(nèi)連接 :
內(nèi)連接和等值鏈接作用一樣,查詢到的都是兩個(gè)表的交集數(shù)據(jù),查詢不到交集以外的數(shù)據(jù)
格式: select * from A join B on 關(guān)聯(lián)關(guān)系 where 條件
1.查詢每個(gè)員工的姓名和對(duì)應(yīng)的部門(mén)名
select e.name,d.name from emp e join dept d on e.dept_id=d.id;
2.查詢工資高于2000的員工姓名,工資和部門(mén)信息
select e.name,sal,d.* from emp e join dept d on e.dept_id=d.id where sal>2000;
外連接:
????作用:查詢一張表的全部和另外一張表的交集數(shù)據(jù)
????格式:select * from A left/right join B on 關(guān)聯(lián)關(guān)系 where 條件;
1.查詢所有員工名和對(duì)應(yīng)的部門(mén)名
select e.name,d.name from emp e left join dept d on e.dept_id=d.id;
2.查詢所有部門(mén)名稱(chēng),地點(diǎn)和對(duì)應(yīng)的員工姓名和工資,只查詢有領(lǐng)導(dǎo)的員工
select d.name,loc,e.name,sal from emp e right join dept d on dept_id=d.id where e.manager is not null;
關(guān)聯(lián)查詢總結(jié):
????如果查詢的是多張表中的數(shù)據(jù)則使用關(guān)聯(lián)查詢:(等值鏈接,內(nèi)連接和外連接)
????如果查詢的是多張表的交集數(shù)據(jù),則使用等值鏈接或內(nèi)連接(推薦)
????如果查詢的是一張表的全部和其它表的交集數(shù)據(jù)則使用外連接
JDBC:
????作用: 通過(guò)Java代碼執(zhí)行所學(xué)的SQL語(yǔ)句
????JavaDataBaseConnectivity: Java數(shù)據(jù)庫(kù)鏈接
????JDBC是Sun公司提供的一套專(zhuān)門(mén)用于Java語(yǔ)言和數(shù)據(jù)庫(kù)進(jìn)行連接的API(Application Programma Interface應(yīng)用程序編程接口)
????Sun公司為了避免Java程序員每一種數(shù)據(jù)庫(kù)軟件都學(xué)習(xí)一套新的方法, 通過(guò)JDBC接口將方法名定義好,各個(gè)數(shù)據(jù)庫(kù)廠商根據(jù)此接口中的方法名寫(xiě)各自的實(shí)現(xiàn)類(lèi)(jar包(驅(qū)動(dòng))), 這樣Java程序員只需要掌握J(rèn)DBC接口中方法的調(diào)用即可訪問(wèn)任何數(shù)據(jù)庫(kù), 即使在工作中換了數(shù)據(jù)庫(kù)軟件,代碼也不需要發(fā)生改變.
Statement執(zhí)行SQL語(yǔ)句的對(duì)象:
????execute("sql"); ?可以執(zhí)行任意sql語(yǔ)句,但是推薦執(zhí)行DDL(數(shù)據(jù)定義語(yǔ)言,包括數(shù)據(jù)庫(kù)相關(guān)和表相關(guān))
????int row = executeUpdate("sql"); 執(zhí)行增刪改, 此方法的返回值為生效的行數(shù)
????ResultSet rs = executeQuery("sql"); 執(zhí)行查詢相關(guān)的SQL語(yǔ)句, ?ResultSet對(duì)象里面裝著查詢回來(lái)的結(jié)果.
DBCP數(shù)據(jù)庫(kù)連接池:
???DataBaseConnectionPool: 數(shù)據(jù)庫(kù)連接池
????作用: 將鏈接重用,避免頻繁的開(kāi)關(guān)鏈接所帶來(lái)的資源浪費(fèi).

SQL注入:
SQL注入是往本應(yīng)該傳值的地方,傳遞進(jìn)去了SQL語(yǔ)句導(dǎo)致原有SQL語(yǔ)句的邏輯發(fā)生改變,這個(gè)過(guò)程稱(chēng)為SQL注入
where username='xxx' and password='' or '1'='1'
SQL注入漏洞是網(wǎng)站的低級(jí)漏洞,但是危害比較大, 黑客可以利用此漏洞對(duì)網(wǎng)站進(jìn)行攻擊
PreparedStatement:
? ?通過(guò)PreparedStatement執(zhí)行SQL語(yǔ)句可以避免出現(xiàn)SQL注入漏洞
????內(nèi)部原理: ? 使用PreparedStatement 可以起到預(yù)編譯的作用, 可以將編譯SQL語(yǔ)句的時(shí)間點(diǎn)提前(從之前執(zhí)行SQL語(yǔ)句時(shí)編譯提前到創(chuàng)建時(shí)), ?編譯時(shí)間點(diǎn)提前可以將SQL語(yǔ)句中業(yè)務(wù)邏輯部分編譯好(將業(yè)務(wù)部分鎖死),之后將用戶輸入的內(nèi)容替換到SQL語(yǔ)句時(shí),用戶輸入的內(nèi)容只能以值的形式添加到SQL語(yǔ)句中,不會(huì)影響原有SQL語(yǔ)句的判斷邏輯,所以避免了SQL注入的漏洞
????執(zhí)行setString()方法替換?時(shí) 會(huì)自動(dòng)給字符串添加引號(hào),并且用戶輸入的字符串中出現(xiàn)了引號(hào)關(guān)鍵字時(shí)會(huì)進(jìn)行轉(zhuǎn)義操作 ? ? ? ? ? ? ? ? ? ? password=''' or ''1''=''1'
數(shù)據(jù)庫(kù)常見(jiàn)錯(cuò)誤列表:
????1.未開(kāi)啟數(shù)據(jù)庫(kù)服務(wù)

2.用戶名或密碼錯(cuò)誤

3.SQL語(yǔ)句拼寫(xiě)錯(cuò)誤

4.創(chuàng)建或刪除數(shù)據(jù)庫(kù)時(shí)存在的錯(cuò)誤

5.主鍵值插入錯(cuò)誤
