Common statement MYSQL database

First, the command used to operate the database
1.show databases; to see all database
2.create database test; create a database called test of
3.drop database test; delete a test database called
5.show tables; the database selected view all the tables
4.use test; select the library, before the construction of the table must choose the database
8.drop table table name; delete the table
6.create table table name (field type 1, type 2 field);
7.desc table name; see the field where the table
10.show create table table name; see more information on creating tables of
two, command to modify the table
1. modify the field type alter table table name field, modify field types;
2. Add a new field alter table add the table name field type field
3. Add the specified location field and add alter table table name field type field after field;
4. remove field alter table drop table field name;
5. modify the specified field alter table change the original table name field name of the new field name field type
three operations on the data
. increase the data (insert) 3 ways to insert into table values (value 1, value 2, ...) (less)
insert into table (Field 1, Field 2 ...) values (value 1, value 2, ...); (more commonly)
insert into table (Field 1, Field 2 ...) values (value 1, value 2, ...), (value 1, value 2, ...), (value 1, value 2, ...);
2. delete data (delete) delete from table where conditions Note: where conditions must be added, otherwise all data will be deleted
3. update data (Update)
Update table set field value 1 = 1, the value of field 2 = 2 WHERE condition
3. field select distinct screening duplicate field from table values
Note: where must be added to otherwise modify the data of all
4 queries data (select)
1. All the data in the lookup table select * from table
2. designating data query select field from table
set id [not] in (1,2) Fuzzy queries: like '% a%'; 5 sort the result set.
4 according to the condition check out the data select field from table where condition (most common)
conditions behind with the conditions where the
relationship:>, <,> =, <=, =,!
logic: or, and
the interval: id between 4 and 6; closed interval, including the boundary
1, be sorted by a field
example: select * from star orser by money desc, age asc; 6. result set limits
select field from table order by sorting keyword field (desc | asc)
sorting keyword desc asc ascending descending (default)
2, multi-field sort
select field from table order by field desc. 1 | asc, desc ... n-field | asc;
select limit the number of fields from table;
example: select sum (id) from star 8. packet
select * from limit table offset, number
Description:
1. do not write is the default offset is 0, then
when the tab 2. to achieve you must write offset
offset how to calculate? :
Limit (. 1-n-) * Number, Number
7 functions commonly used statistical
sum, avg, count, max, min
only packets: select * from table group by field
example: select count (sex) as re , sex from star group by sex having re> 3;
packet statistics: select count (sex) from star group by sex;
the packet filtering result set
multi-table queries joint

1, the connecting
implicit within the connector select username, name from user, goods where user, gid = gods, gid;
connected to the display
SELECT username, from the Join User Inner Goods user.gid ON = goods.gid;
SELECT * from left User Goods ON user.gid = goods.gid the Join;
2. external link
left connected to the left contains a record of all records in the table and the right table does not match his
right connecting
select * from user where gid in ( select gid from goods) ;
SELECT * from User right user.gid = ON JOIN Goods goods.gid;
sub nested query
data federation query
select * from user left join goods on user.gid = goods.gid union select * from user right join goods on user. gid = goods.gid;
two tables while updating the
update User U, SET u.gid Goods G = 12 is, g.price = WHERE u.id = 2. 1 and u.gid = g.gid;
the DCL data control language
Create a user: create user'xiaoming '@' localhost ' identified by' 666666 ';
authorized user:. Grant All ON the Test to'xiaoming' @ 'localhost';
refresh permissions: flush privileges;
deauthorize: All REVOKE ON the Test.
From 'xiaoming' @ 'localhost';
delete users: drop user'xiaoming '@' localhost ';
DTL language data transaction
opening transaction: set autocommit = 0;
rollback: rollback;
commits the transaction: commit;

Guess you like

Origin blog.csdn.net/qq_40380640/article/details/98098825