The second day of database learning

The second day of database learning

Today's content

Data in DML operation table

1 Insert record

  1. Insert all fieldsinsert into 表名 (字段名1,字段名2,字段名3.....)values(值1,值2,值3.....);
  2. You can also not write the field nameinsert into 表名 values(值1,值2,值3.。。);
  3. Insert some datainsert into 表名(字段名1,字段名2,。。。)values (值1,值2,。。。);
    Note that fields with no added data will use null
    When inserting character type and date type data, you must add double quotation marks or single quotation marks

2 Update table records

  1. update 表名,set 列名=值[where 条件表达式];如:UPDATE table1 SET NAME="yyy";At this time, if no conditions are added, all fields in this column will be changed.

3 Delete table records

  1. delete from 表名 [where 条件表达式];如delete from table1 where name=yyy;If the where clause is not specified, all records will be deleted, so it is a dangerous move, and generally not recommended to use this method, the efficiency is very low, it will delete statements one by one. Generally, truncate table 表名;this statement is used to directly delete the table, and then create a table with exactly the same structure.

4 DQL query table records

Simple query

1. Query all columns select * from 表名;
2. Query the data of the column of the specified field, separate multiple data with commasselect 字段名1,字段名2,...from 表名;

Specify the alias of the column to query

  1. select 字段名1 as 别名1,字段名2 as 别名2... from 表名 as 表别名;Both columns and tables can have aliases that can be used to simplify queries

Clear duplicate values

  1. Query the specified column and ensure that there are no duplicate fieldsselect distinct 字段名 from 表名;

Participate in the calculation of query results

  1. Calculation of a certain column of data and fixed valueselect 列名+固定值 from 表名;
  2. A column of data and other column data participate in the calculationselect 列名1+列名2 from 表名;
  3. The calculation here must be a numeric type

Conditional query

  1. Conditional query syntax: select 字段名 from 表名 where 条件;its process is to take out all the data in the table, and return if the condition is met, and not return if the condition is not met.
  2. Operator
    Insert picture description here
  3. Logical operator
    Insert picture description here
    The wildcard in fuzzy query is explained here: Insert picture description here
    _ represents a character, and% is 0 or more.

Sort query

  1. Sort query
    * Syntax: order by clause
    *order by 排序字段1 排序方式1 , 排序字段2 排序方式2...

     * 排序方式:
     	* ASC:升序,默认的。
     	* DESC:降序。
    
     * 注意:
     	* 如果有多个排序条件,则当前边的条件值一样时,才会判断第二条件。
    

Aggregate function

Take a column of data as a whole and perform vertical calculations.

  1. count: count the number
    1. Generally choose non-empty columns: primary key
    2. count(*)
    2. max: calculate the maximum value
    3. min: calculate the minimum value
    4. sum: calculate and
    5. avg: calculate the average
    note! The calculation of aggregate functions automatically exclude fields with null
    Solution: 1. Use the ifnull statement to judge. select count(ifnull(id,0)) from student;If the id value is null, give a default value of 0;
    2. Select a column that does not contain non-empty for calculation

Group query

  1. Syntax: group by group field;
    2. Note:
    1. Fields to be queried after grouping: grouping fields, aggregate functionsFor example: At SELECT sex , AVG(math) FROM student GROUP BY sex;this time, except for sex and aggregate function followed by select, nothing else is meaningful.
    2. The difference between where and having?
    (1) Where is defined before grouping. If the conditions are not met, it will not participate in grouping. Having is limited after grouping. If the result is not satisfied, it will not be queried.
    (2) Aggregate functions cannot be followed after here, and having aggregate functions can be judged.
    Comprehensive use: SELECT sex , AVG(math),COUNT(id) FROM student WHERE math > 70 GROUP BY sex HAVING COUNT(id) > 2;
    You can also use aliases to simplify:SELECT sex , AVG(math),COUNT(id) 人数 FROM student WHERE math > 70 GROUP BY sex HAVING 人数 > 2;

Paging query

  1. Syntax: index starting with limit, the number of queries per page;
    2.Formula: starting index = (current page number-1) * the number of items displayed on each page(Very important!) The back is how much data you display on each page, and the front is to calculate the data that should be transmitted on the first page
    -each page shows 3 records

     	SELECT * FROM student LIMIT 0,3; -- 第1页
     	
     	SELECT * FROM student LIMIT 3,3; -- 第2页
     	
     	SELECT * FROM student LIMIT 6,3; -- 第3页
    
     3. limit 是一个MySQL"方言",别的不是这么用的!
    

constraint

Concept: Limit the data in the table to ensure the correctness, validity and completeness of the data.

  • Classification:
    1. Primary key constraint: primary key
    2. Non-empty constraint: not null
    3. Unique constraint: unique
    4. Foreign key constraint: foreign key

  • Non-empty constraint: not null, the value cannot be null
    1. Add constraints when creating a tableCREATE TABLE stu( id INT, NAME VARCHAR(20) NOT NULL -- name为非空 );

  1. After creating the table, add a non-null constraint: ALTER TABLE stu MODIFY NAME VARCHAR(20) NOT NULL;(At this time, ensure that the column does not have null data before adding this constraint)
    `

  2. Remove the non-empty constraint of name:ALTER TABLE stu MODIFY NAME VARCHAR(20);

  • Unique constraint: unique, the value cannot be repeated
    1. When creating a table, add a unique constraintCREATE TABLE stu(id INT,phone_number VARCHAR(20) UNIQUE -- 添加了唯一约束)

    Note that in mysql, the value of the column limited by the unique constraint can have multiple nullsThat is to say, multiple nulls in mysql do not mean repetition, mysql will consider this value to be uncertain

    1. Delete the unique constraint:ALTER TABLE stu DROP INDEX phone_number;

    2. After creating the table, add a unique constraint:ALTER TABLE stu MODIFY phone_number VARCHAR(20) UNIQUE;

Guess you like

Origin blog.csdn.net/m0_54814599/article/details/115256521