1 关于数据库
create database db charset=utf8 //创建数据库。charset=utf-8,默认为安装mysql时选的字符集
show create database db //展示数据库相关信息
drop database db //删除数据库
show databases //展示所有数据库
use db //使用数据库
2 关于表
create table t1(name varchar(50),age int)charset=utf8 //创建表
create table t2(id int primary key auto_increment) //id为主键约束,并且自动增加
show create table t1 //查询表信息
desc t1 //查询表字段
rename table t1 to t2 //修改表名。t1=原表名 t2=新名
drop table //删除表
3 关于表字段
alter table t1 add sex varchar(2) //表最后面sex字段
alter table t1 add sex varchar(2) first //表最前面添加sex字段
alter table t1 add sex varchar(2) after name //在字段name后添加字段
alter table t1 drop sex //删除字段sex
alter table t1 change sex xingbie varchar(5) //修改字段名 sex原名 xingbie新名
4 关于插入数据
insert into t1 values('李白',18) //插入一行全部数据
insert into t1(name) values('李白') //插入一行,部分数据
insert into t1 values('李白',18),('杜甫',20) //插入多行,全部数据
insert into t1(name) values('李白','杜甫') //插入多行,部分数据
5 关于数据
select name from t1 //查看t1表中,name的值
update t1 set age=12 where name='李白' //修改‘李白’的,年龄
delete from t1 where name='杜甫' //删除名字为‘杜甫’的一行数据
6 批处理.sql文件
source d:/db.sql
7 查询相关distinct,is null ,比较运算符,between,in
select distinct name from t1 //distinct,去除name中重复的列
select name from t1 where age is null //年龄为null
select name from t1 where age is not null //年龄不是null
select * from t1 where age>30 and age<50 //年龄在30-50,不包括30和50
select * from t1 where age>30 or age<50 //年龄小于30,或年龄大于50
select * from t1 where job!="李白" //名字不是李白
select * from t1 where job<>"李白" //名字不是李白
select * from t1 where age between 30 and 50 //年龄在30-50,包括30和50
select * from t1 where age not between 30 and 50 //年龄不在30-50
select * from t1 where age in(20,50) //年龄是20或50
8 模糊查询like
select * from t1 where name like "李%" //名字以'李'开头的 %:代表0或多个未知字符
select * from t1 where name like "_白%" //第二个字是'白'的 _:代表1个未知字符
9 排序order by
select * from t1 order by age //按年龄升序排列 (不写默认升序)
select * from t1 order by age asc //按年龄升序排列
select * from t1 order by age desc //按年龄降序排列
10 聚合函数
select avg(age) from t1 //平均年龄
select max(age) from t1 //最大年龄
select min(age) from t1 //最小年龄
select sum(age) from t1 //年龄总和
select count(age) from t1 //统计,总共多少行数据
select name 名字 from ti //'name'起别名,为'名字'
11 分页查询 limit
//0 代表跳过条数 3 代表请求条数
select * from t1 order by age limit 0,3 //age升序排列,从0行开始,显示三行
select * from t1 order by age limit 3,3 //age升序排列,从第三行开始,显示前三行
12 值运算 + - * / %
select age,age*2 from t1 //年龄扩大2倍
select age,age+2 from t1 //年龄增加2
select age,age-2 from t1 //年龄减少2
select age,age/2 from t1 //年龄减少2倍
select age,age%2 from t1 //年龄对2求余