数据库语言(一):SQL语法实例整理
数据库系统以一些语句作为输入,并返回一些输出,例如sql查询总是返回一张表,我们定义:具有相同格式的记录的集合是一张表。
考虑大学数据库系统:
SQL中的数据类型:
char(n) 字符串长度为n 等价于 character
varchar(n) 可变字符串 最大长度n,等价于 character varing, 推荐都使用这种类型
int 整数 等价于 integer
smallint 小整数
numeric(p,d) 定点数,加上符号为共有p位数,其中d位数字在小数点右边,d表示精度
real 浮点数
double real 双精度浮点数
float(n) 浮点数,至少精度为n (float不精确,不能用于精确计算)
decimal(m,n) 表示数字。不仅仅是my sql,所有与精确数字有关的都不应该用float ,如价格、金额等
创建表:
create table department
(dept_name varchar(20) not null,
building varchar(15),
budget numeric(12,2),
primary key(dept_name, building));
create table course
(course_id varchar(7) not null,
title varchar(50),
dept_name varchar(20),
primary key (course_id),
foreign key(dept_name) references department);
实际上,我们创建了一个关系模式,也创建了一个不包含记录的关系实例。
tips: 可以对一个属性添加not null约束,表示属性不能为空值。
create table course2 like course;
根据已有表创建新的空表,
create table course3 as
(select *
from course)
with data;
根据已有表创建新的表,并载入数据。
检索表:
select instrutor.*, department.dept_name
from instructor, department
where instructor.dept_name = department.dept_name and
department.budget > 95000
order by instructor.salary desc, instructor.name asc;
我们知道笛卡尔积是这样一个函数(运算),接受多个集合作为参数,返回一个新的元组的集合,新集合的每个元组元素满足:元组中的各个元素分别属于各个参数集合。一张表是记录的集合,当然约束条件是每个记录都必须是相同结构的,多个记录的集合构成笛卡尔积,这个笛卡尔积也是一张表,从笛卡尔积中选择合适的元组组成一张表,最后从这张表中选择两个属性组成新的表,返回。语句执行顺序是 from -> where and -> group by -> select。
tips:where 子句可以用 and or not 连接谓词,也可以使用 between and 谓词
tips:desc 表示降序,asc表示如果salary相同按照name升序
tips:关系是记录的集合,所以不应该有重复项,但是SQL查询语句对应的返回值(也就是一张新的表)中默认有重复项,可以检查重复并且去除重复,但是这是很费时的。
select distinct dept_name
from instructor;
这表示返回的表中不能有重复
select all dept_name
from instructor;
显式地指明允许重复。
插入记录:
insert into instructor
values(10211,'Smith','Biology',66000);
值被给出的顺序与关系模式中的属性顺序相同。
insert into student
(select *
from student_old);
更新记录:
update instructor
set salary = salary * 1.05
where salary < 7000;
删除记录:
delete from student;
这删除了全部的记录。
delete from student
where age < 20;
删除表:
drop table student;
改变属性:
alter table r
add A D;
这是增添属性A,其中r是关系(表),A是属性名,D是属性的域。
alter table r
drop A;
这是删除属性A。
自然连接:
select name, course_id
from instructor natural join teaches natural join department;
连接(仅仅按照指定属性名称来匹配出记录元组):
select name, title
from (instructor natural join teaches) join course using (course_id);
别名:
select T.name as t_name
frome instructor as T
where T.salary > 8000;
as关键字用于创建一个新的引用,作为原来属性的别名。
模式匹配:
select dept_name
from department
where building like '_Waston\%_a%' escape '\' and building not like 'abc%';
tips: SQL中用单引号引用字符串,用双单引号''表示字符串内的双引号。可以用 escape定义转义字符。
tips: SQL1999中的 similar to 和like一样是模式匹配,语法类似unix中的正则表达式。
集合运算:
(select course_id from section
where year = 2010)
union all
(select course_id from section
where year = 2014);
all的含义是保留重复,否则默认删除重复记录,union, intersect, except用法相同。
基本聚集函数:
select avg(salary) as avg_salary
from instructor
where dept_name = 'Computer Science';
计算平均值,可以用distinct修饰一个属性表示去除重复
select count(distinct ID)
from teaches
where year = 2010;
计算年份在2010年的教学课程数目,可以计算总数
select count(*) from teaches;
分组聚集:
select dept_name, avg(salary) as avg_salary
from instructor
where age > 20
group by dept_name
having avg(salary) > 7000;
注意到,由group by 定义了多个分组,只能在分组内调用聚集函数,将一个集合映射称为一个值。
having关键字和where关键字后都跟一个谓词子句,然而区别是,where后的谓词作用于一个记录,having后的谓词作用于一个分组。也就是说,having后的子句必然包含有聚集函数。如果没有group by子句,则表示只有一个分组。
嵌套查询:
连接词 in 和 not in 可以检查元组(记录)是不是集合(表)中的成员。
select distinct course_id
from section
where year = 2010 and
course_id in (select course_id
from section
where semester = 'Fall');
量词:some / all / exists
设集合为A,some(A)表示存在某个元素,all(A)表示任意一个元素。在用法上,可以把两者的返回值视为某条记录。
select name
from instructor
where salary > some(select salary
from instructor
where dept_name = 'Biology');
用all找出工资最高的系:
select dept_name
from instrutor
group by dept_name
having avg(salary) >= all(select avg(salary)
from instructor
group by dept_name);
exists(A)量词也表示存在,判定集合是否不为空集,返回布尔值。 not exists(A) 则判断A是否为空集。
如果不用联合查询,希望找到在不同学期同时开课的课程:
select course_id
from section as T
where year = 2011 and
exists(select *
from section as S
where T.course_id = S.course_id and year = 2009);
select ID
from student as T
where exists(select *
from takes as S
where T.ID = S.student_id and
S.course_id in (select course_id
from course
where dept_name = 'Biology');
max函数:
select dept_name, max(sum_salary)
from (select dept_name, sum(salary),
from instructor
group by dept_name) as dept_total(dept_name, sum_salary);
with子句:
它用于定义起到支持作用的关系模式。with 关系模式 as 关系实例,类似于程序语言中定义函数,用with子句避免了大量嵌套,使得SQL更有条理。注意它在定义的时候不执行
with sum_dept_salary(dept_name, sum_salary) as
(select dept_name, sum(salary)
from instructor
group by dept_name),
avg_sum_dept_salary(salary) as
(select avg(sum_salary)
from sum_dept_name)
select dept_name
from sum_dept_salary, avg_sum_dept_salary
where sum_dept_salary.sum_salary > avg_sum_dept_salary.salary;
选择结构case:
case
when pred1 then result1
when pred2 then result2
else result0
end
从case到end之间的部分可以作为一个整体子句,返回一个特定的值,可以嵌套在SQL中。
数据库语言(一):SQL语法实例整理的更多相关文章
- 数据库语言(二):SQL语法实例整理
连接表达式: select * from student join takes on student.ID = takes.ID; 通过on后面的谓词作为连接条件,相同的属性可以出现两次,也就是等价于 ...
- 网络安全从入门到精通 (第二章-2) 后端基础SQL—MySQL数据库简介及SQL语法
本文内容: 什么是数据库 常见数据库 数据库的基本知识 基本SQL语法 1,什么是数据库? 数据库就是将大量数据保存起来,通过计算机加工,可以高效访问的数据聚合. 数据库就是长期存储在计算机内,有组织 ...
- SQL语法粗整理
1.在同一张表中,对前一条数据进行更新性插入操作,即:
- PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
1. 首先要得到该数据库中有哪些表,所用函数 mysql_list_tables(),然后可以将获取的所有表名存到一个数组.----------------该函数由于被弃用 用show table ...
- access 数据库创建表SQL语法
create table R_CAIFA_B13 ( ID AUTOINCREMENT PRIMARY KEY, XB varchar(255), C1 varchar(50), C2 varchar ...
- 整理的一些数据库不容易想到的SQL语句实例一
1.行转列SQL语句 SELECT * FROM ( SELECT [FID] , [Weeks] , [Qty] FROM dbo.TempTable where Weeks is not null ...
- 数据库SQL语法到MySQL实操
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname 3.说明:备份sql server--- 创建 ...
- NoSQL 数据库概览及其与 SQL 语法的比较
NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用的难题. 本文对NoSQL数据库的定义.分类.特征.当前比较流行的NoSQL数据库系统等进行了简单的介绍,并对N ...
- Oracle数据库常用的Sql语句整理
Oracle数据库常用的Sql语句整理 查看当前用户的缺省表空间 : select username,default_tablespace from user_users; 2.查看用户下所有的表 : ...
随机推荐
- SqlBulkCopy 简单运用
using(SqlConnection conn = new SqlConnection(str)) { conn.Open(); using (System.Data.SqlClient.SqlBu ...
- nodeJS实战
github代码托管地址: https://github.com/Iwillknow/microblog.git 根据<NodeJS开发指南>实例进行实战{{%并且希望一步步自己能够逐步将 ...
- 关闭VS时, 每次都 会弹出 保存以下各项的更改吗?
如果是添加项目, 或修改了解决方案的配置, 会弹出这个是正常的. 如果在没有修改的情况下还这样就是有问题的. 原因: 在Vs中安装了PowerDesigner插件, 并在VS中点了该插件的东西 ...
- win8 任务栏不合并隐藏标题
让win8任务栏不合并,并且隐藏标题的办法: 效果如下: 首先让win8不合并任务栏 1.任务栏上点鼠标右键 -- "属性" 2."任务栏按钮"选择" ...
- PE文件结构详解(三)PE导出表
上篇文章 PE文件结构详解(二)可执行文件头 的结尾出现了一个大数组,这个数组中的每一项都是一个特定的结构,通过函数获取数组中的项可以用RtlImageDirectoryEntryToData函数,D ...
- Java中super的用法并与this的区别(转载)
一.子类中如果需要调用父类中的构造函数,则需要使用super(),且必须在构造函数中的第一行 public class Demo1 { public static void main(String[] ...
- linux上部署应用
1.编写traffic.sh 引入相关的jar包及java环境路径 2.crontab -e 加入: */10 * * * * cd /opt/sys/traffic_version/bin & ...
- NET 使用HtmlAgilityPack抓取网页数据
刚刚学习了XPath路径表达式,主要是对XML文档中的节点进行搜索,通过XPath表达式可以对XML文档中的节点位置进行快速定位和访问,html也是也是一种类似于xml的标记语言,但是语法没有那么严谨 ...
- Java注解全面解析
1.基本语法 注解定义看起来很像接口的定义.事实上,与其他任何接口一样,注解也将会编译成class文件. @Target(ElementType.Method) @Retention(Retentio ...
- sudo: /etc/sudoers 的模式为 0551,应为 0440
环境:Ubuntu 12.04.4 LTS 32bit 本想修改/etc/sudoers文件,取消sudo权限的密码.但是因为sudoers文件无‘w’(写)的权限,然后用命令加写权限的时候加错了,加 ...