MySQL数据库基础02
- sql表查询增强
-
- 多表查询
-
- 问题的引出
- 多表查询的练习
- 自连接
- 子查询
-
- 子查询当作临时表使用
- 在多行子查询中使用all操作符
- 在多行子查询中使用any操作符
- 多列子查询
- 子查询练习
-
- 表复制
- 合并查询
- 外连接
-
- mysql约束
-
- primary key(主键)-基本使用
- not null的使用
- unique的使用
- foreign key(外键)
- check的使用
- mysql约束练习
- 自增长
- mysql索引
-
- 索引的快速入门
- 索引的原理
- 索引的类型
- 索引的使用
-
- 索引小结
sql表查询增强
SELECT * FROM db_02.`emp`;
SELECT * FROM db_02.`dept`;
SELECT * FROM db_02.`salgrade`;
SELECT * FROM db_02.`emp` a
WHERE a.`hiredate` > '1992-01-01';
SELECT a.`ename`,a.`sal` FROM db_02.`emp` a
WHERE a.`ename` LIKE 'S%';
SELECT a.`ename`,a.`sal` FROM db_02.`emp` a
WHERE SUBSTRING(a.`ename`,3,1) = 'O';
SELECT a.`ename`,a.`sal` FROM db_02.`emp` a
WHERE a.`ename` LIKE '__O%';
SELECT * FROM db_02.`emp` a
WHERE a.`mgr` IS NULL;
DESC db_02.`emp`;
SELECT * FROM db_02.`emp` a
ORDER BY a.`sal`;
SELECT * FROM db_02.`emp` a
ORDER BY a.`deptno`, sal DESC;
分页查询
SELECT * FROM db_02.emp a ORDER BY a.`empno`
LIMIT 0,3;
SELECT * FROM db_02.emp a ORDER BY a.`empno`
LIMIT 3,3;
SELECT * FROM db_02.emp a ORDER BY a.`empno`
LIMIT 6,3;
SELECT * FROM db_02.emp a ORDER BY a.`empno`
LIMIT 9,3;
SELECT * FROM db_02.emp a ORDER BY a.`empno`
LIMIT 12,3;
SELECT * FROM db_02.emp a ORDER BY a.`empno`
LIMIT 12,3;
SELECT * FROM db_02.emp a ORDER BY a.`empno`
LIMIT 每页显示的记录数 * (第几页 - 1),每页显示的记录数;
SELECT * FROM db_02.`emp` a
ORDER BY a.`empno` DESC
LIMIT 10,5;
SELECT * FROM db_02.`emp` a
ORDER BY a.`empno` DESC
LIMIT 20,5;
分组函数和分组子句
SELECT
a.`job`,
SUM(1) zs,
AVG(a.`sal`)
FROM
db_02.`emp` a
GROUP BY a.`job` ;
SELECT * FROM db_02.`emp` a;
SELECT
COUNT(*) gyzs,
(SELECT
COUNT(*)
FROM
db_02.`emp` a
WHERE a.`comm` IS NOT NULL) bzgys
FROM
db_02.`emp` a ;
SELECT COUNT(*) gyzs,COUNT(a.`comm`) FROM db_02.`emp` a;
SELECT COUNT(*) gyzs,COUNT(IF(a.`comm` IS NULL,1,NULL)) FROM db_02.`emp` a;
SELECT COUNT(*) - COUNT(a.comm) FROM db_02.`emp` a;
SELECT COUNT(DISTINCT a.`mgr`) FROM db_02.`emp` a;
SELECT MAX(a.`sal`) - MIN(a.sal) FROM db_02.`emp` a
数据分组的总结
SELECT
a.`deptno`,
b.`dname`,
ROUND(AVG(a.`sal`),2) avg_sal
FROM
db_02.`emp` a,
db_02.`dept` b
WHERE a.`deptno` = b.`deptno`
GROUP BY a.`deptno`,b.`dname`
HAVING avg_sal > 1000
ORDER BY avg_sal DESC
LIMIT 0,2;
多表查询
问题的引出
多表查询的练习
直接拼接查询两张表的结果(笛卡尔集)
SELECT * FROM db_02.`emp`;
SELECT * FROM db_02.`dept`;
SELECT * FROM db_02.`salgrade`;
SELECT
a.`ename`,
a.`sal`,
b.`dname` ,
a.`deptno`
FROM
db_02.`emp` a,
db_02.`dept` b
WHERE a.`deptno` = b.`deptno` ;
SELECT
b.`dname`,
a.`ename`,
a.`sal`,
a.`deptno`
FROM
db_02.`emp` a,
db_02.`dept` b
WHERE b.`deptno` = 10
AND a.`deptno` = b.`deptno`;
DESC db_02.`emp`;
DESC db_02.`dept`;
SELECT
a.ename,
a.sal,
b.`grade`
FROM
db_02.`emp` a,
db_02.`salgrade` b
WHERE a.`sal`BETWEEN b.`losal` AND b.`hisal`;
SELECT
a.`ename`,
a.`sal`,
b.`dname`
FROM
db_02.`emp` a,
db_02.`dept` b
WHERE a.`deptno` = b.`deptno`
ORDER BY b.`dname` DESC ;
自连接
SELECT
a.`ename`,
a.`empno`,
a.`mgr`,
b.`ename` sjxm,
b.`empno` sj
FROM
db_02.`emp` a,
db_02.`emp` b
WHERE a.`mgr` = b.`empno` ;
SELECT * FROM db_02.`emp`;
子查询
SELECT * FROM db_02.`emp`;
SELECT
*
FROM
db_02.`emp` b
WHERE b.`deptno` IN
(SELECT
a.`deptno`
FROM
db_02.`emp` a
WHERE a.`ename` = 'SMITH') ;
SELECT
b.`ename`,
b.`job`,
b.`sal`,
b.`deptno`
FROM
db_02.`emp` b
WHERE b.`job` IN
(SELECT
DISTINCT a.`job`
FROM
db_02.`emp` a
WHERE a.`deptno` = 10)
AND b.`deptno` <> 10 ;
子查询当作临时表使用
SELECT
a.goods_id,
a.cat_id,
a.goods_name,
a.shop_price
FROM
ecs_goods a ;
DESC ecs_goods;
SELECT
a.cat_id,
MAX(a.shop_price)
FROM
ecs_goods a
GROUP BY a.cat_id ;
SELECT
c.goods_id,
c.cat_id,
c.goods_name,
c.shop_price
FROM
ecs_goods c,
(SELECT
a.cat_id,
MAX(a.shop_price) max_price
FROM
ecs_goods a
GROUP BY a.cat_id) b
WHERE c.cat_id = b.cat_id
AND b.max_price = c.shop_price ;
在多行子查询中使用all操作符
SELECT
c.`ename`,
c.`sal`,
c.`deptno`
FROM
db_02.`emp` c,
(SELECT
MAX(a.`sal`) maxsal
FROM
db_02.`emp` a
WHERE a.`deptno` = 30) b
WHERE c.sal > b.maxsal ;
SELECT
b.`ename`,
b.`sal`,
b.`deptno`
FROM
db_02.`emp` b
WHERE b.`sal` > ALL
(SELECT
a.sal
FROM
db_02.`emp` a
WHERE a.`deptno` = 30) ;
在多行子查询中使用any操作符
SELECT
b.ename,
b.`sal`,
b.`deptno`
FROM
db_02.`emp` b
WHERE b.`sal` >
(SELECT
MIN(a.`sal`)
FROM
db_02.`emp` a
WHERE a.`deptno` = 30);
SELECT
b.ename,
b.`sal`,
b.`deptno`
FROM
db_02.`emp` b
WHERE b.`sal` > ANY
(SELECT a.`sal`
FROM
db_02.`emp` a
WHERE a.`deptno` = 30);
多列子查询
SELECT * FROM db_02.`emp` b
WHERE (b.`deptno`,b.`job`) =
(SELECT a.`deptno`,a.`job` FROM db_02.`emp` a
WHERE a.`ename` = 'SMITH')
AND b.`ename` <> 'SMITH';
SELECT * FROM db_02.`emp` b
WHERE (b.`deptno`,b.`job`) =
(SELECT a.`deptno`,a.`job` FROM db_02.`emp` a
WHERE a.`ename` = 'ALLEN')
AND b.`ename` <> 'ALLEN';
SELECT
*
FROM
db_02.`student` a
WHERE (
a.`chinese`,
a.`english`,
a.`math`
) =
(SELECT
b.`chinese`,
b.`english`,
b.`math`
FROM
db_02.`student` b
WHERE b.`name` = '宋江');
子查询练习
from子句中使用子查询
SELECT
b.*
FROM
db_02.emp b,
(SELECT
AVG(a.`sal`) avg_sal,
a.`deptno`
FROM
db_02.emp a
GROUP BY a.`deptno`) c
WHERE b.`sal` > c.avg_sal
AND b.`deptno` = c.deptno ;
SELECT b.* FROM db_02.emp b,(
SELECT MAX(a.`sal`) max_sal,a.`deptno` FROM db_02.emp a
GROUP BY a.`deptno`) c
WHERE b.`sal` = c.max_sal
AND b.`deptno` = c.deptno;
SELECT
a.`dname`,
a.`deptno`,
a.`loc`,
c.rs
FROM
db_02.`dept` a,
(SELECT
b.deptno,
COUNT(*) rs
FROM
db_02.`emp` b
GROUP BY b.deptno) c
WHERE a.`deptno` = c.deptno ;
表复制
CREATE TABLE my_table01
(id INT,
NAME VARCHAR(32),
sal DOUBLE,
job VARCHAR(32),
deptno INT);
DESC my_table01;
INSERT INTO my_table01
(id,NAME,sal,job,deptno)
SELECT empno,ename,sal,job,deptno
FROM db_02.`emp`;
SELECT COUNT(*) FROM db_02.`my_table01`;
INSERT INTO my_table01
SELECT * FROM my_table01;
CREATE TABLE db_02.`my_table02` LIKE emp;
DESC db_02.`my_table02`;
SELECT * FROM db_02.`my_table02`;
INSERT INTO db_02.`my_table02`
SELECT * FROM db_02.`emp`;
SELECT * FROM db_02.`my_table02`;
INSERT INTO db_02.`my_table02`
SELECT * FROM db_02.`emp`;
SELECT * FROM db_02.`my_table02`;
CREATE TABLE db_02.temp LIKE db_02.`emp`;
INSERT INTO db_02.temp
SELECT DISTINCT * FROM db_02.`my_table02`;
SELECT * FROM db_02.temp;
DELETE FROM db_02.`my_table02`;
INSERT INTO db_02.`my_table02`
SELECT * FROM db_02.`temp`;
DROP TABLE db_02.`temp`;
合并查询
SELECT a.`ename`,a.`sal`,a.`job` FROM db_02.`emp` a WHERE a.`sal` > 2500
UNION ALL
SELECT b.`ename`,b.`sal`,b.`job` FROM db_02.`emp` b WHERE b.`job` = 'MANAGER';
SELECT a.`ename`,a.`sal`,a.`job` FROM db_02.`emp` a WHERE a.`sal` > 2500
UNION
SELECT b.`ename`,b.`sal`,b.`job` FROM db_02.`emp` b WHERE b.`job` = 'MANAGER';
外连接
CREATE TABLE stu (
id INT,
`name` VARCHAR(32));
INSERT INTO stu VALUES(1, 'jack'),(2,'tom'),(3, 'kity'),(4, 'nono');
SELECT * FROM stu;
CREATE TABLE exam(
id INT,
grade INT);
INSERT INTO exam VALUES(1, 56),(2,76),(11, 8);
SELECT * FROM exam;
SELECT * FROM stu LEFT JOIN exam
ON stu.`id` = exam.`id`
ORDER BY stu.id;
SELECT * FROM stu RIGHT JOIN exam
ON stu.`id` = exam.`id`
ORDER BY exam.id;
外连接课后习题
SELECT
a.`dname`,
b.`ename`,
b.`job`
FROM
db_02.`dept` a LEFT JOIN
db_02.`emp` b ON
a.`deptno` = b.`deptno`
ORDER BY a.`dname`;
SELECT
a.`ename`,
a.`job`,
b.dname
FROM
db_02.`emp` a RIGHT JOIN
db_02.`dept` b ON
a.`deptno` = b.`deptno`
ORDER BY b.`dname` ;
mysql约束
primary key(主键)-基本使用
CREATE TABLE t17
(`id` INT PRIMARY KEY,
`name` VARCHAR(32),
`email` VARCHAR(32)
);
INSERT INTO t17
VALUES(1,'jack','[email protected]');
INSERT INTO t17
VALUES(2,'tom','[email protected]');
INSERT INTO t17
VALUES(1,'hsp','[email protected]');
INSERT INTO t17
VALUES(NULL,'hsp','[email protected]');
CREATE TABLE t18
(`id` INT PRIMARY KEY,
`name` VARCHAR(32) PRIMARY KEY,
`email` VARCHAR(32)
);
CREATE TABLE t18
(`id` INT,
`name` VARCHAR(32) ,
`email` VARCHAR(32),
PRIMARY KEY (`id`,`name`)
) ;
INSERT INTO t18
VALUES(1,'tom','[email protected]');
SELECT * FROM t18;
INSERT INTO t18
VALUES(1,'jack','[email protected]');
INSERT INTO t18
VALUES(1,'tom','[email protected]');
CREATE TABLE t19
(`id` INT PRIMARY KEY,
`name` VARCHAR(32) ,
`email` VARCHAR(32)
) ;
CREATE TABLE t20
(`id` INT,
`name` VARCHAR(32) ,
`email` VARCHAR(32),
PRIMARY KEY(`id`)
) ;
DESC t20;
not null的使用
unique的使用
CREATE TABLE t21
(`id` INT UNIQUE,
`name` VARCHAR(32) ,
`email` VARCHAR(32)
) ;
INSERT INTO t21
VALUES(1,'jack','[email protected]');
INSERT INTO t21
VALUES(1,'tom','[email protected]');
INSERT INTO t21
VALUES(NULL,'tom','[email protected]');
INSERT INTO t21
VALUES(NULL,'jerry','[email protected]');
INSERT INTO t21
VALUES(NULL,'kear','[email protected]');
SELECT * FROM t21;
CREATE TABLE t22
(`id` INT UNIQUE,
`name` VARCHAR(32) UNIQUE,
`email` VARCHAR(32)
) ;
DESC t22;
foreign key(外键)
CREATE TABLE class
(id INT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL DEFAULT '');
DESC class;
CREATE TABLE classmate
(id INT PRIMARY KEY,
`NAME` VARCHAR(255) NOT NULL DEFAULT '',
classid INT,
FOREIGN KEY (classid) REFERENCES class(id)
);
DESC classmate;
INSERT INTO class
VALUES
(100,'Java');
INSERT INTO class
VALUES
(200,'Web');
SELECT * FROM class;
INSERT INTO classmate
VALUES
(1,'tom',100);
SELECT * FROM classmate;
INSERT INTO classmate
VALUES
(2,'tom',200);
INSERT INTO classmate
VALUES
(3,'Jerry',300);
INSERT INTO classmate
VALUES
(3,'Jerry',NULL);
SELECT * FROM classmate;
DESC classmate;
DELETE FROM class
WHERE id = 100;
check的使用
CREATE TABLE t23
(id INT PRIMARY KEY,
`name` VARCHAR(32),
`sex` CHAR(1) CHECK(sex IN ('男','女')),
`sal` DOUBLE CHECK(sal > 1000 AND sal < 5000)
);
INSERT INTO t23
VALUES
(1,'Jack','未',6000);
SELECT * FROM t23;
mysql约束练习
CREATE DATABASE shop_db
CHARACTER SET utf8
COLLATE utf8_bin;
CREATE TABLE goods
(`goods_id` INT,
`goods_name` VARCHAR(100),
`unitprice` DOUBLE CHECK(`unitprice` > 1.0 AND `unitprice` < 9999.99),
`category` VARCHAR(20),
`provider` VARCHAR(50),
PRIMARY KEY(`goods_id`)
) ENGINE INNODB;
CREATE TABLE customer
(`customer_id` INT PRIMARY KEY,
`name` VARCHAR(100) NOT NULL,
`address` VARCHAR(100),
`email` VARCHAR(50) UNIQUE,
`sex` ENUM('男','女') NOT NULL CHECK(`sex` IN ('男','女')),
`card_id` INT
)ENGINE INNODB;
CREATE TABLE purchase
(`order_id` INT UNSIGNED PRIMARY KEY,
`customer_id` INT,
`goods_id` INT,
`nums` INT NOT NULL DEFAULT 0,
FOREIGN KEY(`customer_id`) REFERENCES customer(`customer_id`),
FOREIGN KEY(`goods_id`) REFERENCES goods(`goods_id`)
)ENGINE INNODB;
自增长
CREATE TABLE t24
(`id` INT PRIMARY KEY AUTO_INCREMENT,
`email` VARCHAR(32) NOT NULL DEFAULT '',
`name` VARCHAR(32) NOT NULL DEFAULT '');
DESC t24;
INSERT INTO t24
VALUES(NULL,'[email protected]','jack');
SELECT * FROM t24;
INSERT INTO t24
VALUES(NULL,'[email protected]','tom');
INSERT INTO t24
(`email`,`name`)
VALUES('[email protected]','hsp');
CREATE TABLE t16
(`id` INT UNIQUE AUTO_INCREMENT,
`email` VARCHAR(32) NOT NULL DEFAULT '',
`name` VARCHAR(32) NOT NULL DEFAULT '');
CREATE TABLE t25
(`id` INT PRIMARY KEY AUTO_INCREMENT,
`email` VARCHAR(32) NOT NULL DEFAULT '',
`name` VARCHAR(32) NOT NULL DEFAULT '');
ALTER TABLE t25 AUTO_INCREMENT = 100;
INSERT INTO t25
VALUES(NULL,'[email protected]','hsp');
SELECT * FROM t25;.
INSERT INTO t25
VALUES(666,'[email protected]','hsp');
INSERT INTO t25
VALUES(NULL,'[email protected]','hsp');
mysql索引
索引的快速入门
SELECT * FROM emp a
WHERE a.empno = 1234567;
CREATE INDEX empno_index ON emp(empno);
SELECT * FROM emp a
WHERE a.empno = 1234578;
SELECT * FROM emp
WHERE ename = 'axJ';
CREATE INDEX ename_index ON emp(ename);
SELECT * FROM emp
WHERE ename = 'YcsDQt';
索引的原理
索引的类型
索引的使用
CREATE TABLE t26
(id INT,
`name` VARCHAR(32));
CREATE TABLE t03
(id INT PRIMARY KEY,
`name` VARCHAR(32));
SHOW INDEX FROM t26;
CREATE UNIQUE INDEX id_index ON t26(id);
CREATE INDEX id_index ON t01(id);
ALTER TABLE t02 ADD INDEX id_index (id);
ALTER TABLE t03 ADD PRIMARY KEY (id);
SHOW INDEX FROM t03;
SHOW INDEX FROM t26;
DROP INDEX id_index ON t26;
ALTER TABLE t01 DROP INDEX id_index;
SHOW INDEX FROM t01;
ALTER TABLE t03 DROP PRIMARY KEY;
SHOW INDEX FROM t25;
SHOW INDEXES FROM t25;
SHOW KEYS FROM t25;
DESC t25;
课后作业
CREATE TABLE order1
(`id` INT PRIMARY KEY,
`goods_name` VARCHAR(100),
`purchase` VARCHAR(50),
`num` INT);
CREATE TABLE order2
(`id` INT,
`goods_name` VARCHAR(100),
`purchase` VARCHAR(50),
`num` INT,
PRIMARY KEY (id)
);
CREATE TABLE order3
(`id` INT,
`goods_name` VARCHAR(100),
`purchase` VARCHAR(50),
`num` INT
);
ALTER TABLE order3 ADD PRIMARY KEY (id);
CREATE TABLE menu1
(`id` INT PRIMARY KEY,
`menu_name` VARCHAR(255),
`cook` VARCHAR(50),
`card` VARCHAR(20) UNIQUE,
`price` DOUBLE);
CREATE TABLE menu2
(`id` INT PRIMARY KEY,
`menu_name` VARCHAR(255),
`cook` VARCHAR(50),
`card` VARCHAR(20),
`price` DOUBLE);
CREATE UNIQUE INDEX card_index ON menu2(card);
CREATE TABLE menu3
(`id` INT PRIMARY KEY,
`menu_name` VARCHAR(255),
`cook` VARCHAR(50),
`card` VARCHAR(20),
`price` DOUBLE);
ALTER TABLE menu3 ADD UNIQUE INDEX (card);
CREATE TABLE sportsman1
(`id` INT PRIMARY KEY,
`name` VARCHAR(100),
`hobby` VARCHAR(100));
CREATE INDEX name_index ON sportsman1 (`hobby`);
CREATE TABLE sportsman2
(`id` INT PRIMARY KEY,
`name` VARCHAR(100),
`hobby` VARCHAR(100));
ALTER TABLE sportsman2 ADD INDEX (`hobby`);
SHOW INDEX FROM sportsman2;
索引小结