MySQL study notes [Bili Shang Silicon Valley 2022]

1. Database Overview

1. Why use a database?

Implement data persistence

2. Database and database management system

2.1 Database related concepts

DB:数据库

A warehouse that stores data is essentially a file system. A series of organized data is saved.

DBMS:数据库管理系统

It is a large-scale software that manipulates and manages databases. It is used to establish, use and maintain databases, and conduct unified management and control of databases. Users access data in tables in the database through the database management system.

SQL:结构化查询语句

Statements specifically used to communicate with the database.

2.2 Relationship between database and database management system

2.3 Introduction to common databases

  • Oracle
  • MySQL
  • DB2
  • PostgreSQL
  • SQLite
  • informix

3. RDBMS vs. non-RDBMS

3.1 Relational database (RDBMS)

3.1.1 Essence
  • The relational database model reduces complex data structures into simple binary relationships (ie, two-dimensional table form).
  • Relational data models store data in the form of rows and columns.
  • SQL is the query language for relational databases.

3.2 Non-relational database (non-RDBMS)

3.2.1 Category
  • key-value database
  • document database
  • search engine database
  • Column database
  • graph database
3.2.2 NoSQL

4. Relational database design rules

4.1 Tables, records, fields

  1. There are three main concepts in the ER model: entity set, attribute, and relationship set

  2. An entity set (class) <> a table (table)

    An entity (instance) <> a row (row)/a record (record) in the database table

    An attribute (attribute) <> A column (column)/a field (field) in the database table

4.2 Table relationships

4.2.1 One-to-one relationship

Two principles for creating tables:

  • Unique foreign key: The primary key of the master table and the foreign key of the slave table form a primary and foreign key relationship, and the foreign key is unique.
  • The foreign key is the primary key: the primary key of the primary table and the primary key of the secondary table form a primary and foreign key relationship.
4.2.2 One-to-many relationship
4.2.3 Many-to-many
4.2.4 Self-reference

2. Basic SELECT statement

1. SQL Overview

1.1 SQL classification

DDL (Data Definition Language)

Define different database objects such as databases, tables, views, indexes, etc., which can be used to create, delete, and modify the structure of data tables.

  • Keywords for main statements include CREATE , DROP , ALTER , etc.

DML (Data Manipulation Language)

Used to add, delete, update and query database records, and check data integrity.

  • Keywords for main statements include INSERT , DELETE , UPDATE , SELECT , etc.

DCL (Data Control Language)

Used to define access permissions and security levels for databases, tables, fields, and users.

  • The keywords of the main statements include GRANT , REVOKE , COMMIT , ROLLBACK , SAVEPOINT , etc.

2. Rules and specifications of SQL language

2.1 Basic rules

  • SQL can be written in one or more lines. Multiple lines are recommended and indented if necessary.
  • Each command ends with; or \g or \G
  • Keywords cannot be abbreviated or split into lines
  • Regarding punctuation marks,
    you must ensure that all (), single quotes, and double quotes end in pairs. You
    must use the half-width input method in English. For
    string and date and time type data, you can use single quotes ('') to indicate
    column aliases. , try to use double quotes (" "), it is not recommended to omit it

2.2 SQL writing specifications

  1. It is not case sensitive in Windows environment.
  2. It is case sensitive in Linux environment
  3. It is recommended to use unified writing standards:
    database names, table names, table aliases, field names, field aliases, etc. should all be in lowercase.
    SQL keywords, function names, bind variables, etc. should all be in uppercase.

2.3 Comments

Single line comment: #Single
line comment: -- comment text
Multi-line comment: /* */

2.4 Naming convention

2.5 Data import instructions

Method 1: Full path name of the source file
Method 2: Import data based on a specific graphical interface

3. Basic SELECT statement

3.0 SELECT…

SELECT 1

3.1 SELECT…FROM

Wildcard (*), deprecated

# 选择全部字段
SELECT *
FROM department;

Select specific columns:

SELECT departments.department_id,departments.location_id
FROM departments

3.2 Column aliases

  • Rename a column to make calculations easier.
  • method:

1. Use the keyword AS (AS can also be omitted)

SELECT employees.last_name AS name , employees.commission_pct comm
FROM employees

2. Use double quotes for aliases

SELECT employees.last_name "name"
FROM employees

3.3 Remove duplicate rows

By default, the query returns all rows, including duplicate rows.
Method: Use the DISTINCT keyword to remove duplicate rows

SELECT DISTINCT employees.department_id
FROM employees;

Wrong behavior:

SELECT employees.salary,DISTINCT employees.department_id
FROM employees;

Meaningless behavior:

SELECT DISTINCT employees.department_id,employees.salary
FROM employees;

3.4 Null values ​​participate in operations

  1. Null value: null
  2. null is not equivalent to 0
  3. Null values ​​participate in operations: all operators or column values ​​encounter a null value, and the result of the operation is null.
  4. In MySQL, a null value is not equal to an empty string. The length of an empty string is 0, and the length of a null value is empty. At the same time, the null value takes up space.
SELECT employees.employee_id, employees.salary "月工资", employees.salary * ( 1 + employees.commission_pct ) * 12 "年工资"
FROM employees
SELECT employees.employee_id, employees.salary "月工资", employees.salary * ( 1 + IFNULL(employees.commission_pct,0) ) * 12 "年工资"
FROM employees

3.5 Emphasis

  • After using keywords to name, you need to use emphasis numbers to distinguish them.

3.6 Query constants

SELECT "尚硅谷" , employees.first_name
FROM employees;

4. Display table structure

  • Displays details of the fields in the table.
DESCRIBE employees;
DESC employees;

5. Filter data WHERE

The filter conditions are after FROM

SELECT *
FROM employees
WHERE employees.last_name = 'King'

$Chapter 3 Exercises:

# 查询员工12个月的工资总和,并起别名ANNUAL SALARY 
SELECT employees.employee_id , employees.last_name , employees.salary * 12 "ANNUAL SALARY"
FROM employees;

# 查询employees.salary表中去除重复的employees.job_id以后的数据
SELECT DISTINCT employees.job_id
FROM employees;

# 查询工资大于12000的员工姓名和工资
SELECT employees.last_name , employees.salary
FROM employees
WHERE employees.salary > 12000

# 查询员工号为·176的员工的姓名和部门号
SELECT employees.last_name , employees.department_id
FROM employees
WHERE employees.employee_id = 176;

# 显示表departments的结构·,并查询其中的全部数据
DESCRIBE departments;

3. Operators

1. Arithmetic operators

Arithmetic operators are mainly used for mathematical operations. They can connect two numbers or expressions before and after the operator, and perform +, -, *, \, and % on numbers or expressions.

1. Addition and subtraction operators

  • A value of type integer performs addition and subtraction operations on integers, and the result is still an integer;
  • An integer type value performs addition and subtraction operations on floating point numbers, and the result is a floating point number;
  • In MySQL, + only adds numbers. (If a non-numeric type is encountered, it will be converted to a numeric value first; if it fails, it will be calculated as 0) [Supplement: String concatenation in MySQL is implemented using string CONCAT()]

2. Multiplication and division remote operators

  • When a number is multiplied by the integer 1 and divided by the integer 1, the original number is still obtained;
  • A number multiplied by a floating point number 1 and divided by a floating point number 1 becomes a floating point number, and the value is equal to the original number;
  • After a number is divided by an integer, the result will be a floating point number regardless of whether it can be divided completely;
  • When one number is divided by another number and cannot be divided, the result is a floating point number and is retained to 4 decimal places;
  • Multiplication and division have the same priority. Multiplication first and then division will produce the same result as division first and then multiplication.
  • In mathematical operations, 0 cannot be used as a divisor. In MySQL, a number divided by 0 is NULL.

2. Multiplication and division remote operators

Guess you like

Origin blog.csdn.net/xuansoyo/article/details/127579420