标题:sql编程


数据库编程主要包括数据库设计,数据库连接,数据库查询与操作。完成这些任务的主要工具为SQL语言。笔者在面试某知名互联网公司电子商务职位时,就遇到过设计数据库表并用SQL语句查询数据库表的题目。因此,SQL语言是数据库编程的基础。概括起来,SQL语言可分为如下三类:

    DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)

    DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)

    DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)

其中SQL四条最基本的数据操作语句:Insert,Select,Update和Delete,这四条语句是数据库查询中使用最频繁的四条语句,因此应该牢固掌握。

首先,将SQL语言的基础语句总结如下:

1.创建数据库

CREATE DATABASE dbname

2.删除数据库

drop database dbname

3.创建新表

create table tbname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根据已有的表创建新表:

A:create table table_new like table_old (使用旧表创建新表)

B:create table table_new as select col1,col2… from table_old definition only

4.删除新表:drop table tablename

5.增加一个列:alter table tablename add column col type

6.添加主键:alter table tablename add primary key(col)

删除主键:alter table tablename drop primary key(col)

7.创建索引:create [unique] index idxname on tablename (col1, ….)

删除索引:drop index idxname

8.创建视图:create view viewname as select statement

删除视图:drop view viewname

9.基本sql语句:

选择:select * from table1 where 范围

插入:insert into table1(field1,field2) values(value1,value2)

删除:delete from table1 where 范围

更新:update table1 set field1=value1 where 范围

查找:select * from table1 where field1 like ‘%value%’

排序:select * from table1 order by field1,field2 [desc]

总数:select count * as totalcount from table1 [group by fieldx]

求和:select sum(field1) as sumvalue from table1 [group by fieldx]

平均:select avg(field1) as avgvalue from table1 [group by fieldx]

最大:select max(field1) as maxvalue from table1 [group by fieldx]

最小:select min(field1) as minvalue from table1 [group by fieldx]

10.高级查询运算词

Union 运算符

Union 运算符通过组合其他两个结果表(例如 table1 和 table2)并消去表中任何重复行而派生出一个结果表。当 all随 union 一起使用时(即 union all),不消除重复行。两种情况下,派生表的每一行不是来自 table1 就是来自 table2。

Except 运算符

Except 运算符通过包括所有在 table1 中但不在 table2 中的行并消除所有重复行而派生出一个结果表。当 all 随 except 一起使用时 (except all),不消除重复行。

Intersect 运算符

Intersect运算符通过只包括 table1 和 table2 中都有的行并消除所有重复行而派生出一个结果表。当 all随 intersect 一起使用时 (intersect all),不消除重复行。

11.使用外连接

 左外连接(左连接):结果集既包括连接表的匹配行,也包括左连接表的所有行。

SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。

全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。

12.子查询(表名1:a 表名2:b)

select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)

13.外连接查询(表名1:a 表名2:b)

select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

14.在线视图查询(表名1:a )

select * from (SELECT a,b,c FROM a) T where t.a > 1;

15.between的用法,between限制查询数据范围时包括了边界值,not between不包括

select * from table1 where time between time1 and time2

select a,b,c, from table1 where a not between 数值1 and 数值2

16.in的使用方法

select * from table1 where a [not] in (‘val1’, ’val2’, ’val4’, ’val6’)

17.两张关联表,删除主表中已经在副表中没有的信息

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

18.四表联查问题:

select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

19.日程安排提前15分钟提醒

select * from 日程安排 where datediff('minute',f开始时间,getdate())>15

20.前10条记录

select top 10 * form table1 where 范围

21.随机取出10条数据

select top 10 * from tablename order by newid()

22.随机选择记录

select newid()

23.删除重复记录

delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

 

数据库的范式:

 

范式:英文名称是 Normal Form,它是英国人 E.F.Codd(关系数据库的老祖宗)在上个世纪70年代提出关系数据库模型后总结出来的,范式是关系数据库理论的基础。通常所用到的是三个范式,即:第一范式(1NF),第二范式(2NF),第三范式(3NF)。

 

第二范式(2NF):首先是 1NF,另外包含两部分内容,一是表必须有一个主键;二是没有包含在主键中的列必须完全依赖于主键,而不能只依赖于主键的一部分。 

 

第三范式(3NF):首先是 2NF,另外非主键列必须直接依赖于主键,不能存在传递依赖。即不能存在:非主键列 A 依赖于非主键列 B,非主键列 B 依赖于主键的情况。 

 

关于范式更多的说明,请参考:链接1,链接2

 

思考题1:

人员情况表(employee)中字段包括:员工号(id),姓名(name),年龄(age),文化程度(wh):包括四种情况(本科以上,大专,高中,初中以下),现在我根据年龄字段统计出:表中文化程度为本科以上,大专,高中,初中以下,各有多少人,占总人数多少,结果如下:

 

学历                   年龄     人数     占百分比

本科以上              20          34          14

大专                     20          33          13

高中                     20          33          13

初中以下              20          100        40

本科以上              21          50          20

 

请问SQL查询如何写?

 

思考题2:

一个简单的论坛系统,以数据库储存如下数据:

 

用户名,email,主页,电话,联系地址,发帖标题,发帖内容,回复标题,回复内容。 每天论坛访问量300万左右,更新帖子10万左右。

 

请给出数据库表结构设计,并结合范式简要说明设计思路。

思考题3:

有三个表:

S是学生表,有SID和SNAME两个字段。

T是课程信息表,有TID,TEACHER,TNAME。

ST是成绩表,SID,TID,SCORE。

1:查出同时学习了课程ID为1和课程ID为2的学生ID。

2:查出有两门以上(包括两门)课程没及格的学生ID和姓名,及格分60分。

3:查出所有课程ID为1成绩比课程ID为2好的学生ID。



看文字不过瘾?点击我,进入周哥教IT视频教学
麦洛科菲长期致力于IT安全技术的推广与普及,我们更专业!我们的学员已经广泛就职于BAT360等各大IT互联网公司。详情请参考我们的 业界反馈 ,《周哥教IT.C语言深学活用》视频。

我们的微信公众号,敬请关注
: