数据库 SQL 2005 知识点汇总
2015-10-03 13:09
363 查看
数据库基础
数据库含义 是数据和数据库的集合,是指以一定的组织形式存放在计算机存储介质上的相互关联的数据的集合。
DBS 包括 Data DB DBA (数据库管理员) DBMS(数据管理系统)
SQL 分类
数据库定义语言 DDL create alter drop
数据库操作语言DML insert update delete select
数据控制语言DCL grant 赋予 revoke 撤销
数据库特点 冗余度小
经历了三个阶段 网状数据库 层次数据库 关系数据库 (柯德)
实体联系有三种 一对一 一对多 多对多
关系 就是一张二维表 行代表元组 记录 ,列代表属性 记录
关系运算有三种形式 ,选择 (从行的角度),投影(从列的角度),连接(从行和列的角度)
关系的完整性约束 实体完整性(主键) ,参照完整性(外键), 用户自定义完整性
关系模型规范为三个范式,第一范式 即每个属性都是不可再分的,
第二范式 关系R满足第一范式 , 每一个非主属性完全函数依赖于主键,每行必须可以被唯一区分 都设立主键
第三范式 每一个非主属性既不部分函数依赖于主键,也不传递函数依赖于主键。
数据库在物理磁盘上是以文件为单位存储的,有主数据文件(。mdf) 事务日志文件(。ldf)
创建数据库方式
create database 数据名字
on
(name=' ',
filename=' \data_name.mdf',
size=,
maxsize= ,
filegrowth= ,)
log on
(name=' database_log',
filename=' \database_log.ldf',
size= ,
maxsize= ,
filegrowth= ,)
删除数据库
drop database 数据库名字
增加数据文件初始容量
alter database 数据库名
modify file
( name=数据库文件名,
size= 新初始值,
或filegrowth=新值 )
修改数据库名字
exec 原数据库名, 新数据库名
数据类型
数字的数据类型 bigint . int .smallint.
浮点数据类型 float . real
固定精度数据类型 decimal numeric (8,3) 含义8代表总共的位数 ,3代表小数点后面的位数
货币数据类型 money smallmoney
字符数据类型 varchar存放动态长度的字符 char 存放静态长度的字符 text nchar
日期数据类型 datetime
创建表的语法
create table 表名
{ 列名 数据类型 约束条件,
列名 数据类型 约束条件,
..............
}注意 表名和列明不用是SQL 中的关键字
例如 create table people
{
p_name varchar (10) not null primary key,
p_id int
}
创建外键表
use school
create table sc
( sno varchar(20) not null foreign key references student(sno),
cno varchar (50) not null foreign key references course(cno),
degree int )
修改表
添加列
alter table 表名
add 列名 数据类型 ()
修改列的属性
use school
alter table 表名
alter column class_no char(8)
约束条件分类 primary key 主键对应实体约束, foreign key 外键对应参照约束 ,unique 唯一约束 check检查约束 default 默认约束 三个对应用户自定义约束
增加完整性约束定义
alter table 表名
add
constraint 约束名
删除表中的约束
use school
alter table school
drop colum class_no char(8)
删除列
alter table 表名
drop colum 列名
删除表
drop table 表名
添加数据
--为表插入数据的命令
insert 表名 values (' 张三',123)
--查询表的内容 select *from peple
单表查询分为
投影从列的角度
选择 从行的角度
连接 从行和列
创建虚表
select 列名
into 新表名 from 原表
多表查询
嵌套查询,连接查询。
1、select 列名1,列名2,列名3, from 表名
2、select sno as ‘学号’,sname as '姓名‘ from sc
3、select top 4* from sc
4、use school select sno,degree
from sc
where cno='3-105'
order by degree desc(降序)
5、select *
from student
order by class ,sbirthday
6、比较搜索条件
select sno,sname
from student
where sbirthday >'1975-01-01'
7、范围搜索条件
select *
from sc
where degree between 60 and 80 (包括左右两个边界)
8、列表搜索条件
select *
from sc
where degree in (86,89,89)
9、搜索条件中的模式匹配
select *
from student
where sno like " "
通配符 含义
% 包含零个或是多个字符的任意字符 例如 王% 王在前的任意字符,%王% ,王的任意位置
涉及空值得查询
使用谓词 is null 或者is not null
多重条件查询,
select * from sc where degree >=60 and degreee<=80
10、 聚合函数
select count (*)
from student
select count (distinct sno )
from sc
select avg(degree)
from sc
where cno='3-105'
11、数据分组
select cno ,cout(*) as '选课人数'
from sc
group sc
select sno
from sc
group by sno
having cout(*)>=2
查询选修了2门和2门以上课程都至少为85分的学生学号以及课程书
select sno ,count(*)
from sc
where degree>=85
group by sno
having count(*)>=2
多表查询的子查询 分为嵌套查询和连接查询
select sno,sname
from student
where sno in (select sno
from sc where cno='3-105')
带有in谓词的子查询 in返回的是一个集合
查询与‘王芳’在同一班级学习的学生详细信息
select*
from student
where class in (select class from student where sname=' 王芳’)
查询选修课名为‘操作系统’的学生学号和姓名
select student .sno ,sname
from student
where sno in (select sno from sc where cno in (select cno from course where canme='操作系统‘)) 个人建议用三表连接会简单
带有exists的子查询
只产生逻辑真值true或逻辑假值false,若内层查询结果为非空,则返回真值,若内层查询结果为空,则返回假值
查询所有选修了3-245 课程的学生姓名
select sname
from student
where exists (select * from sc where sc.sno=student.sno and cno='3-245')
表内连接 两种写法
select student *,sc.*
from student,sc
where student.sno=sc.sno
select student*,sc.*
from student join sc on student.sno=sc.sno
表自身连接
select x.*,y.* from sc x,sc.y
where x.cno='3-105' and y.cno='3-105'
and y.sno='109' and x.degree>y.degree
order by x.degree desc
表外连接
左连接:将左表的所有记录分别与右表的每一条记录进行连接组合,结果集中除显示符合条件的数据行以外,还显示左边表中不符合条件的数据行,此时右边数据行会以null来显示。select T1.*,T2.* from T1 left outer join T2 on T1.sno =T2.sno
右连接:将右表的所有记录分别与左表的每一条记录进行连接组合,结果集中除显示符合条件的数据行以外,还显示右边表中不符合条件的数据行,此时左边数据行会以null来显示。 select T1.*, T2.* from T1 right outer join T2 on T1.sno=T2.sno
全外连接 select T1.*,T2.* from T1 full join T2 on T1.sno=T2.sno
复合条件连接 多个表
查询选修3-105 号课程且成绩在90 分以上的所有学生的学号,姓名
select student sno,student.sname
from student sc
where student .sno=sc.sno and sc.cno='3-105' and sc.degree>90
方法二 select student.sno ,student.sname
from student join sc on student.sno=sc.sno and sc.cno='3-105'and sc.degree>90
多表连接
select student.sno ,sname ,cname,degree
from student ,sc, course
where student,sno=sc,sno and sc.cno=course.cno
select student.sno ,sname ,cname ,degree
from student join sc on sutdent.sno=sc.sno join course on sc.cno=course.cno
from A join B on 连接条件1 join C on 连接条件2
使用update 修改数据
update student
set sname='新名’
where sno='101'
update sc
set degree=degree+5
删除表 delete student from student where sname=' 王二小‘
数据库含义 是数据和数据库的集合,是指以一定的组织形式存放在计算机存储介质上的相互关联的数据的集合。
DBS 包括 Data DB DBA (数据库管理员) DBMS(数据管理系统)
SQL 分类
数据库定义语言 DDL create alter drop
数据库操作语言DML insert update delete select
数据控制语言DCL grant 赋予 revoke 撤销
数据库特点 冗余度小
经历了三个阶段 网状数据库 层次数据库 关系数据库 (柯德)
实体联系有三种 一对一 一对多 多对多
关系 就是一张二维表 行代表元组 记录 ,列代表属性 记录
关系运算有三种形式 ,选择 (从行的角度),投影(从列的角度),连接(从行和列的角度)
关系的完整性约束 实体完整性(主键) ,参照完整性(外键), 用户自定义完整性
关系模型规范为三个范式,第一范式 即每个属性都是不可再分的,
第二范式 关系R满足第一范式 , 每一个非主属性完全函数依赖于主键,每行必须可以被唯一区分 都设立主键
第三范式 每一个非主属性既不部分函数依赖于主键,也不传递函数依赖于主键。
数据库在物理磁盘上是以文件为单位存储的,有主数据文件(。mdf) 事务日志文件(。ldf)
创建数据库方式
create database 数据名字
on
(name=' ',
filename=' \data_name.mdf',
size=,
maxsize= ,
filegrowth= ,)
log on
(name=' database_log',
filename=' \database_log.ldf',
size= ,
maxsize= ,
filegrowth= ,)
删除数据库
drop database 数据库名字
增加数据文件初始容量
alter database 数据库名
modify file
( name=数据库文件名,
size= 新初始值,
或filegrowth=新值 )
修改数据库名字
exec 原数据库名, 新数据库名
数据类型
数字的数据类型 bigint . int .smallint.
浮点数据类型 float . real
固定精度数据类型 decimal numeric (8,3) 含义8代表总共的位数 ,3代表小数点后面的位数
货币数据类型 money smallmoney
字符数据类型 varchar存放动态长度的字符 char 存放静态长度的字符 text nchar
日期数据类型 datetime
创建表的语法
create table 表名
{ 列名 数据类型 约束条件,
列名 数据类型 约束条件,
..............
}注意 表名和列明不用是SQL 中的关键字
例如 create table people
{
p_name varchar (10) not null primary key,
p_id int
}
创建外键表
use school
create table sc
( sno varchar(20) not null foreign key references student(sno),
cno varchar (50) not null foreign key references course(cno),
degree int )
修改表
添加列
alter table 表名
add 列名 数据类型 ()
修改列的属性
use school
alter table 表名
alter column class_no char(8)
约束条件分类 primary key 主键对应实体约束, foreign key 外键对应参照约束 ,unique 唯一约束 check检查约束 default 默认约束 三个对应用户自定义约束
增加完整性约束定义
alter table 表名
add
constraint 约束名
删除表中的约束
use school
alter table school
drop colum class_no char(8)
删除列
alter table 表名
drop colum 列名
删除表
drop table 表名
添加数据
--为表插入数据的命令
insert 表名 values (' 张三',123)
--查询表的内容 select *from peple
单表查询分为
投影从列的角度
选择 从行的角度
连接 从行和列
创建虚表
select 列名
into 新表名 from 原表
多表查询
嵌套查询,连接查询。
1、select 列名1,列名2,列名3, from 表名
2、select sno as ‘学号’,sname as '姓名‘ from sc
3、select top 4* from sc
4、use school select sno,degree
from sc
where cno='3-105'
order by degree desc(降序)
5、select *
from student
order by class ,sbirthday
6、比较搜索条件
select sno,sname
from student
where sbirthday >'1975-01-01'
7、范围搜索条件
select *
from sc
where degree between 60 and 80 (包括左右两个边界)
8、列表搜索条件
select *
from sc
where degree in (86,89,89)
9、搜索条件中的模式匹配
select *
from student
where sno like " "
通配符 含义
% 包含零个或是多个字符的任意字符 例如 王% 王在前的任意字符,%王% ,王的任意位置
涉及空值得查询
使用谓词 is null 或者is not null
多重条件查询,
select * from sc where degree >=60 and degreee<=80
10、 聚合函数
select count (*)
from student
select count (distinct sno )
from sc
select avg(degree)
from sc
where cno='3-105'
11、数据分组
select cno ,cout(*) as '选课人数'
from sc
group sc
select sno
from sc
group by sno
having cout(*)>=2
查询选修了2门和2门以上课程都至少为85分的学生学号以及课程书
select sno ,count(*)
from sc
where degree>=85
group by sno
having count(*)>=2
多表查询的子查询 分为嵌套查询和连接查询
select sno,sname
from student
where sno in (select sno
from sc where cno='3-105')
带有in谓词的子查询 in返回的是一个集合
查询与‘王芳’在同一班级学习的学生详细信息
select*
from student
where class in (select class from student where sname=' 王芳’)
查询选修课名为‘操作系统’的学生学号和姓名
select student .sno ,sname
from student
where sno in (select sno from sc where cno in (select cno from course where canme='操作系统‘)) 个人建议用三表连接会简单
带有exists的子查询
只产生逻辑真值true或逻辑假值false,若内层查询结果为非空,则返回真值,若内层查询结果为空,则返回假值
查询所有选修了3-245 课程的学生姓名
select sname
from student
where exists (select * from sc where sc.sno=student.sno and cno='3-245')
表内连接 两种写法
select student *,sc.*
from student,sc
where student.sno=sc.sno
select student*,sc.*
from student join sc on student.sno=sc.sno
表自身连接
select x.*,y.* from sc x,sc.y
where x.cno='3-105' and y.cno='3-105'
and y.sno='109' and x.degree>y.degree
order by x.degree desc
表外连接
左连接:将左表的所有记录分别与右表的每一条记录进行连接组合,结果集中除显示符合条件的数据行以外,还显示左边表中不符合条件的数据行,此时右边数据行会以null来显示。select T1.*,T2.* from T1 left outer join T2 on T1.sno =T2.sno
右连接:将右表的所有记录分别与左表的每一条记录进行连接组合,结果集中除显示符合条件的数据行以外,还显示右边表中不符合条件的数据行,此时左边数据行会以null来显示。 select T1.*, T2.* from T1 right outer join T2 on T1.sno=T2.sno
全外连接 select T1.*,T2.* from T1 full join T2 on T1.sno=T2.sno
复合条件连接 多个表
查询选修3-105 号课程且成绩在90 分以上的所有学生的学号,姓名
select student sno,student.sname
from student sc
where student .sno=sc.sno and sc.cno='3-105' and sc.degree>90
方法二 select student.sno ,student.sname
from student join sc on student.sno=sc.sno and sc.cno='3-105'and sc.degree>90
多表连接
select student.sno ,sname ,cname,degree
from student ,sc, course
where student,sno=sc,sno and sc.cno=course.cno
select student.sno ,sname ,cname ,degree
from student join sc on sutdent.sno=sc.sno join course on sc.cno=course.cno
from A join B on 连接条件1 join C on 连接条件2
使用update 修改数据
update student
set sname='新名’
where sno='101'
update sc
set degree=degree+5
删除表 delete student from student where sname=' 王二小‘
相关文章推荐
- HPUX在oracle10g安装和卸载缩写
- oracle start
- Ubuntu 15.04 安装 MongoDB 3.x
- SQL查询分析器使用
- oracle2
- MySQL事务隔离级别
- web.config connectionStrings 数据库连接字符串的解释
- postgresql 触发器
- mysql编码修改
- My SQL的内连接,外链接查询
- OSSIM4主要数据库及表分布
- OSSIM4主要数据库及表分布
- 怎么会Sql serverW数据库模型图转化成ord于--您还可以查看属性信息字段
- 06数据库设计02
- 如何使用MySQL——函数
- MySQL的初级问题——如何使用MySQL的客户端程序、使用程序
- MySql存储过程 MySql存储过程详解
- MySql 基本操作
- mysql忘记root密码
- 【自考】数据库系统原理