1. Data Type
CHAR & VARCHAR
-
CHAR: fixed-length string
-
VARCHAR: variable-length string
ENUM & SET
-
ENUM: enumerated types, in a selected number of enumeration values
-
SET: set of multiple-choice
2. Keywords
LIKE
-
Keywords can be achieved LIKE fuzzy query, use wildcards% and _ on behalf of an unknown character
-
%: Variable number of unspecified characters
-
_: A character not specified
-
3. Statement
Sort --ORDER BY
SELECT name,age,salary,phone FROM employee ORDER BY salary DESC;
Aggregate Functions
SELECT MAX(salary) AS max_salary,MIN(salary) FROM employee;
4. Query
Subqueries
Want to know where the department is named "Tom" employees to do a few projects. Employee information is stored in the employee table, but the project engineering information stored in the table.
For such cases, we can use sub-query:
SELECT of_dpt,COUNT(proj_name) AS count_project FROM project GROUP BY of_dpt
HAVING of_dpt IN
(SELECT in_dpt FROM employee WHERE name='Tom');
Join query
SELECT id,name,people_num
FROM employee JOIN department
ON employee.in_dpt = department.dpt_name
ORDER BY id;
#另一种等价写法
SELECT id,name,people_num
FROM employee,department
WHERE employee.in_dpt = department.dpt_name
ORDER BY id;
5. Other
index
ALTER TABLE employee ADD INDEX idx_id (id); #在employee表的id列上建立名为idx_id的索引
CREATE INDEX idx_name ON employee (name); #在employee表的name列上建立名为idx_name的索引
view
#这里把视图当作一张表使用
CREATE VIEW v_emp (v_name,v_age,v_phone) AS SELECT name,age,phone FROM employee;
Import Data
LOAD DATA INFILE '文件路径和文件名' INTO TABLE 表名字;
Backup
mysqldump -u root 数据库名>备份文件名; #备份整个数据库
mysqldump -u root 数据库名 表名字>备份文件名; #备份整个表
restore
Combined with backup, restore backed up data to the database just
mysql -u root test < bak.sql