在日常的网站维护和管理中,会用到非常多的 SQL 语句,
熟练使用对网站管理有很多好处,尤其是站群管理的时候。
下面列一些常用的命令做备记。
1 、显示资料库
show databases
显示表
show tables;
2 、建立使用者
建立 root 使用者密码为 123
use mysql;
grant all on *.* to root@’%’ identified by ‘123’ with grant option;
commit;
3 、修改密码
grant all on *.* to xing@’localhost’ identified by ‘123456’ with grant option;
update user set password = password(‘newpwd’) where user = ‘xing’ and host=’localhost’;
flush privileges;
4 、建立资料库 testdb:
create database testdb;
5 、预防性建立资料库:
create database if not testdb;
6 、建立表:
use testdb;
create table table1(
username varchar(12),
password varchar(20));
7 、预防性建立表 aaa:
create table if not exists aaa(ss varchar(20));
8 、检视表结构:
describe table1;
9 、插入资料到表 table1:
insert into table1(username,password) values
(‘leizhimin’,’lavasoft’),
(‘hellokitty’,’hahhahah’);
commit;
10 、查询表 table1:
select * from table1;
11 、更改资料:
update table1 set password=’hehe’ where username=’hellokitty’;
commit;
12 、删除资料:
delete from table1 where username=’hellokitty’;
commit;
13 、给表新增一列:
alter table table1 add column(
sex varchar(2) comment ‘ 性别’,
age date not null comment ‘ 年龄’
);
commit;
14 、修改表结构
从查询建立一个表 table1:
create table tmp as
select * from table1;
15 、删除表 table1:
drop table if exists table1;
drop table if exists tmp;
16 、备份资料库 testdb
mysqldump -h 192.168.3.143 -u root -p pwd -x –default-character-set=gbk >C:testdb.sql
17 、删除资料库 testdb
drop database testdb;
18 、恢复 testdb 资料库
首先先建立 testdb 资料库,然后用下面命令进行本地恢复
mysql -u root -pleizhimin testdb