Add, delete, modify and query syntax of mysql database.

1. View database (3 types)
(1) show databases;
(2) show databases \G
(3) mysql -e 'show databases' -uroot -p123.com

2. Create database
Syntax: create database database name;

3. View the current database
Syntax: show tables;

4. Delete database
Syntax: drop database database name;

5. If clause
Syntax: drop database if exists table name; (delete if it exists)
Syntax: create database if exists table name; (create if it does not exist)

6. Syntax for creating a table: create table indicates (segment name type);

7. View the structure of the table: desc table name;

8. Check the commands executed when creating the table: show create table table name;

9. Delete table: drop table table command; delete database: drop database database name;

10. Modify the table name alter
syntax: alter table table name rename new table name;

11. Modify the field type in the table Syntax
: alter table table name modify modify name type;

12. Modify the field type and field name in the table
Syntax: alter table table name change source field name new field new type;

13. Add fields to the table
Syntax: alter table table name add field name field type;

14. Add a field at the specified position in the table and
 add it in the first row, syntax: alter table table name add field name field type first;
add at a specified position, syntax: alter table table name add field field type after specified field;

15. Delete the fields in the table
Syntax: alter table table name drop field name;

    About the operation in the table
1. Insert record
Syntax: insert into table name values ​​(field value 1, field value 2);

2. Separately insert table records
Syntax: insert into table command (field 1, field 2) values ​​(field value 1, field value 2);

3. Query the records in the table
Syntax: select * from table name;

4. View the specified field record
Syntax: select field from table name;

5. View tables in other databases or not in this database
Syntax: select field from database name.table name;

6. Delete records in the table
Syntax: delete from table name where field = field value;

7. Update records, update modifies the data in the table.
Syntax: update table name set field name to be modified where field number;

    SQL conditional query statement
1. Deduplication query
Syntax: select distinct field from table name;

2. Use AND and OR for multi-condition query
Syntax: select field from table where condition and condition; (jointly established)
Syntax: select field from table where condition or condition; (one of them can be established)

3.mysql is case-sensitive to query binary.
mysql is case-insensitive by default; binary is case-sensitive.
Syntax: select field from table where field condition; (for example: where name='qwe' or where name='QWE')

4. MYSQL query sorting
Syntax: select field 1 field 2 from table order order by field name asc (desc); (asc defaults to ascending order, desc descending order)

5.Mysql query command help
Syntax: help command to be queried;

    mysql data type
1.int type modifier
unsigned unsigned integer, can only save positive integers; zerofill modifier, fill the output value with 0.
Syntax: create table table (field type unsigned or zerofill, field type unsigned zerofill);

2. The floating-point data type
Float represents a single-precision floating-point value, double represents a double-precision floating-point value, float and double are floating-point types, and decimal is a fixed-point type.
The double and decimal numeric types are more accurate than float for storing decimals.
Syntax: create table table (field floating point type (n, m)); (n, m) n is an integer bit, m is a decimal reserved bit

3. The difference between char and varchar
:
char needs to specify the range of the string, 0~255; char processes data types quickly.
The varchar string can be of variable length, 0~65535, except the start and end 3 characters; varchar is more flexible to use and occupies less memory and hard disk space.

4. enum enumeration: only one of the values ​​can be taken; set: multiple values ​​cannot be repeated.

     Mysql basic command statement
1. Modify data type, add field
  Syntax: alter table table name add field name column type [not null | null] [primary key] [unqiue] [auto_increment] [default varlue]; not null not empty,
null Empty; primartkey primary key; unqiue unique index; auto_increment column value increases automatically; default default.

2. The difference between the primary key and the unique index:
the primary key is a constraint, and the unique index is an index, and the two are essentially different;
after the primary key is created, it must contain a unique index, and the unique index is not necessarily the primary key;
unique Indexed columns allow null values, while primary key columns do not allow null values.

    Commonly used select commands
1. Current date and time: select now( );

2. Current date: select curdate( );

3. Current time: select curtime( );

4. Current database: select database( );

5. Current user: select user( );

6. Current system information: select variables;
Syntax: show [global | sesslon] variables [like_or_where];

7. View system running status information: show processlist;
running status: show [global | session] status [like_or_where];

    Import and export database mysqldump
1. Export database (backup)
Syntax: mysqldump -u username -p password database name > exported file name

2. Import database (restore)
syntax: mysql -u username -p password database name < exported file name or source /root/database.table name

     Advanced mysql statement
1. Logical operator
and and; or or; not not
For example: select field from table name where condition (eg price>40 and price<60);

2. Arithmetic operator
= is equal to; =! Not equal to; > Greater than; < Less than; >= Greater than or equal to; <= Less than or equal
In the where statement, not in is opposite to in.
 Syntax: select field from table name where condition;

3. Sorting
Ascending order: order by 'sort field' asc
Descending order: order by 'sort field' desc
For example: select price from books where price in(30,40) order by price desc; (in the books table according to price 30 to 40 Sort in descending order.

4. Range operation
Between......and... between what to what
Between....and.... You can use > and < instead of more accurate
example: select price from books where price between 10 and 20 ; or select price from books where price >30 or price <50;

5. Fuzzy matching query
Syntax: field [not]like 'wildcard'; % any character
For example: select bname from books where bname like '%program%'; In the books table, the name of the query starts or ends with the title of the program .

     mysql subquery (use the result of subquery for external query)
1. Common query
syntax: select field from table where field condition;

2. Nested query
Syntax: select field from table where field=(select field from table where field condition);
for example: select bname,btypeid from books where btypeid=(select btypeid from category where btypename='plane');

3. Limit limits the displayed entries
Syntax: select * from table limit offset, number of rows;

4. Combine sub-queries with limit entries and arithmetic operations to query
1) Nested sub-queries
such as: select bname, price from books where price < (select price from books where publishing="Tsinghua University Press" order by price limit 0 ,1);
2) limit is not applicable, and all means the minimum value among the returned values ​​in the subquery.
For example: select bname,price from books where price < all(select price from books where publishing="Tsinghua University Press");

5. Multi-table join query
Inner join: Match according to the common fields in the table
Outer join: Left outer join, right outer join.

1) Inner join Matching
is performed based on the common fields in the table, and the records that meet the conditions in the table are connected.
Syntax: select field from table 1 inner join table 2 on table 1. field = table 2. field;
for example: select i.xh,i.xm,c.yw,c.shx from stu_info i inner join stu_chj c on i. xh=c.xh;

2) Outer join (divided into left outer join; right outer join)
left join: select field from a table left join b table on connection condition; a main table displays the content of the specified field, b from the table

Right join: select field from a table right join b table on connection condition; a is the slave table; b is the main table, both are displayed.

6. Aggregation function (a code block that performs a specific function)
1) sum ( ) sum; syntax: select sum (field) from table;

2) avg ( ) average value; syntax: select avg (field) from table where condition;

3) max( ) maximum value; syntax: select max(field) from table;

4) minimum value of min( ); Syntax: select min(field) from table;

5) count( ) Count the number of records; Syntax: select count(*) from table; select count(distinct price) from books; remove duplicate prices in the books table and make statistics.

7. Arithmetic operation
+ - * /
Syntax: update table set condition where condition;

8. String function
Substr (field name, start, end) intercept
Syntax: select substr (field, start, end) from table where condition;

Concat (beginning, separation, end) splicing
Syntax: select concat (field, division, end) from table;

9. Case conversion
upper( ) uppercase; lower( ) lowercase
Syntax: select upper[lower](character) from table where specified condition;

The difference between Null and not null
1) The field type is not null, why can a null value be inserted?
Null values ​​take no space; nulls in mysql take space.
2) Why not null is more efficient than null?
Null is not an empty value and needs to occupy space. When comparing, null will participate in the comparison of fields.
3) When judging whether a field is null, you can use: select * from table name where is not null; or select * from table name where <>'';

The difference between delete and truncate
1) delete is used to delete the data in the table, add the where condition to delete the matching condition, and clear the table if not added; truncate clears the table without adding the where condition.
2) The fields in the table are set into_increment, and delete will not clear the auto-increment counter; truncate will clear the auto-increment counter.
3) The deletion operation of delete will be recorded in the bin-log log; the deletion operation of truncate will not be recorded in the bin-log log.

Guess you like

Origin blog.csdn.net/kking_Ran/article/details/124783401