复习之前课本上的sql语句,以前上课的时候都是老师在上面讲,我们在下面玩,根本没有把这个放在心上,等到考试的时候临时学习突击下,就可以顺利过60了,但是现在不行了,自己要重新的学习sql,应该把里面最基本的语句和语法都全部的学习下,每个案例都要在数据库中执行下,我们需要这些基本的知识点。

1:创建表,修改表,添加列

/*创建基本表Student*/
create table Student(
Sno char(5) not null unique, /*定义的学号不能为空,并且是唯一的值。*/
Sname char(20),
Ssex char(2),
Sage int,
Sdept Char(15)
);
/*修改基本表Student*/
/*添加新列Scome*/
alter table Student Add Scome DATE;
/*修改数据类型*/
alter table Student MODIFY Sage SMALLINT;
/*删除学号的唯一标识*/
alter table Student DROP UNIQUE(Son);
/*删除表*/
drop table Student;
/*创建索引*/
CREATE CLUSTER INDEX Stusname ON Student(Sname);
/*删除索引*/
drop index Stusname;

2:基本的查询

/*查询*/
select Sname,Sage from Student; select Sname,Sage,Sdept from Student; select * from Student; select Sname,1996-Sage from Student; select Sname,'Year of Birth:',1996-Sage,LOWER(Sdept) from Student; select Sname NAME,'Year of Birth:'BIRTH,1996-Sage BIRTHDAY,LOWER(Sdept) DEPARTMENT FROM Student; /*为列创建别名*/

3:复杂的查询

	    /*消除重复行*/
select DISTINCT Sno from Student; /*查询满足条件的元组*/
select Sname from Student where Sdept="CD"; select Sname,Sage from Student where Sage>=20; alter table Student add Grade int; select DISTINCT Sno from Student where Grade<60;
/*确定范围-----------between and */
select Sname,Sage,Sdept from Student where Sage between 20 and 23;
/*确定集合-----------IN('','','')*/
select Sname,Ssex from Student where Sdept IN('IS','MA','CS');
/*字符匹配-----------LIKE+++( %:代表任意长度)( _ :代表单个字符)*/
select Sname,Sage from Student where Sname LIKE 'A%'; SELECT Sname ,Sage from Student where Sname LIKE 'A_'; /*字符匹配-------里面的转义字符-----ESCAPE'\':表示\为转义字符*/
/*查询条件是“A_”此时这里的_不是代表一个字符,只是单纯的表示下划线而已。因为语句前面有转义字符。*/
select Sname from Student where Sage LIKE 'A\__' ESCAPE '\'; /*涉及空值的查询*/
select Sname,Sage from Student where Grade IS NULL; /*查询成绩为空的学生*/

4:更复杂查询

/*多重条件查询*/
select Sname from Student where Sdept='cd' and Sage>20; select Sname from Student where Sdept='cd' or Sage>20; /*对查询结果进行排序*/ select Sname Grade from Student where Sage>20 order by Sage DESC; select * from Student order by Sage DESC; /*使用集函数*/
select count(*) from Student; /*求总个数*/ select count(distinct Sno) from Student; select avg(Sage) from Student where Sname='ahui'; /*avg:求平均值*/ select Sname,count(Sage) from Student group by Sname; select MAX(Sage) from Student where Sno='1'; /*最大值*/

5:连接查询

/*连接查询*/
/*等值的查询*/
select Student.*,SC.* from Student,SC where Student.Sno=SC.Sno; /*自然连接两个表*/
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,SC.Grade from Student,SC where Student.Sno=SC.Sno;
/*将表Student进行了重新命名,为两个名字,从而进行对自己的查询*/
select FIRS.Sage,SECO.Sno from Student FIRS,Student SECO where FIRS.Sno=SECO.Sage; /*外连接*/ select Student.Sno,Sname,Sage,Sdept,Ssex,Cno,SC.Grade from Student,SC where Student.Sno=SC.Sno(*); /*复合条件连接--------就是利用and来进行操作*/ select Student.Sno,Sname from Student,SC where Student.Sno=SC.Sno and SC.Cno='2' and Student.Sage=2;

6: 嵌套查询

这个查询是将最里面的查询结果当作外面查询的条件来进行查询的,执行顺序是从里到外执行的。注意:最里面的查询(子查询的select语句中不能使用ORDER BY子句,ORDER BY子句只能对最终的查询结果进行排序)

   /*嵌套查询---就是把一个查询的结果当做另一个查询的条件来进行查询*/

      select Sname from Student where Sno
IN(
select Sno from SC where Cno='2'
);
/*--01:带有IN的子查询*/
select Sno,Sname,Sdept from Student where Sdept IN( select Sdept from Student where Sname='ahui');
/*--02:带有比较运算符的子查询*/
select Sno,Sname,Sdept from Student where Sdept=( select Sdept from Student where Sname='ahui');

7:集合查询

每一个select语句都能获得一个或一组元组,若要把多个select语句的结果合并为一个结果,可用集合操作来完成。集合操作主要包括并操作(UNION)交操作(INTERSECT),差操作(MINUS).

使用UNION将多个查询结果合并起来,形成一个完整的查询结果时,系统会自动去掉重复的元组;

注意的是参加UNION操作的各数据项数目必须相同,对应项的数据类型也要必须相同。

/*集合查询*/
/*查询计算机系的学生及年龄不大于19学生----是个并集*/
select * from Student where Sdept='CS' UNION select * from Student where Sage<=19; select Sno from SC where Cno='1' UNION select Sno from SC where Cno='2';

8:数据更新

 /*插入数据*/
insert into Student values('05','aaa','男','25','3','2015-11-26','3'); insert into SC(Sno,Cno) values('06','3'); insert into Deptage(Sdept,Avgage) select Sdept,AVG(Sage) from Student GROUP BY Sdept;
/*修改数据*/ update Student set Sage=233 where Sno='01'; update Student Set Sage=Sage+1; update SC set Grade=1 where '1'=( select Sdept from Student where Student.Sno=SC.Sno); /*删除数据*/
delete from Student where Sno='01';

常用的 SQL语句------CRUD的更多相关文章

  1. MySQL 常用的sql语句小结(待续)

    mysql 常用的sql语句 1.查看数据库各个表中的记录数 USE information_schema; SELECT table_name,table_rows FROM tables WHER ...

  2. 经典SQL语句大全以及50个常用的sql语句

    经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...

  3. SQL Server中常用的SQL语句(转):

    SQL Server中常用的SQL语句 转自:http://www.cnblogs.com/rainman/archive/2013/05/04/3060428.html 1.概述 名词 笛卡尔积.主 ...

  4. oracle常用的SQL语句

    一些常用的SQL语句: --建表 create table adolph (id number(10,0),              name varchar2(20),              ...

  5. SQL之50个常用的SQL语句

    50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,T ...

  6. MySQL的一些常用的SQL语句整理

    安装MySQL有两种的方式,一种是解压版本,但是需要配置环境变量,相对而言比较麻烦.所以我们一般采取第二种方式,那就是到MySQL的官网上下载安装版.这样就会省去很多麻烦,在这里我就不再详细的介绍具体 ...

  7. 50个常用的sql语句

    50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,T ...

  8. 测试常用的sql语句总结

    测试中常用的sql语句,排名部分先后 1. 查询 SELECT * FROM 表名称 SELECT COUNT(DISTINCT column_name) FROM table_name 指定列的不同 ...

  9. 常用经典SQL语句大全完整版--详解+实例 (存)

    常用经典SQL语句大全完整版--详解+实例 转 傻豆儿的博客 http://blog.sina.com.cn/shadou2012  http://blog.sina.com.cn/s/blog_84 ...

随机推荐

  1. Visual Studio 2012 Visual C++ 入门

    改进的C++11标准的支持 标准模板库 为STL添加了新的库文件:<atomic>.<chrono>.<condition_variable>.<filesy ...

  2. 博客的QQ群群规

    本人博客园博客夏天的森林相关的QQ群一共有3个,它们分别是Web前端及Web开发技术群(群号:262413025).JS及Web前端技术群(群号:35079861)和Web服务端技术群(群号:3414 ...

  3. i++ and ++i efficiency

    其实这个问题,百度的话,有一大堆的参考资料,但是,在这里,我产生了一些困惑,他们所分析的结果,和我的测试代码不一致,这让我纠结了,所以,再次的写一下这个问题,顺顺思路. 我的测试环境:系统:Windo ...

  4. RHCE 系列(九):如何使用无客户端配置 Postfix

    转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0220/152.html?1456382561 尽管现在有很多在线联系方式,电子邮件仍然是一个人传递 ...

  5. Android 使用Font Awesome 显示文字图标

    Android 使用Font Awesome 显示文字图标 简单几步就可以完成 简单的效果图: 1. 创建 assets 文件夹 在Android Studio 上的创建步骤为: 在 src/main ...

  6. fir.im Weekly - 你与优秀源码之间只差一个 Star

    说起开源社区,Github 是一个不可缺少的存在.作为全球最大的同性交友网站,上面有太多优秀的开源代码库和编程大神,让无数开发者心生向往.那么如何正确的使用 Github,也许是编程学习之必要.来看下 ...

  7. css 文本溢出显示省略号

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. WPF入门教程系列十三——依赖属性(三)

    四. 只读依赖属性 在以前在对于非WPF的功能来说,对于类的属性的封装中,经常会对那些希望暴露给外界只读操作的字段封装成只读属性,同样在WPF中也提供了只读属性的概念,如一些 WPF控件的依赖属性是只 ...

  9. ServletConfig接口默认是哪里实现的?

    问题:Servlet接口默认是哪里实现的? 答:GenericServlet 1.结构 2.ServletConfig.GenericServlet.HttpServlet的关系如下: public ...

  10. CCNA网络工程师学习进程(6)vlan相关协议的配置与路由器简单配置介绍

        前面已经介绍了大部分与vlan技术相关的交换机的协议的配置,更深层次的还有STP协议和以太网端口聚合技术,接着还会简单介绍一下路由器的基本应用.     (1)STP(Spanning-tre ...