Getting a MySQL Notes

MySQL application note a
MySQL relational database, open source, small and medium companies commonly used type of database
Oracle database used large companies

MySQL basic commands a,

Create, delete, view database (database)
to create a database
create database database_name;
the default is to create a database of the Latin character set

Create a character set gbk format database
create database database_name DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

Delete database
drop database database_name;

View database
show database; - See all database
select database (); - view the database currently in use
show database create database_name; - see create database database_name statements
show database like 'database%'; - view the database to begin with the database
select user (); - which users view the current database connection

Create a user, authorization, revoke permissions
Grand * All ON database_name to 'user_name' @ 'localhost' IDENTIFIED by '123456';.
- Create a user user_name database_name database and grant all permissions for all tables
flush Privilege;
- permission rights, privileges become effective

Grants for Show 'user_name' @ 'localhost';
- view the user user_name which have operating authority

Revoke the authority (REVOKE)
. REVOKE INSERT, Update, Delete, database_name ON SELECT * from 'user_name' @ 'localhost';
- revoke user user_name deletions of all tables in the database in addition to change permissions database

 

Create table, drop table

Create Table
Create Table table_name (
id int (. 4) Not null,
name char (20 is) Not null,
Age int (. 3) Not null)
- creates a table named table_name, there are three non-empty fields in the table id, name, age

table_name Show;
- see the table table_name

table_name desc;
- see table table_name table structure

show create table table_name / G; (examination revealed different versions of different syntax: Show the Create the Table table_name;)
- view the construction of the table statement

Drop table
drop tables table_name;

 


Database Backup - Import Export (test test libraries often need to back up data to deploy new environments)
the SELECT * from database_name;
- view the database database_name

Single database backup
the mysqldump -uroot--p database_name> / Home / downloads / database_name $ (DATE +% F.) .Sql;
- database_name database backup to sql file, contents of the backup table statement is built, the data insert statements like

Compressed backup
mysqldump -p-uroot--B the Test | gzip> / download / testbak _ $ (DATE +% F) .sql.gz
- generated gz file, reducing the space occupied by memory

Backup single table (common)
the mysqldump -uroot--B -p database_name table_name> / Home / downloads / database_name_table_name $ (DATE +% F.) .Sql
- database_name bid database backup to sql file table_name

 

 

MySQL database common operation command
to create a user
the Create the User user_name IDENTIFIED by '123456';
- create a user user_name, password 123456

Change Password
the SET password for user_name = password ( '234567')
--MySQL5.5 and below applicable

SET = the mysql.user authentication_string password Update ( '123456') WHERE User = 'USER_NAME';
--MySQL5.6 and above applicable

Insertion syntax

INTO table_name values INSERT ( 'wangjiasen', 25)
- the default table is inserted all the field information

INTO table_name INSERT (name, Age) values ( 'wangjiasen', 25)
- the corresponding mode field value insertion, fields greater than 2

Query syntax
the SELECT * from table_name;
- print table_name all fields

10 * from table_name limit SELECT;
- Printing 10 records

select name , age from table_name where name like 'wang%';
--模糊匹配

name SELECT, age by age desc Order from table_name;
- sorted in descending order according to age

name SELECT, WHERE age from table_name age> 20;
- Screening recording age greater than 20

Remove fields increase
the ALTER the Table table_name the Add id int (18) not null;
- table table_name add a field id is not empty 18 characters

the Table table_name drop age the ALTER;
- delete fields age

Update Data
Update table_name SET Age = 26 is WHERE name = 'wangjiasen'
- Update table table_name

Delete data
the Delete from table_name the WHERE Age = 25;
- delete data


Create, delete, index
the Create index index_name ON table_name (id (18));
- the fields to the table table_name id Create an index named index_name

index index_name ON table_name drop;
- delete table table_name index_name Index of

Create and delete constraints of
the ALTER the Table table_name the Add Primary Key (name)
- create table table_name in the name field primary key constraint

Table table_name the Add UNIQUE ALTER (id)
- Create a table id field in table_name unique constraint

Table Primary Key drop table_name ALTER;
- remove the primary key constraint


Create, delete, view
the Create View view_name AS the SELECT * from table_name;
- create a name for table_name view: view_name

View view_name drop;
- Delete View

MySQL commonly used functions
the SELECT COUNT (1) from table_name;
- to see how many records table_name

select sum(age) from table_name;
--求和

max SELECT (Age) from table_name;
- maximum

 

 

Record wave

Guess you like

Origin www.cnblogs.com/zhangqinANDwangjiasen/p/12134237.html