sql学习1

创建表
create table user_b(
 id int not null primary key,
 name char(20) not null
);
删除表
drop table xxx;
eg:drop table user_b;
添加字段
// 字段不能为空
alter table user_b add(count int not null);
//默认可以为空 alter table user_b add(count int null);
alter table user_b add(title varchar(100));
移除字段
alter table user_b drop column count;
变更字段
alter table user_b modify column title varchar(255);
插入数据
插入一条完整数据
insert into user_b values(1,'房价啥时候降下来',10);
单表查询
// 单表全字段查询
select * from user_b;
// 单表个别字段查询
select title from user_b;
// 条件查询
select title from user_b where count>90;
多表查询
SELECT a.nickname,a.role,b.title,b.count from user as a,user_b as b;
// select 表一字段,表二字段,表三字段,…… from 表一,表二,表三,……
嵌套查询
select * from user_b 
where id=(select id from user_b where count>5);
查询头几条数据
select top 2 * from user_b;
删除
// 删除表 drop table user_b;
// 删除表中数据 delete from user_b;
delete from user_b where id=4;
更新
update user_b set title="这是一条添加数据" where id=4;
//更新多条数据 update user_b set title="这是一条添加数据",count=50 where id=4;
求和
select sum(count) from user_b;
ps:sum(字段) 对字符串和时间无效
求平均值
select avg(count) from user_b; 
ps:avg(字段)对字符串和时间无效
计数
select count(*) from user_b;
ps:count(字段名)不包含NULL
求最大值
select max(count) from user_b;
ps:字符串的话返回字母序最大的
   数值返回数值最小值
求最小值
select min(count) from user_b;
ps:字符串的话返回字母序最小值,
   数值返回数值最小值
查询结果数限制
select * from user_b limit 1;
排序
select * from user_b order by count;
ps:默认从小到大排序
// 升序 select * from user_b order by count asc;
// 降序
select * from user_b order by count desc;
待完成...
union(并查询)
in(交查询)
group by(分组)
索引
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值