复习之前课本上的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. Backbone源码解析(五):Route和History(路由)模块

    今天是四月十二号,距离上次写博已经将近二十天了.一直忙于工作,回家被看书的时间占用了.连续两个礼拜被频繁的足球篮球以及各种体育运动弄的精疲力竭,所以很少抽时间来写技术博客.今天抽出时间把backbon ...

  2. web.xml详解

    web.xml的作用 web.xml,一个Tomcat工程中最重要的配置文件.web.xml没有其实也可以----只要你确定你的项目里面不需要任何过滤器.监听器.Servlet等等.我试了一下,没有w ...

  3. Word文档合并的一种实现

    今天遇到一个问题,就是需要把多个Word文档的内容追加到一个目标Word文档的后面,如果我有目标文档a.doc以及其他很多个文档b.doc,c.doc…等等数量很多.这个问题,如果是在服务端的话,直接 ...

  4. common-dbcp2数据库连接池参数说明

    参数 默认值 描述 建议值 DefaultAutoCommit  null 通过这个池创建连接的默认自动提交状态.如果不设置,则setAutoCommit 方法将不被调用.  true Default ...

  5. 上学时的HTML+JS+CSS(小总结)

    html:超文本标记语言 基本标签: { 文本标签:<pre></pre>:原封不动的保留空白区域.      <br />:换行.      <hr wid ...

  6. MySQL 5.7新特性之Generated Column(函数索引)

    MySQL 5.7引入了Generated Column,这篇文章简单地介绍了Generated Column的使用方法和注意事项,为读者了解MySQL 5.7提供一个快速的.完整的教程.这篇文章围绕 ...

  7. 我心中的核心组件~HttpHandler和HttpModule实现图像的缩放与Url的重写

    回到目录 说在前 对于资源列表页来说,我们经常会把图像做成N多种,大图,小图,中图等等,很是麻烦,在数据迁移时,更是一种痛快,而如果你把图像资源部署到nginx上,那么这种图像缩放就变得很容易了,因为 ...

  8. 说说设计模式~组合模式(Composite)

    返回目录 何时能用到它? 组合模式又叫部分-整体模式,在树型结构中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦.对于今天这个 ...

  9. js里cookie操作

    原生js操作cookie 创建和存储 cookie 在这个例子中我们要创建一个存储访问者名字的 cookie.当访问者首次访问网站时,他们会被要求填写姓名.名字会存储于 cookie 中.当访问者再次 ...

  10. from表单iframe原网页嵌入

    今天是巩固的from表单跟嵌入其他页面,同样的,学习到了新的知识. 温故而知新: iframe--在原页面嵌入其他页面,以窗口的样式 其中scrolling--滚动条 noresize--可调整大小 ...