Add, delete, check and modify (CRUD) in MySQL

Table of contents

New

insert into table name value(data, data),.....;

insert into table name (column 1, column 2.....) value(data, data),.....;

How to insert data of datatime type?

Inquire

select * from table name;

select column 1, column 2... from table name;

The query field is an expression

Aliases the query result columns

Remove duplicates: DISTINCT

Sort by: ORDER BY

Conditional query

comparison operator

Logical Operators

Note: Execution order of select conditional query

Pagination query: LIMIT

delete

delete from table name;

delete from table name where condition;

Revise

update table name set (column name = value), (column name = value).... where condition;


First create a table named: title: all operations below are based on this table

Note: BecauseMySQL is not case-sensitive so both uppercase and lowercase are acceptable.

New

insert into Table name values/value(number position, number position),.... ..;

Can be inserted in single line or multiple lines.

insert into Table name (column 1, column 2....) value( number position, number position),.......;

Specify column insertion, can be inserted in single row or multiple rows.

How to insert data of datatime type?

You can use afixed formatstring to represent the date

You can also use the now() function to obtain the current time

Add new table field

alter table table name add field name and type

The fields of the table after insertion are:


Inquire

select * from 表名;

Full column query Display all data in the table

* display pass mark possibleindex possessive sequence

select 列1,列2...... from 表名;

Specify column query

QueryThe field is an expression

  • QueryMinus 20 points for Chinese language score

  • Check each subjectThe total score

Note:The last two rows are null becauseIn MySQl, the result of null and any value is null

Give an alias for the query result column 

select expression/column name as alias from Table name;

Left weight:DISTINCT


select distinct single column/multiple columns from table name;

willremoveduplicates from the query results (retaining only one item)

排序:ORDER BY

select * from 表名 order by 列名 asc/desc

  • ASC is ascending order (from small to large)
  • DESC is descending order (from large to small)
  • Default is ASC

Column name can be single orcan be multiple

例:select * from 表名 order by A,B ;

Sort by B if A is equal

select * from 表名 order by A asc,B desc

Arrange according toA column in ascending order and B column in descending order

Specify a column to be sorted in ascending/descending order 

null is considered the minimum value, but only in sorting.


Conditional query

select * from table namewhere expression/condition (cannot be an alias )

Display data that meets the conditions

comparison operator

operator illustrate
>, >=, <, <= Greater than, greater than or equal to, less than, less than or equal to
= Equal to, NULL is unsafe, for example, the result of NULL = NULL is NULL -> false
<=> Equal to, NULL is safe, for example, NULL <=> The result of NULL is TRUE(1)
!=, <> not equal to
BETWEEN a0 AND
a1
range matching, [a0, a1], closed interval, if a0 <= value <= a1, return TRUE(1)
IN (option, ...) If it is any one of options, return TRUE(1)
IS NULL is NULL
IS NOT NULL Not NULL
LIKE ‘..%../.._..’ Fuzzy matching. % represents any number (including 0) of any character; _ represents any character
character

LIKE ‘..%../.._..’ usage example:

select * from table name where column name like 'Sun%';

Find records in the specified column that meet the conditions behind like.

  1. ‘%Sun’: The last subcharacter of the string is ‘Sun’
  2. ‘Sun%’: The first character of the string is ‘Sun’
  3. ‘%Sun%’: The string contains ‘Sun’ 

Logical Operators

operator illustrate
AND Multiple conditions must all be TRUE(1) for the result to be TRUE(1)
OR If any condition is TRUE(1), the result is TRUE(1)
NOT The condition is TRUE(1), the result is FALSE(0)

Example: Meet people whose math score is greater than 80

Note: Execution order of select conditional query

  1. Iterate through each record in the table
  2. Bring the value of the current record into the condition and filter based on the condition
  3. If this record meets the conditions, retain it and evaluate the expression on the column.
  4. If there is order by, all results will be sorted after all rows have been obtained (the expression has been calculated).

Becausethe third step is to define the alias, and where is executed in the second step< /span>So an error will be reported;

Pagination query: LIMIT


select * from table name limit the number of rows to be queried;

select * from table name limit the number of rows to be queried  offset offset ('subscript'from 0 start);

delete

delete from table name;

Deletes all data in this table, but does not delete the table.

delete from table name where condition;

Revise

update table nameset column name=value, column name=value.... where Condition;

You can modify one column or multiple columns.

The where condition here is to limit which values ​​can be modified.

Modify one column at a time:

Modify multiple columns at once:

Numeric types in MySQL

type of data size illustrate
BIT[ (M) ] M-designated position
number, black
为1
Binary number, M ranges from 1 to 64,
stores value range from 0 to 2^M-1
TINYINT 1 byte Equivalent to byte in JAVA language
SMALLINT 2 bytes Equivalent to short in JAVA language
INT 4 bytes
BIGINT 8 bytes Equivalent to Long in JAVA language
FLOAT(M, D) 4 bytes Single precision, M specifies the length, D specifies
the number of decimal places. Loss of precision will occur
DOUBLE(M,
D)
8 bytes Double precision, M specifies the length, D specifies
the number of decimal places. Loss of precision will occur
DECIMAL(M,
D)
M/D maximum
值+2
Double precision, M specifies the length, D represents
the number of decimal places. Exact value
NUMERIC(M,
D)
M/D maximum
值+2
Same as DECIMAL

Character types in MySQL:

type of data size illustrate
VARCHAR (SIZE)  0-65,535 bytes Variable length string, size is the character length
TEXT 0-65,535 bytes long text data
MEDIUMTEXT 0-16 777 215 bytes Medium length text data
BLOB 0-65,535 bytes Long text data in binary form

Date types in MySQL:

type of data size illustrate
DATETIME 8 bytes The range is from 1000 to 9999, and no time zone retrieval and conversion will be performed
.
TIMESTAMP 4 bytes Ranges from 1970 to 2038, automatically retrieves the current time zone
and converts.

Constraint types in MySQL

constraint type illustrate Example
NULL constraint Use NOT NULL to specify that the column is not
empty
name varchar(20) not null,
UNIQUE unique constraint Specify columns that are unique and non-duplicate name varchar(20) unique,
DEFAULT default value is about
bundle
Specifies the default value when the column is empty age int default 20,
primary key constraints NOT NULL and UNIQUE
combined
id int primary key,
foreign key constraints Relate other tables’ primary or unique keys foreign key (field name) references main
table (column)
CHECK promise (completed
solution)
Guarantee that the values ​​in the column meet the specified conditions
check (sex ='male' or sex='female')

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/133554676