windows命令行操作数据库

cmd//打开命令小黑框
e: //从c盘转到e盘
cd E:\make\PHPTutorial\MySQL\bin //切换路径
mysql -uroot -p;//进入数据库命令行
Enter password:root//连接数据库密码
mysql>//进入数据库
show databases;//展示所有数据库
use hehe;//使用数据库
set names gbk;//设置windows7数据库编码方式
show tables;/展示所有表
select * from biao;//查询语句
delete from biao where id=2;//删除语句
insert into biao(name,age,sex)value(‘xiaohuang’,18,1);//插入语句
select * from biao order by id asc;//按照id正序排列
select * from biao order by id desc;//按照id倒序排列
update biao set name = 'hhyy’where id=5;
update biao set money = money+1 where id=5;
truncate biao;//清空表的内容
exit;//从命令行退出
set names gbk;//设置windows7数据库编码方式
delete from biao;//删除表数据不重置id
truncate biao;//删除表数据重置id
delete与update 操作时务必加where条件
drop database 数据库名;//完全删除数据库
创建数据库
create database [if not exists]数据库名 [character set = 字符集] [collate = 校对集];
create database if not exists hehe character set =utf8 collate = utf8_general_ci;
创建表
create table[if not exists]表名(字段列表[,索引或约束列表])[表选项列表]
create table hema
(
id int,
name varchar(255), //必须写字符串长度否则命令行报错
age int,
sex int
);
show create table hema;//展示建表语句
在sql中最标准的做法是在表和字段外边都标一对小引号例如:name

select id,name from admin;//查询表中的id以及name
select * from admin where name in(‘xhenxu’,‘hippo’);
select * from admin where name like ‘%100%’;//模糊查询 %匹配任意多个字符
select * from admin where name like ‘%100_’;// _匹配任意一个字符
select * from admin where id between 2 and 5;

select 字段名 from 表明 group by 字段名;

select id,name from admin group by type;//按照某个字段把数据进行分组
select id,name,type from admin group by type having id>1;//having与where效果一样,having单独使用与where一样,where不能用在group by之后
聚合函数
avg(字段名)平均值
count(字段名);总数量//求数据条数
max(字段名);最大值
min(字段名);最小值
sum(字段名);总和
select avg(money) as avgmoney from admin;

select count(*) from admin;//查询有多少条数据
在命令行中修改字段名与字段类型
alter table 表名 change 旧字段名 新字段名 新字段类型;
alter table 表名 modify 旧字段名 新字段类型;
添加字段
alter table 表名 add 新字段名 字段类型【字段属性列表】;
命令行修改表名
rename table 旧表名 to 新表名;
限制
limit offset,pagesize
offset偏移量,为0时可以忽略
pagesize 每页显示的行数
select *from user limit 0,4;
select 字段1,字段2 from 表名 where 条件 group by 字段名 having 限制条件 order by 字段名 desc limit 开始值,每页显示数量;

id int not null auto increment; 自动递增
default null 默认空
删除字段:
alter table 表名 drop 字段名;
删除表
drop table [if exists]表名;

左表名 inner join 右表名 on 条件 //内连接:两个表中都满足条件的数据
select user.name,job1.job from user inner join job on user.id = job.pid;
左表名 left join 右表名 on 条件 //左连接:左表数据都出现,满足条件的右表数据出现
左表名 right join 右表名 on 条件

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值