Overview of important knowledge of MYSQL (advanced)

1. Data integrity

2. Multi-table query

3. Supplement

4. Database optimization

5. MySQL installation and download

6. Exercises


Arrangement hastily, if there are any questions in the article, please ask, thank you for your support, let us make progress together!

An overview of MYSQL related knowledge, divided into basic articles, advanced articles and advanced articles!

1. Data integrity

(1) Integrity of the database

It is used to ensure that the data stored in the database is valid , that is, the validity and accuracy of the data, ensuring the integrity of the data = adding constraints to the table when creating the table
Complete classification:
- Entity Integrity ( Line Integrity ):
- Domain Integrity ( Column Integrity ):
- Referential integrity ( association table integrity ):
Primary key constraint: primary key
Unique constraint: unique [key]
Not null constraint: not null
Default constraint: default
Auto-increment: auto_increment
Foreign key constraint : foreign key
It is recommended that these constraints should be set when creating the table, and spaces should be used between multiple constraints

(2) Entity integrity

Entity: that is, a row ( a record ) in the table represents an entity ( entity )
The role of entity integrity: to identify that each row of data is not repeated.
Constraint Type:
primary key constraint
unique constraint
auto-increment column
 

(3) Domain Integrity

The role of domain integrity: limit the data in this cell to be correct, and do not compare against other cells in this column
Field represents the current cell
Domain Integrity Constraints: Data Type Not Null Constraints ( not null ) Default Value Constraints (default)
check constraint ( mysql does not support) check(sex=' male ' or sex=' female ')

(4) Referential integrity

referential integrity
Foreign key constraint: FOREIGN KEY

2. Multi-table query

 

(1) Relationship

One-to-many / many-to-one relationship

many-to-many relationship

one-to-one relationship

(2) Grammar

1. Merge result set : UNION , UNION ALL
2. Connection query
2.1 Inner join [INNER] JOIN ON
2.2 Outer join OUTER JOIN ON
- LEFT [OUTER] JOIN
- RIGHT [OUTER] JOIN
- Full outer join ( not supported by MySQL ) FULL JOIN
2.3 Natural connection NATURAL JOIN
3. Subqueries

 

3. Supplement

(1) Multi-line addition

insert into table name ( column name ) values ​​( column value ), ( column value ), ( column value ) ;

(2) Multi-table update

(1) update table 1, table 2 set column name = column value where table 1. column name = table 2. column name and other qualifications
(2)update 1
inner join table2 on table1.column_name = table2.column_name _ _ _
set column name = column value
where qualification

(3) Multi-table deletion

delete table of deleted data from table used in delete operation
where qualification
Note : Use commas to separate multiple tables

(4) Date operation function

now() gets the current system time
year( datevalue ) gets the year in the date value
date_add( date , the field of interval calculation value calculation );
Note: If the calculated value is greater than 0 , it means to push the date back, and if it is less than 0 , it means to push the date forward.

4. Database optimization

1. To optimize the query, try to avoid full table scan. First, you should consider creating indexes on the columns involved in where and order by .
2. Try to avoid the null value judgment of the field in the where clause , otherwise the engine will give up the use of the index and perform a full table scan. It is best not to leave NULL for the database , and fill the database with NOT NULL as much as possible . Remarks, descriptions, comments and the like can be set to NULL . For others, it is best not to use NULL .
3. Try to avoid using the != or <> operator in the where clause , otherwise the engine will give up using the index and perform a full table scan.
4. Try to avoid using or to connect conditions in the where clause . If a field has an index and a field does not have an index, it will cause the engine to give up using the index and perform a full table scan.

5. MySQL installation and download

https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.28-winx64.zip

6. Exercises

topic

data sheet:
Employee table (employee): employee number (empid, primary key), name (name), gender
(sex), title (title), date of birth (birthday), department (depid)
Department (department): department number (depid, primary key), department name (depname)
Salary: employee number (empid), basic salary (basesalary), position worker
capital (titlesalary), deduction (deduction)

need

DDL statement

employee:

CREATE TABLE `employee` (
  `empid` int(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `sex` varchar(2) COLLATE utf8_bin DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `birthday` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `depid` int(11) DEFAULT NULL,
  PRIMARY KEY (`empid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

department:

CREATE TABLE `department` (
  `depid` int(20) NOT NULL,
  `depname` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`depid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

salary:

CREATE TABLE `salary` (
  `empid` int(20) NOT NULL,
  `basesalary` decimal(10,2) DEFAULT NULL,
  `titlesalary` decimal(10,2) DEFAULT NULL,
  `deduction` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`empid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

1. Modify the table structure and add a department profile field to the department table
ALTER TABLE department ADD departdesc VARCHAR(50);
2. Change Li Si's job title to "Engineer", change her basic salary to 2000, and her job salary to 700
UPDATE employee e INNER JOIN salary s ON e.empid=s.empid SET e.title='工程师' ,s.basesalary=2000,s.titilsaraly=700 WHERE e.name='李四';
3. Delete department records of HR department
DELETE FROM department WHERE depname='人事部';
4. Find out the employee number of each employee, the actual salary, and the salary to be paid
SELECT empid,basesalary+titilsaraly-deduction AS '实发工资',basesalary+titilsaraly AS '应发工资' FROM salary;
5. Query the records of employees whose surname is Zhang and whose age is less than 40
SELECT * FROM employee WHERE LIKE '张%' AND `DATABASE`(NOW(),`INTERVAL -40 YEAR)<birthday;
6. Query the employee's employee number, name, title, department name, and actual salary
SELECT empid,name,title,depid,basesalary+titilsaraly-deduction AS '实发工资' FROM salary
7. Query the name and salary of the employees in the sales department
SELECT e.name,e.basesalary,e.titilsaraly,e.deduction from employee e,department d,salary s WHERE w,empid=s.empid and w.empid=d.dename='销售部';
8. Count the number of people with each title
SELECT title,COUNT(*) FROM employee GROUP BY title;
9. Statistics on the department name of each department, the total salary paid, and the average salary
SELECT dename,sum(s.basesalary+s.titilsaraly-s.deduction),AVG([DISTINCT] s.basesalary+s.titilsaraly-s.deduction) FROM employee e,department d,salary s WHERE
e.empid=s.empid AND d.empid=e.empid GROUP BY depname;
10. Find the name of the employee whose base salary is higher than that of all employees in the sales department
SELECT name,e.basesalaryfrom employee e,salary s WHERE e.empid=s.empid AND e.empid=s,empid AND e.basesalary>(SELECT MAX(basesalary) FROM employee e,salary s
department d WHERE e.empid=s.empid AND d,empid=e.empid AND d.department='销售部');


The advanced chapter is over! !

Guess you like

Origin blog.csdn.net/qinluyu111/article/details/123019947