mysql的使用

liudemeng發表於2019-07-02

-- 資料庫的操作

-- 連結資料庫
mysql -uroot -p
mysql -uroot -pmysql

-- 退出資料庫
exit/quit/ctrl+d

-- sql語句最後需要有分號;結尾
-- 顯示資料庫版本
select version();

-- 顯示時間
select now();

-- 檢視所有資料庫
show databases;

-- 建立資料庫
-- create database 資料庫名 charset=utf8;
create database python04;
create database python04new charset=utf8;

-- 檢視建立資料庫的語句
-- show crate database ....
show create database python04;

-- 檢視當前使用的資料庫
select database();

-- 使用資料庫
-- use 資料庫的名字
use python04new;

-- 刪除資料庫
-- drop database 資料庫名;
drop database python04;

-- 資料表的操作

-- 檢視當前資料庫中所有表
show tables;

-- 建立表
-- auto_increment表示自動增長
-- not null 表示不能為空
-- primary key 表示主鍵s
-- default 預設值
-- create table 資料表名字 (欄位 型別 約束[, 欄位 型別 約束]);
create table xxxxx(

  id int, name varchar(30)

);

create table yyyyy(

  id int primary key not null auto_increment,

  name varchar(30)

);

create table zzzzz(
  id int primary key not null auto_increment,
  name varchar(30)
);

-- 檢視錶結構
-- desc 資料表的名字;
desc xxxxx;

-- 建立students表(id、name、age、high、gender、cls_id)
create table students(
  id int unsigned not null auto_increment primary key,
  name varchar(30),
  age tinyint unsigned default 0,
  high decimal(5,2),
  gender enum("男", "女", "中性", "保密") default "保密",
  cls_id int unsigned
);

insert into students values(0, "老王", 18, 188.88, "男", 0);
insert into students values(0, "王小二", 19, 150.12, "女", 1);
insert into students values(0, "李爽", 22, 165.00, "女", 2);
insert into students values(0, "李傑", 60, 158.10, "男", 3);
insert into students values(0, "劉菲菲", 12, 175.50, "女", 4);
insert into students values(0, "小明", 19, 161.30, "男", 0);
insert into students values(0, "李蕾", 16, 158.20, "女", 1);
insert into students values(0, "韓沒滅", 25, 150.00, "男", 2);
insert into students values(0, "傑克", 36, 180.12, "女", 3);
insert into students values(0, "路西", 18, 183.12, "男", 4);
insert into students values(0, "王靜", 12, 196.12, "中性", 1);
insert into students values(0, "王姐", 34, 188.12, "男", 1);
insert into students values(0, "徐開心", 35, 200.12, "女", 2);
insert into students values(0, "江配", 39, 196.12, "", 3);
insert into students values(0, "盛淑星", 26, 162.12, "女", 4);
insert into students values(0, "鄧程飛", 19, 169.12, "男", 5);
insert into students values(0, "江鈴", 18, 175.12, "女", 6);
insert into students values(0, "唐世菊", 13, 190.12, "男", 1);

select * from students;

-- 建立classes表(id、name)
create table classes(
  id int unsigned not null auto_increment primary key,
  name varchar(30)
  gender enum("男", "女", "中性", "保密") default "保密"
);

insert into classes values(0, "python04大神");
select * from classes;

-- 檢視錶的建立語句
-- show create table 表名字;
show create table students;

-- 修改表-新增欄位
-- 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 default "2000-01-01";

-- 修改表-刪除欄位
-- alter table 表名 drop 列名;
alter table students drop high;

-- 刪除表
-- drop table 表名;
-- drop database 資料庫;
-- drop table 資料表;
drop table xxxxx;


-- 增刪改查(curd)

-- 增加
-- 全列插入
-- insert [into] 表名 values(...)
-- 主鍵欄位 可以用 0 null default 來佔位
-- 向classes表中插入 一個班級
insert into classes values(0, "菜鳥班");

+--------+-------------------------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------------------------------+------+-----+------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| gender | enum('男','女','中性','保密') | YES | | 保密 | |
| cls_id | int(10) unsigned | YES | | NULL | |
| birth | date | YES | | 2000-01-01 | |
+--------+-------------------------------------+------+-----+------------+----------------+

-- 向students表插入 一個學生資訊
insert into students values(0, "小李飛刀", 20, "女", 1, "1990-01-01");
insert into students values(null, "小李飛刀", 20, "女", 1, "1990-01-01");
insert into students values(default, "小李飛刀", 20, "女", 1, "1990-01-01");

-- 失敗
-- insert into students values(default, "小李飛刀", 20, "第4性別", 1, "1990-02-01");

-- 列舉中 的 下標從1 開始 1---“男” 2--->"女"....
insert into students values(default, "小李飛刀", 20, 1, 1, "1990-02-01");

-- 部分插入
-- insert into 表名(列1,...) values(值1,...)
insert into students (name, gender) values ("小喬", 2);

-- 多行插入
insert into students (name, gender) values ("大喬", 2),("貂蟬", 2);
insert into students values(default, "西施", 20, "女", 1, "1990-01-01"), (default, "王昭君", 20, "女", 1, "1990-01-01");

-- 修改
-- update 表名 set 列1=值1,列2=值2... where 條件;
update students set gender=1; -- 全部都改
update students set gender=1 where name="小李飛刀"; -- 只要name是小李飛刀的 全部的修改
update students set gender=1 where id=3; -- 只要id為3的 進行修改
update students set age=22, gender=1 where id=3; -- 只要id為3的 進行修改

-- 查詢基本使用
-- 查詢所有列
-- select * from 表名;
select * from students;

---定條件查詢
select * from students where name="小李飛刀"; -- 查詢 name為小李飛刀的所有資訊
select * from students where id>3; -- 查詢 name為小李飛刀的所有資訊

-- 查詢指定列
-- select 列1,列2,... from 表名;
select name,gender from students;

-- 可以使用as為列或表指定別名
-- select 欄位[as 別名] , 欄位[as 別名] from 資料表 where ....;
select name as 姓名,gender as 性別 from students;

-- 欄位的順序
select id as 序號, gender as 性別, name as 姓名 from students;

-- 刪除
-- 物理刪除
-- delete from 表名 where 條件
delete from students; -- 整個資料表中的所有資料全部刪除
delete from students where name="小李飛刀";

-- 邏輯刪除
-- 用一個欄位來表示 這條資訊是否已經不能再使用了
-- 給students表新增一個is_delete欄位 bit 型別
alter table students add is_delete bit default 0;
update students set is_delete=1 where id=6;

 

-- 資料的準備

    -- 建立一個資料庫

    create database python_test charset=utf8;

    -- 使用一個資料庫

    use python_test;

    -- 顯示使用的當前資料是哪個?

    select database();

    -- 建立一個資料表

    -- students表

    create table students(

        id int unsigned primary key auto_increment not null,

        name varchar(20), --default '',

        age tinyint unsigned default 0,

        height decimal(5,2),

        gender enum('男','女','中性','保密'),-- default '保密',

        cls_id int unsigned default 0,

        is_delete bit default 0

    );

    -- classes表

    create table classes (

        id int unsigned auto_increment primary key not null,

        name varchar(30) not null

    );

 

-- 查詢

    -- 查詢所有欄位

    -- select * from 表名;

    select * from students;

    select * from classes;

    select id, name from classes;

    -- 查詢指定欄位

    -- select 列1,列2,... from 表名;

    select name, age from students;

 

    -- 使用 as 給欄位起別名

    -- select 欄位 as 名字.... from 表名;

    select name as 姓名, age as 年齡 from students;

    -- select 表名.欄位 .... from 表名;

    select students.name, students.age from students;

 

    -- 可以通過 as 給表起別名

    -- select 別名.欄位 .... from 表名 as 別名;

    select students.name, students.age from students;

    select s.name, s.age from students as s;

    -- 失敗的select students.name, students.age from students as s;

 

    -- 消除重複行

    -- distinct 欄位

    select distinct gender from students;

 

-- 條件查詢

    -- 比較運算子

        -- select .... from 表名 where .....

        -- >

        -- 查詢大於18歲的資訊

        select * from students where age>18;

        select id,name,gender from students where age>18;

        -- <

        -- 查詢小於18歲的資訊

        select * from students where age<18;

        -- >=

        -- <=

        -- 查詢小於或者等於18歲的資訊

        -- =

        -- 查詢年齡為18歲的所有學生的名字

        select * from students where age=18;

        -- != 或者 <>

    -- 邏輯運算子

        -- and

        -- 18到28之間的所以學生資訊

        select * from students where age>18 and age<28;

        -- 失敗select * from students where age>18 and <28;

        -- 18歲以上的女性

        select * from students where age>18 and gender="女";

        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 not age>18 and gender=2;

        select * from students where not (age>18 and gender=2);

        -- 年齡不是小於或者等於18 並且是女性

        select * from students where (not age<=18) and gender=2;

    -- 模糊查詢

        -- like

        -- % 替換1個或者多個

        -- _ 替換1個

        -- 查詢姓名中 以 "小" 開始的名字

        select name from students where name="小";

        select name from students where name like "小%";

        -- 查詢姓名中 有 "小" 所有的名字

        select name from students where name like "%小%";

        -- 查詢有2個字的名字

        select name from students where name like "__";

        -- 查詢有3個字的名字

        select name from students where name like "__";

        -- 查詢至少有2個字的名字

        select name from students where name like "__%";

        -- rlike 正則

        -- 查詢以 周開始的姓名

        select name from students where name rlike "^周.*";

        -- 查詢以 周開始、倫結尾的姓名

        select name from students where name rlike "^周.*倫$";

    -- 範圍查詢

        -- in (1, 3, 8)表示在一個非連續的範圍內

        -- 查詢 年齡為18、34的姓名

        select name,age from students where age=18 or age=34;

        select name,age from students where age=18 or age=34 or age=12;

        select name,age from students where age in (12, 18, 34);     

        -- not in 不非連續的範圍之內

        -- 年齡不是 18、34歲之間的資訊

        select name,age from students where age not in (12, 18, 34);

        -- between ... and ...表示在一個連續的範圍內

        -- 查詢 年齡在18到34之間的的資訊

        select name, age from students where age between 18 and 34;

        -- not between ... and ...表示不在一個連續的範圍內

        -- 查詢 年齡不在在18到34之間的的資訊

        select * from students where age not between 18 and 34;

        select * from students where not age between 18 and 34;

        -- 失敗的select * from students where age not (between 18 and 34);

    -- 空判斷

        -- 判空is null

        -- 查詢身高為空的資訊

        select * from students where height is null;

        select * from students where height is NULL;

        select * from students where height is Null;

        -- 判非空is not null

        select * from students where height is not null;

-- 排序

    -- order by 欄位

    -- asc從小到大排列,即升序

    -- desc從大到小排序,即降序

    -- 查詢年齡在18到34歲之間的男性,按照年齡從小到到排序

    select * from students where (age between 18 and 34) and gender=1;

    select * from students where (age between 18 and 34) and gender=1 order by age;

    select * from students where (age between 18 and 34) and gender=1 order by age asc;

    -- 查詢年齡在18到34歲之間的女性,身高從高到矮排序

    select * from students where (age between 18 and 34) and gender=2 order by height desc; 

    -- order by 多個欄位

    -- 查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序

;

    -- 查詢年齡在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;

-- 聚合函式

    -- 總數

    -- count

    -- 查詢男性有多少人,女性有多少人

    select * from students where gender=1;

    select count(*) from students where gender=1;

    select count(*) as 男性人數 from students where gender=1;

    select count(*) as 女性人數 from students where gender=2;

    -- 最大值

    -- max

    -- 查詢最大的年齡

    select age from students;

    select max(age) from students;

    -- 查詢女性的最高 身高

    select max(height) from students where gender=2;

    -- 最小值

    -- min

    -- 求和

    -- sum

    -- 計算所有人的年齡總和

    select sum(age) from students;

    -- 平均值

    -- avg

    -- 計算平均年齡

    select avg(age) from students;

    -- 計算平均年齡 sum(age)/count(*)

    select sum(age)/count(*) from students;

    -- 四捨五入 round(123.23 , 1) 保留1位小數

    -- 計算所有人的平均年齡,保留2位小數

    select round(sum(age)/count(*), 2) from students;

    select round(sum(age)/count(*), 3) from students;

    -- 計算男性的平均身高 保留2位小數

    select round(avg(height), 2) from students where gender=1;

    -- select name, round(avg(height), 2) from students where gender=1;

-- 分組

    -- group by

    -- 按照性別分組,查詢所有的性別

    select name from students group by gender;

    --select * from students group by gender;

    select gender from students group by gender;

    -- 失敗select * from students group by gender;

    -- 計算每種性別中的人數

    select gender,count(*) from students group by gender;

    -- 計算男性的人數

    select gender,count(*) from students where gender=1 group by gender;

    -- group_concat(...)

    -- 查詢同種性別中的姓名

    select gender,group_concat(name) from students where gender=1 group by gender;

    select gender,group_concat(name, age, id) from students where gender=1 group by gender;

    select gender,group_concat(name, "_", age, " ", id) from students where gender=1 group by gender;

    -- having

    -- 查詢平均年齡超過30歲的性別,以及姓名 having avg(age) > 30

    select gender, group_concat(name),avg(age) from students group by gender having avg(age)>30;    

    -- 查詢每種性別中的人數多於2個的資訊

    select gender, group_concat(name) from students group by gender having count(*)>2;

-- 分頁

    -- limit start, count

    -- 限制查詢出來的資料個數

    select * from students where gender=1 limit 2;

    -- 查詢前5個資料

    select * from students limit 0, 5;

    -- 查詢id6-10(包含)的書序

    select * from students limit 5, 5;

    -- 每頁顯示2個,第1個頁面

    select * from students limit 0,2;

    -- 每頁顯示2個,第2個頁面

    select * from students limit 2,2;

    -- 每頁顯示2個,第3個頁面

    select * from students limit 4,2;

    -- 每頁顯示2個,第4個頁面

    select * from students limit 6,2; -- -----> limit (第N頁-1)*每個的個數, 每頁的個數;

    -- 每頁顯示2個,顯示第6頁的資訊, 按照年齡從小到大排序

    -- 失敗select * from students limit 2*(6-1),2;

    -- 失敗select * from students limit 10,2 order by age asc;

    select * from students order by age asc limit 10,2;

    select * from students where gender=2 order by height desc limit 0,2;

-- 連線查詢

    -- inner join ... on

    -- select ... from 表A inner join 表B;

    select * from students inner join classes;

    -- 查詢 有能夠對應班級的學生以及班級資訊

    select * from students inner join classes on students.cls_id=classes.id;

    -- 按照要求顯示姓名、班級

    select students.*, classes.name 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;

    -- 給資料表起名字

    select s.name, c.name from students as s inner join classes as c on s.cls_id=c.id;

    -- 查詢 有能夠對應班級的學生以及班級資訊,顯示學生的所有資訊,只顯示班級名稱

    select s.*, c.name from students as s inner join classes as c on s.cls_id=c.id;

    -- 在以上的查詢中,將班級姓名顯示在第1列

    select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id;

    -- 查詢 有能夠對應班級的學生以及班級資訊, 按照班級進行排序

    -- select c.xxx s.xxx from student as s inner join clssses as c on .... order by ....;

    select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;

    -- 當時同一個班級的時候,按照學生的id進行從小到大排序

    select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;

    -- left join

    -- 查詢每位學生對應的班級資訊

    select * from students as s left join classes as c on s.cls_id=c.id;

    -- 查詢沒有對應班級資訊的學生

    -- select ... from xxx as s left join xxx as c on..... where .....

    -- select ... from xxx as s left join xxx as c on..... having .....

    select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;

    select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;

    -- right join   on

    -- 將資料表名字互換位置,用left join完成

-- 自關聯

    -- 省級聯動 url:http://demo.lanrenzhijia.com/2014/city0605/

    -- 查詢所有省份

    select * from areas where pid is null;

    -- 查詢出山東省有哪些市

    select * from areas as province inner join areas as city on city.pid=province.aid having province.atitle="山東省";

    select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="山東省";

    -- 查詢出青島市有哪些縣城

    select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="青島市";

    select * from areas where pid=(select aid from areas where atitle="青島市";

-- 子查詢

    -- 標量子查詢

    -- 查詢出高於平均身高的資訊

    -- 查詢最高的男生資訊

    select * from students where height = 188;

    select * from students where height = (select max(height) from students);

    -- 列級子查詢

    -- 查詢學生的班級號能夠對應的學生資訊

    -- select * from students where cls_id in (select id from classes);

-- 資料的準備
    -- 建立一個資料庫
    create database python_test charset=utf8;

    -- 使用一個資料庫
    use python_test;

    -- 顯示使用的當前資料是哪個?
    select database();

    -- 建立一個資料表
    -- students表
    create table students(
        id int unsigned primary key auto_increment not null,
        name varchar(20), --default '',
        age tinyint unsigned default 0,
        height decimal(5,2),
        gender enum('男','女','中性','保密'),-- default '保密',
        cls_id int unsigned default 0,
        is_delete bit default 0
    );

    -- classes表
    create table classes (
        id int unsigned auto_increment primary key not null,
        name varchar(30) not null
    );



-- 查詢
    -- 查詢所有欄位
    -- select * from 表名;
    select * from students;
    select * from classes;
    select id, name from classes;

    -- 查詢指定欄位
    -- select 列1,列2,... from 表名;
    select name, age from students;
    
    -- 使用 as 給欄位起別名
    -- select 欄位 as 名字.... from 表名;
    select name as 姓名, age as 年齡 from students;

    -- select 表名.欄位 .... from 表名;
    select students.name, students.age from students;

    
    -- 可以通過 as 給表起別名
    -- select 別名.欄位 .... from 表名 as 別名;
    select students.name, students.age from students;
    select s.name, s.age from students as s;
    -- 失敗的select students.name, students.age from students as s;


    -- 消除重複行
    -- distinct 欄位
    select distinct gender from students;


-- 條件查詢
    -- 比較運算子
        -- select .... from 表名 where .....
        -- >
        -- 查詢大於18歲的資訊
        select * from students where age>18;
        select id,name,gender from students where age>18;

        -- <
        -- 查詢小於18歲的資訊
        select * from students where age<18;

        -- >=
        -- <=
        -- 查詢小於或者等於18歲的資訊

        -- =
        -- 查詢年齡為18歲的所有學生的名字
        select * from students where age=18;


        -- != 或者 <>


    -- 邏輯運算子
        -- and
        -- 18到28之間的所以學生資訊
        select * from students where age>18 and age<28;
        -- 失敗select * from students where age>18 and <28;


        -- 18歲以上的女性
        select * from students where age>18 and gender="女";
        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 not age>18 and gender=2;
        select * from students where not (age>18 and gender=2);

        -- 年齡不是小於或者等於18 並且是女性
        select * from students where (not age<=18) and gender=2;


    -- 模糊查詢
        -- like
        -- % 替換1個或者多個
        -- _ 替換1個
        -- 查詢姓名中 以 "小" 開始的名字
        select name from students where name="小";
        select name from students where name like "小%";

        -- 查詢姓名中 有 "小" 所有的名字
        select name from students where name like "%小%";

        -- 查詢有2個字的名字
        select name from students where name like "__";

        -- 查詢有3個字的名字
        select name from students where name like "__";

        -- 查詢至少有2個字的名字
        select name from students where name like "__%";


        -- rlike 正則
        -- 查詢以 周開始的姓名
        select name from students where name rlike "^周.*";

        -- 查詢以 周開始、倫結尾的姓名
        select name from students where name rlike "^周.*倫$";


    -- 範圍查詢
        -- in (1, 3, 8)表示在一個非連續的範圍內
        -- 查詢 年齡為18、34的姓名
        select name,age from students where age=18 or age=34;
        select name,age from students where age=18 or age=34 or age=12;
        select name,age from students where age in (12, 18, 34);


        
        -- not in 不非連續的範圍之內
        -- 年齡不是 18、34歲之間的資訊
        select name,age from students where age not in (12, 18, 34);


        -- between ... and ...表示在一個連續的範圍內
        -- 查詢 年齡在18到34之間的的資訊
        select name, age from students where age between 18 and 34;

        
        -- not between ... and ...表示不在一個連續的範圍內
        -- 查詢 年齡不在在18到34之間的的資訊
        select * from students where age not between 18 and 34;
        select * from students where not age between 18 and 34;
        -- 失敗的select * from students where age not (between 18 and 34);


    -- 空判斷
        -- 判空is null
        -- 查詢身高為空的資訊
        select * from students where height is null;
        select * from students where height is NULL;
        select * from students where height is Null;

        -- 判非空is not null
        select * from students where height is not null;



-- 排序
    -- order by 欄位
    -- asc從小到大排列,即升序
    -- desc從大到小排序,即降序

    -- 查詢年齡在18到34歲之間的男性,按照年齡從小到到排序
    select * from students where (age between 18 and 34) and gender=1;
    select * from students where (age between 18 and 34) and gender=1 order by age;
    select * from students where (age between 18 and 34) and gender=1 order by age asc;


    -- 查詢年齡在18到34歲之間的女性,身高從高到矮排序
    select * from students where (age between 18 and 34) and gender=2 order by height desc;
    

    -- order by 多個欄位
    -- 查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序
;

    -- 查詢年齡在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;


-- 聚合函式
    -- 總數
    -- count
    -- 查詢男性有多少人,女性有多少人
    select * from students where gender=1;
    select count(*) from students where gender=1;
    select count(*) as 男性人數 from students where gender=1;
    select count(*) as 女性人數 from students where gender=2;

相關文章