数据插入:
单行数据插入:
语句:insert into 表名(列1,列2....) values(值1,值2....)
案列:假设数据库表名 student 里面2列 姓名 年龄,现在想插入姓名是小芳 年龄18的信息
案列插入语句:insert into Student(name,age) values('小芳',18)
多行数据插入一:
将现有表中的数据添加到已存在的表中。
语法:insert into 表名(列1,列2...) select 列名 from 原表名
案例:要将 原表名Student里的所有名称name 复制到已存在的Class表中Sname列中
案列插入语句:insert into Class(Sname) select name from Student
注意:Class表是已经存在的 表中的Sname列也是早已经创建完的
多行数据插入二
将现有表中的数据添加到新建的表中。
语法:select(列1,列2...) into 表名 from 原表名
案例:要将 原表名Student里的所有名称name 复制到一个新建的表Class里
案列插入语句:select name into Class from Student
多行数据插入三:
多行数据进行插入。
语法:insert into 表名(列1,列2...)
select <列名> union
select <列名> union
select <列名> union
.........
案例:将姓名小商 年龄20, 姓名赵三 年龄21 ,姓名网二 年龄17,插入到数据表名Student中
案列插入语句:insert into Student(name,age)
select '小商',20 union
select '赵三',21 union
select '网二',17
数据修改:
update 表名 set 列名1=更新值1,列名2=更新值2,........where 条件
案例:将年龄17的人的姓名 改为赵四
语句:update Student set name='赵四' where age=17
数据删除:
deletefrom 表名 where 条件 (将满足条件的数据删除)
案例:将年级大于20的人员数据删除
语句:delete from Student where age>20
truncate table 表名 (将表名数据清空)
数据查询:
1.全部查询(输出当前表的全部信息)
select * from 表名
案例:输出表People所有人员信息
语句:select * from People
2.条件查询 (根据条件输出当前表的全部信息)
select * from 表名 where 条件
案例:输出表People 名字叫“万盛1号”并且的所有人员信息
语句:select * from People where name='万盛1号'
2.多条件查询 (根据条件输出当前表的全部信息)
select * from 表名 where 条件 and 条件
案例:输出表Home 社区是“万盛” 小区是“A” 的所有房间信息
语句:select * from Home where sqname='万盛' and xqname='A'
3.查询部分信息
select 部分信息 from 表名 where 条件
案例:输出表People 名字叫“万盛1号”的年龄,姓名,性别信息
语句:select name,age,sex from People where name='万盛1号'
2.多表查询 (根据条件输出当前表的全部信息)
select * from 表名,表名 where 条件
案例:输出表Home,people 小区现在已有的所有人员姓名
语句:select distinct Home.sqname,Home.xqname,People.name from Home,People where Home.xqname=People.xqname
扩展:distinct 去重
4.数量查询
select COUNT(*) from 表名 where 条件
案例:输出表People中 所属社区是倚澜观邸的人员数量
语句:select COUNT(*) from People where sqname='倚澜观邸'
5.查询拼接
select a+''+b from 表名 where 条件
案例:输出表People中 名字叫“保利2号”的地址
语句:select sqname+':'+xqname from People where name='保利2号'
6.限制行数查询(查询前几个数据)
select top 数量 * from 表名 where条件
案例:输出表Home 中 所属社区是倚澜观邸的前2个的房间信息
语句:select top 2 * from Home where sqname='倚澜观邸'
扩展:按照百分比显示 : select top 20 percent * from Home where sqname='倚澜观邸'
6.排序查询
select * from 表名 where条件 order by 排序条件
案例:输出表Lou 中按升序查询
语句:select* from Lou order by id asc
扩展:desc是降序, asc是升序, 不写默认升序
7.多列排序查询(按照多个条件进行排序)
select * from 表名 where条件 order by 排序条件,排序条件
案例:输出表Lou 中按升序查询
语句:select* from Lou order by id,louId asc
8.模糊查询
select * from 表名 where条件 like '张%'
案例:输出姓张的人员信息
语句:select * from People where name like'张%'
扩展:通配符
8.数值范围查询
select * from 表名 where条件between
案例:输出10岁到40岁的人员信息
语句:select * from People where age between 10 and 40
9.聚合函数
1.sum()
select sum() from 表名 where条件
案例:查询学号为21的学生总分
语句:select sum(score) from People where id=21
其余的简写:
- 管理系统开发一: winform连接sql数据库 https://blog.csdn.net/m0_37583098/article/details/88546146
- 管理系统开发二: winforml登录界面sql数据查询和修改 https://blog.csdn.net/m0_37583098/article/details/88547123
- 管理系统开发三: winforml录入界面 https://blog.csdn.net/m0_37583098/article/details/88547805
- 管理系统开发四: 在主窗体中嵌入子窗体的实现 https://blog.csdn.net/m0_37583098/article/details/88549151
- 管理开发系统五:winform连接数据库查询 使用DataGridView展示查询结果 https://blog.csdn.net/m0_37583098/article/details/88549461
- 管理开发系统六:winform连接数据库修改 https://blog.csdn.net/m0_37583098/article/details/88550157
- 管理开发系统七:winform连接数据库删除 https://blog.csdn.net/m0_37583098/article/details/88578796
- 管理开发系统八:winform导出excel https://blog.csdn.net/m0_37583098/article/details/88579043
- 管理开发系统九:winform带sql数据库导出 https://blog.csdn.net/m0_37583098/article/details/88580311
- sql service 常用语句基础https://blog.csdn.net/m0_37583098/article/details/87876264