SQLServer数据库查询语法
SQLServer数据库查询语法
前言:
SQLServer数据库介绍:
SQLServer数据库是微软公司推出的一款关系型数据库系统,SQL Server是一个可扩展的、高性能的、为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有机结合,提供了基于事务的企业级信息管理系统方案。
重要的SQL命令
备注:
数据库不区分大小写 所以这里用了全小写
- select --从数据库中提取数据
- update --更新数据库中的数据
- delete --删除数据库中的数据
- insert into --向数据库中插入数据
- create database --创建数据库
- alert database --修改数据库
- crete table --创建数据表
- alert table --修改数据表
- drop table --删除数据表
- create index --创建索引
- drop index --修改索引
创建数据表
语法
craete table [创建表名称](
[列名] [数据类型] [约束],
[列名] [数据类型] [约束]
)
创建学生表(student)
create table student(
id int identity(1,1) primary key, --设置id为主键,并为标识列自增1
sex bit not null, --学员性别 0:女 1:男
name varchar(50) not null, --学员姓名
age int not null, --学员年龄
classId int references Class(id) --ClassId为外键指向Class表
)
创建班级表(class)
create table class(
id int identity(1,1) primary key,
name varchar(50) --班级名称
)
向表中插入数据
由于学生表(student)引用了班级表(class)表所以我们先插入班级表中的数据
向表中插入数据的语法为
insert into [表名]([列名1],[列名2],[列名3]) values([数据1],[数据2],[数据3]) --插入数据项应与列明一一对应
向班级表(class)插入数据
insert into class(name) values('大一') --由于id为标识列有自增属性这里不用插入自增列的信息
--象征性的多插入几条
insert into class(name) values('大二')
insert into class(name) values('大三')
insert into class(name) values('大四')
向学生表(student)中插入数据
insert into student (name, sex,age,classId) values('小明',1,18,1)
insert into student (name, sex,age,classId) values('小红',0,18,1)
insert into student (name, sex,age,classId) values('小蓝',1,17,1)
insert into student (name, sex,age,classId) values('小绿',1,19,2)
insert into student (name, sex,age,classId) values('小紫',0,18,1)
insert into student (name, sex,age,classId) values('小张',1,20,2)
insert into student (name, sex,age,classId) values('小李',1,18,1)
insert into student (name, sex,age,classId) values('小王',0,21,3)
insert into student (name, sex,age,classId) values('小钱',1,18,1)
insert into student (name, sex,age,classId) values('小赵',0,19,2)
insert into student (name, sex,age,classId) values('小周',1,18,1)
insert into student (name, sex,age,classId) values('小丽',0,20,2)
insert into student (name, sex,age,classId) values('小聪',1,18,1)
insert into student (name, sex,age,classId) values('小慧',0,19,3)
insert into student (name, sex,age,classId) values('小星',1,21,1)
insert into student (name, sex,age,classId) values('小思',1,20,4)
查询表中数据
select 查询
select [查询列名称],[查询列名称] from [表名] select * from student --查询student表中所有列信息
where 条件查询
select name,age from student where age>19 --查询年龄大于19学生的姓名和年龄
order by 排序
select * from student where age>19 order by age asc --按年龄升序
select * from student where age>19 order by age desc --按年龄降序
group by 分组,查询全部学生男女生各多少人
select case when sex=1 then '男'
when sex=0 then '女' end as 性别 --这里使用了case when than的语法,相当于编程语言中if else 判断条件
,COUNT(name) as 总数 --count()函数返回总计数
from student group by sex

注意:
在group by 后不能使用where条件语句,group by 语句后可以使用having 进行条件约束 ,
后面也可以可以使用 order by进行排序
修改表数据
修改数据表中的数据使用Update
语法
update [表名] set [列名]=[修改的值] where [列名] = [条件]
下面修改id=16 同学的姓名:

update student set name='小林' where id=16
select * from student where id=16
修改之后的数据为 小思的名字就改成了小林

注意:
如果不给定条件会修改表中所有的数据
删除表中数据
*删除表中数据Delete
语法
delete [表名] where [条件]
下面删除id=16的小林的信息
delete student where id=16
执行结果:可以清楚的看到id=16小林的数据就没有了,

总结:
修改和删除要谨慎使用,使用时一定要给定条件,不然就会修改、删除整张表中的数据。格外注意
SQLServer数据库查询语法的更多相关文章
- 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现
关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...
- Mysql数据库查询语法详解
数据库的完整查询语法 在平常的工作中经常需要与数据库打交道 , 虽然大多时间都是简单的查询抑或使用框架封装好的ORM的查询方法 , 但是还是要对数据库的完整查询语法做一个加深理解 数据库完整查询语法框 ...
- sqlserver数据库查询
帮助类 using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; ...
- sqlserver数据库查询,在数据类型不一致时容易出错
1. 如此句sql: select SysNo from User_MainInfo where Ouid=@Ouid 在 User_MainInfo表中Ouid是nvarchar类型,但当我们传入的 ...
- Yii2数据库查询语法
一: $con = Yii::$app->db; $rel = $con->createCommand("select * from user");//预处理对象 $r ...
- sqlserver数据库查询语句
--数据库所有表select * from sysobjects where type='u'; --指定表的所有列select name from syscolumns where id=(sele ...
- 查询Sqlserver数据库死锁的一个存储过程(转)
使用sqlserver作为数据库的应用系统,都避免不了有时候会产生死锁, 死锁出现以后,维护人员或者开发人员大多只会通过sp_who来查找死锁的进程,然后用sp_kill杀掉.利用sp_who ...
- SQLServer数据库之连接查询
SQLServer数据库之连接查询 表的连接查询的几种方法介绍: inner join on内连接,left join on 左连接 , rigth join on 右连接, full join on ...
- Sqlserver数据库分页查询
Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...
随机推荐
- GraphPad Prism 9.0安装破解教程
graphpad prism 9.0是一款强大的科学软件,拥有大量分析图表,prism是回归分析的著名软件之一,非常适用于科研生物医学等领域.本文提供其破解版,激活码,序列号,破解教程等,可以完美激活 ...
- 利用postman进行api接口开发
场景: api接口开发时,经常使用一些工具来帮助设计开发.Yapi主要是在设计阶段进行api接口设计,统一前后端参数请求和返回体:swagger主要在开发阶段,用来显示实际上后端开发进度和接口情况:p ...
- Hadoop 3.1.1 - 概述 - 集群安装
Hadoop 集群安装 目标 本文描述了如何从少数节点到包含上千节点的大规模集群上安装和配置 Hadoop 集群.如果只是为了尝试,你可以先从单台机器上安装开始(参阅单节点安装). 本文并不包含诸如安 ...
- 2010 NOIP提高组题解
机器翻译 用队列模拟题意即可 #include<cstdio> #include<iostream> #include<cstring> using namespa ...
- JavaScript new 关键词解析及原生实现 new
java里面,new 运算符是用来实例化一个类,从而在内存中分配一个实例对象. 但在 javascript 中,原型语言没类,只有对象与原型链继承 JavaScript 中 new 表达式的作用是生成 ...
- FSM自动售货机 verilog 实现及 code 细节讲解
1.题目: 饮料1.5 元, 可投入硬币1 元 0.5 元,输出饮料 零钱 2. 画出状态机. 3.仿真结果:coin=1 --> 0.5 元 coin=2-->1元 4.关键代码分析: ...
- nat转换技术,且用且珍惜
一.NAT转换技术 1.1.NAT技术概述 随着Internet的发展和网络应用的增多,IPv4地址枯竭已经成为制约网络发展的瓶颈.尽管IPv6可以从根本上解决IPv4地址空间不足的问题,但目前众多的 ...
- [.NET大牛之路 007] 详解 .NET 程序集
.NET大牛之路 • 王亮@精致码农 • 2021.07.13 上一篇我们介绍了 Roslyn 编译器,我们知道,我们编写的 C#/VB 代码经过 Roslyn 编译器编译后会生成程序集文件.按照之前 ...
- DVWA靶场之Command Injection(命令行注入)通关
Command Injection Low: <?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = $_REQUES ...
- Spring 学习笔记(3)Spring MVC
一.什么是 MVC MVC 实际上就是一种设计模式 Model-View-Controller Model 模型其实就是数据,Dao,Bean 等等 View 视图就是所看到的东西,网页,JSP,展示 ...