使用SQL Sever语言进行数据库的操作

常用关键字
identity 自增长
primary key 主键
unique 唯一键
not null 非空
references 外键(引用)

在使用查询操作数据库是,要设置好需要操作的数据库,避免出现错误

1、删除表
drop table 表名
2、修改表
alter table 表名 add 列名 数据类型 ---追加
alter table 表名 drop column 列名

CRUD操作 ☆★☆

create 添加数据
read 读取数据
update 修改数据
delete 删除数据

1、添加数据
insert into 表名 value(```,```) 有几列加几列,不能遗漏

insert into 表名(列名,列名2) value(```,```) 改哪列表名后面加哪列

SQL Server中第一列如果是自增长列,那么添加时第一列可以忽略
其他数据库需要留空

2、删除数据
delete from 表名 逻辑上可行,运行删除删除所有数据,实际使用时禁止。

delete from 表名 where ids=5 删除ids=5这一行

3、修改数据
update 表名 set fcode='p016' 修改所有fcode的数据
update 表名 set fcode='p016' where ids=6 修改ids=5的fcode的数据
update 表名 set fcode='p016',mcode='p002' where ids=6

SQL中,布尔型的数据也需要加单引号

查询

1、简单查询

select * from 表名 --查询所有数据,*代表所有列

select 列名,列名 from 表名 --差指定列的数据,要查多列用逗号分隔

select 列名 as '代号’',列名 as '姓名' from 表名 --给列指定别名

2、条件查询

select * from 表名 where 条件

select * from 表名 where 条件 and 条件 --多条件并的关系

select * from 表名 where 条件 or 条件 --多条件或的关系

3、范围查询
select * from 表名 where 范围
例子:select * from 表名 where price>40 and price<50
同:select * from 表名 where price between 40 and 50

4、离散查询
select * form 表名 where 列名 in ('值','值','值')

select * form 表名 where 列名 not in ('值','值','值')

5、模糊查询

关键字查询

select * from 表名 where 列名 like '%关键字%' --%代表任意多个字符,%关键字 
% 查询包含所写关键字的行,关键字% 查询以关键字开头的行,%关键字 查询以关键 
字结尾的行,关键字 查询等于关键字的行

select * from 表名 where 列名 like '__E%' --查询第三个字符是E的

_代表一个字符

6、排序查询

select * from 表名 order by 列名 --根据by后面的列里的数据进行排序,默认是 
升序

select * from 表名 order by 列名 desc --desc代表降序,asc 代表升序

select * from 表名 order by 列名 desc,列名 asc --先根据第一个条件排序,相 
同的再根据第二个条件排序。前是主条件,后面是次要条件

7、分页查询

select top 数目 * from 表名

select top 数目 * from 表名 where 别名 not in (select top 数目 列名 from 
表名)

例子:
当前页:page = 2;每页显示:row = 10;
select top 5 * from Car where Code not in (select top (page-1)*row Code 
from Car)

8、去重查询

select distinct 别名 from 表名 --把重复的去掉

9、分组查询
select * from 表名 group by 列名 having count(*)>2
根据列名 进行分组,条件是数量大于2 count(*)代表个数

10、聚合函数(统计查询)

select count(*) from 表名 --查询所有数据条数

select count(列名) from 表名

select sum(列名) from 表名 --查询总和

select avg(列名) from 表名 --查询平均

select max(列名) from 表名 --查询最大值

select min(列名) from 表名 --查询最小值

关键字不区分大小写

SQL server 数据库 操作及简单查询的更多相关文章

  1. 【转】sql server数据库操作大全——常用语句/技巧集锦/经典语句

    本文为累计整理,有点乱,凑合着看吧! ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ ☆ ☆ ☆ sql 宝 典 ☆ ☆ ☆ 2012年-8月 修订版 ☆ ...

  2. mongodb数据库操作之简单查询

    1. 2. 3.修改器 默认一条一条修改 4. 5.查询 6.mysql简单操作

  3. SQL Server数据库阻塞,死锁查询

    sql 查询卡顿数据库 SELECT SPID=p.spid, DBName = convert(CHAR(20),d.name), ProgramName = program_name, Login ...

  4. sql server数据库操作

    --插入整行数据 , '1983-08-29', 'A', 'A', 'A') --插入部分列数据 , '1983-08-29') --删除行记录 delete from person where n ...

  5. 菜鸟级asp.net 与ms sql server数据库打交道的简单总结

    using System.Data.SqlClient;using System.Data; 上面是必须的 下面说的都是用存储过程 首先是webconfig里面的连接字符串: <connecti ...

  6. Sql Server数据库之多表查询

    一.连接查询 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据 目的:实现多表查询操作 语法:From join_table join_type join_table[ON(join_con ...

  7. c# SQL Server数据库操作-数据适配器类:SqlDataAdapter

    SqlDataAdapter类主要在MSSQL与DataSet之间执行数据传输工具,本节将介绍如何使用SqlDataAdapter类来填充DataSet和MSSQL执行新增.修改..删除等操作. 功能 ...

  8. SQL SERVER 数据库操作脚本

    创建数据库 create Database MYDB on ( Name=mydb_dat, FileName='c:\data\mydate.mdf',size=10,maxsize=50 ) LO ...

  9. SQL Server 数据库操作类

    /// <summary> /// SQLServerHelper的摘要说明. /// </summary> public class SQLServerHelper { pu ...

随机推荐

  1. php+mysqli实现批量执行插入、更新及删除数据的方法

    本文实例讲述了php+mysqli实现批量执行插入.更新及删除数据的方法.分享给大家供大家参考.具体如下: mysqli批量执行插入/更新/删除数据,函数为 multi_query(). 下面的代码只 ...

  2. Swift-9-类和结构体

    // Playground - noun: a place where people can play import UIKit // 几个重要的概念Properties/Methods/Subscr ...

  3. 判断下列语句是否正确,如果有错误,请指出错误所在?interface A{

    判断下列语句是否正确,如果有错误,请指出错误所在? interface A{ int add(final A a); } class B implements A{ long add(final A ...

  4. C# 路径的使用

    // 摘要: // 获取或设置包含该应用程序的目录的名称. // // 返回结果: // 应用程序基目录的名称. AppDomain.CurrentDomain.SetupInformation.Ap ...

  5. (转)SQL执行顺序

    SQL语句理解:http://blog.jobbole.com/55086/ 窗口函数/分析函数:http://blog.csdn.net/mfkpie/article/details/1636451 ...

  6. Asynchronous calls and remote callbacks using Lingo Spring Remoting

    http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...

  7. noip2006 金明的预算

    题目链接:传送门 题目大意:略.. 题目思路:其实单就这道题来说,一个主件最多两个附件,且附件不再包含附件,所以很简单,但是如果主件的附件无限制,附件也可包含无限制的附件,应该怎么做? 首先推荐一篇论 ...

  8. 【BZOJ4382】[POI2015]Podział naszyjnika 堆+并查集+树状数组

    [BZOJ4382][POI2015]Podział naszyjnika Description 长度为n的一串项链,每颗珠子是k种颜色之一. 第i颗与第i-1,i+1颗珠子相邻,第n颗与第1颗也相 ...

  9. 一个有意思的 Java HashSet 问题

    昨天,在百度的 java吧 看到有人问关于 HashSet 的问题.下面是他贴出的代码: import java.util.HashSet; public class JavaTest { publi ...

  10. js如何去除多个cookie?

    转自:https://zhidao.baidu.com/question/211006012.html :1:设置cookie 最简单的就是:document.cookie="user=aa ...