[MySQL] MySQL entry study notes

table of Contents

SQL

SQL statement classification

DDL statement

    DDL statement operating database

    DDL statement operating data table

DML statement

    increase

    delete

    change

DQL statement

DQL sentence enhancement

limit statement

Database constraints:


SQL

    Structured Query Language (Structured Query Language) is an operating language for databases. We can use SQL statements to conveniently operate on data tables in the database.

SQL statement classification

  • DDL: Data Definition Language
    • Used to define database objects: databases, tables, columns, etc.
    • Keywords: create drop alter truncate
  • DML: Data Manipulation Language
    • Used to add, delete and modify the data in the database table
    • Keywords: insert delete update
  • DQL: Data Query Language
    • Language for querying the database
    • Keyword: select
  • DCL: Data Control Language
    • Used to define database access rights and security levels and create users
    • Keywords: GRANT REVOKE
  • TCL: Transaction Control Language
    • Used to control the transaction operation of the database
    • Keywords: COMMIT SAVAPOINT ROLLBACK

DDL statement

Operate the database through DDL statements: create a library, display a library, modify a library, delete a library

Operate database tables through DDL statements: create table display table modify table delete table operation

Operate the columns in the database table through DDL statements: add columns modify columns delete columns   

    DDL statement operating database

//创建数据库
CREATE DATABASE 数据库名;

//判断是否存在并创建数据库
CREATE DATABASE IF NOT EXISTS  数据库名

//创建数据库并指定字符集
CREATE DATABASE 数据库名 CHARACTER SET 字符集;

//查看数据库
SHOW  Database

//查看某个数据库的定义信息
SHOW CREATE DATABASE 数据库名

//修改数据库(修改字符集)
ALTER DATABASE 数据库名 DEFAULT CHARACTER SET 字符集;


//删除数据库
DROP DATABASE 数据库名

//使用数据库
//查看正在使用的数据库
SELECT DATABASE();

//使用/切换数据库
USE 数据库名

    DDL statement operating data table

//创建表
CREATE TABLE 表名 (
    字段名1 字段类型1,
    字段名2 字段类型2…
);

//查看表
SHOW TABLES;

//查看表结构
DESC 表名;

//查看创建表的SQL语句
SHOW CREATE TABLE 表名;

//快速创建一个相同的表
CREATE TABLE 新表明 LIKE  旧表名



//删除表
DROP TABLE 表名;
//表存在则删除
DROP TABLE IF EXISTS 表名;


//修改表
//表中添加列
ALTER TABLE 表名 ADD 列名 类型;

//修改列类型
ALTER TABLE 表名 MODIFY 列名 新的类型;

//修改列名
ALTER TABLE 表名 CHANGE 旧列名 新列名 类型;

//删除列
ALTER TABLE 表名 DROP 列名;

//修改表名
RENAME TABLE 表名 TO 新表名;

//修改字符集
ALTER TABLE 表名 character set 字符集;

DML statement

    increase

//插入全部字段
INSERT INTO 表名 (字段名1, 字段名2, 字段名3…) VALUES (值1, 值2, 值3);
//或者
INSERT INTO 表名 VALUES (值1, 值2, 值3…);
//注意:不写字段名的情况下,values后面值的顺序要和数据内字段的定义顺序要保持一致。
//注意:
//值与字段必须对应 ,个数相同,类型相同
//值的数据大小必须在字段的长度范围内
//除了数值类型外,其他的字段类型的值必须使用引号引起(建议单引号)
//如果要插入空值,可以不写字段,或者插入null



//批量插入数据
INSERT INTO 表名 
    (字段名1, 字段名2, ...) 
    VALUES     
    (值1, 值2, ...),
    (值3, 值4, ...);

    delete

//不带条件的删除数据
DELETE FROM 表名;

//带条件的删除
DELETE FROM 表名 WHERE 字段名=值;

    change

//不带条件的修改
UPDATE 表名 SET 字段名=值;


//带条件的修改
UPDATE 表名 SET 字段名=值 WHERE 字段名=值;

DQL statement

//简单查询
Select *  from 表名

//查询某列数据
select 字段名1, 字段名2 ...from 表名

//别名查询
select 字段名1 as 别名,字段名2 as 别名 ... from 表名

//查询结果参与运算
select 列名1+固定值  from 表名
select 列名1+列名2 from 表名

 

Remove duplicate values ​​and null value processing:


  • //Clear duplicate values
    //The key to clear the field name is DISTINCT
    //A select statement can only have one distinct, and it can only be written after select
    select distinct field name from table
  • IFNULL (expression 1, expression 2): If the value of expression 1 is null, the result of expression 2 is returned. If the value of expression 1 is not null, the result of expression 1 is returned

DQL sentence enhancement

  • Determine whether a variable is null (field is null)
    • select * from table name where column name is null 
  • Comparison operator
    • > Greater than <less than <= less than or equal >= greater than or equal
    • = Equal to <>! = Not equal to
      • select * from table name where column name comparison operator comparison value
  • Logical Operators
    • and (&&): multiple conditions are met at the same time
    • or (||): one of multiple conditions is met
    • not (!): Not satisfied
      • select * from table name where condition 1 logical operator condition 2
  • range
    • in  : Each data in in will be used as a condition, and it will be displayed as long as the condition is met
      • SELECT field name FROM table name WHERE field in (data 1, data 2...);
    • between...and..: used to represent data that meets a range condition, header and end
      • select * from table name where condition between value 1 and value 2 
    • Like Keyword : Indicates fuzzy query, the data meet the following wildcard rules will be displayed
      • %: Represents 0 or more characters (any characters)
      • _: Represents a character
//查询姓马的学生
select * from student where name like "马%";

//查询姓马的且为两个字符的学生
select * from student where name like "马_"

//查询姓名含有马的学生
select * from student where name like"%马%" 
  • Sort (ORDER BY)
    • Single column sort
    • SELECT 字段名 FROM 表名 WHERE 字段=值 ORDER BY 字段名 [ASC|DESC];
      ​​​​​​​
      • ​​​​​​​ASC: Ascending order, default value
      • DESC: descending order
    • Combination sort
      • Sort by the first field first, if the first field is the same, sort by the second field, and so on
      • ​​​​​​​
        SELECT 字段名 FROM 表名 WHERE 字段=值 ORDER BY 字段名1 [ASC|DESC], 字段名2 [ASC|DESC];
        
  • Aggregate function (written in the field name after select)
    • count : Count the number of records in the specified column, and the records that are NULL are not counted
    • sum : Calculate the numerical sum of the specified column, if it is not a numerical type, the calculation result is 0
    • max : Calculate the maximum value of the specified column
    • min : Calculate the minimum value of the specified column
    • avg : Calculate the average value of the specified column, if it is not a numeric type, then the calculation result is 0

 

select 聚合函数(字段名) from 表名
  • Grouping
    • GROUP BY treats the same content in the grouping field results as a group, and returns the first data of each group.The purpose of grouping is to count the number.General grouping and aggregation functions are used together.
    • SELECT field 1, field 2... FROM table name GROUP BY grouping field [HAVING condition];
  • The difference between having and where
    • having performed on the data in the packet filter.
    • where is to filter the data before grouping
    • Aggregate functions can be used behind having
    • Aggregate functions cannot be used behind where

limit statement

     limit in a limiting sense, the role Limit is the number of records for limiting the queries,

  • The syntax format of limit:
    • ​​​​​​​LIMIT offset,length ;  或者LIMIT length
    • offset refers to the number of skipped records (scientific name: offset), the default is 0
    • length refers to the total number of records that need to be displayed
//跳过前面2条,取6条数据 
SELECT * FROM student3 LIMIT 2,6;

 

Database constraints:

  • Primary key constraint: PRIMARY KEY
    • ​​​​​​​Used to uniquely identify a record, each table can only have one primary key
  • Unique constraint: UNIQUE
    • ​​​​​​​Uniqueness, the value of this field cannot be repeated
  • Non-empty constraint: NOT NULL
    • ​​​​​​​Constrain a field not to be a null value
  • Default value: DEFAULT
    • ​​​​​​​When adding data to the table, the data of the field is not specified, and the set value is displayed by default

Guess you like

Origin blog.csdn.net/weixin_43267344/article/details/108685823