Outline
Start MySQL service:
services.msl command to open the service panel, then click the mouse to complete the open service.
Run as administrator CMD: net stop mysql (shut down service), net start mysql (open service)
Login and logout (measured total failure):
mysql -uroot -plove
mysql –uroot –p
mysql -h+IP -uroot -plove
mysql –host=ip –user=root –password=password
The general syntax:
- Writing may be single or multiple rows, end with a semicolon
- Use a space or tab to improve readability
- MySQL is not case sensitive, but it is recommended to use keywords in uppercase
- Comment:
a) Single-line comments: - + + space annotation content
b) single-line comments (MySQL-specific): # + Notes content
c) Multi-line comments: / * comment * content /
What is SQL: Structured Query Language: Structured Query Language, defines the operating rules for all relational databases
classification:
DDL: operation of the database and tables
DML: deletions change table data
DQL: data look-up table
DCL: access authorization and security operations
DDL: operation of the database and tables
Operation of the database:
C (create): Creating
Create a database: CREATE DATABASE DBname;
Before creating the first check whether there is: CREATE DATABASE IF NOT EXISTS DBname;
Manually set character set: CREATE DATABASE DBname CHARACTER SET gbk;
R (Retrieve): inquiry
All queries the database name: SHOW DATABASE;
Query a database character sets: SHOW CREATE DATABASE DBname;
U (Update): Modify
Modify the database character set: ALTER DATABASE DBname CHARACTER SET jbk;
D (Delete): Delete
Delete: DROP DATABASE DBname;
First determines whether there is: DROP DATABASE IF EXISTS DBname;
Use database:
DB query is being used: SELECT DATABASE ();
Switching using DB: USE DBname;
Operating Table:
C (create): Creating
CREATE TABLE table_name(
Column name Data type,
Column name data type 2,
……
Column Name Data Type n
);
Types of:
int: Plastic
double: decimal type
date: it contains only date date format yyyy-mm-dd
datetime: yyyy-mm-dd HH:mm:ss
timestamp: timestamp is not automatically assigned to the current system time
varchar (n): string type, n represents the maximum string length - a kanji character is also considered a
R (Retrieve): inquiry
All query table name: SHOW TABLES;
Lookup table structure: DESC table_name;
U (Update): Modify
修改表名: ALTER table_name RENAME TO new_table_name;
Modify the table character set: ALTER TABLE table_name CHARACTER SET GBK;
Add one: ALTER TABLE table_name ADD listed types;
Modify the name of a column, type:
Modify the table name and data type: ALTER TABLE table_name CHANGE new column name Column Name Data Type;
Only modify the data type: ALTER TABLE table MODIFY column names new data types;
Delete Column: ALTER TABLE table DROP column names;
D (Delete): Delete
To delete a list: DROP TABLE table_name;
Before deleting first determine: DROP TABLE IF EXISTS table_name;
DML: modify data in the table
adding data
The basic syntax: INSERT INTO table name (column names 1, 2 column name, column name 2 ...) the VALUES (value 1, value 2 and value 3, ...);
Precautions:
- Column names and values need one correspondence
- Do not add in brackets after the table name column names to add value to all columns by default
- Out of the digital type, other types require the use of quotation marks
delete data
The basic syntax: DELETE FROM [WHERE condition];
Note: without conditions will delete all the data in the table
Delete tables and create a table with the same empty: TRUNCATE TABLE table_neme; high-efficiency data than all this instruction to delete the table
change the data
The basic syntax: UPDATE table_name set value 1 = 1 column name, column name = value 2 2, ... [the WHERE condition]
Note: If you do not add any conditions will change all records
DQL: data look-up table
The complete syntax:
SELECT
Field list
FROM
List of table names
WHERE
List of conditions
GROUP BY
Group list
HAVING
Conditions after grouping
ORDER
Sequence
LIMIT
Paging limited
Underlying query:
Multiple fields of inquiry:
After the field name you want to query written in SELECT, using field names separated by commas
Search results are sorted according to the field list
* Query all fields can be used, but with caution
remove duplicates:
Use DISTINCT before the field name
De-duplication will only remove the same line, only a part of the same will not change
Computed column:
The corresponding calculated field list to uninstallation
Use IFNULL (expr1, expr2) may be used instead of expr2 when expr1 NULL value
Surnamed
AS can be used in a query field aliases, similar to the leader packet python
Conditions inquiry
- Followed by the conditions in the WHERE clause
- Operator:
a) < , > , < = , >= , <> , =
b) BETWEEN a AND b: between ab
c) IN: In a collection among
d) IS NULL: NULL use only the equal sign is determined, or not use = <> Analyzing
e) AND OR NOT: You know, do not speak the same python
f) LIKE: Fuzzy query:
regular:
_: Any single character
More than any character%
Sort query
Syntax: ORDER BY sort field 1 Sort by 1, 2 way 2 sort field ...
Sort:. ASC (ascending), DESC (descending) by default to ascending
Note: Only when the same first sort condition when the second sort in order to work more empathy condition
Aggregate function
Meaning: The data as a whole one, then the vertical calculation
Common functions:
COUNT: count the number of
MAX: maximum computing
MIN: minimum calculation
SUM: sum
AVG: average value
Note: aggregate function will automatically send a NULL value calculated
Solution: Select column does not contain non-null value for use in calculation or NULL-function IFBULL
Grouping queries
Syntax: GROUP BY grouping field
Note: The query field list only after the packet is a packet field or aggregate functions, although you can fill in the rest, but no practical significance.
WHERE and HAVING difference:
WHERE before the packets are defined, if the condition is not involved in packet
HAVING defined in the following packet, if the condition is not questioned
WHERE later aggregate functions can not be used after polymerization HAVING function can be determined
Paging query
Syntax: LIMIT beginning of the index, the number of queries per page
Formula: starting index = (current page - 1) * article per page
Note: LIMIT is a MySQL dialect, other databases in particular their way paging
Promise:
Concept: The data in the table are defined to ensure the accuracy, effectiveness and integrity of data.
classification:
Primary key: PRIMARY KEY
-
- It represents a non-empty and the only
- A table can have only one primary key field
- Add to create a table when the primary key id INT PRIMARY KEY,
- Use ALTER TABLE table_name DROP PRIMARY KEY deleted after you create a primary key
- After you create that use MODIFY add a primary key
- Use the keyword AUTO_INCREMENT to grow automatically, often with the use PRIMARY KEY, and its growth only on the data related to a
Non-empty: NOT NULL
-
- That can not be expressed not
- Add constraints when creating tables
- Use ALTER TABLE table_name MODIFY created after the column name + can add or remove constraints NOT NULL constraints
The only: UNIQUE
-
- Represents the value can not be repeated
- When you create a table with the NOT NULL use similar
- Use ALTER TABLE tanle_name DROP INDEX column names created after deleting columns UNIQUE constraint can not be used to delete the same manner as NOT NULL
- After you create a form you can use to add constraints ALTER TABLE table_name MODIFY column names + ways bound
- NULL and NULL is not equal
Foreign key: FOREIGN KEY
-
- When you create a table of add foreign key, foreign key columns: CONSTRAINT foreign key name FOREIGN KEY (foreign key column name) REFERENCES main table name (the primary table column names
- Remove the foreign key: ALTER TABLE table_name DROP FOREIGN KEY foreign key name
- Add foreign key after creating) ALTER TABLE table_nema ADD CONSTRAINT foreign key name FOREIGN KEY (foreign key column name) REFERENCES main table name (the primary table column names
- Foreign key values from the table can be NULL, but does not exist in the primary table value
- Cascade operation: cascading updates and cascading deletes in the foreign key added plus: ON UPDATE CASCADE ON DELETE CASCADE
Database design
The relationship between multi-table
a) Classification:
i-one: A person can have only one identity card
ii. to-many or many-to-one teacher have more than one student, a student is only one teacher
. Iii-many: how a student courses, a course there are more students
b) Implementation:
i-many: one to establish a foreign key in as much, one point of a primary key
. Ii-many: it needs third intermediate sheet, the intermediate sheet contains at least two fields, respectively, as a foreign key to the primary key two table
. Iii-one: The only foreign key refers is added to the other of either a primary key, a one to one relationship between the direct synthesis table can be a general
Database design paradigm
c) a first paradigm (1NF): each column is indivisible atomic data
d) a second paradigm (2NF): non-codebook attributes must rely entirely on candidate code
e) a third paradigm (3NF): Any non-primary property is not dependent on other non-primary property
f) Bath - cod Paradigm (the BCNF)
g) fourth normal form (4NF)
h) The fifth paradigm (5NF, also known as the perfect paradigm)
Backup and restore databases
Command line mode:
Backup: Syntax: database name> Save Path mysqldump -u username -p password to be backed up
reduction:
-
- Log database
- Create a database
- Use Database
- Executable file soure file path
Graphical interface:
You do not know this, because if you look at this document describes the way you use the command line to restore the above you may not know
Multi-table query
Syntax: add multiple query list in the list of queries a single table query on it, use a comma separated
Query results Cartesian product:
classification:
-
- Inner join query
a) the link is implicitly: remove insignificant data using WHERE
b) the explicit link: syntax: SELECT fields FROM table 1 [INNRT] JOIN Table 2 ON conditions name
2. The outer join query
a) an outer left connection:
i.語法:SELECT *FROM table_1 LEFT [OUTER] JOIN table_2 條件
. Ii Note: The result is the intersection of all the data tables left and right of the table
b) right outer connector
i. 語法:SELECT *FROM table_1 RIGHT [OUTER] JOIN table_2 條件
. Ii Note: The result is the intersection of all the data and the right table right table
3. sub-query:
a) Concept: query nested queries, said the embedded query into sub-queries
b) Syntax: see a different view of the back to know
c) different situations:
. I subquery result is single separate: as subquery condition is determined using the operator
. Ii subquery results single multi-line: using the result of the determination to the keyword IN
. Iii sub-query results rows and columns: as a virtual table query on after FROM, but this situation and use common link within the same query
Affairs
basic introduction;
Concept: multi-step business operations are transaction management, while either succeed or fail at the same time.
format:
Open transaction: START TRANSACTION
Commit the transaction: COMMIT
Roll back the transaction: ROLLBACK
note:
-
- MySQL will automatically commit the transaction if the transaction is not manually open the case and each perform a DML will be submitted once, but if you manually open the transaction, but did not submit manually, the system does not default submission, but will be rolled back.
- See: using the command SELECT @@ AOTUCOMMIT, the result is automatically submit 1 represents, expressed as 0 if not automatically submit use SET @@ AOTOCOMMIT = 0 to modify its value.
- Oracle Database is submitting data manually
Four features:
-
- Atomicity: the indivisibility, at the same time either succeed or fail at the same time
- Durability: Once a transaction is committed or rolled back, the database will save persistent data
- Isolation: a plurality of mutually independent transactions
- Consistency: a transaction before and after the operation, the same amount of data
Isolation level (to know):
The concept: between multiple transactions are independent of each other, but if multiple transactions with a number of operational data will cause problems, you can solve the problem by setting the isolation level
QUESTION produced:
-
- Expansion read: a transaction reads data to another transaction uncommitted
- Non-repeatable read: In the same transaction twice the read data is not the same
- Phantom read: record a transaction operation data in the table, add a another transaction data, the first transaction can not find their own records
Isolation level:
-
- Uncommitted Read
- Read Committed
- Repeatable read
- Serialization
DCL
User management:
Query User:
-
- Switch to the mysql database
- Query user table
Add user:
-
- CREATE 'user_name' @ 'hostname' IDENTIFIED password
Change the user password:
-
- UPDATE USER SET PASSWORD = PASSWORD ( 'new password') WHERE USER = 'username';
- SET PASSWORD FOR 'user' @ 'hostname' = PASSWORD ( 'new password');
delete users:
-
- DROP USER 'user_name' @ 'hostname'
Forgot administrator password:
-
- Stop MySQL service by the administrator
- No authentication start using MySQL
- In the new window, you can go directly to MySQL and modify the ROOT password
- MySQL ending process
- Restart MySQL service
- Use the new password
Authorization:
Permissions query: SHOW GROUND FOR 'username' @ 'hostname';
Permissions granted: GRANT permission list (SELECT DELECT UPDATE) ON database table name TO 'username' @ 'hostname;.
Revoke privileges: REWORK list of permissions' ON database 'username' @ 'hostname';