http://www.cnblogs.com/zxlovenet/p/3728842.html

本文语句大部分SQL语句来自《数据库系统概论》(第四版)王珊&萨师煊 ,是我们上课用的教材,感觉很不错,总结了其中第三章的SQL语句,比较实用,希望对大家有帮助。总结如下,可以用来学习基础的SQL语句。

建立数据库

CREATE DATABASE DB_Student

建立表

CREATE TABLE Student

(Sno CHAR(9) PRIMARY KEY,--主码

Sname CHAR(20) UNIQUE,--唯一值

Ssex CHAR(2),

Sage SMALLINT,

Sdept CHAR(20)

);

CREATE TABLE Course

(Cno CHAR(4) PRIMARY KEY,

Cname char(40),

Cpno CHAR(4),

Ccredit SMALLINT,

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 S_entrance date--增加列

alter table student alter column Sage int--修改字段类型

alter table course add unique (Cname)--增加唯一性约束

drop table Student--删除基本表

drop table student cascade--删除基本表及相关依赖对象

创建索引

drop index stusname

查询数据

select sno,sname from student

select sname,sno,sdept from student

select sname,2004-sage from student

select sname,'Year of Birth:',2004-sage, lower(sdept) from student--查询结果第二列是一个算数表达式

select sname name,'Year of Birth:' BIRTH,2004-sage birthday,LOWER(sdept) department from student--LOWER()小写字母

select sno from sc

select distinct sno from sc--消除重复行

select sno from sc

select all sno from sc

select sname from student where sqept='CS'

--=、>、<、>=、<=、!=、<>、!>、!<  比较的运算符

select sname,sage from student where sage<20

select distinct sno from sc where sage<20

select sname,sdept,sage from student where sage between 20 and 23

select sname,sdept,sage from student where sage not between 20 and 23

select sname,ssex from student where sdept in ('CS','MA','IS')

select sname,sage from student where sdept not in('CS','MA','IS')

select * from student where sno like '200215121'

select * from student where sno='200215121'

--字符匹配

--% 任意长度字符串,_ 任意单个字符,ESCAPE 转义字符

select sname,sno,ssex from student where sname like '刘%'

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

select sname,sno from student where sname like '__阳%'

select sname,sno,ssex from student where sname not like '刘%'

select cno,ccredit from course where cname like 'DB\_design' escape '\'

select * from course where cname like 'DB\_%i__' escape '\'

select sno,cno from sc where grade is null --null 空值

select sno,cno from sc where grade is not null

select sname from student where sdept='CS' and sage<20

select sname,sage from studnet where sdept='CS' or sdept='MA' or sdept='IS'

select sno,grade from sc where cno='3' order by grade desc -- order by 排序

select * from student order by sdept,sage desc --空值最大

--聚集函数

select count(*) from student -- count() 行数

select count(distinct sno) from sc

select avg(grade) from sc where cno='1' -- avg() 平均数

select max(grade) from sc where cno='1' -- max() 最大值

select sum(Ccredit) from sc,course where sno='200215012' and sc.cno=course.cno -- sum() 总数

--分组

select cno,count(sno) from sc group by cno

select sno from sc group by sno having count(*) >3 --having 给出选择组的条件

--连接查询

select student. *,SC.* FROM STUDENT,SC where student.sno=sc.sno

select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno =sc.sno

select first.cno,second.cpno from course first,course second fwhere first.cpno=second.cno -- 自身连接

select student.sno,sname,ssex,sage,sdept,cno,grade from student left out join sc in (student.sno=sc.sno)--外连接

--from student left out join sc using (sno)

select student.sno,sname from student,sc where student.sno=sc.sno and sc.cno='2' and sc.grade>90

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 sno in (select sno from sc shere con='2')

select sdept from student where sname='刘晨'

select sno.sname,sdept from student where sdept='CS'

--嵌套查询

select sno,sname,sdept from student where sdept in (select sdept from studnet where sname='刘晨')

select sno,sname,sdept from student where sdept in ('CS')

select s1.sno,s1.sname,s1.sdept from student s1,student s2 where s1.sdept =s2.sdept and s2.sname='刘晨'

select sno,sname from student where sno in (select sno from sc where cno in(select cno from course where cname='信息系统'))

select student.sno,sname from student ,sc,course where student.sno=sc.sno and sc.cno =course.cno and course.cname='信息系统'

--内查询的结果是一个值,因此可以用=代替in

select sno,sname,sdept from student where sdpet=(se3lect sdept from studnet where sname='刘晨')

select sno,sname,sdept from student where(select sdept from student where sname='刘晨')=sdept

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

select avg(grade) from sc y where y.sno='200215121'

select sno,cno from sc x where grade>=88

select sname,sage from student where sage <ANY (SELECT sage from student where sdept='CS') and sdept <>'CS'

select sname,sage from student where sage<(select max(sage) from student where sdept='CS') and sdept <> 'CS'

select sname,sage from student where sage < all (select sage from student where sdept ='CS')

select sname,sage from student where sage<(select min(sage) from student where sdept='CS') and sdept <>'CS'

select sname from student where exists(select * from sc where sno=student.sno and cno='1')

select sname from student where not exists (select * from sc where sno=student.sno and cno='1')

select sno.sname,sdept from student s1 where exists(select * from studetn s2 where s2.sdept=s1.sdept and s2.sname='刘晨')

select sname from student where not exists (select * from course where not exists(select * from sc where sno=student.sno and cno=course.cno))

select distinct sno frome sc scx where not exists (select * from sc scy where scy.sno='200215122' and not exists(select * from sc scz where scz.sno=scx.sno and scz.cno=scy.cno))

http://zxlovenet.cnblogs.com

集合查询

select * from student where sdept ='CS' union select * from student where sage<=19 --union并操作

select sno from sc where cno='1' union select sno from sc where sc where cno='2'

select * from student where sdept='cs' intersect select * from student where sage<=19 --intersect 交操作

select * from student where sdept='cs' and sage<=19

select sno from sc where cno='1' intersect select sno from sc where cno='2'

select sno from sc where cno='1' and sno in (select so from sc where cno='2')

select * from student where sdept='cs' except select * from student where sage<='19' --except 差操作

select * from student where sdept ='cs' and sage>19

插入数据

insert into student(sno,sname,ssex,sdept,sage) values('200215128','陈东','男','IS','18')

insert into student values('200215126','张成敏','男','18','cs')

insert into sc(sno,cno) values('200215128','1')

insert into sc values('200215128','1',null)

更新数据

create table dept_age(sdept char(15) avg_agea smallint)

insert into dept_age(sdept,avg_age) select sdept,avg(sage) from student group by sdept

update student set sage=22 where sno='200215121'

update student set sage=sage+1

update sc set grade=0 where 'cs'=(select sdept from student where student.sno=sc.sno)

update is_student set sname='刘晨' where sno='200215122'

update student set sname='刘晨' where sno='200212122' and sdept='IS'

delete from student where sno='200215128'

delete from is_student where sno='200215129'

delete from student where sno='200215129' and sdept='IS'

delete from sc

delete fro sc where 'cs'=(select sdept from student where student.sno=sc.sno)

删除操作

delete from Student where Sno=’200215128’

delete from SC

delete from SC where ‘cs’ = (select Sdept from Student where Student.Sno=SC.Sno)

创建视图

create view is_student

as

select sno,sname,sage from student where sdpet='IS'

create view is_student

as

select sno,sname,sage from student where sdept='IS' with check option

create view is_s1(sno,sname,grade)

as

select student.sno,sname,grade from student,sc where student,sc where sdept=='IS' and student.sno=sc.sno and sc.cno='1'

http://zxlovenet.cnblogs.com

create view is_s2

as

select sno,sname,grade from is_s1 where grade>=90

create view bt_s(sno,sname,sbirth)

as

select sno,sname,2004-sage from student

create view s_g(sno,gavg)

as

select sno,avg(grade) from sc group by sno

create view f_student(f_sno,name,sex,age,dept)

as

select * from student where ssex='女'

删除视图

drop view is_s1 cascade

select sno,sage from is_student where sage<20

select sno,sage from student where sdept='IS' and sage<20

select is_sutdent.sno,sname from is_student,sc where is_student.sno=sc.sno and sc.cno='1'

select * from s_g where gavg>=90

分组

select sno,avg(grade) from sc group by sno

select sno,avg(grade) from sc where avg(grade)>=90 group by sno

select sno,avg(grade) from sc group by sno having avg(grade) >=90

存储过程

CREATE PROCEDURE Insert_pass

@pass NVARCHAR(50)

AS

BEGIN

DECLARE @count INT

SELECT @count = (SELECT Count(*)

FROM   list

WHERE  pass = @pass)

IF @count = 0

BEGIN

INSERT INTO list

(pass)

VALUES     (@pass)

END

END

执行存储过程

call procedure Insert_pass(2011)

删除存储过程

drop procedure Insert_pass()

触发器

CREATE TRIGGER insert_table1

ON table1

FOR INSERT, DELETE

AS

BEGIN

UPDATE table2

SET    count = (SELECT Count(*)

FROM   table1)

WHERE  id = 1;

END;

http://zxlovenet.cnblogs.com

游标:游标是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果,每个游标区都有一个名字。用户可以通过游标逐一获取记录,并赋值给主变量,交由主语言进一步处理。

练习用基础SQL语句的更多相关文章

  1. mysql使用基础 sql语句(一)

    csdn博文地址:mysql使用基础 sql语句(一)  点击进入 命令行输入mysql -u root -p,回车再输入密码,进入mysql. 终端命令以分号作为一条语句的结束,可分为多行输入,只需 ...

  2. MySQL数据库(一)—— 数据库介绍、MySQL安装、基础SQL语句

    数据库介绍.MySQL安装.基础SQL语句 一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 ...

  3. 《SQL Server基础——SQL语句》

    SQL Server基础--SQL语句       一.创建和删除数据库: 1.创建数据库(默认化初始值) 格式: CREATE DATABASE 数据库名称 例如: CREATE DATABASE ...

  4. 基础SQL语句/语法

    SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库  Create DATABAS ...

  5. mysql数据库之基础SQL语句/语法

    SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库  Create DATABAS ...

  6. oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

    select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...

  7. Oracle数据库 基础SQL语句练习

    一.说明 第一次使用Oracle,想做一些练习,熟悉一些oracle. 表:使用的是scott用户,默认的表 具体表讲解,可以参考该文档:https://www.cnblogs.com/xjcheng ...

  8. MySQL数据库(一)-- 数据库介绍、MySQL安装、基础SQL语句

    一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 (3)一个程序不太可能仅运行在同一台电脑上 ...

  9. 基础SQL语句

    SQL语句: 1.插入 方法一: "INSERT INTO [DB].[dbo].[T_Table] ([ID],[Name],[Amount],[Creater],[CreatedOn], ...

随机推荐

  1. Mac 开发者常用的工具

    转载:http://www.oschina.net/news/53946/mac-dev-tools 在写 Mac 程序员的十个武器之前,我决定先讲一个故事,关于 Mac 和爱情的.(你们不是问 Ma ...

  2. [MySQL Reference Manual] 20 分区

    20 分区 20 分区 20.1 MySQL的分区概述 20.2 分区类型 20.2.1 RANGE分区 20.2.2 LIST分区 20.2.3 COLUMNS分区 20.2.3.1 RANGE C ...

  3. [AlwaysOn Availability Groups]使用Powershell监控AlwayOn健康

    使用Powershell监控AlwayOn健康 1.基本命令概述 AlwayOn Dashboard是很有用的查看整体AG健康状况的工具.但是这个工具不是用于7*24监控的.如果应用程序夜间发送严重的 ...

  4. Oracle行转列、列转行的Sql语句总结

    多行转字符串 这个比较简单,用||或concat函数可以实现  SQL Code  12    select concat(id,username) str from app_userselect i ...

  5. mongodb简介(转)

    1.简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数 ...

  6. Linux 内核版本命名

    Linux 内核版本命名在不同的时期有其不同的规范,我们熟悉的也许是 2.x 版本奇数表示开发版.偶数表示稳定版,但到 2.6.x 以及 3.x 甚至将来的 4.x ,内核版本命名都不遵守这样的约定. ...

  7. jquery validate表单验证插件-推荐

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.     1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素  3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...

  8. 进击的Python【第二十一章】

    s14day21 上节内容回顾: 1.请求周期 url> 路由 > 函数或类 > 返回字符串或者模板语言? Form表单提交: 提交 -> url > 函数或类中的方法 ...

  9. [LeetCode] Strobogrammatic Number 对称数

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  10. 强大的支持多文件上传的jQuery文件上传插件Uploadify

    支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...