mysql 常用命令: 众多关系型数据库中的一种
用来存数据的
进入 mysql 命令行:
mysql -uroot -p123456
show databases; 查看所有数据库
create database zxp charset utf8; 创建数据库
drop database zxp; 删除数据库
use zxp; 选择数据库
数据库:
查看 show databases;
创建 create database zxp charset utf8;
删除 drop database zxp;
选择数据库 use zxp;
表:
约束:
- # 自增长
- auto_increment
- # 非空
- not null
- # 默认值
- default 'xxxx'
- # 唯一约束:
- unique
- #charset
指定字符集
- # 主键
- primary key
具有唯一性, 不能为空
# 外键
用来关联表
增
- #score
- create table score(
- id int auto_increment primary key,
- s_id int not null,
- grade float not null
- )
- create table zxp(
- id int auto_increment primary key,
- name varchar(10) not null,
- sex varchar(2) default '男',
- addr varchar(50) ,
- phone int not null unique,
- );
删:
drop table zxp
改:
- alter table zxp rename stu; #改表名
- alter table zxp modify name varchar(20);# 修改字段的数据类型
- alter table zxp change name stu_name varvhar(30);
- alter table zxp add money float not null after stu_name;# 新增一个字段, 放在哪个位置
- alter table zxp drop addr;# 删除某个字段
查:
show create table tablename;# 查看建表语句
desc tablename; 查看表结构
show tables;# 查看所有的表
数据:
增:
- insert into zxp(stu_name,money,sex,phone)values('赵晓静',100000,'女',110);
- insert into zxp(stu_name,money,phone)values('赵静',100000,,110);
- # 指定字段
- # 不指定字段
- insert into zxp values('','小明',10000,'女',109999);
- # 指定字段的话, 插入的值和字段对应
- # 不指定的话, 要把所有的字段值都写全
- insert into zxp(stu_name,money,phone)values('赵静 1',100000,,110),('赵静 2',100000,,110),('赵静 3',100000,,110);
- # 同时插入多条
删:
- truncate tablename;# 删除整个表里的数据
- delete from zxp;# 删除整个表里的数据
- #delete 清空的表自增长 id 还会继续增长
- #truncate 清空得到表自增长 id 从 1 开始, truncate 速度比 delete 要快, 因为 truncate 是从磁盘中删除, 数据不可恢复
- delete from zxp where stu__name='赵晓静';# 删除指定数据
改:
- update zxp set money=80;# 如果不指定条件得到话, 修改的是整个表的数据
- update zxp set money=90 where stu_name='赵晓静';# 修改指定的数据
- update zxp set money=90,phone=10000 where stu_name='赵晓静';# 修改多个字段
查:
- select stu_name,sex,money,phone from zxp;# 指定查询的字段
- select * from zxp;
- select * from zxp where sex='男' and money>100;# 多个条件, 必须同时满足;
- select * from zxp where sex='男' or sex='未知';# 多个条件, 有一个条件满足即可;
- elect * from zxp where sex !='男';#<> 也是不等于
- ALTER table zxp add addr varchar(50);
- select * from zxp where addr like '% 东京 %';# 模糊匹配,% 代表的是通配符, 必须得用 like
- select * from zxp a where a.stu_name like '姚_'# 下划线通配符表示任意一个单字符
- select a.stu_name,a.phone from zxp as a where a.stu_name='遥远';# 给表起别名, as 可以去省略掉
- select * from zxp a where a.stu_name in('林倩','遥远','阿香') and a.money>100 #in
- select *from zxp a where a.money between 1000 and 10000 #between... and 把 1000 到 10000 之间的查出来, 包含 1000 和 10000, 只能用数值类型
- select * from zxp order by id asc;# 升序, 默认是升序
- select * from zxp order by id desc;# 降序
- select * from zxp where id=3 order by money desc;
- select * from zxp a where a.addr='' or a.addr is null;
select distinct a.money from zxp a; 去重
select count(*) 学生人数 from zxp wheer sex='女'; 统计行数
select max(a.money) 钱最多, a.stu_name 名字 from zxp a;
select min(money) 钱最少, stu_name 名字 from zxp;
select avg(a.money) 平均多少钱, a.stu_name 名字 from zxp a;
select sum(a.money) 总共多少钱, a.stu_name 名字 from zxp a;
select *,count(*) 人数 from zxp group by sex;
select sex 性别, count(*) 人数 from zxp group by sex;# 分组
select sex 性别, count(*) 人数 from zxp a where a.money>300 group by a.sex;# 分组 分组之后不能加 where, 只能在前面加
select sex 性别, count(*) 人数, a.stu_name from zxp a where a.money>300 group by a.id having a.stu_name like '姚 %';# 分组 分组之后还想加条件, 使用 having,having 子句里面用到的字段必须出现在 select 后面 (比如 a.stu_name), 如果 group by 和 order by 一起用的话, order by 必须放在 group by 后面
- select * from zxp group by sex,class;# 按照多个字段来进行分组
- select * ,count(*) from zxp group by sex,class;
- select * from user a,accounts b where a.id=b.user_id and a.username='niuhy';# 多表关联
select a.username 用户名, b.money 账户余额. c,id 订单 id from 'user' a,accounts b,'order' c where a.id=b.user_id and a.username='niuhy' and a.id=c.user_id;
select a.username 用户名, b.money 账户余额 from 'user' a,acounts b,where a.id=b.user_id and username='nhy2';
- select * from zxp a left join score b on a.id=b.s_id;#left join 会把左边表的所有数据都查出来, 右边表有匹配的就查出来
- select * from zxp a right join score b on a.id=b.s_id;#right join 会把右边表的所有数据都查出来, 左边表有匹配的就查出来
- select * from zxp a inner join score b on a.id=b.s_id;#inner join 两边表里都匹配的数据才查到
子查询: 把一条 sql 的结果, 作为另一条 sql 的条件
- ;
- select * from score a where a.s_id=(select * from blk where stu_name='阿翔');
select a.grade 成绩, b.stu_name 学生名称, b.id 学号 from score a,(select id,stu_name from blk where stu_name='阿翔') b where a.s_id=b.id;# 把子查询当成一个表
- select a.stu_name,a.money,b.grade from blk a,score b where a.id=b.s_id and a.stu_name='阿翔';
- select a.stu_name,a.money,b.grade from blk a,(select s_id,grade from score) b where a.id=b.s_id and stu_name='阿翔';
- select * from blk limit 3;# 限制取几行
- select * from blk limit 1,5;# 限定从第几条开始, 再查询下面的 x 条, 不包含开始的那一条
- select id,stu_name from blk union select id,t_name from teacher;#union 用来合并两条 select 语句的结果, 两条 select 语句字段的数量要一致, 并且数据类型也要一致
union 和 union all 的区别就是一个会去重一个不会
update user set password=password('123456') where user ='root'# 修改用户密码
存储过程:
批量的造数据
- delimiter $$; #为了改结束符
- create procedure big_data(num int) #代表要造多少条数据
- begin
- declare i int;
- set i=0;
- while i<num do
- insert into blk (stu_name,money) values('hehe',20000);
- set i=i+1;
- end while;
- end
- $$;
- delimiter;
- select count(*) from blk;
- call big_data(500);# 调用上面造数据的函数
- delimiter $$; #为了改结束符
- create procedure big_data1(num int) #代表要造多少条数据
- begin
- declare i int;
- set i=0;
- while i<num do
- insert into blk (stu_name,money) values(concat('小明',i),20000);#concat 拼接不同类型的数据: 名字依次为, 小明 1, 小明 2....
- set i=i+1;
- end while;
- end
- $$;
- delimiter;
- call big_data1(500)
备份数据库:
mysqldump -uroot -p123456 db> db.sql
mysqldump -uroot -p123456 -A> all.sql
恢复数据:
mysql -uroot -p123456 db < db.sql
3. 环境搭建
1. 下载依赖软件, 安装好
2. 获取源代码 (打包)
根据语言的不同来看是否需要编译, 打包
3. 导入基础数据
4. 修改配置文件
5. 启动 (tomcat,weblogic,jetty,resin,ws)
来源: http://www.bubuko.com/infodetail-2623261.html