【SQLserver】一些SQL语句

-- 1. 数据库的创建与删除  
create database student; 
use student;
drop database student;

-- 2. 模式的创建与删除  
--create schema ty;
--drop schema ty;

-- 3. 表的创建和修改及删除  
create table Student
(
	Sno char(9) primary key,
	Sname varchar(20) unique,
	Ssex char(2),
	Sage smallint,
	Sdept char(20)
);

--create table TTY.Student
--(
--	Sno char(9) primary key,
--	Sname varchar(20) unique,
--	Ssex char(2),
--	Sage smallint,
--	Sdept char(20)
--);

--drop table TTY.Student;

--drop schema TTY ;

create table Course
(
	Cno char(4),
	Cname char(40) not null,
	Cpno char(4),
	Ccredit smallint,
	primary key(Cno),
	foreign key (Cpno) references Course(Cno)
);

create table SC
(
	Sno char(9) ,
	Cno char(4) ,
	Grade smallint,
	primary key(Sno, Cno),
	foreign key (Sno) references Student(Sno),
	foreign key (Cno) references Course(Cno)
);

alter table Student add Entrance date;
alter table Student add unique(Entrance);
alter table Student drop constraint UQ__Student__1CC0725B1273C1CD; 
alter table Student alter column Entrance varchar(20);
alter table Student drop column Entrance;

drop table SC;
drop table Student;

-- 4. 索引的创建与删除

create unique index Courno on Course(Cno);
create CLUSTERED index Courno on Course(Cno);
drop index Course.Courno;

-- 5. 基本表插入数据
	-- A. 满足完整性约束  
	-- B. 主码不能取重复值
	-- C. 注意输入次序
-- 6. 简单查询  
-- ① 查询两列
select sname, sno
from student ;

-- ② 整个基本表
select *
from student;  

-- ③ 运算的表达式
select sname 姓名, 2020 - sage 出生年份
from student;

-- ④ 函数  
select *
from sc;

select MAX(grade) 最大成绩
from sc; 

-- ⑤ 字符串  
select sname , '2020' 年份
from student;  

-- ⑥ 取消重复
select *
from sc;
  
select distinct sno
from sc;

select COUNT(distinct sno) 
from sc;

-- 7. where条件 not  
-- ① 比较大小
 select sname, sage 
 from student  
 where not sage < 20;  
 
-- ② 确定范围  
select sname , sage 
from student 
where sage not between 20 and 23;

-- ③ 确定集合  
select sname , sdept 
from student 
where sdept   in ('MA','IS');

-- ④ 字符串匹配  
select sname 
from student 
where not sname not like '刘%';  

select sname
from student 
where sname like '欧阳__';

-- ⑤ 指明通配符 
select sname
from student
where sname like '%\_%' escape '\';

-- 8. 空值查询  
select *
from course 
where cpno is NULL;  

select sno, cno, grade 
from sc
where grade is not null;

-- 9. 多重条件查询  
select sname, sage, sdept
from student
where sdept = 'CS' or sage < 20 ;

-- 10. 排序  
select sno, grade 
from sc
where sno = '200215121'
order by grade asc;

select *
from student 
order by sdept , sage desc;

-- 11. 聚集函数  
select *
from sc;

select COUNT(distinct sno)
from sc;

select *
from sc;

select AVG(grade)
from sc
where cno = '1';

select SUM(ccredit)
from sc, course
where sno = '200215121' and sc.cno = course.cno;


select *
from sc;

select MIN(grade)
from sc;

select MAX(grade)
from sc;

select * 
from sc
where  grade > AVG(grade);  -- error

-- error
SELECT sno, MIN(grade)
FROM sc

select sno, grade 
from sc
where grade in (select MIN(grade)
                 from sc);



-- 12. 分组查询  
select cno ,COUNT(sno)
from sc
group by cno;

select sno
from sc 
group by sno  
having COUNT(*) >= 2;  


SELECT MIN(grade)
FROM sc

select student.*,cno,grade 
from student, sc
where student.sno = sc.sno;


-- 13.连接查询  
-- a. 等值连接  
select student.*, sc.*
from student , sc
where student.sno = sc.sno;  

-- b. 自然连接  
select sc.sno, sname, ssex, sage, sdept,cno, grade
from student , sc
where student.sno = sc.sno;  

select student.*,cno, grade
from student , sc
where student.sno = sc.sno;  

-- c. 连接相关的复合操作
-- 2号课程成绩大于80分的学生学号、姓名  
select student.sno , sname  
from student , sc
where student.sno = sc.sno
      and  cno = '2'
      and  grade > 80; 
      
-- d. 自身连接  
-- 查询课程的先修课的先修课
      
select F.cno, S.cpno
from course F, course S 
where F.cpno = S.cno;

-- e. 外连接  

select course.* , sno, grade 
from course, sc 
where course.cno = sc.cno;

select course.* , sno, grade 
from course left outer join sc
on course.cno = sc.cno;

-- f.多表连接  
-- 查询学号、姓名、课程名、成绩   
select student.sno, sname, cname, grade 
from student, sc, course 
where student.sno = sc.sno
      and sc.cno = course.cno;
      
      
-- 14. 带有in谓词的子查询  
-- a. 查询与“刘晨”在同一个系学习的学生。  
-- a1. 嵌套查询 
select sno, sname, sdept
from student
where sdept in(select sdept
			   from student
			   where sname = '刘晨');
	  -- and sname <> '刘晨';
-- a2. 连接查询  
select s2.sname,s2.sno, s1.sdept
from student s1, student s2
where s1.sdept = s2.sdept  
      and s1.sname = '刘晨';
      
 
 -- b.  查询选修了课程名为“信息系统”的学生学号和姓名。 
 -- b1. 嵌套查询      
select Sno,Sname 
from Student
where Sno in (select Sno 
             from SC 
             where Cno in( select Cno 
						   from Course
						   where Cname='信息系统'));
-- b2. 连接查询 						   					   
select student.sno,sname
from student ,sc ,course
where student.sno=sc.sno
      and course.cno=sc.cno
      and course.cname ='信息系统'; 
      
-- 15. 带有比较运算符的子查询 
-- a. 查询与“刘晨”在同一个系学习的学生。  
select sno, sname, sdept
from student
where sdept = (select sdept
			   from student
			   where sname = '刘晨');
			   
			   
-- b. 找出每个学生超过他选修课程平均成绩的课程号。 
select sno, cno 
from sc x
where grade >= (
	select AVG(grade)
	from sc y
	where x.sno = y.sno);
	
	
-- 16. 带有any或all谓词的子查询  	
-- a.查询其他系中比信息系(IS)任意一个(其中某一个)学生年龄小的学生姓名和年龄。
-- a1. 使用any或all谓词 
select sname, sage, sdept
from student
where sage < any(
		select sage 
		from student
		where sdept = 'IS')
	  and sdept <> 'IS';
	  
-- a2. 使用聚集函数 
select sname, sage, sdept
from student
where sage < (
		select max(sage)
		from student
		where sdept = 'IS')
	  and sdept <> 'IS';
	  
-- b. 查询其他系中比信息系(IS)所有学生年龄小的学生姓名和年龄。
-- b1. 谓词all
select sname, sage, sdept
from student
where sage < all (
		select  sage 
		from student
		where sdept = 'IS')
	  and sdept <> 'IS';  
-- b2. 聚集函数
select sname, sage, sdept
from student
where sage < (
		select  MIN(sage)
		from student
		where sdept = 'IS')
	  and sdept <> 'IS';  
	  
-- 17. 带有exists谓词的子查询 
-- a. 查询所有选修了1号课程的学生姓名。   
select sname
from student 
where exists( select *
              from sc
              where sno = student.sno
              and cno = '1')
              
-- b. 查询没有选修1号课程的学生姓名。  
select sname
from student 
where not exists( select *
              from sc
              where sno = student.sno
              and cno = '1')
-- 18. 不同形式查询的替换  
-- a. 查询与“刘晨”在同一个系学习的学生。  
-- a1. 其他查询
select sno, sname, sdept
from student
where sdept = (select sdept
			   from student
			   where sname = '刘晨');
-- a2. exists查询 
select sno, sname, sdept
from student s1
where exists (select *
              from student s2
              where s2.sdept = s1.sdept
                    and s2.sname = '刘晨')
      and sname != '刘晨';
-- b. 查询选修了全部课程的学生姓名  
-- (没有一门课程是他不选的学生姓名)  student course sc 
select sname
from student
where not exists( select *
                  from course
                  where not exists( select *
                                    from sc 
                                    where sno = student.sno
                                      and cno = course.cno
                                     ));
-- c. 查询至少选修了学生201215122选修的全部课程的学生号码。  
-- ( 不存在这样的课程y,学生201215122选修了y,而学生x没有选。)
select distinct sno
from sc scx
where not exists( select *
                  from sc  scy
                  where sno = '201215122'
                        and not exists( select *
                                        from sc scz
										where sno = scx.sno
                                      and scy.cno = scz.cno
                                     )); 
                                     
-- 19.集合查询 
-- a. 集合并  
--  查询计算机科学系的学生及(or)年龄不大于19岁的学生   
select *
from student 
where sdept = 'CS'
union all
select *
from student
where sage <= 19;

-- b. 集合交   
-- 查询计算机科学系的学生与年龄不大于19岁的学生 交集  
select *
from student 
where sdept = 'CS'
intersect
select *
from student
where sage <= 19;

-- c. 集合差
                                                 
-- 查询计算机科学系的学生与年龄不大于19岁的学生 集合差  
select *
from student 
where sdept = 'CS'
except
select *
from student
where sage <= 19;

-- 20. 数据的插入   
-- a. 插入元组  
insert into student(sno, sname, ssex, sdept, sage)
values('201215128', '陈冬', '', 'IS', 18);

insert into student(sno, sname, ssex, sdept )
values('201215130', '陈冬1', '', 'IS' );


insert into student 
values ('201215136', '张冬', 18, '', 'IS')
      ,('201215135', '李冬2', 18, '', 'IS') ;
      
insert into sc 
values ('201215121', '1', 92)
      ,('201215121', '2', 85)
      ,('201215121', '3', 88) 
      ,('201215122', '2', 90) 
      ,('201215122', '3', 80)
      ,('201215125', '1', 96);
      
      
insert into student 
select '201215137', '王冬', 18, '', 'IS'
union all select '201215138', '王冬1', 18, '', 'IS'





select *
from student;
-- b. 插入子查询的结果  

-- b1. 创建一个表  
create table depage  
(
  sdept char(15),
  avgage smallint
);
-- b2. 插入子查询结果  
insert into depage(sdept, avgage)
    select sdept, AVG(sage)
    from student
    group by sdept;
    
select *
from depage;

-- 21. 数据修改  
-- a. 修改某个元组的值  
update student
set sage = 22
where sno = '201215121'; 

-- b. 修改多个元组  
update student
set sage = sage + 1;


update student
set sage += 1;

update student
set sage++; -- error 

-- c. 带子查询的修改语句  
update sc
set grade = 0
where sno in (select sno 
              from student
              where sdept = 'CS');
              
select *
from sc;

-- 22.删除数据  
-- a. 删除一个元组  
select *
from student;

delete 
from student
where sno = '201215128';

--b. 删除多个元组  
delete 
from depage;  

drop table depage;

-- c. 带有子查询的删除语句  

delete 
from sc
where sno in (select sno
              from student
              where sdept = 'CS');
              
              
              
 
              
-- 23.视图的创建与删除  
-- a. 创建 
-- a1.  建立信息系学生的视图包括学号 姓名 年龄 院系。

create view is_student
as
select sno, sname, sage, sdept
from student
where sdept = 'IS';

select *
from is_student;

create view is_student9
as
select sno, sname, sage, sdept
from student
where sdept = 'IS'
with check option;  

insert into is_student9
values ('201215142', '李四', 18, 'IS');


--a2.建立信息系选修了1号课程的学生视图。
create view is_s1(sno, sname, grade)
as
select student.sno, sname, grade
from student, SC
where student.sno = SC.sno
      and sdept = 'IS'
      and cno = '1';
      
select *
from is_s1;

insert into sc 
values ('201215125', '1', 96);

-- a3. 建立信息系选修了1号课程且成绩在90分以上的学生视图。
create view is_s2 
as
select *
from is_s1
where grade >= 90;

select *
from is_s2;



-- a4,将学生的学号及他的平均成绩定义为一个视图。

create view s_g(sno, gavg)
as
select sno, AVG(grade)
from SC
group by sno;

select *
from s_g;

--b 删除视图  
--b1 直接删除  
drop view is_s1;

--b2 删除基本表对视图的影响  
drop table sc; 


--24. 视图查询  
--a. 在信息系学生的视图中找出年龄小于20岁的学生。
select sno, sage
from is_student
where sage < 20;

-- 视图消解法
select sno, sage
from student
where sdept = 'IS'
      and sage < 20;
      
      
-- b. 在S_G视图中查询平均成绩在90分以上的学生学号和平均成绩。
select *
from s_g
where gavg >= 90;

-- 视图消解法  
select sno, AVG(grade) 
from SC
group by sno
having AVG(grade) >= 90;

-- 25.更新视图  
-- a 将信息系学生视图IS_Student中学号201215122的学生姓名改为“刘辰” 。
update is_student
set sname = '刘辰'
where sno = '201215122';

-- b 视图S_G为不可更新视图
update s_g
set gavg = 90
where sno = '201215121'; --error

-- 26. 创建登录  
-- a. 创建登录 
create login U1
with password = '123456';

create login U2
with password = '123456';

create login U3
with password = '123456';

-- 27. 创建用户   
create user use1
for login U1;

create user use2
for login U2;

create user use3
for login U3;

--28. 授权  

grant select 
on student 
to use1;


grant all privileges
on course 
to use2, use3;

grant update 
on student 
to use1;

grant select 
on student 
to use1
with grant option;

grant select 
on sc
to public;

--29. 权限回收  
revoke select 
on sc
from public;


revoke select
on course 
from use2;

revoke select
on student
from use1 CASCADE;

--30. 删除用户和登录  
-- a. 手动删除(保证该用户或登录没占用)  
-- b. SQL语句 

drop login U3;  
drop user use3;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/499071.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

武汉星起航:借亚马逊平台优势助力商家精准营销

在全球经济一体化的背景下&#xff0c;跨境电商已成为推动国际贸易发展的重要引擎。作为跨境电商领域的佼佼者&#xff0c;亚马逊平台以其全球化销售渠道和强大的数据分析能力&#xff0c;为商家提供了前所未有的商机。武汉星起航电子商务有限公司深知亚马逊平台的优势&#xf…

企业为什么要用MES管理系统来升级改造生产车间

随着信息技术的迅猛发展&#xff0c;工业制造领域正经历着深刻的变革。在这个大背景下&#xff0c;MES管理系统的引入和应用&#xff0c;已成为企业升级改造生产车间、提升竞争力的关键手段。本文将从多个维度探讨企业为何要用MES管理系统来升级改造生产车间。 MES管理系统能够…

查询优化-提升子查询-UNION类型

瀚高数据库 目录 文档用途 详细信息 文档用途 剖析UNION类型子查询提升的条件和过程 详细信息 注&#xff1a;图片较大&#xff0c;可在浏览器新标签页打开。 SQL: SELECT * FROM score sc, LATERAL(SELECT * FROM student WHERE sno 1 UNION ALL SELECT * FROM student…

云计算——大模型应用发展的“理想支点”

在数字化浪潮中&#xff0c;人工智能技术的突飞猛进为千行百业带来了前所未有的转型机遇。近期出现的Sora模型能够根据文本描述生成高质量的视频内容&#xff0c;为多领域带来大模型技术飞跃的冲击。如何推进大模型应用&#xff0c;已成为各行业面临的重要课题之一。 打造硬核…

亚信安慧AntDB数据库分布式架构剖析之snapshot sender进程

本文主要介绍亚信安慧AntDB数据库的分布式架构下的特有进程之snapshot sender进程的设计&#xff0c;与snapshot receiver进程是一对&#xff0c;也是分布式架构的核心进程之一。 进程简介 与Snapshot Receiver进程相对应&#xff0c;该进程的作用从整体上看也只包含两个方面&a…

Shopee 4月市场趋势及选品分析,shopee虾皮品类爆款预测

废话不多说&#xff0c;马上进入本周Shopee“现象爆品”、“热门爆品趋势”及“热搜词周榜"版块&#xff0c;帮助商家及时了解Shopee最新市场现状&#xff0c;掌握选品趋势。 1 现象爆品 近几来&#xff0c;为健康“买单”&#xff0c;已经成为全年龄层群体的“刚需”。 图…

第P1周:实现mnist手写数字识别

>- **&#x1f368; 本文为[&#x1f517;365天深度学习训练营](https://mp.weixin.qq.com/s/0dvHCaOoFnW8SCp3JpzKxg) 中的学习记录博客** >- **&#x1f356; 原作者&#xff1a;[K同学啊 | 接辅导、项目定制](https://mtyjkh.blog.csdn.net/)** 目录 一、前言 二、我…

青年才俊的聚集地 带你一览DATE 2024会议现场

会议之眼 快讯 第27届欧洲设计、自动化和测试会议&#xff08;Design, Automation and Test in Europe Conference &#xff09;已于2024 年 3 月 25 日-27日在西班牙瓦伦西亚圆满举办&#xff01;DATE第一届会议是在 1998 年在法国巴黎召开的。从那时起&#xff0c;DATE 会议…

ICLR 2024 | FeatUp: A Model-Agnostic Framework for Features at Any Resolution

论文&#xff1a;https://arxiv.org/abs/2403.10516代码&#xff1a;https://github.com/mhamilton723/FeatUp 背景动机 深层特征是计算机视觉研究的基石&#xff0c;捕获图像语义并使社区即使在零或少样本情况下也能解决下游任务。然而&#xff0c;这些特征通常缺乏空间分辨率…

俄罗斯深陷芯片自主困境,良率仅5成 |百能云芯

俄罗斯的芯片产业一直以来都面临着诸多挑战&#xff0c;尤其是在当前的国际形势下&#xff0c;这些挑战更加凸显。随着俄乌冲突的爆发&#xff0c;西方国家对俄罗斯实施了一系列经济制裁&#xff0c;导致俄罗斯科技产业受到了严重影响。据了解&#xff0c;俄国最大的本土芯片厂…

vue3使用vuedraggable实现拖拽(有过渡)

1. 安装与使用 vue中vuedraggable安装&#xff1a; pnpm i -S vuedraggablenext或者 yarn add vuedraggablenext注意&#xff1a;vue2和vue3安装的是不同版本的vuedraggable&#xff0c;写法上也会有一些区别。 比如在vue3中使用拖拽&#xff0c;要以插槽的方式&#xff0c;…

C# 操作 Word 全域查找且替换(含图片对象)

目录 关于全域查找且替换 Word应用样本 SqlServer数据表部分设计样本 范例运行环境 配置Office DCOM 设计实现 组件库引入 实现原理 查找且替换的核心代码 窗格内容 页眉内容 页脚内容 形状内容 小结 关于全域查找且替换 C#全域操作 Word 查找且替换主要包括如下…

GK7205V500国科微 GK7205RNCFV500 GOKE

GK7205V300 芯片是国科针对 IPC 市场推出的新一代支持 H.265 编码的低功耗 IPC SOC 芯片。 该芯片集成专用的 ISP&#xff0c;拥有高效的视频编码处理性能&#xff0c;支持 H.265 编码&#xff0c;满足客户各种差异化业务需求。集 成了 RTC、POR、Audio codec 以及丰富的外设…

大话设计模式之策略模式

策略模式是一种行为设计模式&#xff0c;它允许在运行时选择算法的行为。这种模式定义了一族算法&#xff0c;将每个算法都封装起来&#xff0c;并且使它们之间可以互相替换。 在策略模式中&#xff0c;一个类的行为或其算法可以在运行时改变。这种模式包含以下角色&#xff1…

Elasticsearch:语义搜索即服务处于卓越搜索的中心

作者&#xff1a;来自 Elastic Sherry Ger, Stephen Brown 对于许多企业来说&#xff0c;搜索卓越中心&#xff08;center of excellence - COE&#xff09;向其用户提供搜索服务&#xff0c;从不同的数据源中整理知识&#xff0c;并将搜索功能集成到其内部和外部应用程序中。…

Tensorflow2.0笔记 - metrics做损失和准确度信息度量

本笔记主要记录metrics相关的内容&#xff0c;详细内容请参考代码注释&#xff0c;代码本身只使用了Accuracy和Mean。本节的代码基于上篇笔记FashionMnist的代码经过简单修改而来&#xff0c;上篇笔记链接如下&#xff1a; Tensorflow2.0笔记 - FashionMnist数据集训练-CSDN博…

AI 异构计算机设计方案:902-基于6U VPX 高带宽PCIe的GPU AI 异构计算机

基于6U VPX 高带宽PCIe的GPU AI 异构计算机 一、产品概述 基于6U 6槽 VPX 高带宽PCIe的GPU AI 异构计算机以PCIe总线为架构&#xff0c;通过高带宽的PCIe互联&#xff0c;实现主控计算板、GPU AI板卡&#xff0c;FPGA接口板&#xff0c;存储板的PCIe高带宽互联访问&…

揭秘百度百科审核内幕,百科词条审核究竟需要多久?

百度百科作为国内最大的网络百科全书平台之一&#xff0c;致力于提供全面、准确的知识服务&#xff0c;同时也承担着审核百科词条的工作。在互联网时代&#xff0c;人们对信息的需求日益增长&#xff0c;因此百度百科的审核工作显得尤为重要。那么&#xff0c;百度百科词条审核…

152 Linux C++ 通讯架构实战7 ,makefile编写改成for cpp,读配置文件,内存泄漏查找,设置标题实战

读写配置文件代码实战。nginx.conf 一个项目要启动&#xff0c;需要配置很多信息&#xff0c;第一项就是学习如何配置一个项目 nginx.conf的内容 #是注释行&#xff0c; #每个有效配置项用 等号 处理&#xff0c;等号前不超过40个字符&#xff0c;等号后不超过400个字符&#…

虚幻引擎资源加密方案解析

前段时间&#xff0c;全球游戏开发者大会(Game Developers Conference&#xff0c;简称GDC)在旧金山圆满落幕&#xff0c;会议提供了多份值得参考的数据报告。根据 GDC 调研数据&#xff0c;当下游戏市场中&#xff0c;Unreal Engine (下文简称虚幻)和 Unity 是使用最多的游戏引…