Java in the multi-table & Services

Multi-table query:

Preparation sql:
Creating a Department table

CREATE TABLE dept(
    id INT PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(20)
);
INSERT INTO dept (NAME) VALUES ('开发部'),('市场部'),('财务部');
# 创建员工表
CREATE TABLE emp (
    id INT PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(10),
    gender CHAR(1), -- 性别
    salary DOUBLE, -- 工资
    join_date DATE, -- 入职日期
    dept_id INT,
    FOREIGN KEY (dept_id) REFERENCES dept(id) -- 外键,关联部门表(部门表的主键)
);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('孙悟空','男',7200,'2013-02-24',1);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('猪八戒','男',3600,'2010-12-02',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('唐僧','男',9000,'2008-08-08',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('白骨精','女',5000,'2015-10-07',3);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('蜘蛛精','女',4500,'2011-03-14',1);

Cartesian Product:

Relations combination of all the data in the table. Cross-connect: Table 1 x Table II.
* The FROM emp the SELECT, the dept;
the SELECT * the FROM emp the CROSS JION the dept;
once we found the result set found in the case of Cartesian product, the SQL is because our conditions are incomplete
. There are two sets A, B two sets to take all the composition.
To complete the multi-table query, two tables of the product data will be useless, we want to eliminate unnecessary data
so we should be used: inside and outer joins.
Classification multi-table query:

Inner join query (do not write keyword, where the condition AND condition):

Implicit within the connector: eliminate redundant data using the conditions where

example:

-- 查询所有员工信息和对应的部门信息

SELECT * FROM emp,dept WHERE emp.`dept_id` = dept.`id`;

-- 查询员工表的名称,性别。部门表的名称
SELECT emp.name,emp.gender,dept.name FROM emp,dept WHERE emp.`dept_id` = dept.`id`;

SELECT 
    t1.name, -- 员工表的姓名
    t1.gender,-- 员工表的性别
    t2.name -- 部门表的名称
FROM
    emp t1,
    dept t2
WHERE 
    t1.`dept_id` = t2.`id`;

Explicit inner join (write keyword inner join table on conditions):

Syntax: select field list from table 1 [inner] join condition table 2 on
example:
. * The SELECT Dept the ON the FROM EMP EMP the INNER the JOIN dept_id. = Dept id;
the SELECT * the JOIN Dept the ON the FROM EMP EMP. dept_id= Dept. id;

External link query:

Left and right outer outside can be transformed into each other, generally written outside left.

  1. Left outer:
    Syntax: select field list from Table 1 left [outer] join in Table 2 on condition;
    query is left all data tables and their intersection portion.
    Examples:
    - Query all employee information, if employees department, the query name of the department, no department, department name is not displayed
SELECT  t1.*,t2.`name` FROM emp t1 LEFT JOIN dept t2 ON t1.`dept_id` = t2.`id`;
  1. Right outer join:
    Syntax: select field list from Table 1 right [outer] join in Table 2 on condition;
    query data is all right table and the intersection portion thereof.
    example:
SELECT  * FROM dept t2 RIGHT JOIN emp t1 ON t1.`dept_id` = t2.`id`;
  1. Subqueries (nested query / SQL nested): According to the results of the internal SQL as an external SQL queries
    concept: query nested queries, called nested query into sub-queries.
-- 查询工资最高的员工信息
-- 1 查询最高的工资是多少 9000
SELECT MAX(salary) FROM emp;
                
-- 2 查询员工信息,并且工资等于9000的
SELECT * FROM emp WHERE emp.`salary` = 9000;
                
-- 一条sql就完成这个操作。子查询
SELECT * FROM emp WHERE emp.`salary` = (SELECT MAX(salary) FROM emp);

Subqueries different situations

  1. Subquery is a single separate:
    subquery as conditions, using the operator to judge. Operators:>> = <<= =
-- 查询员工工资小于平均工资的人
SELECT * FROM emp WHERE emp.salary < (SELECT AVG(salary) FROM emp);
  1. Subquery is a single multi-line:
    subquery as the condition used to determine the operator in
-- 查询'财务部'和'市场部'所有的员工信息
SELECT id FROM dept WHERE NAME = '财务部' OR NAME = '市场部';
SELECT * FROM emp WHERE dept_id = 3 OR dept_id = 2;
-- 子查询
SELECT * FROM emp WHERE dept_id IN (SELECT id FROM dept WHERE NAME = '财务部' OR NAME = '市场部');
  1. The results of the sub-query is rows and columns: the
    sub-queries can be used as a virtual table involved in a query, use the time to play the virtual table aliases
-- 查询员工入职日期是2011-11-11日之后的员工信息和部门信息
-- 子查询
SELECT * FROM dept t1 ,(SELECT * FROM emp WHERE emp.`join_date` > '2011-11-11') t2
WHERE t1.id = t2.dept_id;

-- 普通内连接
SELECT * FROM emp t1,dept t2 WHERE t1.`dept_id` = t2.`id` AND t1.`join_date` >  '2011-11-11'

FROM word query sentence:
the WHERE itself subquery: limiting clause
SELECT query word sentence: performing first external, as a result of conditions external to the inside with

En query:
1. From the first to write those words ,, clear queries from Table
2. If the primary foreign key relationship, the conditions first primary foreign key relationships contained in these tables
3. If there are other additional extra conditions, add
4 Finally, the supplemental content select clause.

Multi-table queries practice

-- 部门表
CREATE TABLE dept (
  id INT PRIMARY KEY PRIMARY KEY, -- 部门id
  dname VARCHAR(50), -- 部门名称
  loc VARCHAR(50) -- 部门所在地
);

-- 添加4个部门
INSERT INTO dept(id,dname,loc) VALUES 
(10,'教研部','北京'),
(20,'学工部','上海'),
(30,'销售部','广州'),
(40,'财务部','深圳');



-- 职务表,职务名称,职务描述
CREATE TABLE job (
  id INT PRIMARY KEY,
  jname VARCHAR(20),
  description VARCHAR(50)
);

-- 添加4个职务
INSERT INTO job (id, jname, description) VALUES
(1, '董事长', '管理整个公司,接单'),
(2, '经理', '管理部门员工'),
(3, '销售员', '向客人推销产品'),
(4, '文员', '使用办公软件');



-- 员工表
CREATE TABLE emp (
  id INT PRIMARY KEY, -- 员工id
  ename VARCHAR(50), -- 员工姓名
  job_id INT, -- 职务id
  mgr INT , -- 上级领导
  joindate DATE, -- 入职日期
  salary DECIMAL(7,2), -- 工资
  bonus DECIMAL(7,2), -- 奖金
  dept_id INT, -- 所在部门编号
  CONSTRAINT emp_jobid_ref_job_id_fk FOREIGN KEY (job_id) REFERENCES job (id),
  CONSTRAINT emp_deptid_ref_dept_id_fk FOREIGN KEY (dept_id) REFERENCES dept (id)
);

-- 添加员工
INSERT INTO emp(id,ename,job_id,mgr,joindate,salary,bonus,dept_id) VALUES 
(1001,'孙悟空',4,1004,'2000-12-17','8000.00',NULL,20),
(1002,'卢俊义',3,1006,'2001-02-20','16000.00','3000.00',30),
(1003,'林冲',3,1006,'2001-02-22','12500.00','5000.00',30),
(1004,'唐僧',2,1009,'2001-04-02','29750.00',NULL,20),
(1005,'李逵',4,1006,'2001-09-28','12500.00','14000.00',30),
(1006,'宋江',2,1009,'2001-05-01','28500.00',NULL,30),
(1007,'刘备',2,1009,'2001-09-01','24500.00',NULL,10),
(1008,'猪八戒',4,1004,'2007-04-19','30000.00',NULL,20),
(1009,'罗贯中',1,NULL,'2001-11-17','50000.00',NULL,10),
(1010,'吴用',3,1006,'2001-09-08','15000.00','0.00',30),
(1011,'沙僧',4,1004,'2007-05-23','11000.00',NULL,20),
(1012,'李逵',4,1006,'2001-12-03','9500.00',NULL,30),
(1013,'小白龙',4,1004,'2001-12-03','30000.00',NULL,20),
(1014,'关羽',4,1007,'2002-01-23','13000.00',NULL,10);



-- 工资等级表
CREATE TABLE salarygrade (
  grade INT PRIMARY KEY,   -- 级别
  losalary INT,  -- 最低工资
  hisalary INT -- 最高工资
);

-- 添加5个工资等级
INSERT INTO salarygrade(grade,losalary,hisalary) VALUES 
(1,7000,12000),
(2,12010,14000),
(3,14010,20000),
(4,20010,30000),
(5,30010,99990);
            -- 需求:

- 1. queries all employee information. Queries employee number, employee name, salary, job title, job description
/ *
analysis:
1. The employee number, employee name, salary, need to query the emp table job title, job description need to query the job Table
2. query emp.job_id = job .id

* /
The SELECT
T1. id, - employee numbers
T1. ename, - employee name
T1. salary, - wages
T2. jname, - job title
T2. description- job description of
the FROM
emp T1, T2 the Job
the WHERE
T1. job_id= T2. id;

- 2. Query the employee number, employee name, salary, job title, job description, department name, department, position
/
analysis:
1. The employee number, employee name, salary emp job title, job description job department name, department, position the dept
2 conditions: emp.job_id = job.id and emp.dept_id = dept.id
/

The SELECT
T1. id, - employee numbers
T1. ename, - employee name
T1. salary, - wages
T2. jname, - job title
T2. description, - job description
T3. dname, - name of the department
T3. loc- Department position
the FROM
emp T1, Job T2, T3 Dept
the WHERE
T1. job_id= T2. idthe AND T1. dept_id= T3. id;

- 3. Query employee name, salary, wage scales
/
Analysis:
1. Employee name, salary wage scale emp salarygrade
2. Conditions emp.salary> = salarygrade.losalary and emp.salary <= salarygrade.hisalary
emp.salary the BETWEEN salarygrade and salarygrade.hisalary .losalary
/
the SELECT
t1.ename,
T1. salary,
T2 *.
the FROM EMP T1, T2 salarygrade
the WHERE T1. salarythe BETWEEN T2. losalarythe aND T2. hisalary;

- 4. Inquiries employee name, salary, job title, job description, department name, department, position, pay grade
/ *
Analysis:
1. Employee name, salary emp, job title, job description job department name, department, position, dept wages level salarygrade
2. conditions: emp.job_id = job.id and emp.dept_id = dept.id and emp.salary BETWEEN salarygrade.losalary and salarygrade.hisalary

*/
SELECT
t1.ename,
t1.salary,
t2.jname,
t2.description,
t3.dname,
t3.loc,
t4.grade
FROM
emp t1,job t2,dept t3,salarygrade t4
WHERE
t1.job_id = t2.id
AND t1.dept_id = t3.id
AND t1.salary BETWEEN t4.losalary AND t4.hisalary;

- 5. check out the department number, department name, department, location, number of departments

/ *
Analysis:
1. department number, department name, department, position dept table. The number of departments emp table
2. grouping queries. Emp.dept_id complete packet according to the query COUNT (ID)
3. step using a subquery to the second query result table associated with the query and dept

*/
SELECT
t1.id,t1.dname,t1.loc , t2.total
FROM
dept t1,
(SELECT
dept_id,COUNT(id) total
FROM
emp
GROUP BY dept_id) t2
WHERE t1.id = t2.dept_id;

- 6. Name the query names of all employees and their immediate superiors, there is no need to query the leadership of employees

/
Analyzed:
1. Name emp, direct supervisor name EMP
ID mgr emp table and the autocorrelation
2. Conditions emp.id = emp.mgr, there must be renamed two tables t1, t2. Otherwise the result is empty
all the data 3. Query left the table, and the intersection of data
* Use a left outer join query

/
/

select
t1.ename,
t1.mgr,
t2.id,
t2.ename
from emp t1, emp t2
where t1.mgr = t2.id;

*/

The SELECT
t1.ename,
t1.mgr,
T2. id,
T2. ename
The FROM EMP T1
the LEFT the JOIN T2 EMP
the ON T1. mgr= T2. id; - renamed herein have two tables t1, t2. Otherwise the result is empty

Affairs

1. 事务的基本介绍
    1. 概念:
        *  如果一个包含多个步骤的业务操作,被事务管理,那么这些操作要么同时成功,要么同时失败。只能提交或者回滚一次
        
    2. 操作:
        1. 开启事务: start transaction;
        2. 回滚:rollback;
        3. 提交:commit;
    3. 例子:
        CREATE TABLE account (
            id INT PRIMARY KEY AUTO_INCREMENT,
            NAME VARCHAR(10),
            balance DOUBLE
        );
        -- 添加数据
        INSERT INTO account (NAME, balance) VALUES ('zhangsan', 1000), ('lisi', 1000);
        
        
        SELECT * FROM account;
        UPDATE account SET balance = 1000;
        -- 张三给李四转账 500 元
        
        -- 0. 开启事务
        START TRANSACTION;
        -- 1. 张三账户 -500
        
        UPDATE account SET balance = balance - 500 WHERE NAME = 'zhangsan';
        -- 2. 李四账户 +500
        -- 出错了...
        UPDATE account SET balance = balance + 500 WHERE NAME = 'lisi';
        
        -- 发现执行没有问题,提交事务
        COMMIT;
        
        -- 发现出问题了,回滚事务
        ROLLBACK;
    4. MySQL数据库中事务默认自动提交
        
        * 事务提交的两种方式:
            * 自动提交:
                * mysql就是自动提交的
                * 一条DML(增删改)语句会自动提交一次事务。
            * 手动提交:
                * Oracle 数据库默认是手动提交事务
                * 需要先开启事务,再提交
        * 修改事务的默认提交方式:
            * 查看事务的默认提交方式:SELECT @@autocommit; -- 1 代表自动提交  0 代表手动提交
            * 修改默认提交方式: set @@autocommit = 0;


2. 事务的四大特征:
    1. 原子性:是不可分割的最小操作单位,要么同时成功,要么同时失败。
    2. 持久性:当事务提交或回滚后,数据库会持久化的保存数据。
    3. 隔离性:多个事务之间。相互独立。事务的并发,详见高级隔离。
    4. 一致性:事务操作前后,数据总量不变,从一个一致性状态转变到另一个一致性状态。
3. 事务的隔离级别(了解)
    * 概念:多个事务之间隔离的,相互独立的。但是如果多个事务操作同一批数据,则会引发一些问题,
    设置不同的隔离级别就可以解决这些问题。
    * 存在问题:
        1. 脏读:一个事务,读取到另一个事务中没有提交的数据
        2. 不可重复读(虚读):在同一个事务中,两次读取到的数据不一样。一个事务中读到了另一个事务已提交的update操作。有的要求同一个事务里一开始跟最后操作的数据要一样。
        3. 幻读:一个事务操作(DML)数据表中所有记录,另一个事务添加了一条数据,则第一个事务查询不到自己的修改。
        一个事务读到了另一个事务已提交的delete和
    * 隔离级别:
        1. read uncommitted:读未提交/未提交读:能读到别人没提交的数据
            * 产生的问题:脏读、不可重复读、幻读
        2. read committed:读已提交 (Oracle):能读到别人已提交的
            * 产生的问题:不可重复读、幻读
        3. repeatable read:可重复读 (MySQL默认)解决了事务因为其他事务导致前后数据不一致的问题。
            * 产生的问题:幻读
        4. serializable:串行化:给一个事务操作的数据部分上锁,其他事务访问这段数据的时候无法执行SQL,
        持续等待直到之前的事务释放这段数据资源。
            * 可以解决所有的问题

        * 注意:隔离级别从小到大安全性越来越高,但是效率越来越低
        * 数据库查询隔离级别:
            * select @@tx_isolation;
        * 数据库设置隔离级别:
            * set global transaction isolation level  级别字符串;

    * 演示:
        set global transaction isolation level read uncommitted;
        start transaction;
        -- 转账操作
        update account set balance = balance - 500 where id = 1;
        update account set balance = balance + 500 where id = 2;

DCL:

* SQL分类:
    1. DDL:操作数据库和表
    2. DML:增删改表中数据
    3. DQL:查询表中数据
    4. DCL:管理用户,授权

* DBA:数据库管理员

* DCL:管理用户,授权
    1. 管理用户
        1. 添加用户:
            * 语法:CREATE USER '用户名'@'主机名' IDENTIFIED BY '密码';
        2. 删除用户:
            * 语法:DROP USER '用户名'@'主机名';
        3. 修改用户密码:
            
            UPDATE USER SET PASSWORD = PASSWORD('新密码') WHERE USER = '用户名';
            UPDATE USER SET PASSWORD = PASSWORD('abc') WHERE USER = 'lisi';
            
            SET PASSWORD FOR '用户名'@'主机名' = PASSWORD('新密码');
            SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123');

            * mysql中忘记了root用户的密码?
                1. cmd -- > net stop mysql 停止mysql服务
                    * 需要管理员运行该cmd

                2. 使用无验证方式启动mysql服务: mysqld --skip-grant-tables
                3. 打开新的cmd窗口,直接输入mysql命令,敲回车。就可以登录成功
                4. use mysql;
                5. update user set password = password('你的新密码') where user = 'root';
                6. 关闭两个窗口
                7. 打开任务管理器,手动结束mysqld.exe 的进程
                8. 启动mysql服务
                9. 使用新密码登录。
        4. 查询用户:
            -- 1. 切换到mysql数据库
            USE myql;
            -- 2. 查询user表
            SELECT * FROM USER;
            
            * 通配符: % 表示可以在任意主机使用用户登录数据库

    2. 权限管理:
        1. 查询权限:
            -- 查询权限
            SHOW GRANTS FOR '用户名'@'主机名';
            SHOW GRANTS FOR 'lisi'@'%';

        2. 授予权限:
            -- 授予权限
            grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';
            -- 给张三用户授予所有权限,在任意数据库任意表上
            
            GRANT ALL ON *.* TO 'zhangsan'@'localhost';
        3. 撤销权限:
            -- 撤销权限:
            revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名';
            REVOKE UPDATE ON db3.`account` FROM 'lisi'@'%';

Guess you like

Origin www.cnblogs.com/maomaodesu/p/12000745.html