简单的sql语句及例子

创建表
create table Student(Sid varchar(10),Sname nvarchar(10),Sage date,Ssex nvarchar(10));

添加命令操作
insert into 表名(字段名1,字段名2…) values (值1,值2,…);
表名后面的字段不写表示新增所有

更新数据
update 表名 set 字段名1=新的值, 字段名2=新的值 where id = xxx;

删除表数据
– 物理删除
delete from t_student where id=10;
– 逻辑删除
select * from t_student where isdelete=0;
update t_student set isdelete=1 where id=1;

DQL查询语句
基本查询
select 字段名1,字段名2… from 表名;
*表示所有字段
去重关键字:distinct
select distinct name from t_student ;
起别名:as
as也可以省略
表的别名:
Select ac.列名 from 表名 as(可省略) ac(别名)
字段的别名
Select 列名 as(可省略) ‘活动名’(别名) from 表名

where子句后跟条件
运算符

、< 、<= 、>= 、= 、<>(不等于)
BETWEEN…AND
查询id 10~20
select * from Student (as) ac where ac.id between 10 and 20;

IN( 集合)
查询id=3,4,5
select * from Student ac where ac.id in (3,4,5);

LIKE:模糊查询
select * from Student ac where ac.name like ‘%六%’; //六前面,后面的字符数可以是0个或者多个
select * from Student ac where ac.name like ‘六%’;//六打头
select * from Student ac where ac.name like ‘%六’;//六结尾

名字是两个字的
select * from Student ac where ac.name like ‘__’;//两个_

	占位符:
		_:单个任意字符
		 %:多个任意字符

IS NULL
and 或 &&
or 或 ||
not 或 !

排序查询
语法:order by 子句
order by 排序字段1 排序方式1 , 排序字段2 排序方式2…
排序方式:ASC:升序,默认的 DESC:降序。
select * from Student order by id desc/asc;

按两个字段排序cid asc,如果cid一样, 用id desc
select * from Student order by cid asc, id desc;

  • 注意:* 如果有多个排序条件,则当前边的条件值一样时,才会判断第二条件。

如果单条查询,order by放在where 后面
select * from Student where cid = 1 order by cid;

聚合函数:将一列数据作为一个整体,进行纵向的计算。
count:计算个数
一般选择非空的列:主键
count()
查询总记录数 count(
)
select count(*) from Student;

max:计算最大值
min:计算最小值
sum:计算和
avg:计算平均值

select max(id) from Student;
select min(id) from Student;
select sum(id) from Student;
select avg(id) from Student;

注意:聚合函数的计算,排除null值。
解决方案:选择不包含非空的列进行计算

分组查询
语法:
Select 分组之后查询的字段:分组字段、聚合函数
where 分组前过滤条件
group by 分组字段
having 分组后过滤条件

状态 0:未开始 1:已结束 2:进行中
请统计出0,1,2各多少条
select后边写:分组的字段和聚合函数
select ac.status,count(*) from Student as ac group by ac.status;

Group by having 组的过滤条件
select ac.status,count() from Student as ac
group by ac.status
having count(
)>=3;

分页查询
语法:limit 开始的索引,每页查询的条数;
公式:开始的索引 = (当前的页码 - 1) * 每页显示的条数
比如:每页显示3条记录
Select * from Student limit 0,3;
Select * from Student limit 3,3;
Select * from Student limit 6,3;

主键约束:primary key

  1. 含义:非空且唯一
  2. 一张表只能有一个字段为主键
  3. 主键就是表中记录的唯一标识

创建表添加主键:
create table key_test(
id int primary key,-- 给id添加主键约束
name varchar(20),
cart_no varchar(20)
);
create table key_test(
id int ,-- 给id添加主键约束
name varchar(20),
cart_no varchar(20),
primary key (id)
);

创建表后添加:
alter table key_test(表名) modify id(字段名) int(字段类型) primary key;
在表上右键设计表,查看修改后的效果

删除主键:
alter table key_test drop primary key;

非空约束:not null
建表时添加命令
create table key_test(
id int,
name varchar(20) not null ,
cart_no varchar(20)
);
create table key_test(
id int,
name varchar(20) not null default 0 , 默认是0
cart_no varchar(20)
);

建表后再添加
alter table key_test modify name VARCHAR(20) not null;

删除
删除其实就是修改 modify修改
alter table key_test modify name VARCHAR(10);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值