数据库操作语言定义:
DDL:create alter drop;
DML : update insert delete replace;
DCL: grant revoke
查看mysql的版本
select version();
三种更改或创建密码的方法
mysqladmin -u root -p password '123456' --创建root的初始密码
mysql> set password for 'keven'@'%' =password('keven');
mysql> update user set password=password('12345678') where user='keven';
flush privileges; --重新加载授权表
查看用户
select user,host from mysql.user;
创建用户
create user 'user_name'@'localhost' identified by 'passwd';
create user 'user_name'@'%' identified by 'passwd'; --所有主机
删除用户
删除空用户
drop user 'user_name'@localhost;
创建数据库
create database db_name;
create database if not exists db_name
显示数据库
show databases;
删除数据库
drop database db_name;
设置使用默认数据库
use database_name;
查询当前使用的数据库
select database();
创建表(表名是区分大小写的)
create table table_name(Name char(20) not null,Age tinyint unsigned,Gender char(1) not null);
查看表
show tables from db_name;
查看表结构
desc table_name;
删除表
drop table table_name;
drop table if exists table_name;
修改表字段(查看帮助文档 help alter table;)
alter table table_name add course varchar(100) after Age;
alter table table_name modify course varchar(200);
alter table table_name change course Course char(200);
alter table table_name drop Course;
DML:
插入数据
insert into table_name (Name,Course,Gender) values ('Keven','Chinese','M');
更新数据
update table_name set Age='20' where Name='Keven';
删除数据
delete from table_name where Age='20';
DCL
查看用户权限
show grants for 'keven'@'%';
赋权
grant all privileges on mysql.* to 'keven'@'%';
让root用户可以被远程的某一区段的用户连接
grant all privileges on *.* to 'root'@'192.168.1.%' identified by 'redhat';
回收权限
revoke all privileges on *.* from 'root'@'192.168.1.%' identified by 'redhat';
查看服务变量
SHOW GLOBAL VARIABLES [like 'datadir'];
查看状态变量
SHOW GLOBAL STATUS;
通配符
任意单个字符:_
任意长度字符:%