A multi-table query:
* Query syntax:
select
Column name list
from
List of table names
where....
1, Cartesian product:
* There are two sets A, B. All take the composition of the two sets.
* To complete multi-table queries, the need to eliminate useless data
2, multi-table query classification:
1. inner join query:
Implicit within the connector 1.: eliminate redundant data using the conditions where
* Examples:
- all the query employee information and corresponding information department
SELECT * FROM emp,dept WHERE emp.`dept_id` = dept.`id`;
- Staff table query name, sex. The name of the department table
SELECT emp.name,emp.gender,dept.name FROM emp,dept WHERE emp.`dept_id` = dept.`id`;
* Standardized wording:
SELECT
Name of the employee table - t1.name,
t1.gender, - sex workers table
t2.name - the name of the department table
FROM
emp t1,
dept t2
WHERE
t1.`dept_id` = t2.`id`;
2. Explicit en:
* Syntax: select field list from table 1 [inner] join table 2 on condition
* E.g:
* SELECT * FROM emp INNER JOIN dept ON emp.`dept_id` = dept.`id`;
* SELECT * FROM emp JOIN dept ON emp.`dept_id` = dept.`id`;
3. inner join query:
1. query data from which table
2. What is the condition
3. To find out which fields
3, the outer query link:
1. The left outer:
* Syntax: select field list from Table 1 left [outer] join in Table 2 on condition;
* Query is left of all data tables and their intersection part.
* Examples:
- queries all employee information, if the employee has a 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`; 2. 右外连接:
* Syntax: select field list from Table 1 right [outer] join in Table 2 on condition;
* Query is the right table all the data and its intersection portion.
* Examples:
SELECT * FROM dept t2 RIGHT JOIN emp t1 ON t1.`dept_id` = t2.`id`;
4, sub-query:
* Concept: query nested queries, called nested query into sub-queries.
- Query highest wages employee information
--1 query highest wage in 9000
SELECT MAX(salary) FROM emp;
- 2 query employee information and wage equal to 9000
SELECT * FROM emp WHERE emp.`salary` = 9000;
- a sql complete this operation. Subqueries
SELECT * FROM emp WHERE emp.`salary` = (SELECT MAX (salary) FROM emp); * subqueries different circumstances
1. The results of the sub-query is a single separate:
* Subquery as conditions, using the operator to judge. Operators:>> = <<= =
- Polling staff salaries less than the average wage of people
SELECT * FROM emp WHERE emp.salary < (SELECT AVG(salary) FROM emp);
2. The results of sub-query is a multi-line single-column:
* Subquery as the condition used to determine the operator in
- Query all employee information 'Ministry of Finance' and 'marketing'
SELECT id FROM dept WHERE NAME = '财务部' OR NAME = '市场部'; SELECT * FROM emp WHERE dept_id = 3 OR dept_id = 2;
- subqueries
SELECT * FROM emp WHERE dept_id IN (SELECT id FROM dept WHERE NAME IN (‘财务部’,‘市场部’));
3. The results of the subquery are rows and columns:
* Sub-queries can be used as a virtual table involved in the query
- Staff query entry date is after the employee information and departmental information day 2011-11-11
- subqueries
SELECT * FROM dept t1 ,(SELECT * FROM emp WHERE emp.`join_date` > '2011-11-11') t2 WHERE t1.id = t2.dept_id;
- General En
SELECT * FROM emp t1,dept t2 WHERE t1.`dept_id` = t2.`id` AND t1.`join_date` > '2011-11-11'
Second, the transaction
1. Basic introduction affairs
1. Concept:
Business operations if a multi-step, the transaction management, these operations either at the same time succeed or fail at the same time.
2. Operation:
1. Turn on the transaction: start transaction;
2. Rollback: rollback;
3. Submit: commit;
3. Examples:
CREATE TABLE account (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10),
balance DOUBLE
);
-- adding data
INSERT INTO account (NAME, balance) VALUES ('zhangsan', 1000), ('lisi', 1000);
SELECT * FROM account;
UPDATE account SET balance = 1000; - Doe Zhang to transfer 500 yuan
- 0. open transaction START TRANSACTION;
- 1. Joe Smith account -500 UPDATE account SET balance = balance - 500 WHERE NAME = 'zhangsan';
- 2. John Doe accounts +500
-- error...
UPDATE account SET balance = balance + 500 WHERE NAME = 'lisi';
- Discover perform no problem, commit the transaction COMMIT;
- found a problem, roll back the transaction ROLLBACK;
4. MySQL database default auto-commit transaction
1, two ways of transaction commit:
* Automatic submission:
* Mysql is automatically submitted
* A DML (additions and deletions) statement will automatically submit a transaction.
* Manual submission:
* Oracle Database default is to commit the transaction manually
* You need to enable the transaction, and then submitted
2, modify the default transaction submission:
* View transactions default submission: SELECT @@ autocommit; - 1 representative of automatically submitted 0 for manual submission
* Modify the default submission: set @@ autocommit = 0;
2. affairs of the four characteristics:
Atomic: the operation unit is the smallest indivisible, while either succeed or fail simultaneously.
2. Persistence: When a transaction is committed or rolled back, the database will save persistent data.
3. Isolation: multiple transactions between. Independent.
4. Consistency: a transaction before and after the operation, the amount of data change
Transaction isolation level 3. (understand)
* The concept: isolation between multiple transactions, independent of each other. However, if multiple transactions with a number of operating data, it will cause problems, set different levels of isolation can solve these problems.
* Problems:
1. Dirty read: a transaction, the data read another uncommitted transaction
2. The non-repeatable read (dummy read): In the same transaction twice the read data is not the same.
3. Reading Magic: all records for a transactional operations (DML) data in the table, add a another transaction data, the first transaction can not find their own modifications.
* Isolation level:
1. read uncommitted: read uncommitted
* Problem arises: dirty reads, non-repeatable read, phantom read
2. read committed: Read Committed (Oracle)
* Problems: the non-repeatable read, phantom read
3. repeatable read: Repeatable read (MySQL by default)
* Problem arises: Magic Reading
4. serializable: serialized
* You can solve all problems
* Note: The isolation level from small to large security increasing, but increasingly inefficient
* Database Query isolation level:
* select @@tx_isolation;
* Setting the isolation level database:
* Set global transaction isolation level level string;
Three, DCL:
* SQL Category:
1. DDL: operation of the database tables and
2. DML: additions and deletions to the data table
3. DQL: the data look-up table
4. DCL: user management, authorization
* DBA: Database Administrator
* DCL: user management, authorization
1. Manage Users
1. Add user:
* Syntax: CREATE USER 'username' @ 'hostname' IDENTIFIED BY 'password';
2. Delete user:
* Syntax: DROP USER 'username' @ 'hostname';
3. Modify the user password:
UPDATE USER SET PASSWORD = PASSWORD ( 'new password') WHERE USER = 'username';
比如:UPDATE USER SET PASSWORD = PASSWORD('abc') WHERE USER = 'lisi';
You may also be used: SET PASSWORD FOR 'username' @ 'hostname' = PASSWORD ( 'new password');
比如:SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123');
* Mysql in forgotten the root password?
1. cmd -> net stop mysql service mysql stop
* Requires the administrator to run the cmd
2. Use no authentication start mysql service: mysqld --skip-grant-tables
3. Open a new cmd window, enter the mysql command directly, hit Enter. We can successfully log
4. use mysql;
5. update user set password = password ( 'your new password') where user = 'root';
6. Close the two windows
7. Open Task Manager to end the process manually mysqld.exe
8. Start mysql service
9. Log in using the new password.
4. Query User:
- 1. Switch to mysql database
USE myql;
- 2. Query user table
SELECT * FROM USER;
* Wildcard:% said user can use the database to log on any host
2. Rights Management:
1. Query permissions:
- Query permission
SHOW GRANTS FOR 'username' @ 'hostname';
SHOW GRANTS FOR 'lisi'@'%';
2. Grant permission:
-- Granted permission
grant permission list on the name of the database table name to 'username' @ 'hostname';
- Joe Smith user to grant all privileges on any table in any database
GRANT ALL ON *.* TO 'zhangsan'@'localhost';
3. revoke privileges:
- revoke permission:
revoke permissions list on the database name table name from 'username' @ 'hostname';
REVOKE UPDATE ON db3.`account` FROM 'lisi'@'%';