desc mysql.user; -- 查看表结构
SHOW TABLES; -- 查看表
show variables where variable_name like 'character_set_%'; -- 检查字符集变量
show variables like '%skip_name_resolve%'; -- 查看变量
SHOW GRANTS FOR 'test'@'10.9.119.%'; -- 查看指定用户的授权
create
create database students; -- 建库
CREATE DATABASE `students` CHARACTER SET utf8 COLLATE utf8_general_ci; -- 字符集
CREATE TABLE students2 (id int UNSIGNED NOT NULL ,name VARCHAR(20) NOT NULL,age tinyint UNSIGNED,PRIMARY KEY(id,name)); -- 创建表,联合主键
或者小写: create table students(id int unsigned not null primary key, name varchar(20) not null, age tinyint unsigned); -- unsigned既为非负数,用此类型可以增加数据长度!
create user 'test2'@'%' IDENTIFIED BY 'centos'; -- 创建test2用户密码centos
drop
DROP TABLE students2; -- 删除表
drop user 'test2'@'%'; -- 删除用户
alter
alter table students2 rename s1; -- 修改表名
alter table s1 add phone char(11) after name; -- 加phone字段,默认值是name
ALTER TABLE s1 MODIFY phone int; -- 修改字段属性
ALTER TABLE s1 CHANGE COLUMN phone mobile char(11); -- 修改字段名以及属性
alter table s1 drop column mobile; -- 删除字段名
alter database studentdb character set utf8; -- 修改库字符集
alter table s1 character set utf8; -- 修改表字符集
ALTER TABLE s1 change name name varchar(20) character set utf8; -- 修改字段字符集
insert
insert s1 (id,name,age) values(1,'那么',18);
insert into s1 values(2,'cc',24);
insert into s1 values(4,'cc',24),(3,'dd',22); -- 插入多行
update
-- 注意:一定要有限制条件,否则将修改所有行的指定字段
update s1 set age = 20 where id = 1; -- 将s1表中id=1的age值改成20
update s1 set age = 30 where id = 1 limit 1; -- limit只修改一项
delete
TRUNCATE TABLE 表名; -- 清空表,比delete快
delete from s1 where id=1; -- 删除指定条件的行
delete from mysql.user where user = 'test'and host='%';
select
select host,user, authentication_string from user; 查看mysql用户
select * from s1 order by age; -- 根据age排序
select * from s1 order by age desc; -- 倒序
select * from s1 order by age limit 1; -- 排完序,只显示一项
select * from s1 order by age limit 1,1; -- 排完序,跳过第一项,只显示一项
select gender,sum(age) from students group by gender; -- group by 分组需要有分组条件
select * from students where TeacherID is null; -- null值用where is
select DISTINCT gender from students ; -- 去重
select all gender from students ; -- 所有
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
select name,age from students where age > 25 and gender ='M';
(2) 以ClassID为分组依据,显示每组的平均年龄
select classid,avg(age) from students group by classid;
(3) 显示第2题中平均年龄大于30的分组及平均年龄
select classid,avg(age) as a from students group by classid having a > 30;
(4) 显示以L开头的名字的同学的信息
select * from students where name like 'L%';
(5) 显示TeacherID非空的同学的相关信息
select * from students where teacherid is not null;
(6) 以年龄排序后,显示年龄最大的前10位同学的信息
select * from students order by age desc limit 10;
(7) 查询年龄大于等于20岁,小于等于25岁的同学的信息
select * from students where age >= 20 and age <=25;
授权
grant all on hellodb.* to 'test'@'10.9.119.%' IDENTIFIED BY 'centos'; -- 创建test用户,密码centos。拥有hellodb所有操作权限
revoke delete on hellodb.* from 'test'@'10.9.119.%'; -- 回收delete授权
revoke all on hellodb.* from 'test'@'10.9.119.%'; -- 回收所有授权
SHOW GRANTS FOR 'test'@'10.9.119.%'; -- 查看指定用户的授权
FLUSH PRIVILEGES; -- 更改完需要及时重读授权表,预防有的命令还在内存中,使之生效