一、 数据库及对象
1、系统数据库
- infomation_schema: 存放数据库对象信息,表信息、列信息,权限信息
- performance_schema: 数据库服务器性能参数
- mysql: 用户权限信息
- sys : mysql5.7增加了sys 系统数据库,通过这个库可以快速的了解系统的元数据信息
2、操作
- 创建数据库 : create database learn;
- 查询数据库 : show databases;
- 选择数据库 : use learn;
- 删除数据库 : drop database learn;
二、 引擎与数据类型
1、引擎
查看引擎:show engines ; — show engines G
- Transactions: 是否支持事务
- XA: YES 分布式是否符合XA规范
- Savepoints: 是否支持事务处理中的保存点
2、数据类型
- 整数类型 :
- tinyint
- smallint
- mediumint
- int,integer
- bigint
- 浮点 :
- float
- double
- 定点数类型 :
- decimal->字符串存储
- dec
- 日期 :
- date 4字节
- datetime 8字节
- timestamp 4
- time 3
- year 1
- 字符串:
- char 4
- varchar 可变0~65535
- tinytext -> text -> mediumtext -> longtext
- tinyblob -> blob -> mediumblob -> longblob
三、 表操作
CREATE TABLE t_dept (
dept_no INT,
dept_name VARCHAR(40),
location VARCHAR(80)
);
##描述表结构
desc t_dept;
## 查看创建语句
show create table t_dept;
##删除表
drop table t_dept;
##修改表名
alter table t_dept rename to t_deptartment;
##增加字段
alter table t_deptartment add dept_name_us VARCHAR(40);
##第一个位置增加字段
alter table t_deptartment add first_col VARCHAR(40) first;
## 某列后面添加字段
alter table t_deptartment add after_dept_name VARCHAR(80) after dept_name;
## 删除字段
alter table t_deptartment drop first_col;
##修改字段 数据类型
alter TABLE t_deptartment MODIFY after_dept_name VARCHAR(100);
## 修改字段名字 可以一同修改字段属性
ALTER TABLE t_deptartment CHANGE after_dept_name dept_name_after VARCHAR(10);
四、约束
##创建表的时候直接加入约束
CREATE TABLE t_dept (
dept_no INT(20) PRIMARY KEY NOT NULL, ## 非空约束
type VARCHAR(20) DEFAULT 'Co., Ltd', ##默认值
dept_name VARCHAR(100) UNIQUE ##唯一值
);
desc t_dept ;
##创建有名字的约束值
CREATE TABLE t_dept (
dept_no INT(20) AUTO_INCREMENT, ##自增
type VARCHAR(20),
dept_name VARCHAR(100),
CONSTRAINT uk_dept_name UNIQUE (dept_name), ##唯一值
CONSTRAINT pk_dept_no PRIMARY KEY (dept_no) ##一个字段主键
);
## 联合主键
CREATE TABLE t_dept1 (
dept_no INT(20) AUTO_INCREMENT, ##自增
type VARCHAR(20),
dept_name VARCHAR(100), ##唯一值
CONSTRAINT uk_dept_name1 UNIQUE (dept_name), ##唯一值
CONSTRAINT pk_dept_no_name1 PRIMARY KEY (dept_no, dept_name) ##联合主键
);
#外键
CREATE TABLE t_employee (
emp_no int(30) PRIMARY KEY,
emp_name VARCHAR(20),
dept_no INT(20),
CONSTRAINT fk_dept_no FOREIGN KEY (dept_no) REFERENCES t_dept(dept_no)
);
desc t_employee;
五、索引
1.索引类型
普通索引,唯一索引,全文索引,单列索引,多列索引,空间索引
适用: 经常被查询的字段,分组字段,主键或者外键。唯一完整性约束
不适用: 很少查询;拥有许多重复值的字段
2.语句
#普通索引
CREATE TABLE t_dept_2 (
dept_no INT,
dept_name VARCHAR(20),
loc VARCHAR(40),
INDEX index_dept_no(dept_no)
);
SHOW CREATE TABLE t_dept_2;
## 查看解析计划
EXPLAIN SELECT *
FROM t_dept_2
WHERE dept_no = 1;
## 单独创建索引
CREATE INDEX idx_dept_name
ON t_dept_2 (dept_name);
ALTER TABLE t_dept_2
ADD INDEX idx_loc(loc DESC);
## 唯一索引
CREATE TABLE t_dept_3 (
dept_no INT UNIQUE,
UNIQUE INDEX idx_dept_no(dept_no)
);
CREATE UNIQUE INDEX idx_dept_name
ON t_dept_2 (dept_name);
ALTER TABLE t_dept_2
ADD UNIQUE INDEX idx_loc(loc);
##全文索引
####默认不区分大小写,只能在数据类型char,varchar,text上创建
CREATE TABLE t_dept_4 (
dept_no INT,
dept_name VARCHAR(20),
FULLTEXT INDEX idx_dept_no (dept_name)
);
CREATE FULLTEXT INDEX idx_dept_name
ON t_dept_2 (dept_name);
ALTER TABLE t_dept_2
ADD FULLTEXT INDEX idx_loc(loc);
##多列索引
CREATE TABLE t_dept_5 (
dept_no INT,
dept_name VARCHAR(20),
INDEX idx_dept_no (dept_no, dept_name)
);
CREATE INDEX idx_dept_name
ON t_dept_2 (dept_name);
ALTER TABLE t_dept_2
ADD INDEX idx_loc(loc);
##删除索引
DROP INDEX idx_loc
ON t_dept_2;
六、视图
###创建视图
CREATE VIEW view_dept_no AS
SELECT dept_no
FROM t_dept;
select * from t_dept;
insert into t_dept (dept_no, dept_name) VALUES (1,'ccdata');
select * from view_dept_no ;
#查看view
show tables;
show table status from learn like 'view_dept_no';
use information_schema;
select * from views where table_name = 'view_dept_no' \G;
##删除view
drop view view_dept_no;
##修改视图
CREATE OR REPLACE VIEW view_dept_name
AS
SELECT dept_name
FROM t_dept;
ALTER VIEW view_dept_name AS
SELECT
dept_no,
dept_name
FROM t_dept;
select * from view_dept_name;
SELECT *
FROM t_dept;
##对视图的操作
insert into view_dept_name VALUES (2,'apple');
UPDATE view_dept_name set dept_name = 'Google' WHERE dept_no = 2;
delete from view_dept_name WHERE dept_no = 1;
七、触发器
## 语句
CREATE TRIGGER TRIGGER_NAME
BEFORE | AFTER TRIGGER_EVENT
ON TABLE_NAME FOR EACH ROW TRIGGER_STMT
## before|after 制定触发器触发的时间
## trigger_event 触发的事件(条件)
## for each row 每行改变都触发
## trigger_stmt 触发的语句
##准备
CREATE TABLE department (
no INT(12),
name VARCHAR(40),
location VARCHAR(60)
);
CREATE TABLE diary (
diary_no INT(12) AUTO_INCREMENT PRIMARY KEY,
table_name VARCHAR(50),
diary_time DATETIME
);
## 创建触发器
CREATE TRIGGER trigger_diary_time
BEFORE INSERT ON department
FOR EACH ROW
INSERT INTO diary (table_name, diary_time) VALUES ('department', now());
## 测试
INSERT INTO department VALUES (1, 'apple', 'Peking');
SELECT * FROM diary;
## 触发器中包含多个语句
CREATE TRIGGER trigger_diary_time
BEFORE INSERT ON department
FOR EACH ROW
BEGIN
INSERT INTO diary (table_name, diary_time) VALUES ('department', now());
INSERT INTO diary (table_name, diary_time) VALUES ('department2', now());
END;
## 创建多个语句时候,书上说;可能被当做结束符?(没碰到这种情况)
DELIMITER $$
CREATE TRIGGER trigger_diary_time
BEFORE INSERT ON department
FOR EACH ROW
BEGIN
INSERT INTO diary (table_name, diary_time) VALUES ('department', now());
INSERT INTO diary (table_name, diary_time) VALUES ('department2', now());
END;
$$
DELIMITER ;
## 查看
SHOW TRIGGERS;
USE information_schema;
SELECT * FROM TRIGGERS;
##删除
DROP TRIGGER trigger_diary_time;
八、数据操作
##插入数据
INSERT INTO t_dept_2 (dept_no, dept_name, loc) VALUES ('1', 'apple', 'Peking');
INSERT INTO t_dept_2 (dept_no, dept_name) VALUES ('2', 'Google');
INSERT INTO t_dept_2 VALUES ('3', 'Amazon', 'USA');
## 插入多个值
INSERT INTO t_dept_2 VALUES
('4', 'Tencent', 'Guangzhou'),
('5', '网易', '杭州'),
('6', '饿了吗', '北京');
## 别的表转移
INSERT INTO t_dept_2 (dept_name)
SELECT dept_name
FROM t_deptartment;
##### 更新
UPDATE T_DEPT_2 SET LOC= '美国' WHERE DEPT_NAME = 'GOOGLE';
##### 删除
delete from t_dept_2 WHERE dept_no = 6;
九、记录查询
## 去重复
SELECT DISTINCT
DEPT_NAME,
LOC
FROM T_DEPT_2;
## 数学运算
CREATE TABLE employee (
no INT(10),
name VARCHAR(20),
salary DOUBLE(10, 2)
);
INSERT INTO employee VALUES
(1, 'a', 1234),
(2, 'b', 2500),
(3, 'c', 3600),
(4, 'd', 4500),
(5, 'e', 7800);
SELECT
name,
salary + 100,
salary - 200,
salary * 1.2,
salary / 1000,
salary % 1000
FROM employee;
关系运算符
运算符 | 描述 |
---|
| 大于
< | 小于
= | 等于
!=(<>) | 不等于
= | 大于等于
<= | 小于等于
逻辑运算符
运算符 | 描述 | ||
---|---|---|---|
AND (&&) | 与 | ||
OR (` | `) | 或 | |
XOR | 异或 | ||
NOT(!) | 非 |
## 两侧都包括
select * from employee WHERE salary BETWEEN 2500 and 4500;
## 空值判断
select * from employee where salary is null;
## in 语句
select * from employee WHERE name in ('a','b');
## like 语句
select * from employee WHERE salary like '4%';
select * from employee WHERE salary like '4%00';
## 这样查不出来 why? (double 不太一样?)
select * from employee WHERE salary like '4_00';
select * from employee WHERE name like '_bc';
## 排序
select * from employee order by salary ;
select * from employee order by salary DESC;
SELECT * FROM EMPLOYEE ORDER BY SALARY DESC ,NO;
## 限制查询条数
select * from employee LIMIT 2;
## 指定初始位置 ,显示条猪
select * from employee LIMIT 2,4;
统计函数
select count(*) from employee;
select count(1) from employee;
select count(salary) from employee; -- 将忽略null值,不参与计数
## 平均值,null值既不计入和值也不计入 个数
select avg(salary) from employee;
select sum(salary) from employee;
select max(salary) from employee;
select min(salary) from employee;
## 分组
SELECT
substr(salary, 1, 1),
count(*)
FROM employee
GROUP BY substr(salary, 1, 1);
SELECT
substr(salary, 1, 1),
count(*)
FROM employee
GROUP BY substr(salary, 1, 1)
HAVING count(*) > 1;
十、多表查询
## 内连接
SELECT
a.name,
b.name,
a.salary
FROM employee a
JOIN employee b ON a.salary = b.salary AND a.name != b.name;
SELECT
a.name,
b.name,
a.salary
FROM employee a, employee b
WHERE a.salary = b.salary
AND a.name != b.name;
## 外链接
### 左外联结 left join on
### 右外联结 right join on
### 全外联结 full join on
select * from employee;
select * from t_dept_2 ;
select * from t_dept_2 a LEFT JOIN employee b on a.dept_no = b.dept_no;
select * from t_dept_2 a RIGHT JOIN employee b on a.dept_no = b.dept_no;
##### mysql 不支持。。。 select * from t_dept_2 a FULL JOIN
## 合并
#### union 合并重复记录
SELECT DEPT_NO FROM T_DEPT_2
UNION
SELECT NO FROM EMPLOYEE;
SELECT DEPT_NO FROM T_DEPT_2
UNION ALL
SELECT NO FROM EMPLOYEE;
## 子查询
SELECT *
FROM employee
WHERE (no, dept_no) IN (
SELECT
no,
dept_no
FROM employee
WHERE salary > 4000);
##any
## 类似于 in
select * from employee WHERE salary = ANY (select salary from employee WHERE dept_no = 2);
## 大于最小值
select * from employee WHERE salary > ANY (select salary from employee WHERE dept_no = 2);
## 小于最大值
select * from employee WHERE salary < ANY (select salary from employee WHERE dept_no = 2);
##all
## 大于最大值
select * from employee WHERE salary > ALL (select salary from employee WHERE dept_no = 2);
## 小于最小值
select * from employee WHERE salary < ALL (select salary from employee WHERE dept_no = 2);
## exists
SELECT *
FROM employee a
WHERE exists(
SELECT *
FROM t_dept_2 b
WHERE a.dept_no = b.dept_no
);
## not exists
SELECT *
FROM employee a
WHERE NOT exists(
SELECT *
FROM t_dept_2 b
WHERE a.dept_no = b.dept_no
);
十一、运算符
## 算数运算符
SELECT
6 + 4,
6 - 4,
6 / 2,
6 DIV 2,
6 % 4,
6 MOD 4;
## 结果: 10,2,3.0000,3,2,2
## Why?
### The MySQL DIV function is used for integer division where n is divided by m and an integer value is returned.