1、字符集和存儲引擎
設(shè)置主鍵索引:
create table qianfeng(id int auto_increment primary key, name varchar(30))engine=innodb, default charset=utf8;
create table qianfeng(id int auto_increment, name varchar(30), primary key(id))engine=innodb, default charset=utf8;
2、DML
insert
第一種形式:
insert into star values(1, '王寶強(qiáng)', 0, '河北' ,32, 0);
【注】所有字段的值都得一一寫出來,而且和表中字段要對應(yīng)
第二種形式:
insert into star(name, money, province, age, sex) values('郭德綱', 1000000, '天津', 43, 0);
【注】指定字段,后面的值和前面的字段一一對應(yīng)
第三種形式:
insert into star(name, money, province, age, sex)
values('黃曉明', 2000000, '山東', 39, 0),
( 'angelababy', 3000000, '上海', 27, 1),
( '陳赫', 150000, '福建', 31, 0);
一次性插入多條數(shù)據(jù),中間使用逗號隔開
delete
刪除數(shù)據(jù),【注】使用delete一定要記得使用where條件限制
delete from stars where name='郭德綱';
update
更新數(shù)組,【注】也一定要記得使用where條件限制
update stars set money=4000, age=33 where id=1;
3、DQL(重中之重)
基礎(chǔ)查詢
select * from 表名;
指定字段查詢(重復(fù)和不重復(fù))
select id, name, money from stars;
select distinct province from stars;
屏蔽掉查詢出來重復(fù)的數(shù)據(jù)
條件查詢
select * from stars where id<10 and province='湖北';
select * from stars where id between 3 and 10;
select * from stars where id between 3 and 10;
select * from stars where name like '_超';
_的意思是匹配一個(gè)任意字符
select * from stars where name like '%超';
%的意思就是匹配前面所有的字符,上面的語句代表以超結(jié)尾的所有記錄
結(jié)果集排序
select * from stars order by money desc;
desc:降序排列
asc:升序排列,(默認(rèn)屬性)
select * from stars order by money desc, age desc;
如果金錢相等的話,再按照年齡降序排列
限制結(jié)果集
select * from stars order by money desc limit 1;
只顯示最有錢那一個(gè)明星
select * from stars order by money desc limit 2, 1;
limit后面的參數(shù)
1:偏移量,從0開始
2:數(shù)量,要取出的數(shù)據(jù)個(gè)數(shù)
通過limit實(shí)現(xiàn)分頁,每頁顯示5條數(shù)據(jù)
0 1 2 3 4 5 6 7 8 9 10
第一頁: limit 0, 5;
第二頁: limit 5, 5;
第三頁: limit 10, 5;
。。。
第n頁: limit (n-1)*5, 5;
常用統(tǒng)計(jì)函數(shù)
max
min
avg:平均值
sum:求和
count:個(gè)數(shù)
分組
分組統(tǒng)計(jì)
select * from stars group by province;
按照省份分組,但是這樣顯示的結(jié)果沒有實(shí)際的意義。
select province, count(province) from stars group by province;
按照省份分組,并且顯示每一組有多少人
select province as pro, count(province) as count from stars group by province;
結(jié)果集過濾
select province as pro, count(province) as count from stars group by province having count > 2;
對分組之后的結(jié)果限制,只顯示個(gè)數(shù)大于2個(gè)的省份。
【注】對分組結(jié)果進(jìn)行限制要使用having,其作用和where一樣,但是where一般用在表的后面,having用在分組結(jié)果后面。
整體使用
多表聯(lián)合查詢
鏈接
select * from user join goods;
結(jié)果為兩個(gè)表的笛卡爾乘積,這種結(jié)果對我們沒有意義
內(nèi)連接
隱式
select user.name, goods.name from user, goods where user.gid=goods.gid;
查詢對我們有意義的數(shù)據(jù)
顯式
select user.name, goods.name from user join goods on user.gid=goods.gid;
select u.name, g.name from user as u join goods as g on u.gid=g.gid;
顯示內(nèi)連接,顯示我們需要的數(shù)據(jù),可以給表起別名,使用join on,on后面跟著鏈接兩個(gè)表的條件
外連接
左連接
select * from user left join goods on user.gid=goods.gid;
左邊的表格全部顯示,如果有g(shù)id不相等的,右面的數(shù)據(jù)顯示為空
右連接
select * from user right join goods on user.gid=goods.gid;
右邊的表格全部顯示,如果有g(shù)id不相等的,左面的數(shù)據(jù)顯示為空
子查詢
記錄聯(lián)合
兩個(gè)表同時(shí)更新
清空表記錄
4、DCL
事務(wù)
基本使用(了解)
5、DTL(了解)
創(chuàng)建用戶
刪除用戶
修改密碼
授予權(quán)限
剝奪權(quán)限
Select * from b where s in (1,2,3,4)
要注意字段類型,如果是數(shù)字類型用
Select * from b where s in (1,2,3,4)
如果是字符串類型用
Select * from b where s in ('1','2','3','4')