select distinct [字段名] from [表名];
用于查詢?cè)摿胁恢貜?fù)的所有值。?
where子句中可以加between...and...
模糊查詢:like
%代表多個(gè)字符,_代表一個(gè)字符
like 'M%'代表查詢以M開頭的字符串;
like '%M%'代表查詢包含M的字符串;
like '%M'代表查詢以M結(jié)尾的字符串;
like '%M_'代表查詢M為倒數(shù)第二個(gè)字符的字符串
like 'M_'代表兩位,第一位是M的字符串
like關(guān)鍵字前還可以加上not關(guān)鍵字,表示不是....
in保留字:指定針對(duì)某個(gè)列可能的多個(gè)值
select * from product where market_price in (1999,2999,3999);
order by...
是可以指定多列進(jìn)行排序的
insert into...語句使用方法:
兩種方式:
(1)insert into category values (‘7‘’,‘生活用品’);
(2)insert into category (cid, cname) values ('7', ‘生活用品');
update語句:
例如:
? ? ?update category set cname = ‘書籍’ where cid = '18';
注意:
? ? update語句后面如果不接where子句,就會(huì)使表內(nèi)所有行的值都會(huì)更新,一定要慎重!
在MySQL中,可以通過設(shè)置 set sql_safe_updates = 1; 這樣,update后沒有where子句就會(huì)報(bào)錯(cuò)。
delete語句
limit語句
select * from product limit A;
表示從第一條開始查詢,一共查詢A條;
select * from product limit A ,B;
表示從第A條開始查詢,一共查詢B條;
as為列、表添加別名,作用有:
(1)列名稱很長或者可讀性很差
select name as n, price as p from product;
(2)在查詢中設(shè)計(jì)到了不止一個(gè)表
select o.uid, oi.pid from orders as o, orderitems as oi where o.oid = oi.oid;
通過oid將兩個(gè)表連接到了一起,并輸出兩個(gè)表中各一列;
(3)需要把兩個(gè)列或多格列合到一起,為新列去一個(gè)別名
select pname,?CONCAT(market_price, ',' ,shop_price) as price from product;
join連接:將兩個(gè)或多個(gè)表的行通過一定關(guān)系連接到一起
分為內(nèi)連接、外連接
內(nèi)連接:
select product.pid, product.pname, orderitem.itemid from product (inner) join orderitem on product.pid = orderitem.pid;
只有兩個(gè)表格滿足了?product.pid = orderitem.pid? 這個(gè)條件之后,才會(huì)將符合條件的行輸出出來;
外連接:分為左連接和右連接
select?product.pid, product.pname, orderitem.itemid?from?product?left join?orderitem?on?product.pid = orderitem.pid;
這句是左連接,不管product.pid = orderitem.pid 條件是不是滿足,左邊的表product 所有行全部輸出出來,但是右邊的表格orderitem是否輸出需要依靠這個(gè)條件,條件滿足的行則輸出,條件不滿足的行則輸出null;
右連接類似,只不過將left改為right,右表全部輸出,左表是否輸出看每一行的條件是否滿足;
union語句
select columnName1... from table1?
union
select columnName2... from table2
用于將兩次select的所有值連接到一起顯示,需要保證兩次查詢的列的數(shù)量、類型相同,而且union連接時(shí),不會(huì)顯示重復(fù)值
如需要顯示重復(fù)值,請(qǐng)使用union all 關(guān)鍵字。
insert into? [目標(biāo)表名] ((目標(biāo)表的列))? select [選擇的列名] from [源表名] (where...);
可以將源表中的一些數(shù)據(jù)插入到目標(biāo)表中,注意是插入,并不會(huì)影響目標(biāo)表已有的數(shù)據(jù)。
如果目標(biāo)表不存在,則該語句不會(huì)創(chuàng)建新的目標(biāo)表,而是報(bào)錯(cuò)。
選取的列的數(shù)量、類型要與目標(biāo)表選取的列的數(shù)量、類型保持一致,否則報(bào)錯(cuò)。
可以加where子句,用法和select語句相同。
例:
insert into category_new select product.pid, orderitem.itemid from product join orderitem on product.pid = orderitem.pid;?
create關(guān)鍵字的使用
創(chuàng)建數(shù)據(jù)庫:
create datebase db_1;
創(chuàng)建表格:
create table test_table (
id int,
tname varchar(50);
);
注意,varcahr類型后面需要跟上長度
如果想要復(fù)制一個(gè)表格的類型,可以使用如下語句:
create table test_table_1 as select * from test_table where 1=0;
where 1=0是為了在新表中不添加任何數(shù)據(jù)
創(chuàng)建索引
create index index_test on test_table (column_1 , column_1 );
約束 NOT NULL
NOT NULL指定一列不為空,不許存放null值;
約束UNIQUE
UNIQUE指定一列不許出現(xiàn)重復(fù)值,但是可以出現(xiàn)多個(gè)NULL
使用方法:
使用create來添加 UNIQUE
create table person (
pid int,
pname varchar(20) not null,
unique (pid)
);
如果為多個(gè)列添加unique越約束,使用如下語句:
create table person(
pid int not null,
pname varchar(20) not null,
constraint uc_person unique (pid, pname)
);
這樣一來,pid或者pname本身可以出現(xiàn)重復(fù)值,但是pid+pname不允許出現(xiàn)重復(fù)值。
使用ALTER來添加刪除UNIQUE
alter table person add unique (pid);
或者
alter table person add constraint uc_person unique (pid, pname);
alter table person drop index uc_person;
(刪除?UNIQUE 約束)
PRIMARY KEY約束(主鍵約束)
PRIMARY KEY 相當(dāng)于NOT NULL和UNIQUE放在一起,而且每一個(gè)表中只能有一個(gè)主鍵。
其使用方法與UNIQUE類似。
只不過在刪除主鍵約束時(shí),可以像下面這樣寫
alter table person drop primary key;
因?yàn)橐粋€(gè)表中只有一個(gè)主鍵約束,不需要為其指定約束名稱
FOREIGN KEY約束(外鍵約束)
一個(gè)表中的 FOREIGN KEY 指向另一個(gè)表中的 UNIQUE KEY(唯一約束的鍵)。
FOREIGN KEY 約束用于預(yù)防破壞表之間連接的行為。
FOREIGN KEY 約束也能防止非法數(shù)據(jù)插入外鍵列,因?yàn)樗仨毷撬赶虻哪莻€(gè)表中的值之一。
其用法如下,以create為例:
create table orders (
oid int not null,
pid int not null,
constraint fc_pid foreign key (pid) references person (pid)
);
Check約束
Check約束用于限制列的取值范圍
例如:
create table person (
id int primary key,
pname varchar(20) not null,
age int,
constraint cc_age check (age>10)
);
Default約束
用于向列中插入默認(rèn)值
create table person (
id int primary key,
pname varchar(20) not null,
age int,
constraint dc_age default 15
);
drop語句
刪除數(shù)據(jù)庫
drop database db1;
刪除表
drop table table1;
刪除索引
drop index index1;
drop、delete、truncate刪除表的區(qū)別
drop會(huì)清除表的內(nèi)容,結(jié)構(gòu),約束,并釋放空間,后續(xù)想要向該表中添加數(shù)據(jù)是不允許的,除非建立一個(gè)新的表;
truncate保留表的結(jié)構(gòu),清空表的內(nèi)容,釋放空間,后續(xù)可以繼續(xù)向表中添加內(nèi)容;
delete允許使用where子句一行一行刪除表的內(nèi)容,而且刪除操作會(huì)作為事務(wù)記錄在日志中。當(dāng)不加where子句時(shí),其作用效果與truncate類似。
delete語句是數(shù)據(jù)庫操作語言(dml) ,drop和truncate是數(shù)據(jù)庫定義語言(ddl)。
alter語句允許添加列,刪除列,修改列
alter table table1 add pid int;? //添加列
alter table table1 drop column pid;? //刪除列
alter table table1 modify column pid varchar(20);? //將pid列的類型從int改為varchar
關(guān)于MySQL null值的處理
null值是不可比較的,要想判斷一個(gè)數(shù)據(jù)存的是不是null值,需要使用到is null關(guān)鍵字,或者是 is not null。
select * from test_table2 where age is not null;
如果我們要對(duì)某些列進(jìn)行運(yùn)算,里邊如果摻雜著null值,那么計(jì)算結(jié)果一定是null,不符合我們的要求,所以希望將null值改為一個(gè)可以計(jì)算的數(shù)值。在mysql中,使用到了ifnull()函數(shù);
select product_price * count as total_price from product;
這里面,如果count中有null值,輸出結(jié)果total_price中將會(huì)出現(xiàn)null值,是我們不想看到的,
所以該SQL語句改為如下:
select product_price * ifnull(count, 0) as total_price from product;
該語句的意思是,如果count為空,那么就將其看做成0來計(jì)算。
count()函數(shù)的使用
select count(*) from test_table;? ? //計(jì)算test_table一共有多少行
select count(age) from test_table;? ? //計(jì)算test_table中age列一共有多少數(shù)據(jù),注意,null值不會(huì)計(jì)算在內(nèi)
select count(distinct age) from test_table;? ? //計(jì)算test_table中age列有多少不同的值,注意,null值不會(huì)計(jì)算在內(nèi)
group by和having語句的使用
group by主要是將查詢的數(shù)據(jù)進(jìn)行分組,在單表查詢中,其使用方式如下:
select oid, sum(subtotal) as total from orderitem group by oid;? ? //查詢每一個(gè)訂單中的總金額,最后顯示的是訂單號(hào)和總金額
對(duì)于多表分組查詢,比較復(fù)雜,
比如說我們想要查詢每一個(gè)訂單中的訂單號(hào),總金額,和所有的商品
可以寫下如下的SQL語句
select orderitem.oid, sum(orderitem.count * product.market_price) as totalPrice,?group_concat(product.pname) as product
from orderitem left join product on orderitem.pid = product.pid
group by oid;
having主要是解決了where子句中不能使用聚合函數(shù)的問題
比如說,我們想要從orderitem中查詢訂單總金額大于10000的訂單號(hào)和總金額,可以寫出如下的sql語句
select oid, sum(subtotal) from orderitem?group by oid having sum(subtotal) > 10000;
當(dāng)然,having也可用于多表分組查詢中
對(duì)于sql查詢語句的子句執(zhí)行順序如下:
(1)from子句: 先確定從哪個(gè)表格中獲取數(shù)據(jù),如果有join關(guān)鍵字,則先進(jìn)行多表的連接
(2)where子句:從獲取的表中篩選出符合where子句條件的行;
(3)group by子句:篩選過后,對(duì)所有行進(jìn)行分組
(4)having子句:分組后,通過having子句的篩選獲取符合條件的組;
(5)select子句:選取我們需要查詢的列或者列的組合運(yùn)算(聚合函數(shù));
(6)union子句:將選取出的數(shù)據(jù)放到一起;
(7)order by子句:將選好的列按照一定規(guī)則排列顯示;