数据库相关操作
啟動服務
sudo service msyql start
停止
sudo service msyql stop
重啟
sudo service msyql restart
數(shù)據(jù)庫用戶名: root
密碼: ? ? ? ?mysql
-- 數(shù)據(jù)庫的操作
? ? -- 鏈接數(shù)據(jù)庫
? ? mysql -uroot -p
? ? -- 退出數(shù)據(jù)庫
? ? exit ,quit ,ctr + d
? ? -- 查看創(chuàng)建數(shù)據(jù)庫
? ? show databases;
? ? -- 查看當前使用的數(shù)據(jù)庫
? ? select database();
? ? -- 使用數(shù)據(jù)庫
? ? use python_info;
? ? -- sql語句最后需要有分號;結(jié)尾
? ? -- 顯示數(shù)據(jù)庫版本
? ? select version();
? ? -- 顯示時間
? ? select now();
? ? -- 創(chuàng)建數(shù)據(jù)庫 create?
? ? create database demo;
? ? -- 指定字符集 不是 utf-8
? ? create database demo charset=utf8;
? ? -- 查看數(shù)據(jù)庫的創(chuàng)建語句
? ? show create database demo;
? ? -- 刪除數(shù)據(jù)庫
? ? drop database demo;
? ??
-- 數(shù)據(jù)表的操作
? ? -- 查看當前數(shù)據(jù)庫中所有表
? ? show tables;
? ? -- 創(chuàng)建表
? ? -- auto_increment表示自動增長
? ? -- 創(chuàng)建一個學生的數(shù)據(jù)表(id、name、age、high、gender、cls_id)
? ? -- create table 數(shù)據(jù)表名字 (字段 類型 約束[, 字段 類型 約束]);
? ? -- 多個約束 不分先后順序
? ? -- enum 表示枚舉 ?男: 原始值 會有一個枚舉值(從1開始)對應
? ? -- 最后一個字段不要添加逗號
? ? create table students(
? ? ? ? id int unsigned primary key not null auto_increment,
? ? ? ? name varchar(10) not null,
? ? ? ? age tinyint unsigned default 0,
? ? ? ? high decimal(5,2) default 0,
? ? ? ? gender enum("男", "女", "中性", "保密"),
? ? ? ? cls_id int unsigned default 0
? ? );
? ? -- 創(chuàng)建students表
? ? -- 查看表的創(chuàng)建語句
? ? show create table classes;
? ? ENGINE=InnoDB ? 引擎 ?innodb(支持外鍵 支持事務(可以撤銷之前的操作))
? ? 引擎還有 MyISAM 全文檢索引擎 查詢的速度極快 但是更新是比較慢的
? ? 黑洞black hole
? ? -- 查看表結(jié)構(gòu)
? ? +-------+------------------+------+-----+---------+----------------+
? ? | Field | Type ? ? ? ? ? ? | Null | Key | Default | Extra ? ? ? ? ?|
? ? +-------+------------------+------+-----+---------+----------------+
? ? | id ? ?| int(10) unsigned | NO ? | PRI | NULL ? ?| auto_increment |
? ? | name ?| varchar(15) ? ? ?| NO ? | ? ? | NULL ? ?| ? ? ? ? ? ? ? ?|
? ? +-------+------------------+------+-----+---------+----------------+
? ? desc classes;
? ? 學生表
? ? +--------+-------------------------------------+------+-----+---------+----------------+
? ? | Field ?| Type ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| Null | Key | Default | Extra ? ? ? ? ?|
? ? +--------+-------------------------------------+------+-----+---------+----------------+
? ? | id ? ? | int(10) unsigned ? ? ? ? ? ? ? ? ? ?| NO ? | PRI | NULL ? ?| auto_increment |
? ? | name ? | varchar(10) ? ? ? ? ? ? ? ? ? ? ? ? | NO ? | ? ? | NULL ? ?| ? ? ? ? ? ? ? ?|
? ? | age ? ?| tinyint(3) unsigned ? ? ? ? ? ? ? ? | YES ?| ? ? | 0 ? ? ? | ? ? ? ? ? ? ? ?|
? ? | high ? | decimal(5,2) ? ? ? ? ? ? ? ? ? ? ? ?| YES ?| ? ? | 0.00 ? ?| ? ? ? ? ? ? ? ?|
? ? | gender | enum('男','女','中性','保密') ? ? ? ? | YES ?| ? ? | NULL ? ?| ? ? ? ? ? ? ? ?|
? ? | cls_id | int(10) unsigned ? ? ? ? ? ? ? ? ? ?| YES ?| ? ? | 0 ? ? ? | ? ? ? ? ? ? ? ?|
? ? +--------+-------------------------------------+------+-----+---------+----------------+
? ??
? ? -- 修改表結(jié)構(gòu) ?alter
? ? -- 修改表-添加字段
? ? -- alter table 表名 add 列名 類型/約束;
? ? -- 生日信息?
? ? alter table students add birthday datetime;
? ? -- 修改表-修改字段:不重命名版
? ? -- alter table 表名 modify 列名 類型及約束;
? ? alter table students modify birthday date;
? ? -- 修改表-修改字段:重命名版
? ? -- alter table 表名 change 原列名 新列名 類型及約束;
? ? alter table students change birthday birth date;
? ? -- 修改表-刪除字段
? ? alter table students drop birth;
? ? -- 刪除表
? ? drop table students;
? ??
-- 數(shù)據(jù)增刪改查(curd)
學生表
? ? +--------+-------------------------------------+------+-----+---------+----------------+
? ? | Field ?| Type ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| Null | Key | Default | Extra ? ? ? ? ?|
? ? +--------+-------------------------------------+------+-----+---------+----------------+
? ? | id ? ? | int(10) unsigned ? ? ? ? ? ? ? ? ? ?| NO ? | PRI | NULL ? ?| auto_increment |
? ? | name ? | varchar(10) ? ? ? ? ? ? ? ? ? ? ? ? | NO ? | ? ? | NULL ? ?| ? ? ? ? ? ? ? ?|
? ? | age ? ?| tinyint(3) unsigned ? ? ? ? ? ? ? ? | YES ?| ? ? | 0 ? ? ? | ? ? ? ? ? ? ? ?|
? ? | high ? | decimal(5,2) ? ? ? ? ? ? ? ? ? ? ? ?| YES ?| ? ? | 0.00 ? ?| ? ? ? ? ? ? ? ?|
? ? | gender | enum('男','女','中性','保密') ? ? ? ? | YES ?| ? ? | NULL ? ?| ? ? ? ? ? ? ? ?|
? ? | cls_id | int(10) unsigned ? ? ? ? ? ? ? ? ? ?| YES ?| ? ? | 0 ? ? ? | ? ? ? ? ? ? ? ?|
? ? +--------+-------------------------------------+------+-----+---------+----------------+
? ? -- 增加 insert?
? ? ? ? -- 全列插入 ?值和表的字段的順序一一對應
? ? ? ? -- 可有 可無
? ? ? ? -- insert [into] 表名 values(...)
? ? ? ? -- [] 表示可有 可無
? ? ? ? -- id 字段不能夠省略 必須通過占位來解決 ?0 NULL default
? ? ? ? -- SQL中枚舉值默認從1 開始
? ? ? ? insert into students values (0, "小喬", 25, 180, 1, 1);
? ? ? ? insert into students values (NULL, "曹操", 35, 180, 1, 2);
? ? ? ? insert into students values (NULL, "劉備", 35, 180, "保密", 2);?
? ? ? ? # 錯誤:insert into students values (NULL, "劉備", 35, 180, "不知道", 2);?
? ? ? ? -- 指定列插入
? ? ? ? -- 值和列一一對應
? ? ? ? -- insert into 表名(列1,...) values(值1,...)
? ? ? ? insert into students (name, gender, cls_id) values ("大喬", 2, 1);
? ? ? ? -- 多行插入 ?批量插入
? ? ? ? -- insert into 表名(列1,...) values (值1,...),(值1,...),...
? ? ? ? insert into students (name, gender, cls_id) values ("貂蟬", 2, 1), ("昭君", 2, 2), ("勾踐", 2, 1);
? ? -- 修改
? ? -- where 表示修改的范圍
? ? -- update 表名 set 列1=值1,列2=值2... where 條件
? ? -- 全表更新
? ? -- 搜易貸 P2P ?易租寶
? ? update students set gender = 2;
? ? -- sql中 ?通過一個等于號表示相等?
? ? update students set gender = 1 where id = 2;
? ? -- 刪除
? ? ? ? -- 物理刪除 ?不可撤銷
? ? ? ? -- DELETE FROM tbname [where 條件判斷]
? ? ? ? # delete from students; ? # mysql 從刪表到跑路 ? 去哪兒 dba(數(shù)據(jù)庫管理員)
? ? ? ? delete from students where id = 1;
? ? ? ? -- 邏輯刪除 ?諾基亞手機 物理刪除對應的手機數(shù)據(jù), ?個人中心->我的訂單-> 訂單列表-> 某一個訂單 -> 訂單詳情--> 商品詳情頁面
? ? ? ? -- 給一個數(shù)據(jù)添加一個標記字段 bit 類型 通過設(shè)置 0 或者1 來標識數(shù)據(jù)到底有沒有被刪除
? ? ? ? 1. 修改表結(jié)構(gòu) 添加一個字段
? ? ? ? alter table students add is_delete bit default 0;
? ? ? ? update students set is_delete = 1 where id = 7;
? ? ? ? -- 查詢有哪些學生沒有被刪除
? ? -- 查詢基本使用
? ? ? ? -- 查詢所有列
? ? ? ? -- select * from 表名;
? ? ? ? select * from classes;
? ? ? ? -- 指定字段查詢
? ? ? ? -- sql 中表示相等 使用 = 而不是 ==
? ? ? ? -- 指定條件查詢
? ? ? ? -- 查詢指定列
? ? ? ? -- 字段的順序
? ? ? ? -- 可以使用as為列或表指定別名
? ? --------------------------------------------------------------------------------------------------------------------------------------------------------
簡單的操作上面就夠了,下面是高級查詢:
-- 查詢
?? ?-- 查詢所有字段
?? ?-- select * from 表名;
?? ?select * from students;
?? ?-- 查詢指定字段
?? ?-- select 列1,列2,... from 表名;
?? ?select name,age from students;
?? ?-- 完全的形式
?? ?select students.* from students;
?? ?select python_test_1.students.* from students;
?? ?select students.name,students.age from students;
?? ?-- 數(shù)據(jù)庫名什么時候能夠省略: 查詢的數(shù)據(jù)表在當前正在被使用的數(shù)據(jù)庫中存在
?? ?-- 數(shù)據(jù)表什么時候不能夠省略: 當一個sql語句中有多個表的時候表名不建議省略
?? ?-- 使用 as 給字段起別名
?? ?-- select 字段 as 名字.... from 表名;
?? ?select name as 名字,age as 年齡 from students;
?? ?select name as '名字',age as '年齡' from students;
?? ?-- select 表名.字段 .... from 表名;
?? ?
?? ?-- 可以通過 as 給表起別名
?? ?-- select 別名.字段 .... from 表名 as 別名;
?? ?select s.name,s.age from students as s;
?? ?-- 消除重復行
?? ?-- distinct 字段
?? ?-- 查詢班級學生的性別
?? ?select gender from students;
?? ?-- 查詢班級有多少種性別
?? ?select distinct gender from students;
?? ?-- id, gender
?? ?select distinct id,gender from students;
-- 條件查詢 ?where ?通過某種條件來篩選數(shù)據(jù)
?? ?-- 比較運算符
?? ??? ?-- >
?? ??? ?-- 查詢大于18歲的信息
?? ??? ?select name from students where age > 18;
?? ??? ?-- <
?? ??? ?-- 查詢小于18歲的信息
?? ??? ?-- >=
?? ??? ?-- <=
?? ??? ?-- 查詢小于或者等于18歲的信息
?? ??? ?-- = 而不是 '=='
?? ??? ?-- 查詢年齡為18歲的所有學生的名字
?? ??? ?select name from students where age = 18;
?? ??? ?-- != 或者 <> ?實際開發(fā)中 最好使用 ! 表示不等于
?? ??? ?-- <> 不夠通用
?? ?-- 邏輯運算符
?? ??? ?-- and
?? ??? ?-- 18歲以上的女性
?? ??? ?select * from students where age > 18 and gender = 2;
?? ??? ?-- or
?? ??? ?-- 18以上或者身高超過180(包含)以上
?? ??? ?select * from students where age > 18 or height >= 180;
?? ??? ?-- not ?非
?? ??? ?-- 年齡不是18歲的學生
?? ??? ?select * from students where age != 18;
?? ??? ?select * from students where not age = 18;
?? ??? ?-- 年齡是小于或者等于18 并且是女性
?? ??? ?select * from students where age <=18 and gender = 2;
?? ?-- 模糊查詢
?? ??? ?-- like?
?? ??? ?-- % 表示任意字符可有可無
?? ??? ?-- 查詢姓名中 以 "小" 開始的名字
?? ??? ?select * from students where name like "小%";
?? ??? ?select * from students where name like "%杰%";
?? ??? ?-- _ 表示任意一個字符
?? ??? ?-- 查詢有2個字的名字
?? ??? ?select * from students where name like "__"
?? ??? ?-- 查詢有3個字的名字
?? ??? ?select * from students where name like "___"
?? ??? ?-- rlike 正則
?? ??? ?-- match sub findall
?? ??? ?-- r""
?? ??? ?-- 查詢以 周開始的姓名
?? ??? ?select * from students where name rlike "^周.*";
?? ??? ?select * from students where name rlike "^周.*倫$";
?? ??? ?
?? ?-- 范圍查詢
?? ??? ?-- in表示在一個非連續(xù)的范圍內(nèi)
?? ??? ?-- 查詢 年齡為18、34歲的學生
?? ??? ?select * from students where age = 18 or age = 34;
?? ??? ?select * from students where age in (18,34,40);
?? ??? ?
?? ??? ?-- not in 不在非連續(xù)的范圍之內(nèi)
?? ??? ?-- 年齡不是 18、34歲的學生的信息
?? ??? ?select * from students where age not in (18,34);
?? ??? ?-- 年齡不是 18、34歲之間的信息
?? ??? ?select * from students where age < 18 or age > 34;
?? ??? ?-- 18 ~ 34
?? ??? ?select * from students where not (age < 18 or age > 34);
?? ??? ?select * from students where age >= 18 and age <=34;
?? ??? ?-- between ... and ...表示在一個連續(xù)的范圍內(nèi) ?兩邊都會包含
?? ??? ?-- 查詢 年齡在18到34之間的的信息
?? ??? ?select * from students where age between 18 and 34;
?? ??? ?
?? ??? ?-- not between ... and ...表示不在一個連續(xù)的范圍內(nèi)
?? ??? ?-- 查詢 年齡不在在18到34之間的的信息
?? ??? ?select * from students where age not between 18 and 34;
?? ??? ?錯誤的: select * from students where age not (between 18 and 34);
?? ?-- 空判斷 null ?不能夠使用比較運算符
?? ??? ?-- 查詢身高為空的信息
?? ??? ?select * from students where height is null;
?? ??? ?-- 查詢身高不為空的學生
?? ??? ?select * from students where height is not null;
-- 排序 從大到小--> 降序排序 從小到大-->升序排序
?? ?-- order by 字段 ?默認就是升序排序 asc 可以省略
?? ?-- asc從小到大排列,即升序
?? ?
?? ?-- 查詢年齡在18到34歲之間的男性,按照年齡從小到大排序
?? ?select * from students where age between 18 and 34 and gender = 1 order by age asc;
?? ?-- 降序 desc
?? ?-- desc從大到小排序,即降序
?? ?-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序
?? ?select * from students where age between 18 and 34 and gender = 2 order by height desc;
?? ?-- order by 多個字段 order by age asc, height desc
?? ?-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序
?? ?select * from students where age between 18 and 34 and gender = 2 order by height desc, age asc;
?? ?-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序, 如果年齡也相同那么按照id從大到小排序
?? ?select * from students where age between 18 and 34 and gender = 2 order by height desc, age asc, id desc;
?? ?-- 按照年齡從小到大、身高從高到矮的排序
?? ?select * from students order by age asc, height desc;
-- 聚合函數(shù)
?? ?-- 總數(shù)
?? ?select count(*) from students;
?? ?-- count()
?? ?-- count(*) 以行單位來進行統(tǒng)計個數(shù)
?? ?-- count(*) 效率更高, 效率略差:count(id)--> 獲取對應的行--> 獲取該行對應字段是否為NULL
?? ?-- 查詢男性有多少人,女性有多少人
?? ?select count(*) from students where gender = 1;
?? ?select count(*) from students where gender = 2;
?? ?-- 最大值
?? ?-- max()
?? ?-- 查詢最大的年齡
?? ?select max(age) from students;
?? ?-- 查詢女性的最高 身高
?? ?-- 查詢最大年齡的學生的名字
?? ?select name, max(age) from students;
?? ?# 錯誤: select name, max(height) from students where gender = 2
?? ?-- 靜香,小喬 180
?? ?
?? ?-- 最小值
?? ?-- min()
?? ?select min(age) from students;
?? ?
?? ?-- 求和
?? ?-- sum()
?? ?-- 計算所有人的年齡總和
?? ?select sum(age) from students;
?? ?-- 計算平均年齡
?? ?select sum(age)/count(age) from students;
?? ?# 錯誤select sum(height)/count(*) from students;
?? ?
?? ?-- 平均值
?? ?-- avg()
?? ?-- 計算平均年齡
?? ?select avg(age) from students;
?? ?-- 計算平均身高
?? ?select avg(height) from students
?? ?-- 四舍五入 round(123.23 , 1) 保留1位小數(shù)
?? ?-- 計算所有人的平均年齡,保留2位小數(shù)
?? ?select round(avg(age),2) from students;
?? ?-- 計算男性的平均身高 保留2位小數(shù)
?? ?select round(avg(height),2) from students where gender = 1;
-- 分組
?? ?-- group by 字段
?? ?-- 查詢班級學生的性別
?? ?select gender from students;
?? ?-- 查看有哪幾種性別
?? ?select distinct gender from students;
?? ?
?? ?-- 按照性別分組
?? ?select gender from students group by gender;
?? ?-- 計算每種性別中的人數(shù)
?? ?select count(*) from students group by gender;
?? ?-- group_concat(...)
?? ?# 錯誤的 select gender,name from students group by gender;
?? ?select gender, group_concat(name,"-",age) from students group by gender;
?? ?-- 查詢同種性別中的姓名和身高
?? ?select gender, group_concat(name, "-", height) from students group by gender;
?? ?-- 計算男性的人數(shù)
?? ?select count(*) from students where gender = 1;
?? ?-- 通過分組來實現(xiàn)
?? ?select gender, count(*) from students group by gender having gender = 1;
?? ?-- 使用having
?? ?-- 可以使用having 表示對于已經(jīng)分組的數(shù)據(jù)做進一步的篩選
?? ?-- 除了男生以外的分組的人數(shù)
?? ?select gender, count(*) from students group by gender having not gender = 1;
?? ?select gender, count(*) from students group by gender having gender != 1;
?? ?-- having ?對于分組之后的數(shù)據(jù) 做進一步的篩選
?? ?-- 查詢每種性別中的平均年齡avg(age)
?? ?select gender,avg(age) from students group by gender;
?? ?-- 查詢每種性別中的平均年齡avg(age), 最大年齡,平均身高,最高身高
?? ?select gender,avg(age),max(age), avg(height), max(height) from students group by gender;
?? ?-- 查詢平均年齡超過30歲的性別,以及姓名
?? ?select gender,group_concat(name) from students group by gender having avg(age) > 30;
?? ?-- having 和 where 的區(qū)別
?? ?where: 對于源數(shù)據(jù)的篩選
?? ?having: 需要對于分組之后的數(shù)據(jù)做進一步的篩選操作 有having 就一定有g(shù)roup by, 有g(shù)roup by 不一定有having
-- 分頁
?? ?-- limit start, count
?? ?-- start 默認值為0 可以省略
?? ?-- 每頁取多少條數(shù)據(jù)
?? ?-- start 表示跳過多少條數(shù)據(jù)
?? ?每頁顯示5條數(shù)據(jù)?? ??? ??? ??? ??? ??? ??? ?已知每頁顯示m條數(shù)據(jù),和當前要顯示的頁碼n(從1開始), 求需要顯示哪些數(shù)據(jù)
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?limit (n - 1) * m, m;
?? ?第1頁
?? ?select * from students limit 0,5;
?? ?第2頁
?? ?select * from students limit 5,5;
?? ?第3頁
?? ?select * from students limit 10,5;
?? ?第4頁
?? ?select * from students limit 15,5;
?? ?-- 每頁顯示4個,顯示第3頁的信息, 按照年齡從小到大排序
?? ?# 錯誤 select * from students limit 8,4 order by age asc;
?? ?-- 正確的順序
?? ?select * from students order by age asc limit 8,4;
-- 連接查詢 ?將兩個表按照某種條件合并到一起
?? ?-- 學生名: students;
?? ?-- 班級名: classes
?? ?-- 查詢學生的信息和學生對應的班級名字
?? ?-- 觸發(fā)笛卡爾積查詢
?? ?-- 效率低
?? ?select students.*, classes.* from students, classes where students.cls_id = classes.id;
?? ?-- inner join ... on 內(nèi)連接查詢
?? ?select students.*, classes.* from students inner join classes on students.cls_id = classes.id;
?? ?select * from students inner join classes on students.cls_id = classes.id;
?? ?-- 連接查詢
?? ?-- 按照要求顯示姓名、和學生對應的班級的名字(學生所在的班級)
?? ?select students.name,classes.name from students inner join classes on students.cls_id = classes.id;?? ?
?? ?-- 在以上的查詢中,將班級名字顯示在第1列
?? ?select classes.name ,students.name from students inner join classes on students.cls_id = classes.id;?? ?
?? ?-- 查詢 學生所在的班級, 按照班級進行排序
?? ?-- select c.xxx s.xxx from student as s inner join classes as c on .... order by ....;
?? ?-- 按照條件連接students和classes
?? ?select classes.name ,students.name from students inner join classes on students.cls_id = classes.id order by classes.name;
?? ?select c.name ,s.name from students as s inner join classes as c on s.cls_id = c.id order by c.name;
?? ?-- 外連接查詢: left join + right join
?? ?-- left join 左外連接查詢
?? ?-- 查詢每位學生對應的班級信息
?? ?select * from students left join classes on students.cls_id = classes.id;
?? ?-- right join 右外連接查詢 ?使用的比較少
?? ?-- 將數(shù)據(jù)表名字互換位置,用left join完成
?? ?select * from students right join classes on students.cls_id = classes.id;
?? ?select * from classes left join students on students.cls_id = classes.id;?? ?
?? ?-- 內(nèi)連接和外連接的其他寫法
?? ?-- ?內(nèi)連接的其他寫法?
?? ?select * from students join classes on students.cls_id = classes.id;
?? ?select * from students cross join classes on students.cls_id = classes.id;
?? ?-- 外連接的其他寫法
?? ?select * from students left outer join classes on students.cls_id = classes.id;
-- 自關(guān)聯(lián) ?自己關(guān)聯(lián)自己 a inner join a
?? ?準備數(shù)據(jù)
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
-- 通過 source 指令導入一個sql文件
?? ?-- 省級聯(lián)動 url:http://demo.lanrenzhijia.com/2014/city0605/
?? ?-- 查詢所有省份
?? ?select * from areas where pid is null;
?? ?-- 查詢出廣東省有哪些市
?? ?-- 發(fā)揮想象力 一張表 想象成兩張表
?? ?-- 連接查詢
?? ?select p.atitle, c.atitle from areas as p inner join areas as c on c.pid = p.aid where p.atitle = "廣東省";
?? ?-- 查詢出廣州市有哪些區(qū)縣
?? ?-- p: parent ?s: son
?? ?select p.atitle, s.atitle from areas as p inner join areas as s on s.pid = p.aid where p.atitle = "廣州市";
?
?適用于無限向下分級的業(yè)務場景
總結(jié)
- 上一篇: php图片重复的,很强的PHP图片处理类
- 下一篇: hive转16进制unhex_Hive,