Database operations Collection (ING)

mysql database to create user command

Create a database ----- the CREATE DATABASE DB_NAME ;

Create a user ID and password ----- CREATE USER 'USER_NAME' @ 'localhost' IDENTIFIED BY 'PASSWORD';

-----CREATE USER'USERNAME'@'%IDENTIFIED' BY 'PASSWORD';

Local permissions ------ GRANT SELECT, INSERT, UPDATE, DELETE ON guacamole_db * TO'guacamole_user '@' localhost '.;

Any IP permissions ------- GRANT SELECT, INSERT, UPDATE, DELETE ON guacamole_db * TO 'guacamole_user' @ '%'.;

Refresh ------ FLUSH PRIVILEGES;


Import and export database data (via sql file)

Export data to a sql file:

Under linux

Export database using mysqldump command:

  • 1, export a database or spreadsheet:
## 导出abc数据库到桌面
mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql    
mysqldump -uroot -p abc > /home/Desktop/abc.sql 

## 导出abc数据库中的表name到桌面
mysqldump -u用户名 -p密码 数据库名 表名 > 表名.sql     导出abc数据库的某个表到桌面
mysqldump -uroot -p abc name > /home/Desktop/name.sql   
注:敲回车后会提示输入密码
  • 2, just export the database table structure or structure (not data)
## 导出数据库结构到桌面(没有数据)
mysqldump -u用户名 -p密码 -d 数据库名 > 数据库名.sql  
mysqldump -uroot -p -d abc > home/Desktop/abc.sql  

## 导出某个表结构到桌面(没有数据)
mysqldump -u用户名 -p密码 -d 数据库名 表名 > 表名.sql  
mysqldump -uroot -p -d abc name > home/Desktop/name.sql  

Importing a spreadsheet or database

  • Method One: the database login required
create database abc; # 首先建空数据库
use abc;   
set names utf8;  # 设置数据库编码 
source /home/Desktop/abc.sql;  # 导入数据库abc数据(注意sql文件的路径)
source /home/Desktop/name.sql # 导入name表格数据到数据库abc
  • Method two: without logging database, execute the command line directly
mysql -u用户名 -p密码 数据库名 < 数据库名.sql  
mysql -uroot -p123456 abc < /home/Desktop/abc.sql  
# 同理,只导入name表格到abc数据库时
mysql -uroot -p123456 abc < /home/Desktop/name.sql

PS: sql file before the specified path


Three kinds of methods to delete a database

  • delete method
    to delete part of the data in the table, the table definition, and does not change settings.
    delete from table_name where 条件表达式;
  • Method truncate
    all of the data in the table empty, and does not change the settings table definition.
    truncat table name;
  • drop method
    Delete table, delete a table definition and structure, the table will cease to exist, it can also be used to delete a database.
    drop table name; drop databases;

Clear substances of foreign key constraints

SET foreign_key_checks = 0; ## 解除外键检查
truncate table name ## 清空表格数据,也可只删除部分数据
SET foreign_key_checks = 1; ## 设置外键

Command line directly execute sql statement (without logging database)

mysql -uroot -proot table -e "SQL语句(可多句)"

Form a single field value modification

update table_name set value=4 where value=2;

Database query data, does not exist, create

insert into students
(id,name)
select (11,hujin) 
from students 
where not exists (select * from students
where id = 11)

Database engine settings to speed up the retrieval speed

alter table name engine = myisam;

Guess you like

Origin www.cnblogs.com/superjin/p/11481792.html