1.字符集與儲存引擎
--沒有指定引擎與字符集
create table if not exists `goods`(
`id` int unsigned not null auto_increment primary key,
`goodsname` varchar(255) not null
)engine=innodb default charset=utf8;
1.如果不指定存儲引擎,使用的是配置文件的默認值
2.如果不制定表的字符集,使用的是配置文件設置的字符集
工作中沒有權限修改
3.設置指定數據庫的字符集
alter database 數據庫名 character set 字符集;
alter database jn_user chaeacter SET utf8;
4.修改表的字符集
ALTER TABLE `表名` DEFAULT CHARACTER SET 字符集;
ALTER TABLE `goods` DEFAULT CHARACTER SET gbk;
使用黑窗口查正確顯示內容,請設置 set names gbk;
這是與操作系統有關
無論如何,在建表時,請設置好字符集與存儲引擎,解決不必要的配置
2.查
查詢語句,最靈活,最麻煩
--建表
create table if not exists `goods`(
`id` int unsigned not null auto_increment ,
primary key (`id`),
`name` varchar(255) not null ,
`price` float(8,2) not null ,
`image` varchar(255) not null ,
`color` varchar(32) not null ,
`size` varchar(32) not null ,
`evaluate` int unsigned not null
)engine=innodb default charset=utf8;
insert into `goods`(`name`,`price`,`image`,`color`,`size`,`evaluate`) values
('文胸1',199,'1.jpg','黑色','36e','1'),
('文胸2',199,'2.jpg','藍色','36e','1'),
('文胸3',199,'3.jpg','白色','36e','1'),
('文胸4',199,'4.jpg','黑色','36e','1'),
('文胸5',199,'6.jpg','綠色','36e','1'),
('文胸6',199,'21.jpg','黑色','36e','1'),
('文胸7',199,'13.jpg','黑色','70A','1'),
('文胸8',199,'11.jpg','黑色','36e','1'),
('文胸9',199,'14.jpg','黑色','36e','1'),
('文胸10',199,'51.jpg','黑色','36e','1'),
('文胸11',199,'17.jpg','黑色','71B','1'),
('文胸12',199,'19.jpg','黑色','36e','1'),
('文胸13',199,'12.jpg','黑色','36e','1'),
('文胸14',199,'13.jpg','紅色','72C','1'),
('文胸15',199,'12.jpg','黑色','36B','1'),
('文胸16',199,'16.jpg','黑色','73D','1'),
('文胸17',199,'13.jpg','黑色','36e','1'),
('文胸18',199,'12.jpg','黑色','36e','1'),
('文胸19',199,'1.jpg','黑色','36e','1'),
('文胸20',199,'1.jpg','黑色','36e','1'),
('文胸21',199,'1.jpg','黑色','36e','1'),
('文胸22',199,'1.jpg','黑色','36e','1'),
('文胸23',199,'1.jpg','黑色','36e','1'),
('文胸24',199,'1.jpg','黑色','36e','1'),
('文胸25',199,'1.jpg','黑色','36e','1');
1.查詢所有字段,所有數據
select * from `goods`;
2.查詢指定字段,所有數據
select `id`,`name`,`price` from `goods`;
查詢所有數據,會掃描整個數據庫。
所以,不要在高峯期使用 查詢所有數據。
3.等於 =
select * from `goods` where `size` = '70A';
select * from `goods` where `id` = 16;
4.<=>
添加測試字段
alter table `goods` add `click_num` int unsigned;
--點擊量
-- 不識別 null
select * from `goods` where `click_num` = null;
-- 識別 null
select * from `goods` where `click_num` <=> null;
5.查詢空格 is null 或 is not null
-- 點擊量爲空類型的數據
select * from `goods` where `click_num` is null;
-- 點擊量不爲空的數據
select * from `goods` where `click_num` is not null;
6.不等於 != 或 <> 沒有區別
select * from `goods` where `id` != 10;
select * from `goods` where `id` <> 10;
select * from `goods` where `size` != '36e';
--都不識別null
select * from `goods` where `click_num` != null;
select * from `goods` where `click_num` <> null;
8. > , >= ,< , <=
select * from `goods` where `id` >13;
select * from `goods` where `id` >=13;
select * from `goods` where `id` <13;
select * from `goods` where `id` <=13;
9.between and 數值範圍 , not between and
--id 11到13的數據
select * from `goods` where `id` between 11 and 13;
--除了12到14的數據
select * from `goods` where `id` not between 12 and 14;
10.and 與 , or 或
and 滿足兩條件
or 只滿足一個條件
update `goods` set `price` = 299 , `image` = '2.jpg' where `id` = 11;
update `goods` set `price` = 399 , `image` = '3.jpg' where `id` = 12;
update `goods` set `price` = 499 , `image` = '4.jpg' where `id` = 13;
update `goods` set `price` = 599 , `image` = '5.jpg' where `id` = 14;
update `goods` set `price` = 99 , `image` = '6.jpg' where `id` = 15;
update `goods` set `price` = 9 , `image` = '7.jpg' where `id` = 16;
update `goods` set `click_num` = 100 where `id` = 11;
update `goods` set `click_num` = 200 where `id` = 12;
update `goods` set `click_num` = 300 where `id` = 13;
update `goods` set `click_num` = 400 where `id` = 14;
update `goods` set `click_num` = 500 where `id` = 15;
update `goods` set `click_num` = 600 where `id` = 16;
--價格小於300,點擊量大於400
select * from `goods` where `price` < 300 and `click_num` > 400;
--or 或
select * from `goods` where `price` < 300 or `click_num` > 400;
11.in , not in 範圍集合
select * from `goods` where `id` in(11,13,15);
--除了範圍集合
select * from `goods` where `size` not in('72C','73D','75B');
12.like 模糊查詢
% 任意長度
李%
李開頭
%勇
勇結尾
%寶%
包含寶字的
_一位長度
insert into `goods` values
(null , '凹凸文胸3',999,'a.jpg','灰白','100B',2,899),
(null , '凹凸巨大文胸',999,'a.jpg','灰白','100B',2,899),
(null , '凹凸巨大文胸',999,'a.jpg','灰白','100B',2,899),
(null , '凹凸曼妮芬大文胸',999,'a.jpg','灰白','100B',2,899);
insert into `goods` values
(null , '百度',999,'a.jpg','灰白','100B',2,899),
(null , '白百合',999,'a.jpg','灰白','100B',2,899),
(null , '白骨晶',999,'a.jpg','灰白','100B',2,899),
(null , '白日',999,'a.jpg','灰白','100B',2,899);
select * from `goods` where `name` like '凹凸%';
select * from `goods` where `name` like '白%';
select * from `goods` where `name` like '%胸';
select * from `goods` where `name` like '%大%';
select * from `goods` where `name` like '_日';
select * from `goods` where `name` like '白__';
13.去重查詢
select distinct 字段 from 表名;
select distinct `image`,`size` from `goods`;
14.排序 order by
select * from `goods` order by `id` asc;--順序
select * from `goods` order by `id` desc;--倒敘
15.使用表達式
select `id`,`name`,`price` + 1 from `goods`;
-- 取原數據再計算
update `goods` set `price` = `price` + 1 where `id` = 25;
-- 更新點擊量
update `goods` set `click_num` = `click_num` + 1 where `id` = 24;
1. concat() 字符鏈接
select `id`,concat(`name`,' 大特賣') from `goods`;
2.count()總數
select count(*) from `goods`;--計算表的總數量
3.計算和
select sum(`click_num`) from `goods`;
4.avg() 平均值
select avg(`click_num`) from `goods`;
5.max() 最大
select max`click_num`) from `goods`;
min最小
16.分組查詢
-- 以指定字段分組,並統計每組數量
select `color`,count(*) from `goods` group by `color`;
-- 查詢 evaluate ,並以 evaluate 進行分組,並計算每組的最大值
select `evaluate`,max(`price`) from `goods` group by `evaluate`;
-- 查詢 evaluate ,並以 evaluate 進行分組,並計算每組的最大值
select `evaluate`,max(`price`) from `goods` group by `evaluate`;
17.limit 限制查詢結果
-- 按照默認順序,顯示一條數據
select * from `goods` limit 1;
select * from `goods` order by `id` desc limit 1;
select * from `goods` where `price` <= 599 order by `price` desc limit 3 ;
order by desc limit 按順序來
-- 分頁 limit 偏移量,長度;
-- 獲取總數
select count(*) from `goods`; -- 總數
-- 假設每頁顯示 5 條數據
-- 求總頁碼數 : ceil( 總數 / 每頁顯示條數 ) = 4
-- 偏移量的計算公式:(當前頁碼 - 1) * 每頁顯示條數 == 偏移量
-- 第一頁 (1-1)*5 = 0
select * from goods order by `id` desc limit 0,5;
-- 第2頁 (2 - 1) * 5 = 5
select * from goods order by `id` desc limit 5 ,5; -- 第2頁
-- 第3頁 (3 - 1) * 5 = 10
select * from goods order by `id` desc limit 10,5; -- 第3頁
-- 第4頁 (4 - 1) * 5 = 15
select * from goods order by `id` desc limit 15,5; -- 第4頁
18.連接查詢
--設計用戶表
create table if not exists `shop_user`(
`id` int unsigned not null auto_increment,
primary key(`id`),
`user` varchar(255) not null ,
unique un_user(`user`),
`pass` char(32) not null,
`icon` varchar(255) not null default 'default.jpg',
`sex` enum('0','1','2') not null default '2',
`email` varchar(255) not null default '',
`address` varchar(255) not null default '',
`age` int unsigned not null default 0,
`tel` char(11) not null default '',
`grade` tinyint unsigned not null default 2,
`status` tinyint unsigned not null default 1,
`addtime` int unsigned not null
)engine=innodb default charset=utf8;
-- 添加測試數據
insert into `shop_user`(`user`,`pass`,`addtime`) values('admin',md5('123'),unix_timestamp());
insert into `shop_user`(`user`,`pass`,`addtime`) values('大勇',md5('123'),unix_timestamp());
insert into `shop_user`(`user`,`pass`,`addtime`) values('jack',md5('123'),unix_timestamp());
insert into `shop_user`(`user`,`pass`,`addtime`) values('提莫',md5('123'),unix_timestamp());
--創建收貨地址表
create table if not exists `shop_address`(
`id` int unsigned not null auto_increment ,
primary key(`id`),
`user_id` int unsigned not null ,
`name` varchar(255) not null ,
`address` varchar(255) not null ,
`tel` char(11) not null ,
`email` varchar(255) not null ,
`status` tinyint unsigned not null default 0,
`addtime` int unsigned not null
)engine=innodb default charset=utf8;
insert into `shop_address` values
(null , 1, '大勇','dongpu大馬路','13922130889','[email protected]',1,unix_timestamp()),
(null , 1, '大勇2b','可能還在讀小學','13822130889','[email protected]',1,unix_timestamp()),
(null , 1, '大勇3','東莞XXXX店','13722130889','[email protected]',1,unix_timestamp()),
(null , 2, '浪哥','天河白天月','15722130889','[email protected]',1,unix_timestamp()),
(null , 2, '隔壁老王','天河七天','18722130889','[email protected]',1,unix_timestamp());
--多表查詢
select * from `shop_user` , `shop_address`;
內連查詢
SELECT * FROM 表1,表2 WHERE 表1.字段 = 表2.字段;
select * from `shop_user`,`shop_address` where shop_user.id = shop_address.user_id;
--查詢 用戶表的id、用戶名、地址,收貨地址表的收貨人、收貨地址
select shop_user.id , shop_user.user,shop_address.id , shop_address.name , shop_address.address from `shop_user`,`shop_address` where shop_user.id = shop_address.user_id;
--別名的使用
select u.id , u.user , a.id , a.name , a.address from `shop_user` as u,`shop_address` as a where u.id = a.user_id;
--外連接
以左表爲主: 左表的所有數據,都會查詢出來,沒有與之關聯,則填空類型
select * from 表1 left join 表2 ON 表1.字段 = 表2.字段
select shop_user.id , shop_user.user , shop_address.user_id , shop_address.name , shop_address.address from shop_user left join shop_address on shop_user.id = shop_address.user_id;
-- 起別名
select u.id , u.user , a.user_id , a.name , a.address from shop_user as u left join shop_address as a on u.id = a.user_id;
-- 以右表爲主,右表的所有數據都會查詢,並與左表關聯
右連:SELECT * FROM 表1 RIGHT JOIN 表2 ON 表1.字段 = 表2.字段
select u.id,u.user , a.user_id , a.name , a.address from shop_user as u right join shop_address as a ON u.id = a.user_id;
-- 子查詢
查詢admin用戶的所有收貨地址
1.知道 admin 的 id
select id from shop_user where `user` = 'admin'; 1
2.拿着 admin 的 id去查詢 shop_address 條件就是 id = user_id
select * from shop_address where `user_id` = 1;
合體(子查詢)
select * from shop_address where `user_id` = (select id from shop_user where `user` = 'admin');
select * from shop_address where `user_id` in(select id from shop_user where `user` = 'admin');
數據的複製
-- 複製表結構
create table 新表 like 舊錶;
create table n_address like shop_address;
-- 複製數據
insert into 新表 select * from 舊錶;
insert into n_address select * from shop_address;
創建或授權 必須是 root 用戶纔可以使用授權功能
grant 權限 on 數據庫.數據表 to 用戶名@登錄主機 identified by "密碼"
1.用戶已經存在,則更新權限
2.用戶不存在,添加用戶並授權
% 可遠程登錄
localhost 本地登錄
grant select,update,delete,insert on t4.shop_address to cindy@localhost identified by 'root';
grant select,update,delete,insert on t4.shop_address to cindy@"%" identified by 'root';
all : select , insert , delete , update
*.* 所有庫.所有表
grant all on *.* to cindy@localhost identified by 'root';
--遠程登錄
mysql -h ip -u cindy -proot
--user表在mysql庫裏
--刪除用戶
delete from `user` where `user` = 'cindy';
--更新權限
flush privileges;
--更改密碼 update
update `user` set `password` = password('123') where `user` = 'zhiyong';
-- 更新權限
flush privileges;
-- 超級管理員的密碼忘記了
update `user` set `password` = password('123') where `user` = 'root';
--重啓服務器
數據庫備份
--備份一個庫
mysqldump -u root -p jn > d:/jn.sql
-- 還原一個庫
-- 必須先創建一個庫
mysql -u root -p t6 < d:/jn.sql
--備份一個表
mysqldump -u root -p 庫名 表名 > d:/文件名.sql
--使用source還原數據表
--必須選庫
use 庫;
source 路徑
取別名:
as
select name as '姓名' from jn_user;