select * from Course
select * from SC
select * from Studentdrop table Course
drop table SC
drop table Studentcreate table Student--学生表
( Sno bigint primary key,--列级限制
Sname varchar(20)  not null,--不能为空
Ssex char(2),--中文字符占2个char
Sage smallint  check(Sage between 15 and 30),--限制年龄使用check语句
Sdept varchar(20)
)
create table Course--课程表
( Cno int primary key,--课程代码
Cname varchar(20) unique,--去唯一值,不能重复
Cpno varchar(20),
)
create table SC--选课表
( Sno bigint ,
Cno int,
Grade smallint check(Grade between 0 and 100 ),--限制成绩
primary key(Sno,Cno)--表级限制
)insert into Student values(20161101,'张一','女',20,'计科')
insert into Student values(20161102,'张二','女',21,'物电')
insert into Student values(20161103,'张三','男',19,'计科')
insert into Student values(20161104,'张四','男',20,'计科')
insert into Student values(20161105,'张五','男',21,'物电')
--可以用这种形式insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(20161101,'张一','女',20,'计科')
insert into Course values(1,'高等数学','')--也可以(1,'高等数学',null)
insert into Course values(2,'模拟电路','高等数学')
insert into Course values(3,'数字电路','模拟电路')
insert into Course values(4,'计算机组成原理','数字电路')insert into SC values(20161101,1,null)
insert into SC values(20161102,1,null)
insert into SC values(20161103,1,null)
insert into SC values(20161104,1,null)
insert into SC values(20161105,1,null)
insert into SC values(20161101,2,null)
insert into SC values(20161102,2,null)
insert into SC values(20161103,2,null)
insert into SC values(20161104,2,null)
insert into SC values(20161101,3,null)
insert into SC values(20161102,3,null)
insert into SC values(20161103,3,null)
insert into SC values(20161101,4,null)
insert into SC values(20161102,4,null)delete from SC where Sno=20161102 and Cno=4--删除学号为20161102,选修4号课程的选课记录
delete from SC--删除所有选课记录
delete from SC where Sno in(select Sno from Student where Sage=19)--嵌套子查询:记录唯一可以用=,否则用inselect * from SC where Grade is null--空值判断用is null和is not nullupdate Student set Sage=23 where Sage=19--修改年龄为19岁的学生的年龄为23
update Student set Sage=Sage+1--所有人年龄加1
update SC set Grade=60 where Sno in(select Sno from Student where Sdept='计科')--嵌套子查询,设置所有计科同学分数为60create table Avr_age
( Sdept varchar(20),
age int,
)
insert into Avr_age select Sdept ,avg(Sage) from Student group by Sdept
select * from Avr_age
--插入子查询结果
--函数大小写没有影响alter table Student add Sentrance date--向表增加一列“入学时间”
alter table Student alter column Sentrance varchar(10)--修改列的数据类型
alter table Student add unique(Sname)--向某列增加约束条件
alter table Student drop column Sentrance--删除表中某列
--column:列create unique index SCno on SC(Sno asc,Cno desc)--创建索引,此处条件为学号升序,再课程号降序
alter index SCno RENAME to SCCC--此处有问题
EXEC sp_rename 'SC.SCno','SCSno','INDEX'--这才是正确的
drop index SC.SCSno--索引必须写入表名select Sno,Sname from Student--查询指定内容
select Sage-1 from Student--查询中进行运算
select Sno,'hhh',Sname from Student--无中生有
select distinct Sno from SC --限制重复项多次出现
select * from Student where Sage<23--运算符
select COUNT(*) from Student
select SUM(Sage) from Student
select AVG(Sage) from Student
select MAX(Sage) from Student
select MIN(Sage) from Student
select Sno from Student group by Sno,Sage having AVG(Sage)>0--使用group by就不能使用where ,只能使用having
--凡是在group by后面出现的字段,必须同时在select后面出现;凡是在select后面出现的、
--同时未在聚合函数中出现的字段,必须同时出现在group by后面sel,检查sql是否符合上述法则。
select SC.*,Student.* from SC,Student --笛卡尔积,没有意义
select SC.*,Student.* from SC,Student where SC.Sno=Student.Sno--等值连接
select FIRSTT.Cname,SECONDD.Cpno from Course FIRSTT,Course SECONDD where FIRSTT.Cpno=SECONDD.Cname--自身连接
select Student.Sno,Sname,Ssex,Sdept,Cno,Grade from Student left outer join SC on (Student.Sno=SC.Sno)--外连接
select a.sno,Sname,avg1 from Student,
(select Sno,AVG(Sage) avg1 from Student Group by Sno) a
where Student.Sno=a.Sno--重命名一个虚表
select Student.Sno,Sname,Cname,Grade from Student,SC,Course where Student.Sno=SC.Sno and SC.Cno=Course.Cno--多表连接
select Sname from Student where exists
(select * from SC where Sno=Student.Sno and Cno=1)--exists用法,常用于显示表格列名(not exists)
select Sname,Sage from Student where Sage<all
(select Sage from Student where Sdept='物电')--all的用法
select Sname,Sage from Student where Sage<any
(select Sage from Student where Sdept='物电')--any的用法
select Sname,Sage from Student where Sage<some
(select Sage from Student where Sdept='物电')--some的用法create view JK_Student
as
select Sno,Sname,Sage from Student where Sdept='计科'--创建视图select * from JK_Student
--视图是一系列数据库语句操作select * from student where sage between 19 and 20--between的用法
select * from student where sdept in ('语文')--in/not in 的用法 select * from student where sname like '李%'
select * from student where sname like '李_'
select * from student where sdept like '%电'
select * from student where sno like '201611%'
--字符串替代及like的用法create table SC1
(
Sno char(9) not null,
Cno char(4) not null,
Grade smallint,
primary key(Sno,Cno),
foreign key(Sno) references Student(Sno),
foreign key(Cno) references Course(Cno)
)
--表级定义参照完整性grant select
on table Student
to U1
grant insert,update(Sno)--具体权限限制
on table SC
to U2
grant all privileges--全部权限授予
ob table SC
to U3
with grant option--允许再授予给别人
--授权操作revoke insert
on table SC
from U2 cascade--级联权限收回
revoke insert
on table SC
from public--全部查询权限收回create role xxx--创建角色create table Student
(
Sno numeric(6)
constraint c1 check(Sno Between 90000 and 99999),
Sname char(20)
constraint c2 not null,
Sage numeric
constraint c3 check(Sage<30),
Ssex char(2)
constraint c4 check(Ssex in('男','女')),
constraint StudentKey primary key(Sno)
)
alter table Student
drop constraint c1
alter table Student
add constraint C3 check(Sage<40)
--完整性约束命名子句(方便删除和修改)create trigger Student_Count
after insert on Student
referencing
new table as Delta
for each statement
insert into StudentInsertLog(Numbers)
select COUNT(*) from Delta
--触发器

数据库学习笔记(SQL server语句)相关推荐

  1. 数据库学习笔记---SQL基础-->层次化查询(START BY ... CONNECT BY PRIOR)

    SQL基础-->层次化查询(START BY ... CONNECT BY PRIOR) 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:______ ...

  2. MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)...

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. 【数据库学习笔记】Day03 - SQL语言基础及数据库定义功能

    [数据库学习笔记]Day03 - SQL语言基础及数据库定义功能 〇.本文所用数据库表格: 一.关系运算: 关系运算,数学名词,基本运算有两类:一类是传统的集合运算(并.差.交等),另一类是专门的关系 ...

  4. linux 指定库名 登录mysql_数据库学习笔记之MySQL(01)

    目录 01. 什么是数据库? 02. 常见数据库 03. MySQL简介 04. 数据库系统(Database System)的组成 05. SQL语言 06. SQL语言组成 07. MySQL的常 ...

  5. 数据库学习笔记第三弹——MySQL常用的图形化管理辅助工具及相关问题(图文详解2022))

    数据库学习笔记第三弹--MySQL常用的图形化管理辅助工具(图文详解2022) 文章目录 数据库学习笔记第三弹--MySQL常用的图形化管理辅助工具(图文详解2022) 1.MySQL常用的图形化管理 ...

  6. 数据库学习笔记(1)

    数据库学习笔记(1) 文章目录 数据库学习笔记(1) @[toc] DB 基本概念 连接数据库的三要素 元数据 mongoose 查询符合对象数组中某个对象的值 redis 是什么 存储的数据类型 r ...

  7. linux数据库创建score表,MySQL数据库学习笔记

    MySQL数据库学习笔记phpma (实验环境:Redhat9.0,MySQL3.23.54) 纲要: 一,连接MySQL phpma 二,MySQL管理与授权 三,数据库简单操作 四, 数据库备份 ...

  8. sql数据库 订阅发布_如何使用中央发布者和多个订阅者数据库设置自定义SQL Server事务复制

    sql数据库 订阅发布 In this article, you'll learn how to setup a simple, custom distributed database replica ...

  9. MySQL数据库 学习笔记 零基础入门 面试 整理

    一.MySQL基础篇 1. 数据库技术的基本概念和方法 1.1 数据库基本概念 1] 数据 数据(Data)指对客观事物进行描述并可以鉴别的符号,这些符号是可识别的.抽象的,不仅仅指狭义上的数字,而是 ...

  10. 数据库学习笔记(一) | 数据(Data)的定义

    数据库学习笔记(一) | 数据(Data)的定义和种类 什么是数据(Data) 结构化数据(Structured Data) 半结构化数据(Semi-structured Data) 非结构化数据(U ...

最新文章

  1. 用深度神经网络搭建马赛克神器,高清无码效果感人
  2. 深入理解java虚拟机(7)---线程安全  锁优化
  3. LeetCode Find K Pairs with Smallest Sums(大根堆、小根堆)
  4. 论文阅读:An Enhanced Deep Feature Representation for Person Re-identification
  5. 12_Android中HttpClient的应用,doGet,doPost,doHttpClientGet,doHttpClient请求,另外借助第三方框架实现网络连接的应用,
  6. 【转】Spring Bean单例与线程安全
  7. 华为oj题目c语言,华为OJ机试题目——24点游戏算法
  8. 08产品经理要明白的人性思维-团队管理篇
  9. 学python之前要学c语言吗_学Python之前需要学c语言吗
  10. 抑郁症是不可告人的病吗?
  11. 最优矩阵链乘(动态规划)
  12. ARM体系结构与编程 书
  13. 欢迎北京超图加入openGauss社区
  14. 8uftp更改上传的网页内容
  15. android pc投屏,如何解决乐播投屏中投屏失败的问题【乐播投屏】
  16. 近几天,被这个国家的最高科技奖刷屏了~
  17. GPIOA高8位输入控制低8位输出(位运算)
  18. iphone 计算机 桌面图标不见了,Mac苹果电脑的应用程序图标消失不见了如何恢复...
  19. 是时候该深入解析java虚拟机:编译概述,编译理论基础了
  20. C#经常用到的编程词汇

热门文章

  1. 【高级伪静态】IIS Rewrite 下载与配置
  2. 虽然惊天地,但不是人人都会哭泣 ——再论Kubernetes惊天地泣鬼神之大Bug
  3. 如何搜索海康相机的数量以及得到设备的IP等信息
  4. 基于微信小程序云开发(校园许愿墙app)妄想替代学校的表白墙
  5. 面试ppt计算机自我介绍,面试中技巧及怎样自我介绍.ppt
  6. 联想笔记本bios设置u盘启动
  7. 三菱PLC分拣程序基于三菱FX系列的分拣程序,可用于学习
  8. 第2章-系统控制原理 -> 线性系统理论
  9. b站江科大自化协51单片机入门教程笔记(2)
  10. 广州大学计算机网络实验五,计算机网络实验五.doc