TSQL语言基础笔记之单表查询

db_id()函数

返回对象的id,如果返回的对象为null,则判断不存在

if db_id('testdb') is null --判断数据库是否存在
	create database testdb

在每个数据库中都会自动创建一个名为dbo的架构。当用户没有将默认架构关联到其他架构时,就会将dbo作为默认架构。

use testdb; --切换数据库上下文

object_id()函数

返回对象的id,其中u代表用户表。

if object_id('dbo.Employees','u') is null
begin
	create table dbo.Employees
	(
		empid int not null,
		firstname varchar(30) not null,
		lastname varchar(30) not null,
		hiredate date not null,--date类型为2008版本新增加的功能
		mgrid int null,--经理id
		ssn varchar(20) not null,--social security number
		salary money not null
	);
end

数据完整性

  1. 声明式完整性

作为模型的一部分而实施的数据完整性。
主键、唯一约束、检查约束、default约束。除了default约束外,其他所有约束都可以定义为组合约束(即基于一个或多个属性的约束)。

  1. 过程式完整性

用代码来实施的数据完整性

SELECT语句的元素

查询语句的逻辑处理顺序

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING 过滤组
  5. SELECT
    1. OVER
    2. DISTINCT
    3. TOP
      1. with ties
  6. ORDER BY

with ties选项:能够请求返回与TOP n行中最后一行的排序值相同的其他所有行

OVER子句

谓词和运算符

原文地址:https://www.cnblogs.com/liuguangyin/p/6557531.html