重慶安全員c證在哪里報(bào)名百度搜索怎么優(yōu)化
結(jié)構(gòu)化查詢語言
DDL
(數(shù)據(jù)定義語言)
刪除數(shù)據(jù)庫drop database DbName;
創(chuàng)建數(shù)據(jù)庫create database DbName;
使用數(shù)據(jù)庫use DbName;
查看創(chuàng)建數(shù)據(jù)庫語句以及字符編碼show create database 43th;
修改數(shù)據(jù)庫屬性(字符編碼改為gbk)alter database DbName default character set gbk;
字符校對(duì)集
查看數(shù)據(jù)庫支持的字符集show character set;
查看相應(yīng)字符集的校對(duì)規(guī)則show collation;
數(shù)據(jù)類型
TIMESTAMP
在進(jìn)行insert/updata
自動(dòng)記錄
創(chuàng)建表結(jié)構(gòu)
create table tName(field1 datatype,field2 datatype,...
);
查看表結(jié)構(gòu)desc tName;
show create table person;
修改表結(jié)構(gòu)
添加列(字段)alter table tName add field datatype;
對(duì)字段名進(jìn)行修改alter table tName change field1 field2datatype;
對(duì)字段名的類型進(jìn)行修改alter table tName modify field datatype;
刪除一個(gè)字段alter yable tName drop field;
刪除表結(jié)構(gòu)drop table tName;
查看數(shù)據(jù)庫中有多少表show tables;
DML
(數(shù)據(jù)控制語言)
在表中對(duì)指定的列插入數(shù)據(jù)insert into tName(field1,field2...) values (field1Vale,field2Vale...);
對(duì)所有列都進(jìn)行數(shù)據(jù)添加insert into tName values(field1Vale,field2Vale....);
對(duì)所有列添加多行數(shù)據(jù)insert into tName(field1,field2...) values(field1vale,field2vale....),(field1Vale,field2Vale....),...;
效率高于每次insert一條語句
修改某一條數(shù)據(jù)進(jìn)行修改update tName set field1=XXX[where..];
帶where作用于某一行,不帶where作用于所有行
添加新的列alter table tName add coluName coluType;
復(fù)制表
復(fù)制表結(jié)構(gòu)不復(fù)制數(shù)據(jù)create table newTName like oldTName;
復(fù)制表結(jié)構(gòu)和數(shù)據(jù)create table newTName select * from oldTName;
刪除表中數(shù)據(jù)不刪除表結(jié)構(gòu)delete from tName[whilecondition];
DQL
(數(shù)據(jù)查詢語言)
查詢命令select[*]|{field1,field2...}while tName [where condition];
去除重復(fù)列,使用distinct
關(guān)鍵字,當(dāng)distinct
作用于單個(gè)字段時(shí),去除重復(fù)的列即可,當(dāng)distinct
作用于多個(gè)字段時(shí),必須要所有字段相同才可以消除
distinct
必須放在第一個(gè)關(guān)鍵字之前,不然會(huì)報(bào)錯(cuò)
select語句選擇的列可以進(jìn)行運(yùn)算
使用as還可以對(duì)列區(qū)別名
獲取當(dāng)前使用數(shù)據(jù)庫 select database();
獲取當(dāng)前時(shí)間,使用curdate()
函數(shù)
選擇查詢select id,name,chinese from person where chinese between 10 and 90;
枚舉查詢select id,name,chinese from person where chinese in(88,77,90,60);
模糊查詢like
通配符_
:下劃線代表一個(gè)任意字符select * from person where name like '-i';
通配符%
:百分號(hào)代表多個(gè)字符slelect * from person name like 'z%';
對(duì)查詢的結(jié)果集進(jìn)行排序默認(rèn)采用升序ASC
方式排序,DESC
為降序select [*] from tName orger by culName;
分頁查詢(常考點(diǎn))
(1)第一種寫法limit
只是查看前三名的字段select * from person order by english desc limit 3;
每頁三個(gè)數(shù)據(jù),查看第二頁的數(shù)據(jù)select * from person order by english desc limit 3,3;
(2)第二種寫法limit m offset n
n表示的是偏移記錄條數(shù),m表示要顯示的數(shù)據(jù)條數(shù)
只是查看前三名的字段select * from person order by english desc limit 3 offset 0;
每頁三個(gè)數(shù)據(jù),查看第二頁的數(shù)據(jù)select * from person order by english desc limit 3offset 3;
數(shù)據(jù)完整性:
(1)實(shí)體完整性:表中的每一行數(shù)據(jù)都是唯一的,不能重復(fù)出現(xiàn),通過主鍵(PRIMARYKEY
)來實(shí)現(xiàn)。
(2)域(field 字段,列)完整性:每一行必須要符合某種特定的數(shù)據(jù)類型或約束,
非空約束 NOT NULL
該字段不能為空
唯一約束 UNIQUE
該字段不允許重復(fù),允許為空
獲取唯一約束的名字show create table student;
刪除一個(gè)唯一約束 alter table tName drop index name ;
(3)參照完整性:外鍵約束 FOREIGNN KEY
,外鍵 是另一張表的主鍵
表已經(jīng)存在的情況下,需要使用alter table tName add constraint fk_1 fGPRRIGNN KEY(s_id) REFERENCES student(id);
數(shù)據(jù)庫的備份和恢復(fù)
數(shù)據(jù)庫的備份在終端下使用命令mysqldump -u root -p 43th>43th.sql
恢復(fù)數(shù)據(jù)庫在終端下使用命令mysql -u root -p 43th<43th.sql
或者在mysql>
中使用source 43th.sql;
相當(dāng)于在當(dāng)前的空數(shù)據(jù)庫下執(zhí)行43th.sql
中的sql
語句導(dǎo)入數(shù)據(jù)
因?yàn)槲覀儌浞莸臄?shù)據(jù)庫文件中不存在創(chuàng)建數(shù)據(jù)庫的語句,因此我們導(dǎo)入數(shù)據(jù)庫之前因該先創(chuàng)建數(shù)據(jù)庫在進(jìn)行數(shù)據(jù)數(shù)據(jù)的導(dǎo)入
聯(lián)合主鍵
對(duì)某一張表,一條的記錄的唯一性由兩個(gè)或者以上的字段共同決定。
如果是由兩個(gè)字段決定的,只要其中一個(gè)之一不相同,就是一條不同的記錄。只有當(dāng)兩個(gè)字段都相同時(shí),該記錄不能插入到表中
DQL復(fù)雜查詢
連接查詢
針對(duì)多張表
交叉連接(笛卡爾積)[三種寫法操作結(jié)果一致]
select * from letfTable cross join rightTable;
selsct * from leftTable join rightTable;
select * from leftTable,rightTable;
內(nèi)連接
關(guān)鍵字inner join
(一般都會(huì)加上on選擇條件)
select * from leftTable inner join rightTable on leftTable.id=rightTable.id;
select * from leftTable inner join rightTable on leftTable.id=rightTable.id where leftTable.id>3;
外連接
執(zhí)行左右外連接需要使用on
關(guān)鍵字加入選擇條件,如果使用左外連接是,當(dāng)右表中沒有與左表中對(duì)應(yīng)的記錄時(shí),查詢出的右表數(shù)據(jù)全部用NULL代替,左表中的所有數(shù)據(jù)都出現(xiàn);對(duì)于右外連接也是一樣的
左外連接letf outer join
select * from leftTable letfr outer join rightTable on leftTable.id=rightTable;
右外連接right outer join
select * from leftTable letfr outer join rightTable on leftTable.id=rightTable;
子查詢
子查詢也叫嵌套查詢,是指在where子句或from子句中又嵌入select查詢語句(一般寫在where子句)
select * from person where id = (select id from person where id < 4);
select * from (select id,name,chinese from person where name like ‘k%’) as t;
聯(lián)合查詢
聯(lián)合查詢能夠合并兩條查詢語句的查詢結(jié)果,去掉其中重復(fù)數(shù)據(jù)行,然后返回沒有重復(fù)數(shù)據(jù)行的查詢結(jié)果。聯(lián)合查詢使用union關(guān)鍵字
select * from person where chinese > 80 union select * from person where english > 80;
報(bào)表查詢
統(tǒng)計(jì)函數(shù)
cout()計(jì)數(shù)
sum()求和
avg()求平均值
max()最大值
min()最小值
select count(*) ,chinsesfrom person group by chinese;
//對(duì)chinese中數(shù)值相同的進(jìn)行統(tǒng)計(jì),打印出相同不同數(shù)據(jù)的相同數(shù)量
select count(*) ,chinsesfrom person group by chinese having chinses >80;
//進(jìn)行分組之后還要對(duì)數(shù)據(jù)進(jìn)行過濾使用having
子句,where
是在沒有分組之前進(jìn)行過濾,having
是在分組之后進(jìn)行過濾