主頁 > 資料庫 > MySQL資料庫進階版

MySQL資料庫進階版

2021-04-20 12:31:24 資料庫

MySQL資料庫進階版

  • MySQL資料庫進階版是在MySQL資料庫的基礎操作的基礎上推出的
  • MySQL資料庫的基礎操作文章鏈接:MySQL資料庫的基礎操作

一、資料庫的約束

  • 1.NULL約束

指定屬性的陳述句不能為NULL

//建表,準備作業
mysql> drop table if exists student;
Query OK, 0 rows affected (0.05 sec)

mysql> create table student(
->id int,
->sn int,
->name varchar(20) not null,
->qq_mail varchar(20)
->);
Query OK, 0 rows affected (0.15 sec)

mysql> show tables;
+-------------------------+
| Tables_in_huashanzhizai |
+-------------------------+
| student                 |
+-------------------------+
1 row in set (0.00 sec)

//此時準備往表里插入元素
mysql> insert into student(id,sn,name,qq_mail)
-> values(1,101,NULL,'123@qq.com');
ERROR 1048 (23000): Column 'name' cannot be null

//但是報錯了,原因如下
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| sn      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(20) | NO   |     | NULL    |       |
| qq_mail | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
//name這一列不能為NULL,否則報錯,這就是NULL約束
  • 2.唯一約束 unique

指定屬性的陳述句不能插入兩次

//建表,準備作業
mysql> drop table if exists student;

Query OK, 0 rows affected (0.02 sec)mysql> create table student(
-> id int,
-> sn int unique,
-> name varchar(20) NOT NULL,
-> qq_mail varchar(20)
-> );
Query OK, 0 rows affected (0.04 sec)

//添加第一條陳述句
mysql> insert into student(id,sn,name,qq_mail)
-> values(1,101,'bit','123@qq.com');
Query OK, 1 row affected (0.03 sec)

//添加后的效果
mysql> select * from student;
+------+------+------+------------+
| id   | sn   | name | qq_mail    |
+------+------+------+------------+
| 1    | 101  | bit  | 123@qq.com |
+------+------+------+------------+
1 row in set (0.00 sec)

//添加sn和第一條陳述句相同的第二條陳述句時:
mysql> insert into student(id,sn,name,qq_mail)
-> values(2,101,'bit2','1232@qq.com');
ERROR 1062 (23000): Duplicate entry '101' for key 'sn'
  • 3.默認值約束 default:

在宣告屬性的時候就賦值,如果后面添加成員變數,則系統會默認賦值

//建表,準備作業
mysql> drop table if exists student;
Query OK, 0 rows affected (0.03 sec)

mysql> create table student(
-> id int,
-> sn int unique,
-> name varchar(20) NOT NULL,
-> qq_mail varchar(20) default '110@qq.com'
-> );
Query OK, 0 rows affected (0.05 sec)

//第一次給qq_mail賦值的情況:
mysql> insert into student(id,sn,name,qq_mail)
-> values(1,101,'bit','123@qq.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+------+------+------+------------+
| id   | sn   | name | qq_mail    |
+------+------+------+------------+
| 1    | 101  | bit  | 123@qq.com |
+------+------+------+------------+
1 row in set (0.00 sec)

//第二次給qq_mail賦值為NULL的情況:
mysql> insert into student(id,sn,name,qq_mail)
-> values(2,102,'bit2',NULL);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+------+------+------+------------+
| id   | sn   | name | qq_mail    |
+------+------+------+------------+
| 1    | 101  | bit  | 123@qq.com |
| 2    | 102  | bit2 | NULL       |
+------+------+------+------------+
2 rows in set (0.00 sec)

//第三次不給qq_mail賦值的情況:
mysql> insert into student(id,sn,name)
-> values(3,103,'bit3');
Query OK, 1 row affected (0.02 sec)

mysql> select * from student;
+------+------+------+------------+
| id   | sn   | name | qq_mail    |
+------+------+------+------------+
| 1    | 101  | bit  | 123@qq.com |
| 2    | 102  | bit2 | NULL       |
| 3    | 103  | bit3 | 110@qq.com |
+------+------+------+------------+
3 rows in set (0.00 sec)
//發現不給qq_mail賦值時,系統會自動給qq_mail附上我們最開始設定的默認值
  • 4.主鍵約束 primary key

是NOT NULL 和 unique的結合, 也就是說,當一個欄位被primary key
修飾后,那么這個欄位就是不能為空且是獨一無二的!!! 一般搭配:auto_increment;

//建表,準備作業
mysql> drop table if exists student;
Query OK, 0 rows affected (0.02 sec)

mysql> create table student(
-> id int primary key auto_increment,
-> sn int unique,
-> name varchar(20) NOT NULL,
-> qq_mail varchar(20) DEFAULT '110@qq.com'
-> );
Query OK, 0 rows affected (0.04 sec)

//當創建好表之后,表中沒有任何的資料,當第一次執行插入的時候,當前主鍵,也就是ID,會自動從1開始
mysql> insert into student(sn,name,qq_mail)
-> values(101,'bit','123@qq.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+------------+
| id | sn   | name | qq_mail    |
+----+------+------+------------+
| 1  | 101  | bit  | 123@qq.com |
+----+------+------+------------+
1 row in set (0.00 sec)

//清除資料
mysql> delete from student where id = 1;
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
Empty set (0.00 sec)

//當我將剛剛插入的資料洗掉后,再次進行插入的時候,就會在原來的基礎,也就是上一次最后插入的陳述句的ID上開始加1
mysql> insert into student(sn,name,qq_mail)
-> values(101,'bit','123@qq.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+------------+
| id | sn   | name | qq_mail    |
+----+------+------+------------+
| 2  | 101  | bit  | 123@qq.com |
+----+------+------+------------+
1 row in set (0.00 sec)
//如果再想讓id=1,則需要洗掉表,重新來!!!
  • 5.外鍵約束 foreign key

外鍵用于關聯其他表的主鍵或唯一鍵

  • 語法:foreign key (欄位名) references 主表(列)
//1、建表
mysql> drop table if exists classes;
Query OK, 0 rows affected, 1 warning (0.00 sec)

//先建主表
mysql> create table classes(
-> id int primary key auto_increment,
-> name varchar(20),
-> `desc` varchar(30)
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> drop table if exists student;
Query OK, 0 rows affected, 1 warning (0.00 sec)

//再建附表
mysql> create table student(
-> id int PRIMARY KEY AUTO_INCREMENT,
-> sn int unique,
-> name varchar(20) NOT NULL,qq_mail varchar(20) DEFAULT '110@qq.com',
-> classes_id int,
-> foreign key (classes_id) references classes(id)
-> );
Query OK, 0 rows affected (0.05 sec)

//建表成功
mysql> show tables;
+-------------------------+
| Tables_in_huashanzhizai |
+-------------------------+
| classes				  |
| student				  |
+-------------------------+
2 rows in set (0.00 sec)

//2、插入(可以自增長)
//先插入主表
mysql> insert into classes (name,`desc`) values('java18','今天也要加油鴨!');
Query OK, 1 row affected (0.00 sec)

mysql> select * from classes;
+----+--------+--------------------------+
| id | name   | desc					 |
+----+--------+--------------------------+
| 1  | java18 | 今天也要加油鴨!			 |
+----+--------+--------------------------+
1 row in set (0.00 sec)

//再插入附表
mysql> insert into student (sn,name,qq_mail,classes_id) values
(101,'bit','123@qq.com',1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+------------+------------+
| id | sn   | name | qq_mail    | classes_id |
+----+------+------+------------+------------+
| 1  | 101  | bit  | 123@qq.com | 1          |
+----+------+------+------------+------------+
1 row in set (0.00 sec)

//附表再次插入
mysql> insert into student (sn,name,qq_mail,classes_id) values
(102,'bit','365@qq.com',1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+------------+------------+
| id | sn   | name | qq_mail    | classes_id |
+----+------+------+------------+------------+
| 1  | 101  | bit  | 123@qq.com | 1          |
| 2  | 102  | bit  | 365@qq.com | 1          |  //開始自增長
+----+------+------+------------+------------+
2 rows in set (0.00 sec)

//3、洗掉
//先洗掉子表中與主表想關聯的,或者洗掉沒有與子表關聯的主表
mysql> delete from student where id = 1 or id = 2;
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
Empty set (0.00 sec)

//再洗掉主表
mysql> delete from classes where id = 1;
Query OK, 1 row affected (0.01 sec)

mysql> select * from classes;
Empty set (0.00 sec)
  • 6.check約束
mysql> drop table if exists test_user;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table test_user (
-> id int,
-> name varchar(20),
-> sex varchar(1),
-> check (sex ='男' or sex='女') //表示在sex只能插入‘男’or‘女’,否則系統報錯
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+-------------------------+
| Tables_in_huashanzhizai |
+-------------------------+
| classes                 |
| student                 |
| test_user               |
+-------------------------+
3 rows in set (0.00 sec)

mysql> insert into test_user values(1,'bit','哈');
Query OK, 1 row affected (0.01 sec)
//但是在MySQL上并不會報錯!!!

二、進階查詢

建表,準備作業

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| huashanzhizai      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use huashanzhizai;
Database changed

mysql> show tables;
+-------------------------+
| Tables_in_huashanzhizai |
+-------------------------+
| classes                 |
| exam                    |
| student                 |
| test_user               |
+-------------------------+
4 rows in set (0.00 sec)

mysql> select * from exam;
Empty set (0.00 sec)

mysql> DROP TABLE IF EXISTS exam;
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TABLE exam (
-> id INT,
-> name VARCHAR(20),
-> chinese DECIMAL(3,1),
-> math DECIMAL(3,1),
-> english DECIMAL(3,1)
-> );
Query OK, 0 rows affected (0.10 sec)

mysql> INSERT INTO exam (id,name, chinese, math, english) VALUES
-> (1,'唐三藏', 67, 98, 56),
-> (2,'孫悟空', 87.5, 78, 77),-> (3,'豬悟能', 88, 98.5, 90),
-> (4,'曹孟德', 82, 84, 67),
-> (5,'劉玄德', 55.5, 85, 45),
-> (6,'孫權', 70, 73, 78.5),
-> (7,'宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> select * from exam;
+------+-----------+---------+------+---------+
| id   | name      | chinese | math | english |
+------+-----------+---------+------+---------+
| 1    | 唐三藏     | 67.0    | 98.0 | 56.0    |
| 2    | 孫悟空     | 87.5    | 78.0 | 77.0    |
| 3    | 豬悟能     | 88.0    | 98.5 | 90.0    |
| 4    | 曹孟德     | 82.0    | 84.0 | 67.0    |
| 5    | 劉玄德     | 55.5    | 85.0 | 45.0    |
| 6    | 孫權       | 70.0    | 73.0 | 78.5    |
| 7    | 宋公明     | 75.0    | 65.0 | 30.0    |
+------+-----------+---------+------+---------+
7 rows in set (0.00 sec)
  • 1.聚合函式
//1、查詢資料的數量
mysql> select count(*) from exam;
+----------+
| count(*) |
+----------+
| 7		   |
+----------+
1 row in set (0.01 sec)

mysql> select count(0) from exam;
+----------+
| count(0) |
+----------+
| 7        |
+----------+
1 row in set (0.00 sec)

mysql> select count(1) from exam;
+----------+
| count(1) |
+----------+
| 7        |
+----------+
1 row in set (0.00 sec)

//count(*)中*并不特指
//又例如:
mysql> insert into exam (id,name,chinese,math,english) values
-> (1,'唐三藏',67,98,56);
Query OK, 1 row affected (0.01 sec)

mysql> select count(*) from exam;
+----------+
| count(*) |
+----------+
| 8        |
+----------+
1 row in set (0.00 sec)

//2、對計數的列進行去重
mysql> select * from exam;
+------+-----------+---------+------+---------+
| id   | name      | chinese | math | english |
+------+-----------+---------+------+---------+
| 1    | 唐三藏     | 67.0    | 98.0 | 56.0    |
| 2    | 孫悟空     | 87.5    | 78.0 | 77.0    |
| 3    | 豬悟能     | 88.0    | 98.5 | 90.0    |
| 4    | 曹孟德     | 82.0    | 84.0 | 67.0    |
| 5    | 劉玄德     | 55.5    | 85.0 | 45.0    |
| 6    | 孫權       | 70.0    | 73.0 | 78.5    |
| 7    | 宋公明     | 75.0    | 65.0 | 30.0    |
| 1    | 唐三藏     | 67.0    | 98.0 | 56.0    |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)

mysql> select count(distinct id) from exam; **先去重再計數
+--------------------+
| count(distinct id) |
+--------------------+
| 7                  |
+--------------------+
1 row in set (0.01 sec)

//3、sum 加權求和
mysql> select sum(math) from exam;
+-----------+
| sum(math) |
+-----------+
| 679.5     |
+-----------+
1 row in set (0.00 sec)

mysql> select sum(math)from exam where math < 70;
+-----------+
| sum(math) |
+-----------+
| 65.0      |
+-----------+
1 row in set (0.00 sec)

//4、avg 查詢平均成績
mysql> select avg(math) from exam;
+-----------+
| avg(math) |
+-----------+
| 84.93750  |
+-----------+
1 row in set (0.00 sec)

//5、max 查詢最大值
mysql> select max(math) from exam;
+-----------+
| max(math) |
+-----------+
| 98.5      |
+-----------+
1 row in set (0.00 sec)

//6、min 查詢最小值
mysql> select min(math) from exam;
+-----------+
| min(math) |
+-----------+
| 65.0      |
+-----------+
1 row in set (0.00 sec)

//注意點:在where后面,不要出現聚合函式
mysql> select id,name from exam where max(math) > 60;
ERROR 1111 (HY000): Invalid use of group function
  • 2.分組 group by
    準備資料
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| huashanzhizai      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use huashanzhizai;
Database changed

mysql> drop tables if exists emp;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> create table emp(
-> id int primary key auto_increment,
-> name varchar(20) not null,
-> role varchar(20) not null,
-> salary numeric(11,2)
-> );
Query OK, 0 rows affected (0.08 sec)

mysql> insert into emp(name, role, salary) values
-> ('鵬哥','講師', 1000.20),
-> ('高博','講師', 2000.99),
-> ('老湯','講師', 999.11),
-> ('靜靜','班主任', 333.5),
-> ('莎莎姐','班主任', 700.33),
-> ('隔壁老王','市場', 12000.66);
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> select * from emp;
+----+--------------+-----------+
| id | name | role  | salary    |       
+----+--------------+-----------+
| 1  | 鵬哥 | 講師   | 1000.20   |
| 2  | 高博 | 講師   | 2000.99   |
| 3  | 老湯 | 講師   | 999.11    |
| 4  | 靜靜 | 班主任 | 333.50    |
| 5  | 莎莎姐 | 班主任 | 700.33  |
| 6 | 隔壁老王 | 市場 | 12000.66 |
+----+--------------+----------+
6 rows in set (0.00 sec)

查詢每個角色的最高工資、最低工資和平均工資

mysql> select role,max(salary),min(salary),avg(salary) from emp group by role;
+-----------+-------------+-------------+--------------+
| role      | max(salary) | min(salary) | avg(salary)  |
+-----------+-------------+-------------+--------------+
| 市場       | 12000.66    | 12000.66    | 12000.660000 |
| 班主任     | 700.33      | 333.50      | 516.915000   | //同為班主任,這個類的最高,最低,平均
| 講師       | 2000.99     | 999.11      | 1333.433333  |
+-----------+-------------+-------------+--------------+
3 rows in set (0.03 sec)
//不正確表達
//區別
mysql> select max(salary),min(salary),avg(salary)from emp;
+-------------+-------------+-------------+
| max(salary) | min(salary) | avg(salary) |
+-------------+-------------+-------------+
| 12000.66    | 333.50      | 2839.131667 | //這個是在一個表中的最高,最低,平均
+-------------+-------------+-------------+
1 row in set (0.00 sec)
  • 過濾條件 having
mysql> select role,max(salary),min(salary),avg(salary) from emp group by role
having avg(salary) < 1500;
+-----------+-------------+-------------+-------------+
| role      | max(salary) | min(salary) | avg(salary) |
+-----------+-------------+-------------+-------------+
| 班主任     | 700.33      | 333.50      | 516.915000  |
| 講師       | 2000.99     | 999.11      | 1333.433333 |
+-----------+-------------+-------------+-------------+
2 rows in set (0.00 sec)
  • 3.聯合查詢

( 兩張表或者兩張以上的表,進行連接查詢)

準備作業

mysql> drop database if exists test0311;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create database test0311;
Query OK, 1 row affected (0.01 sec)

mysql> use test0311;
Database changed

mysql> drop table if exists classes;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table classes(
-> id int primary key auto_increment,
-> name varchar(50),
-> `desc` varchar(50)
-> );
Query OK, 0 rows affected (0.07 sec)

mysql> insert into classes(name, `desc`) values
-> ('計算機系2019級1班', '學習了計算機原理、C和Java語言、資料結構和演算法'),
-> ('中文系2019級3班','學習了中國傳統文學'),
-> ('自動化2019級5班','學習了機械自動化');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> drop table if exists student;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table student(
-> id int primary key auto_increment,
-> sn int,
-> name varchar(30),
-> qq_mail varchar(30),
-> classes_id int
-> );
Query OK, 0 rows affected (0.09 sec)

mysql> insert into student(sn, name, qq_mail, classes_id) values
-> ('09982','黑旋風李逵','xuanfeng@qq.com',1),
-> ('00835','菩提老祖',null,1),
-> ('00391','白素貞',null,1),
-> ('00031','許仙','xuxian@qq.com',1),
-> ('00054','不想畢業',null,1),
-> ('51234','好好說話','say@qq.com',2),
-> ('83223','tellme',null,2),
-> ('09527','老外學中文','foreigner@qq.com',2);
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0

mysql> drop table if exists course;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table course(
-> id int primary key auto_increment,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.05 sec)

mysql> insert into course(name) values
-> ('Java'),('中國傳統文化'),('計算機原理'),('語文'),('高階數學'),('英文');
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> drop table if exists score;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table score(
-> id int primary key auto_increment,
-> score DECIMAL,
-> student_id int,
-> course_id int
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> insert into score(score, student_id, course_id) values
-> -- 黑旋風李逵
-> (70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),
-> -- 菩提老祖-> (60, 2, 1),(59.5, 2, 5),
-> -- 白素貞
-> (33, 3, 1),(68, 3, 3),(99, 3, 5),
-> -- 許仙
-> (67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),
-> -- 不想畢業
-> (81, 5, 1),(37, 5, 5),
-> -- 好好說話
-> (56, 6, 2),(43, 6, 4),(79, 6, 6),
-> -- tellme
-> (80, 7, 2),(92, 7, 6);
Query OK, 20 rows affected, 3 warnings (0.01 sec)
Records: 20 Duplicates: 0 Warnings: 3

//查看已建好的表
mysql> show tables;
+--------------------+
| Tables_in_test0311 |
+--------------------+
| classes            |
| course             |
| score              |
| student            |
+--------------------+
4 rows in set (0.00 sec)
mysql> select * from classes;
+----+-------------------------+-------------------------------------------------------------------+
| id | name                    |desc 															   |
+----+-------------------------+-------------------------------------------------------------------+
| 1 | 計算機系20191| 學習了計算機原理、C和Java語言、資料結構和演算法						   |
| 2 | 中文系20193| 學習了中國傳統文學												   |
| 3 | 自動化20195| 學習了機械自動化												   |
+----+-------------------------+-------------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql> select * from course;
+----+--------------------+
| id | name               |
+----+--------------------+
| 1  | Java               |
| 2  | 中國傳統文化        |
| 3  | 計算機原理          |
| 4  | 語文               |
| 5  | 高階數學            |
| 6  | 英文                |
+----+--------------------+
6 rows in set (0.00 sec)
mysql> select * from score;
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 1  | 71    | 1          | 1         |
| 2  | 99    | 1          | 3         |
| 3  | 33    | 1          | 5         |
| 4  | 98    | 1          | 6         |
| 5  | 60    | 2          | 1         |
| 6  | 60    | 2          | 5         |
| 7  | 33    | 3          | 1         |
| 8  | 68    | 3          | 3         |
| 9  | 99    | 3          | 5         |
| 10 | 67    | 4          | 1         |
| 11 | 23    | 4          | 3         |
| 12 | 56    | 4          | 5         |
| 13 | 72    | 4          | 6         |
| 14 | 81    | 5          | 1         |
| 15 | 37    | 5          | 5         |
| 16 | 56    | 6          | 2         |
| 17 | 43    | 6          | 4         |
| 18 | 79    | 6          | 6         |
| 19 | 80    | 7          | 2         |
| 20 | 92    | 7          | 6         |
+----+-------+------------+-----------+
20 rows in set (0.00 sec)

mysql> select * from student;
+----+-------+-----------------+------------------+------------+
| id | sn    | name            | qq_mail          | classes_id |
+----+-------+-----------------+------------------+------------+
| 1  | 9982  | 黑旋風李逵        | xuanfeng@qq.com | 1         |
| 2  | 835   | 菩提老祖         | NULL             | 1         |
| 3  | 391   | 白素貞           | NULL             | 1         |
| 4  | 31    | 許仙            | xuxian@qq.com     | 1         |
| 5  | 54    | 不想畢業         | NULL             | 1         |
| 6  | 51234 | 好好說話         | say@qq.com       | 2         |
| 7  | 83223 | tellme          | NULL             | 2         |
| 8  | 9527  | 老外學中文       | foreigner@qq.com | 2         |
+----+-------+-----------------+------------------+------------+
8 rows in set (0.00 sec)

內連接

語法1:select 欄位 from 表1 別名1 [inner] join 表2 別名2 on 連接條件 and 其他條件;
語法2:select 欄位 from 表1 別名1,表2 別名2 where 連接條件 and 其他條 件;

查詢“許仙”同學的 成績(逐漸增加約束條件)

語法1:

//查詢student和score兩個表所有的笛卡爾積
mysql> select * from student inner join score;
...//太多,就不一一展示了
...
...
160 rows in set (0.01 sec)

//顯示student表中id和score表中student_id兩個相等的對應的值
mysql> select * from student inner join score on student.id = score.student_id;
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
| id | sn    | name            | qq_mail         | classes_id | id | score |student_id  | course_id |
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
| 1  | 9982  | 黑旋風李逵 	   | xuanfeng@qq.com | 1		  | 1  | 71    | 1  		| 1 		|
| 1  | 9982  | 黑旋風李逵 	   | xuanfeng@qq.com | 1          | 2  | 99    | 1  		| 3			|
| 1  | 9982  | 黑旋風李逵	   | xuanfeng@qq.com | 1 		  | 3  | 33    | 1   		| 5  		|
| 1  | 9982  | 黑旋風李逵 	   | xuanfeng@qq.com | 1 		  | 4  | 98    | 1   		| 6  		|
| 2  | 835   | 菩提老祖 		   | NULL 			 | 1		  | 5  | 60    | 2   		| 1  		|
| 2  | 835   | 菩提老祖 		   | NULL 			 | 1 		  | 6  | 60    | 2   		| 5  		|
| 3  | 391   | 白素貞 		   | NULL		     | 1 		  | 7  | 33    | 3   		| 1  		|
| 3  | 391   | 白素貞 		   | NULL 			 | 1 		  | 8  | 68    | 3  		| 3  		|
| 3  | 391   | 白素貞		   | NULL 			 | 1		  | 9  | 99    | 3   	    | 5  		|
| 4  | 31    | 許仙 		   | xuxian@qq.com   | 1 		  | 10 | 67    | 4  		| 1  		|
| 4  | 31    | 許仙 		   | xuxian@qq.com   | 1		  | 11 | 23    | 4  		| 3  		|
| 4  | 31    | 許仙			   | xuxian@qq.com   | 1 		  | 12 | 56    | 4 		    | 5  		|
| 4  | 31    | 許仙			   | xuxian@qq.com   | 1		  | 13 | 72    | 4 		    | 6  		|
| 5  | 54    | 不想畢業		   | NULL			 | 1		  | 14 | 81    | 5  		| 1  		| 
| 5  | 54    | 不想畢業		   | NULL 			 | 1 		  | 15 | 37    | 5  		| 5  		|
| 6  | 51234 | 好好說話		   | say@qq.com 	 | 2		  | 16 | 56	   | 6  		| 2  		|
| 6  | 51234 | 好好說話 		   | say@qq.com 	 | 2 		  | 17 | 43    | 6  		| 4  		|
| 6  | 51234 | 好好說話 		   | say@qq.com 	 | 2 		  | 18 | 79    | 6  		| 6  		|
| 7  | 83223 | tellme  	 	   | NULL			 | 2 		  | 19 | 80    | 7  		| 2  		|
| 7  | 83223 | tellme 		   | NULL			 | 2 		  | 20 | 92    | 7  		| 6  		|
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
20 rows in set (0.01 sec)

//顯示student和score兩個表的所有內容
mysql> select * from student inner join score on student.id = score.student_id
and student.id = 4;
+----+------+--------+---------------+------------+----+-------+------------+-----------+
| id | sn   | name   | qq_mail       | classes_id | id | score | student_id |course_id  |
+----+------+--------+---------------+------------+----+-------+------------+-----------+
| 4  | 31   | 許仙   | xuxian@qq.com  | 1          | 10 | 67   | 4			| 1  		|
| 4  | 31   | 許仙   | xuxian@qq.com  | 1          | 11 | 23   | 4			| 3  		|
| 4  | 31   | 許仙   | xuxian@qq.com  | 1          | 12 | 56   | 4			| 5  		|
| 4  | 31   | 許仙   | xuxian@qq.com  | 1          | 13 | 72   | 4			| 6  		|
+----+------+--------+---------------+------------+----+-------+------------+-----------+
4 rows in set (0.01 sec)

//顯示student和score兩個表的選定的內容
mysql> select student.id,student.name,score.score from student inner join score
on student.id = score.student_id and student.id = 4;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
| 4  | 許仙   | 67    |
| 4  | 許仙   | 23    |
| 4  | 許仙   | 56    | 
| 4  | 許仙   | 72    |
+----+--------+-------+
4 rows in set (0.00 sec)

//用score表的course_id將course表也連接起來
mysql> select student.id,student.name,score.score,course.name from student inner
join score on student.id = score.student_id inner join course on score.course_id
= course.id and student.id = 4;
+----+--------+-------+-----------------+
| id | name   | score | name            |
+----+--------+-------+-----------------+
| 4  | 許仙   | 67     | Java 			|
| 4  | 許仙   | 23     | 計算機原理 		|
| 4  | 許仙   | 56     | 高階數學 		|
| 4  | 許仙   | 72     | 英文 			|
+----+--------+-------+-----------------+
4 rows in set (0.00 sec)

//修改名字
mysql> select student.id,student.name as 姓名,score.score as 分數,course.name as 課程表
from student inner join score on student.id = score.student_id inner join course
on score.course_id = course.id and student.id = 4;
+----+--------+--------+-----------------+
| id | 姓名    | 分數   | 課程表           |
+----+--------+--------+-----------------+
| 4  | 許仙    | 67    | Java 			 |
| 4  | 許仙    | 23    | 計算機原理 		 |
| 4  | 許仙    | 56    | 高階數學		 |
| 4  | 許仙    | 72    | 英文			 |
+----+--------+--------+-----------------+
4 rows in set (0.00 sec)

語法2:

mysql> select student.id,student.name as 姓名,score.score as 分數,course.name as 課程表
-> from student,score,course where student.id = score.student_id
-> and score.course_id = course.id
-> and student.id = 4;
+----+--------+--------+-----------------+
| id | 姓名   | 分數    | 課程表			 |
+----+--------+--------+-----------------+
| 4  | 許仙   | 67     | Java 			 |
| 4  | 許仙   | 23	   | 計算機原理		 |
| 4  | 許仙   | 56 	   | 高階數學 		 |
| 4  | 許仙   | 72 	   | 英文 			 |
+----+--------+--------+-----------------+
4 rows in set (0.00 sec)

外連接

( 外連接分為左外連接和右外連接,如果聯合查詢,左側的表完全顯示我們就說是左 外連接;右側的表完全顯示我們就說是右外連接)

查詢所有同學的成績,及同學的個人資訊,如果該同學沒有成績,也需要顯示

①左外連接

mysql> select * from student,score;
...
...//結果太多不一一展示
...
160 rows in set (0.01 sec)

//通過student表中的id進行排序
mysql> select * from student,score group by student.id;
+----+-------+-----------------+------------------+------------+----+-------+------------+-----------+
| id | sn    | name            | qq_mail 		  | classes_id | id | score |student_id  | course_id |
+----+-------+-----------------+------------------+------------+----+-------+------------+-----------+
| 1 | 9982   | 黑旋風李逵 	   | xuanfeng@qq.com  | 1 			| 1 | 71	| 1 		 | 1 		 |
| 2 | 835    | 菩提老祖 		   | NULL 			  | 1 			| 1 | 71	| 1			 | 1 	     |
| 3 | 391    | 白素貞 	       | NULL 			  | 1 			| 1 | 71	| 1 		 | 1 		 |
| 4 | 31     | 許仙 		   | xuxian@qq.com    | 1 			| 1 | 71	| 1 		 | 1 		 |
| 5 | 54     | 不想畢業 		   | NULL 			  | 1 			| 1 | 71	| 1 		 | 1 	     |
| 6 | 51234  |  好好說話        | say@qq.com		  | 2 			| 1 | 71	| 1 		 | 1 	 	 |
| 7 | 83223  | tellme		   | NULL 			  | 2 			| 1 | 71	| 1 		 | 1 		 |
| 8 | 9527   | 老外學中文	   | foreigner@qq.com | 2 			| 1 | 71	| 1 		 | 1 		 |
+----+-------+-----------------+------------------+------------+----+-------+------------+-----------+
8 rows in set (0.01 sec)

//此時發現student表中的name老外學中文這一項沒有了,即沒有score.student_id與之匹配
mysql> select * from student,score where student.id = score.student_id group by
student.id;
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
| id | sn    | name 		   | qq_mail 		 | classes_id | id | score |student_id  | course_id |
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
| 1  | 9982  | 黑旋風李逵	   | xuanfeng@qq.com | 1		  | 1  | 71    | 1 			| 1 		|
| 2  | 835   | 菩提老祖 		   | NULL			 | 1		  | 5  | 60		| 2			 | 1 		|
| 3  | 391   | 白素貞		   | NULL			 | 1 		  | 7  | 33		| 3			 | 1 		|
| 4  | 31    | 許仙 		   | xuxian@qq.com   | 1 		  | 10 | 67		| 4 		| 1 		|		
| 5  | 54    | 不想畢業		   | NULL 			 | 1 		  | 14 | 81		| 5 		| 1 		|
| 6  | 51234 | 好好說話 		   | say@qq.com      | 2 		  | 16 | 56		| 6 		| 2		    |
| 7  | 83223 | tellme		   | NULL 			 | 2		  | 19 | 80		| 7 		| 2 		|
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
7 rows in set (0.00 sec)//利用左外連接可以將其進行顯示

mysql> select * from student left join score on student.id = score.student_id
group by student.id;
+----+-------+-----------------+------------------+------------+------+-------+------------+-----------+
| id | sn    | name 		   | qq_mail		  | classes_id | id   | score |student_id  | course_id |
+----+-------+-----------------+------------------+------------+------+-------+------------+-----------+
| 1  | 9982  | 黑旋風李逵 	   | xuanfeng@qq.com  | 1 			| 1   | 71	  | 1 			| 1 		|
| 2  | 835   | 菩提老祖 		   | NULL 			  | 1 			| 5   | 60	  | 2 			| 1 		|
| 3  | 391   | 白素貞 		   | NULL 			  | 1 			| 7   | 33	  | 3 			| 1 		|
| 4  | 31    | 許仙 		   | xuxian@qq.com    | 1 			| 10  | 67	  | 4 			| 1 		|
| 5  | 54    | 不想畢業 		   | NULL 			  | 1 			| 14  | 81	  | 5 			| 1 		|
| 6  | 51234 | 好好說話 		   | say@qq.com       | 2			 | 16 | 56	  | 6 			| 2 		|
| 7  | 83223 | tellme		   | NULL 			  | 2 			 | 19 | 80	  | 7 			| 2 		|
| 8  | 9527  | 老外學中文 	   | foreigner@qq.com | 2 			| NULL | NULL | NULL 		| NULL 		|
+----+-------+-----------------+------------------+------------+------+-------+------------+-----------+
8 rows in set (0.00 sec)

②右外連接

mysql> select * from score right join student on student.id = score.student_id
group by student.id;
+------+-------+------------+-----------+----+-------+-----------------+------------------+------------+
| id   | score | student_id | course_id | id | sn    |  name 			|qq_mail 		  | classes_id |
+------+-------+------------+-----------+----+-------+-----------------+------------------+------------+
| 1    | 71 	| 1 		| 1 		| 1  | 9982  | 黑旋風李逵 		|xuanfeng@qq.com  | 1			|
| 5    | 60 	| 2 		| 1 		| 2  | 835   | 菩提老祖 			|NULL 			  | 1		   |
| 7    | 33 	| 3 		| 1 		| 3  | 391   | 白素貞			 |NULL 			  | 1 			|
| 10   | 67 	| 4 		| 1 		| 4  | 31    | 許仙 			|xuxian@qq.com    | 1 			|
| 14   | 81 	| 5 		| 1 		| 5  | 54    | 不想畢業 			|NULL 			  | 1 			|
| 16   | 56 	| 6 		| 2 		| 6  | 51234 | 好好說話 			|say@qq.com 	  | 2 			|
| 19   | 80 	| 7 		| 2 		| 7  | 83223 | tellme 			|NULL			  | 2 			|
| NULL | NULL   | NULL 	    | NULL 		| 8  | 9527  | 老外學中文 		|foreigner@qq.com | 2 			|
+------+-------+------------+-----------+----+-------+-----------------+------------------+------------+
8 rows in set (0.00 sec)

自連接 ( 自連接是指在同一張表連接自身進行查詢)

顯示所有“計算機原理”成績比“Java”成績高的成績資訊

mysql> select * from score as s1,score as s2 //這時候因為兩個表名字一樣,所以需要起別名
-> where s1.student_id = 1
-> and s2.student_id = 3
-> and s1.score < s2.score;
+----+-------+------------+-----------+----+-------+------------+-----------+
| id | score | student_id | course_id | id | score | student_id | course_id |
+----+-------+------------+-----------+----+-------+------------+-----------+
| 3  | 33 	 | 1 		  | 5		  | 8  | 68    | 3		    | 3 		|
| 1  | 71	 | 1 		  | 1 		  | 9  | 99    | 3 		    | 5 		|
| 3  | 33	 | 1 		  | 5 		  | 9  | 99    | 3		    | 5 		|
| 4  | 98 	 | 1		  | 6 		  | 9  | 99    | 3		    | 5 		|
+----+-------+------------+-----------+----+-------+------------+-----------+
4 rows in set (0.00 sec)

//改進
mysql> select s2.* from score as s1,score as s2
->where s1.student_id = 1
->and s2.student_id = 3
->and s1.score < s2.score;
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 8  | 68    | 3 		  | 3 		  |
| 9  | 99 	 | 3 		  | 5		  |
| 9  | 99	 | 3		  | 5		  |
| 9  | 99 	 | 3 		  | 5		  |
+----+-------+------------+-----------+
4 rows in set (0.00 sec)

子查詢( 子查詢是指嵌入在其他sql陳述句中的select陳述句,也叫嵌套查詢)

查詢與“不想畢業” 同學的同班同學

mysql> select * from student where classes_id = (select classes_id from student
where name = '不想畢業');
+----+------+-----------------+-----------------+------------+
| id | sn   | name 			  | qq_mail 		| classes_id |
+----+------+-----------------+-----------------+------------+
| 1  | 9982 | 黑旋風李逵 	  | xuanfeng@qq.com | 1 		 |
| 2  | 835  | 菩提老祖 		  | NULL 		    | 1 		 |
| 3  | 391  | 白素貞			  | NULL 			| 1			 |
| 4  | 31   | 許仙 			  | xuxian@qq.com   | 1 		 |
| 5  | 54   | 不想畢業		  | NULL 			| 1 		 |
+----+------+-----------------+-----------------+------------+
5 rows in set (0.01 sec)

//這句話等價于
mysql> select * from student where classes_id = 1;
+----+------+-----------------+-----------------+------------+
| id | sn   | name 			  | qq_mail 		| classes_id |
+----+------+-----------------+-----------------+------------+
| 1  | 9982 | 黑旋風李逵		  | xuanfeng@qq.com | 1 		 |
| 2  | 835  | 菩提老祖 		  | NULL 			| 1			 |
| 3  | 391  | 白素貞 		  | NULL		    | 1			 |
| 4  | 31   | 許仙 			  | xuxian@qq.com   | 1			 |
| 5  | 54   | 不想畢業 		  | NULL 		    | 1			 |
+----+------+-----------------+-----------------+------------+
5 rows in set (0.00 sec)

//多行子查詢:回傳多行記錄的子查詢,查詢“語文”或“英文”課程的成績資訊使用in
mysql> select * from score where course_id in(select id from course where name =
'語文' or name='英文');
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 4  | 98    | 1		  | 6 		  |
| 13 | 72    | 4 		  | 6		  |
| 17 | 43    | 6 		  | 4		  |
| 18 | 79    | 6		  | 6		  |
| 20 | 92    | 7		  | 6		  |
+----+-------+------------+-----------+
5 rows in set (0.00 sec)

//使用 not in
mysql> select * from score where course_id not in(select id from course where
name != '語文' and name !='英文');
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 4  | 98    | 1 		  | 6		  |
| 13 | 72    | 4 		  | 6		  |
| 17 | 43    | 6 	 	  | 4		  |
| 18 | 79    | 6 		  | 6		  |
| 20 | 92    | 7 		  | 6		  |
+----+-------+------------+-----------+
5 rows in set (0.00 sec)

//使用not exists(只要括號內的運算式為true,則就執行括號外的陳述句)
mysql> select * from student where exists(select id from student where id = 1);
+----+-------+-----------------+------------------+------------+
| id | sn    | name 		   | qq_mail		  | classes_id |
+----+-------+-----------------+------------------+------------+
| 1  | 9982  | 黑旋風李逵 	   | xuanfeng@qq.com  | 1 		   |
| 2  | 835   | 菩提老祖 		   | NULL 			  | 1		   |
| 3  | 391   | 白素貞		   | NULL 			  | 1 		   |
| 4  | 31    | 許仙			   | xuxian@qq.com    | 1 		   |
| 5  | 54    | 不想畢業		   | NULL 			  | 1 		   |
| 6  | 51234 | 好好說話 		   | say@qq.com       | 2 		   |
| 7  | 83223 | tellme 		   | NULL 		      | 2 		   |
| 8  | 9527  | 老外學中文 	   | foreigner@qq.com | 2 		   |
+----+-------+-----------------+------------------+------------+
8 rows in set (0.02 sec)

//聯合查詢(求交集)
mysql> select * from student where id <= 3
-> union
-> select * from student where name = '白素貞';
+----+------+-----------------+-----------------+------------+
| id | sn   | name 			  | qq_mail  		| classes_id |
+----+------+-----------------+-----------------+------------+
| 1  | 9982 | 黑旋風李逵		  | xuanfeng@qq.com | 1 		 |
| 2  | 835  | 菩提老祖 		  | NULL		    | 1 		 |
| 3  | 391  | 白素貞 		  | NULL 		    | 1			 |
+----+------+-----------------+-----------------+------------+
3 rows in set (0.01 sec)

mysql> select * from student where id <= 3
-> union
-> select * from student where name = '好好說話';
+----+-------+-----------------+-----------------+------------+
| id | sn    | name 		   | qq_mail  	     | classes_id |
+----+-------+-----------------+-----------------+------------+
| 1  | 9982  | 黑旋風李逵  	   | xuanfeng@qq.com | 1 		  |
| 2  | 835   | 菩提老祖 		   | NULL		     | 1 		  |
| 3  | 391   | 白素貞 		   | NULL			 | 1 		  |
| 6  | 51234 | 好好說話 		   | say@qq.com	     | 2		  |
+----+-------+-----------------+-----------------+------------+
4 rows in set (0.00 sec)

對于資料庫來說,用的最多的就是資料庫的查詢,即使在作業中,使用的最多的也將是資料庫的查詢,因為資料庫的添加和洗掉作業風險系數都會很高,同時參與的機會也不多,最后,希望大家再接再厲,

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/278146.html

標籤:其他

上一篇:DataGuard GAP修復的幾種方式(11G 12C 18C)

下一篇:求助:同一個賬戶不同的終端看到的表數量不一樣?

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • GPU虛擬機創建時間深度優化

    **?桔妹導讀:**GPU虛擬機實體創建速度慢是公有云面臨的普遍問題,由于通常情況下創建虛擬機屬于低頻操作而未引起業界的重視,實際生產中還是存在對GPU實體創建時間有苛刻要求的業務場景。本文將介紹滴滴云在解決該問題時的思路、方法、并展示最終的優化成果。 從公有云服務商那里購買過虛擬主機的資深用戶,一 ......

    uj5u.com 2020-09-10 06:09:13 more
  • 可編程網卡芯片在滴滴云網路的應用實踐

    **?桔妹導讀:**隨著云規模不斷擴大以及業務層面對延遲、帶寬的要求越來越高,采用DPDK 加速網路報文處理的方式在橫向縱向擴展都出現了局限性。可編程芯片成為業界熱點。本文主要講述了可編程網卡芯片在滴滴云網路中的應用實踐,遇到的問題、帶來的收益以及開源社區貢獻。 #1. 資料中心面臨的問題 隨著滴滴 ......

    uj5u.com 2020-09-10 06:10:21 more
  • 滴滴資料通道服務演進之路

    **?桔妹導讀:**滴滴資料通道引擎承載著全公司的資料同步,為下游實時和離線場景提供了必不可少的源資料。隨著任務量的不斷增加,資料通道的整體架構也隨之發生改變。本文介紹了滴滴資料通道的發展歷程,遇到的問題以及今后的規劃。 #1. 背景 資料,對于任何一家互聯網公司來說都是非常重要的資產,公司的大資料 ......

    uj5u.com 2020-09-10 06:11:05 more
  • 滴滴AI Labs斬獲國際機器翻譯大賽中譯英方向世界第三

    **桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......

    uj5u.com 2020-09-10 06:11:29 more
  • MPP (Massively Parallel Processing)大規模并行處理

    1、什么是mpp? MPP (Massively Parallel Processing),即大規模并行處理,在資料庫非共享集群中,每個節點都有獨立的磁盤存盤系統和記憶體系統,業務資料根據資料庫模型和應用特點劃分到各個節點上,每臺資料節點通過專用網路或者商業通用網路互相連接,彼此協同計算,作為整體提供 ......

    uj5u.com 2020-09-10 06:11:41 more
  • 滴滴資料倉庫指標體系建設實踐

    **桔妹導讀:**指標體系是什么?如何使用OSM模型和AARRR模型搭建指標體系?如何統一流程、規范化、工具化管理指標體系?本文會對建設的方法論結合滴滴資料指標體系建設實踐進行解答分析。 #1. 什么是指標體系 ##1.1 指標體系定義 指標體系是將零散單點的具有相互聯系的指標,系統化的組織起來,通 ......

    uj5u.com 2020-09-10 06:12:52 more
  • 單表千萬行資料庫 LIKE 搜索優化手記

    我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......

    uj5u.com 2020-09-10 06:13:25 more
  • 滴滴Ceph分布式存盤系統優化之鎖優化

    **桔妹導讀:**Ceph是國際知名的開源分布式存盤系統,在工業界和學術界都有著重要的影響。Ceph的架構和演算法設計發表在國際系統領域頂級會議OSDI、SOSP、SC等上。Ceph社區得到Red Hat、SUSE、Intel等大公司的大力支持。Ceph是國際云計算領域應用最廣泛的開源分布式存盤系統, ......

    uj5u.com 2020-09-10 06:14:51 more
  • es~通過ElasticsearchTemplate進行聚合~嵌套聚合

    之前寫過《es~通過ElasticsearchTemplate進行聚合操作》的文章,這一次主要寫一個嵌套的聚合,例如先對sex集合,再對desc聚合,最后再對age求和,共三層嵌套。 Aggregations的部分特性類似于SQL語言中的group by,avg,sum等函式,Aggregation ......

    uj5u.com 2020-09-10 06:14:59 more
  • 爬蟲日志監控 -- Elastc Stack(ELK)部署

    傻瓜式部署,只需替換IP與用戶 導讀: 現ELK四大組件分別為:Elasticsearch(核心)、logstash(處理)、filebeat(采集)、kibana(可視化) 下載均在https://www.elastic.co/cn/downloads/下tar包,各組件版本最好一致,配合fdm會 ......

    uj5u.com 2020-09-10 06:15:05 more
最新发布
  • day02-2-商鋪查詢快取

    功能02-商鋪查詢快取 3.商鋪詳情快取查詢 3.1什么是快取? 快取就是資料交換的緩沖區(稱作Cache),是存盤資料的臨時地方,一般讀寫性能較高。 快取的作用: 降低后端負載 提高讀寫效率,降低回應時間 快取的成本: 資料一致性成本 代碼維護成本 運維成本 3.2需求說明 如下,當我們點擊商店詳 ......

    uj5u.com 2023-04-20 08:33:24 more
  • MySQL中binlog備份腳本分享

    關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......

    uj5u.com 2023-04-20 08:28:06 more
  • day02-短信登錄

    功能實作02 2.功能01-短信登錄 2.1基于Session實作登錄 2.1.1思路分析 2.1.2代碼實作 2.1.2.1發送短信驗證碼 發送短信驗證碼: 發送驗證碼的介面為:http://127.0.0.1:8080/api/user/code?phone=xxxxx<手機號> 請求方式:PO ......

    uj5u.com 2023-04-20 08:27:27 more
  • 快取與資料庫雙寫一致性幾種策略分析

    本文將對幾種快取與資料庫保證資料一致性的使用方式進行分析。為保證高并發性能,以下分析場景不考慮執行的原子性及加鎖等強一致性要求的場景,僅追求最終一致性。 ......

    uj5u.com 2023-04-20 08:26:48 more
  • sql陳述句優化

    問題查找及措施 問題查找 需要找到具體的代碼,對其進行一對一優化,而非一直把關注點放在服務器和sql平臺 降低簡化每個事務中處理的問題,盡量不要讓一個事務拖太長的時間 例如檔案上傳時,應將檔案上傳這一步放在事務外面 微軟建議 4.啟動sql定時執行計劃 怎么啟動sqlserver代理服務-百度經驗 ......

    uj5u.com 2023-04-20 08:26:35 more
  • 云時代,MySQL到ClickHouse資料同步產品對比推薦

    ClickHouse 在執行分析查詢時的速度優勢很好的彌補了MySQL的不足,但是對于很多開發者和DBA來說,如何將MySQL穩定、高效、簡單的同步到 ClickHouse 卻很困難。本文對比了 NineData、MaterializeMySQL(ClickHouse自帶)、Bifrost 三款產品... ......

    uj5u.com 2023-04-20 08:26:29 more
  • sql陳述句優化

    問題查找及措施 問題查找 需要找到具體的代碼,對其進行一對一優化,而非一直把關注點放在服務器和sql平臺 降低簡化每個事務中處理的問題,盡量不要讓一個事務拖太長的時間 例如檔案上傳時,應將檔案上傳這一步放在事務外面 微軟建議 4.啟動sql定時執行計劃 怎么啟動sqlserver代理服務-百度經驗 ......

    uj5u.com 2023-04-20 08:25:13 more
  • Redis 報”OutOfDirectMemoryError“(堆外記憶體溢位)

    Redis 報錯“OutOfDirectMemoryError(堆外記憶體溢位) ”問題如下: 一、報錯資訊: 使用 Redis 的業務介面 ,產生 OutOfDirectMemoryError(堆外記憶體溢位),如圖: 格式化后的報錯資訊: { "timestamp": "2023-04-17 22: ......

    uj5u.com 2023-04-20 08:24:54 more
  • day02-2-商鋪查詢快取

    功能02-商鋪查詢快取 3.商鋪詳情快取查詢 3.1什么是快取? 快取就是資料交換的緩沖區(稱作Cache),是存盤資料的臨時地方,一般讀寫性能較高。 快取的作用: 降低后端負載 提高讀寫效率,降低回應時間 快取的成本: 資料一致性成本 代碼維護成本 運維成本 3.2需求說明 如下,當我們點擊商店詳 ......

    uj5u.com 2023-04-20 08:24:03 more
  • day02-短信登錄

    功能實作02 2.功能01-短信登錄 2.1基于Session實作登錄 2.1.1思路分析 2.1.2代碼實作 2.1.2.1發送短信驗證碼 發送短信驗證碼: 發送驗證碼的介面為:http://127.0.0.1:8080/api/user/code?phone=xxxxx<手機號> 請求方式:PO ......

    uj5u.com 2023-04-20 08:23:11 more