Home > Database > Mysql Tutorial > Summary of common SQL statement usage methods in MySQL

Summary of common SQL statement usage methods in MySQL

大家讲道理
Release: 2017-02-11 14:51:14
Original
1627 people have browsed it

Create user

CREATE USER 'root'@'%' IDENTIFIED BY 'password';
Copy after login

Create user and grant specified permissions

grant create,select,update,insert,delete,alter on bbs.* to lvtao@localhost identified by 'password';
Copy after login

Create user and grant full permissions

Grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
Copy after login

Create backup user

GRANT SELECT,RELOAD,SHOW DATABASES,LOCK TABLES,EVENT,REPLICATION CLIENT  ON *.* TO 'bak'@'localhost' IDENTIFIED BY 'password';
Copy after login

Back up all databases

mysqldump -u root -p --all-databases --ignore-database=performance_schema --ignore-database=information_schema --skip-lock-tables > /home/db.sql
Copy after login

Export a database structure

mysqldump -u root -p -d –add-drop-table database >/home/db.sql
Copy after login

Restore data

A:常用source 命令
进入mysql数据库控制台,
如mysql -u root -p
mysql>use 数据库
然后使用source命令,后面参数为脚本文件(如这里用到的.sql)
mysql>source wcnc_db.sql

B:使用mysqldump命令
mysqldump -u username -p dbname < filename.sql

C:使用mysql命令
mysql -u username -p -D dbname < filename.sql
Copy after login

Create database

create database <数据库名>;
Copy after login

Display all databases

show databases;
Copy after login

Delete database

drop database <数据库名>;
Copy after login

Select database

use <数据库名>;
Copy after login

View the currently used database

select database();
Copy after login

Table information contained in the current database:

show tables;
Copy after login

Create table

create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);

mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default &#39;0&#39;,
> degree double(16,2));
Copy after login

Get the table structure

desc 表名,或者show columns from 表名

mysql>DESCRIBE MyClass;
mysql>desc MyClass; 
mysql>show columns from MyClass;
Copy after login

Delete the table

drop table <表名>

mysql> drop table MyClass;
Copy after login

Insert data

insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]

mysql> insert into MyClass values(1,&#39;Tom&#39;,96.45),(2,&#39;Joan&#39;,82.99), (2,&#39;Wang&#39;, 96.59);
Copy after login

Query the data in the table

1)、查询所有行
命令: select <字段1,字段2,...> from < 表名 > where < 表达式 >
例如:查看表 MyClass 中所有数据
mysql> select * from MyClass;2)、查询前几行数据
例如:查看表 MyClass 中前2行数据
mysql> select * from MyClass order by id limit 0,2;
或者:
mysql> select * from MyClass limit 0,2;
Copy after login

Delete the data in the table

delete from 表名 where 表达式

mysql> delete from MyClass where id=1;
Copy after login

Modify the data in the table

update 表名 set 字段=新值,… where 条件

mysql> update MyClass set name=&#39;Mary&#39; where id=1;
Copy after login

Add a field to the table:

alter table 表名 add字段 类型 其他; 

mysql> alter table MyClass add passtest int(4) default &#39;0&#39;
Copy after login

Change the table name:

rename table 原表名 to 新表名; 

mysql> rename table MyClass to YouClass;
Copy after login

Update field content

update 表名 set 字段名 = 新内容update 表名 set 字段名 = replace(字段名,&#39;旧内容&#39;,&#39;新内容&#39;);
文章前面加入4个空格update article set content=concat(&#39;  &#39;,content);
Copy after login

Update field part string

update contents set `text`=REPLACE(text,&#39;http://www.lvtao.net&#39;,&#39;https://www.lvtao.net&#39;)
Copy after login

Field: Numeric type
Summary of common SQL statement usage methods in MySQL

Field: String type

Summary of common SQL statement usage methods in MySQL
Field: Date type
Summary of common SQL statement usage methods in MySQL



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template