MySql 在线练习网站
http://sample.jimstone.com.cn/xsql/

常用命令
查看 mysql 版本
mysql -V
连接
连接 Mysql 数据库
mysql -u user -p password mysql -u user -p password -P port -h host
显示所有数据库
show databases;
转到数据库 database01
use database01;
Select
查询表 user 中所有内容
select * from user;
查询表 user 中 score 大于 80 的所有数据
select * from user where score>80;
查询表 user 中字段 gender 为 '男' 的所有内容
select * from user where gender='男';
查询表 user 中字段 students 开头为'小'字的内容
select * from user where students like '小%';
查询表 user 中字段 students 开头不是为'小'字的内容
select * from user where students not like '小%;
查询表 user 中字段 students 包含'聪'字的所有内容
select * from user where students like '%聪%';
查询表 user 中字段 score 为 98,60,92 的所有内容
select * from user where score in(98,60,92);
查询表 user 中字段 score 大于 95 或者 gender 为女性的所有内容
select * from user where score>95 or gender='女';
合并查询表 user 和表 user_ext 中 id 相同的所有数据
select * from user where user.id=user_ext.id;
获取表 user 中字段 score 大于 60 的内容数量
select count() from user where scoe>60;
获取表 user 中字段 score 的平均值
select avg(score) from user;
获取表 user 中字段 score 的总分数
select sum(score) from user;获取总分后标题为“sum(score)” select sum(score) as 总分数 from user; 获取总分后标题为“总分数”
获取表 user 中字段 score 的最大值
select max(score) from user;
获取表 user 中字段 score 的最小值
select min(score) from user;
获取表 user_ext 中所有不同的字段 age 并设置字段别名为'年龄'
select distinct(age) as 年龄 from user_ext;
获取表 user_ext 中的所有数据并且按照字段 weight 进行降序/升序排序
select _ from user_ext order by weight desc;倒序 select _ from user_ext order by weight asc;升序
通过左连接 获取表 user(别名 t1) 和表 user_ext(别名 t2) 中字段 id 相同的数据,其中字段 age 大于 9,并仅返回 id、students、age、weight 这几个字段的数据
select t1.id,t1.students,t1.age,t1.weight from user as t1 left join user_ext as t2 on t2.id=t1.id where t2.age>9;
Update
把 user 表 中字段 students 为'小明' 所在字段 score 更改为 30 分
update user set score=30 where students='小明';
Insert
在 user 表 所有字段 中添加记录
insert into user(studens,score,gender) values('小白',30,'男');
Delete
把 user 表 students 字段为'小明'的记录删除
delete from user where students='小明';
清空表 user
delete from user; truncate user; delete 与 truncate 清空数据表的区别
Create
创建表'test'
create table test(id int,students char,gender char);
Drop
删除表'test'
drop table test;
其他
修改表名
alter table 旧表名 rename 新表名;
修改字段名
待补充
在指定位置插入新字段
ALTER TABLE 表名 ADD COLUMN 字段名 字段类型 是否可为空 COMMENT '注释' AFTER 指定某字段 ; --COLUMN 关键字可以省略不写