--创建表格

CREATE TABLE STUDENT
(
    STUDENT_ID CHAR(5) NOT NULL PRIMARY KEY,
    NAME VARCHAR(20) UNIQUE,
    GENDER CHAR(10) DEFAULT('MALE'),
    BIRTHDAY INT,
    SDEPT VARCHAR(15)
);
--创建索引
create index student_name on student(name)

create table course
(
    cno char(4) not null primary key,
    cname char(40),
    cpno char(4),
    CCREADIT int,
    FOREIGN key(cpno) REFERENCES course(cno)
)

CREATE TABLE SC
(
    STUDENT_ID CHAR(5) NOT NULL,
    CNO CHAR(4) NOT NULL,
    GRADE SMALLINT,
    PRIMARY KEY(STUDENT_ID,CNO),
    FOREIGN KEY(STUDENT_ID) REFERENCES    STUDENT(STUDENT_ID),
    FOREIGN KEY(CNO) REFERENCES COURSE(CNO)
);

--选择某些列、计算表达式、常量、函数、别名
select student_id,name,gender,2012-birthday  年份,'中国人' 国籍,upper(sdept) 系别 from student;
--消除重复的行
select distinct student_id from sc;

--查询练习

select * from student where birthday>21;
select * from student where birthday between 21 and 22;
select * from student where sdept not in('cs','is')

select * from student where name like '张%';
select * from student where name like '张__';

insert into student values('00004','王五','female',24,'ma');
insert into student values('00005','张家明','female',25,'ma');
insert into student(student_id,name) values('00006','张大明');

--空值查询
select * from student where gender is null;

--排序(order by)
select * from student order by birthday asc
select * from student order by birthday desc
select * from student order by name desc;

--聚合函数
select count(*) from student;
select sum(grade) from sc where student_id='00001'
select avg(grade) from sc where student_id = '00001';
select max(grade) from sc where student_id = '00001';
select min(grade) from sc where student_id = '00001';

select * from sc;
select student_id, sum(grade) from sc group by student_id;
select student_id, sum(grade) from sc group by student_id having sum(grade)>140;
select sdept,count(*) from student group by sdept having count(*)>1;

select sdept ,count(*) from student group by sdept;
select gender,count(*) from student where sdept= 'cs' group by gender
select sdept,gender,count(*) from student group by sdept,gender
select sdept,count(*) from student where gender='male' group by sdept having count(*)>1;

--等值连接
select * from  student,sc where student.student_id = sc.student_id;

--自身连接
select * from course a,course b where a.cpno = b.cno;

select * from course;
select * from student a,student b
select * from student a,student b where b.name='张三' and a.sdept = b.sdept and a.name!='张三';
select a.name,a.gender from student a,student b where b.name='张三' and a.birthday>b.birthday

select * from sc;
select * from student b;
select * from student a,sc b where a.student_id = b.student_id;

--左连接
select * from student a left outer join sc b on a.student_id = b.student_id;
select * from sc a right join student b on a.student_id = b.student_id;
select * from sc;

--多张表的等值连接
select a.student_id,a.name,b.cname,c.grade from student a,course b,sc c
    where a.student_id = c.student_id and c.cno = b.cno
    
select a.student_id,a.cno,a.grade from sc a,student b where a.student_id = b.student_id and b.name='张三' ;

select b.grade from student a,sc b,course c
    where a.student_id = b.student_id and b.cno = c.cno and a.name='张三' and c.cname='高等数学上'
select b.student_id,b.name  from sc a,student b,course c
    where a.student_id= b.student_id and c.cno = a.cno and c.cname='高等数学上'
select c.cno,c.cname from student a,sc b,course c
    where a.student_id = b.student_id and b.cno = c.cno and a.name='张三'

select a.*  from student a,sc b where a.student_id = b.student_id and b.cno='c01'
select * from student where student_id in (select student_id from sc where cno='c01');

select * from student where sdept in (select sdept from student where name='张三');

--不相关嵌套查询
select * from student where student_id in
    (select student_id from sc where cno in
        (select cno from course where cname='高等数学上'));

select * from sc;

--相关嵌套查询
select * from sc x where grade>
(
    select avg(grade) from sc y where y.student_id=x.student_id
)

--查询比张三年龄大的学生学号、姓名
select student_id,name from student where birthday>(select birthday from student where name='张三')

--查询选修每门课程中成绩小于其平均成绩的学生学号
select * from sc x
where x.grade<
(
    select avg(grade) from sc y where y.cno = x.cno
)
--查询张三选修高等数学1的课程的成绩
select grade from sc where student_id=(select student_id from student where name='张三')
and cno=(select cno from course where cname='高等数学上')

--查询张三选修课程的平均成绩
select avg(grade) from sc where student_id = (select student_id from student where name='张三')

--查询选修课程的平均成绩小于张三平均成绩的学生学号
select student_id, avg(grade) from sc group by student_id having avg(grade)<
(
select avg(grade) from sc where student_id = (select student_id from student where name='张三')
)

--查询课程的平均成绩低于张三平均成绩的课程号
select cno,avg(grade) from sc group by cno having avg(grade)<
(
    select avg(grade) from sc where student_id = (select student_id from student where name='张三')
)

--查询选修课程成绩大于等于该课程平均成绩的学生学号

select * from sc x
where x.grade>=
(
    select avg(grade) from sc y where y.cno = x.cno
)

select * from
(
    select * from student where student_id in
    (
        select student_id from sc where cno in
        (
            select cno from sc where student_id=
            (
                    select student_id from student where name='张三'
            )
        )
    )
) a where a.gender='male'

--union
create table stu
(
    id char(5),
    name char(20),
    age int
)

insert into stu values('90001','陈三',21);
insert into stu values('90002','陈四',22);
insert into stu values('90003','陈五',23);

insert into stu(id) values('90004')

--union
--在线视图
select * from
view_a a
where a.age>22;

select * from
view_a a
where a.age>22;

select * from
view_a a
where a.age>22;

select * from
view_a a
where a.age>22;

create view view_a as

select * from stu
union
select student_id,name,birthday from student

select * from student;
insert into stu values('00001','张三',21);

--except
select * from stu
except
select student_id,name,birthday from student

--INTERSECT

select * from stu
INTERSECT
select student_id,name,birthday from student

--获取记录的前3条
select top 3 * from student order by birthday desc;

--复制表
select * into b from student;
select * from b;

--仅仅复制表结构
select * into c from student where 2>10
select * from c;

--批量插入数据
insert into stu select student_id,name,birthday from student

select name from sysobjects where type='U'

--批量初始化表
TRUNCATE TABLE stu
select * from stu

--随机取出某表中2条记录
select   top 2 *   from   student order  by  NewID()

create table deptage
(
    dept char(15),
    avgage int
)

insert into deptage select sdept,avg(birthday) from student group by sdept
select * from deptage;

select * from student;

update student set gender='male' ,birthday=23,sdept='ma' where student_id= '00006'

update student set birthday=20;
 
 update student set birthday = birthday+1;
 
 select * from sc;
 
 update sc  set grade=grade+10 where
  'cs'=(select sdept from student y where y.student_id=sc.student_id);
 
  select * from student;
delete from student where student_id='00006'

delete from sc where student_id= (select student_id from student where name='张三');

create view stu1 as
select student_id,name,gender from student

select * from stu1;

CREATE VIEW IS_STUDENT
AS
SELECT STUDENT_ID,NAME,BIRTHDAY FROM STUDENT WHERE
SDEPT = 'IS';

select * from IS_STUDENT

select * from student;

SELECT STUDENT_ID,NAME,2010-BIRTHDAY
FROM STUDENT;

-------------------------------------------存储过程-游标-触发器-----------------------------------------------
create procedure CheckStudent
as
 insert into stu values('80001','zs',23)
 select * from stu;
 
 
alter procedure CheckStudent @id char(5),@name char(20),@age int
as
 insert into stu values(@id,@name,@age)
 select * from stu;

------存储过程的定义开始---第一次创建用CREATE,修改用ALTER--------
 
---多表操作  try catch    ;output; set;
alter procedure AddStuInfo @student_id char(5),@name char(20),@cno char(5),
    @cname char(20),@grade int,@myerror char(50) output
as
    begin try
        insert into student(student_id,name) values(@student_id,@name)
        insert into course(cno,cname) values(@cno,@cname)
        insert into sc values(@student_id,@cno,@grade)
        set @myerror='成功!'
        return 0
    end try
    begin catch
        set @myerror='主键冲突,新增失败'
        return -1
    end catch
------存储过程的定义结束-----------

----------------存储过程的执行开始-------------
--定义变量
declare @myerror char(50)
--使用包含output参数
execute AddStuInfo '20002','陈七','c06','工商管理2',90,@myerror output
--显示值
select @myerror
----------------存储过程的执行结束-------------

alter procedure AddStuInfo2
     @student_id char(5),--学号
     @name char(20),    --姓名
     @cno char(5),        --课程号
     @cname char(20),    --课程名
     @grade int,         --成绩
     @myerror char(50) output  --输出错误信息
as
    begin try
        insert into student(student_id,name) values(@student_id,@name)  --新增学生表
        insert into course(cno,cname) values(@cno,@cname)                --新增课程表
        insert into sc values(@student_id,@cno,@grade)                    --新增选课表
        set @myerror = '执行成功'
        return 0
    end try
    begin catch
        set @myerror = '参数错误,请检查'
        return -1
    end catch
    
    
declare @myerror char(50)
execute AddStuInfo2 '91003','陈9','c83','机械工程',60,@myerror output
select @myerror

select * from student;
select * from course;
select * from sc;

execute CheckStudent '10002','ls',21;

-----游标运用的准备,准备一个分数调整表,用于修正学生考试分数
create table AddGrade
(
    student_id char(5),
    cno char(5),
    ModiGrade int
)
insert into AddGrade values('00001','c01',10);
insert into AddGrade values('00001','c02',-10);
insert into AddGrade values('00002','c01',20);
insert into AddGrade values('00002','c02',10);

select * from AddGrade;
select * from sc;

-----创建包含游标的存储过程,逐条读取和更新分数
create procedure ModiGrade
as
    --申明变量
    declare @student_id char(5),
            @cno char(5),
            @ModiGrade int
    --申明游标
    declare mycursor cursor for select student_id,cno,ModiGrade from AddGrade
    --打开游标
    open mycursor
    --将游标里的值放置到变量里
    fetch next from mycursor into @student_id,@cno,@ModiGrade
    while(@@fetch_status=0)
    begin
        update sc set grade=grade+@ModiGrade where student_id=@student_id and cno=@cno
        fetch next from mycursor into @student_id,@cno,@ModiGrade
    end
    close mycursor   --关闭游标
    deallocate mycursor    --释放游标

---执行此存储过程    
exec ModiGrade

create table SC_HIS
(
    student_id char(5),
    cno char(5),
    grade int
)

create trigger SCDelete on sc for delete
as
    insert into sc_his select * from deleted;
    
alter trigger SCInsert on sc instead of insert
as
    insert into student(student_id) select student_id from inserted
    insert into course(cno) select cno from inserted
    insert into sc select * from inserted
    
insert into sc values('70002','c88',90)

select * from student;
select * from course;
select * from sc;
select * from sc_his;

delete from sc where student_id='91002'

转载于:https://www.cnblogs.com/holyknight-zld/archive/2012/08/03/sql.html

摘记_SQL常用语法相关推荐

  1. sql常用语法命令及函数_SQL右连接命令:语法示例

    sql常用语法命令及函数 For this guide we'll discuss the SQL RIGHT JOIN. 对于本指南,我们将讨论SQL RIGHT JOIN. 正确加入 (Right ...

  2. Markdown通用的常用语法说明

    前言 Markdown 是一种轻量级的 标记语言,语法简洁明了.学习容易,还具有其他很多优点,目前被越来越多的人用来写作使用. Markdown具有一系列衍生版本,用于扩展Markdown的功能(如表 ...

  3. jsp 4种常用语法3个编译指令7个动作指令

    4种常用语法: 注释:<%--注释--%> 声明:<%!声明部分%> 输出:<%=%> 脚本:可以包含任何java可执行代码.例 <% for(int i=0 ...

  4. [转]C++/CLI与C#常用语法对比

    [转]C++/CLI与C#常用语法对比 Kenny Kerr 一篇名为C++: The Most Powerful Language for .NET Framework Programming文章中 ...

  5. 每天学一点儿shell:Shell的常用语法规则

    文章目录 Shell 参数传递 Shell 数组 Shell 基本运算符 算数运算符 关系运算符 布尔运算符 逻辑运算符 字符串运算符(重点) 文件测试运算符 Shell 常用语法 Shell 函数 ...

  6. 【shell】常用语法 -b file -c file -f file-d file -x file

    [shell]常用语法 -b file  -c file  -f file-d file  -x file      一.test条件判断 1,test文件测试: -b file     若文件存在且 ...

  7. Go Time常用语法

    Go Time常用语法 目录 获取当前时间戳和string类型的格式化时间 构造指定时间 时间戳和格式化时间相互转换 获取当前几月,几号,星期几 待续,用到什么补充什么 1. 获取当前时间戳和stri ...

  8. Mysql常用语法总结

    Mysql常用语法总结如下: #连接mysql数据库(Dos下面) mysql -u root -p 123 #创建数据库 create database myschool; #创建表 drop ta ...

  9. MySQL常用语法记录

    0.说明 记录MySQL使用到的常用语法. 1.MySQLDISTINCT唯一 2.MySQLLIMIT限制 3.MySQL ORDER排序升序 4.MySQL DESC降序 5.MySQL WHER ...

最新文章

  1. react学习(6)----react样式多用内联
  2. Python 远程部署利器 Fabric2 模块
  3. Android日志[进阶篇]二-分析堆栈轨迹(调试和外部堆栈)
  4. 关于事件委托的整理 ,另附bind,live,delegate,on区别
  5. [转载] python下 import Matplotlib.pyplot as plt 的使用
  6. charts引入icon图片_如何在React中优雅的使用icon
  7. VS Code\unins000.exe创建报错解决方法
  8. 汉语言01C1501,2019年北京自考汉语言文学(本科)专业(01C1501)要考哪些科目?...
  9. excel vba 数据分析
  10. 3种结构ZnO基半导体纳米复合材料-图文详解
  11. 数据结构几类排序的总结和完整代码 待续。。
  12. 扇贝python骗局_北斗揭獐子岛扇贝骗局:27条采捕船数万航行数据还原轨迹
  13. AE教程丨1分钟学会制作信号故障风特效
  14. c++三国杀【免费复制】——转载
  15. Office word 列表级别不显示解决办法
  16. 计算机故障维修智能检测平台管理系统,计算机检测维修与数据恢复技能大赛竞赛设备采购项目.doc...
  17. 智能车的“耳朵”电磁检测传感器
  18. AURIX TC397 SCU 之 Watchdog 看门狗
  19. 银行核心系统如何选型分布式数据库(含6大落地要点验证)
  20. Solr基础教程之环境搭建(一)

热门文章

  1. 饶毅是一个什么样的人?
  2. Qt通过setProperty来达到设置控件的不同样式表
  3. Java Script
  4. 计算机网络安全人为因素,计算机网络安全问题
  5. 今日端午,竟然这几家互联网大厂没有福利礼盒……
  6. browserng7.1.5 vs opera mobile 10
  7. 如果你要开发一个中小学生学习数学的软件,你应该找谁做用户调研?
  8. 浪潮计算机组装过程,浪潮财务软件系统基本安装简易流程(附图)-20210419193145.docx-原创力文档...
  9. python scipy.stats.norm.cdf_python的scipy.stats模块中正态分布常用函数总结
  10. 小满是什么季节?小满如何养生?