SQL语言——高级方法

本文深入探讨了SQL视图的创建方法,特别是SCHEMABINDING选项的应用,以及如何通过GROUP BY操作对数据进行分组并标记结果。内容涵盖了基本的SQL查询语句、视图创建步骤、SCHEMABINDING选项的影响,以及GROUP BY操作的具体应用,包括数据分组、条件判断和结果标记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、GROUP BY

原表:
courseid coursename score
-------------------------------------
1 java 70
2 oracle 90
3 xml 40
4 jsp 30
5 servlet 80
-------------------------------------
为了便于阅读,查询此表后的结果显式如下(及格分数为60):
courseid coursename score mark
---------------------------------------------------
1 java 70 pass
2 oracle 90 pass
3 xml 40 fail
4 jsp 30 fail
5 servlet 80 pass
---------------------------------------------------
写出此查询语句


select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') as mark from course


如果score>60,sign(score-60)=1;

       score==60,sign(score-60)=0;

       score<60,sign(score-60)=-1;

create a view

USE Northwind // 使用Northwind这个数据库

GO

CREATE VIEW vwSample

As

SELECT CustomerID, CompanyName, ContactName FROMCUSTOMERS

GO


use a view

SELECT * from vwSample


drop a view

DROP VIEW vwSample


Creating Views with theSCHEMABIND Option

使用了这个选项之后,view里面引用到的表的shema就不能被改变了。

Creating a view withthe SCHEMABINDING option locks the tables being referred by the view andprevents any changes that may change the table schema.

Notice two importantpoints while creating a view with SCHEMABINDING OPTION:

  • The objects should be referred to by their owner names [two part name].
     
  • SELECT * is not permitted.

Here’s an example fora view with the SCHEMABIND OPTION:

CREATE VIEW vwSample

With SCHEMABINDING

As

SELECT

          CustomerID,

          CompanyName,

          ContactName

FROM DBO.CUSTOMERS -- Two part name [ownername.objectname]

GO


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值