5. MySQL installation and download
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 tableComplete classification:- Entity Integrity ( Line Integrity ):- Domain Integrity ( Column Integrity ):- Referential integrity ( association table integrity ):Primary key constraint: primary keyUnique constraint: unique [key]Not null constraint: not nullDefault constraint: defaultAuto-increment: auto_incrementForeign key constraint : foreign keyIt 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 constraintunique constraintauto-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 columnField represents the current cellDomain 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 integrityForeign 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 ALL2. Connection query2.1 Inner join [INNER] JOIN ON2.2 Outer join OUTER JOIN ON- LEFT [OUTER] JOIN- RIGHT [OUTER] JOIN- Full outer join ( not supported by MySQL ) FULL JOIN2.3 Natural connection NATURAL JOIN3. 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 表 1inner join table2 on table1.column_name = table2.column_name _ _ _set column name = column valuewhere qualification
(3) Multi-table deletion
delete table of deleted data from table used in delete operationwhere qualificationNote : Use commas to separate multiple tables
(4) Date operation function
now() gets the current system timeyear( datevalue ) gets the year in the date valuedate_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 workercapital (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;
ALTER TABLE department ADD departdesc VARCHAR(50);
UPDATE employee e INNER JOIN salary s ON e.empid=s.empid SET e.title='工程师' ,s.basesalary=2000,s.titilsaraly=700 WHERE e.name='李四';
DELETE FROM department WHERE depname='人事部';
SELECT empid,basesalary+titilsaraly-deduction AS '实发工资',basesalary+titilsaraly AS '应发工资' FROM salary;
SELECT * FROM employee WHERE LIKE '张%' AND `DATABASE`(NOW(),`INTERVAL -40 YEAR)<birthday;
SELECT empid,name,title,depid,basesalary+titilsaraly-deduction AS '实发工资' FROM salary
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='销售部';
SELECT title,COUNT(*) FROM employee GROUP BY title;
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;
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='销售部');