现在的位置: 首页 > 综合 > 正文

SQL21天自学通

2012年09月24日 ⁄ 综合 ⁄ 共 6607字 ⁄ 字号 评论关闭

 SQL21自学通
P18
第一周
第一天
1.SQL函数,联合查询及子查询(嵌于查询中的查询)
Structured Query Language(结构化查询语言)
RDBMS(关系型数据库管理系统)
2.非过程性语言,即与具体过程无关.
SQL只描述如何对数据进入检索,插入,删除.
并不说明如何进行这样的操作,如何操作是sql server的事.
P20
1.十二条规则
P23
设计数据库的结构

第二天
P32
1.sql语句大小写不敏感,但查询条件,即数据项是区分大小写的.
2.select 列1 列2 from table --排序
3.select distinct amount from table
4.select 与 select all是等价的.

第三天  表达式,条件语句与运算
P42
1.where name = 'tree' --根据=号来返回值true或false
2.where 工资 > 2000
3.select price price+10 from table
允许创建一个虚拟列或对已有的列进行组合或修改后产生的派生列.
4.select (price+10) newPrice from table
别名,
5.减号的作用,可以取负,也可以减去.
6.空值与空白值区分.
 select * from table where name = null;
7.select state < 'la'
结果st:co//il
8.<>不等于!=
9.where name like '%tree%'
10.where like '张_强'
11.select firstname || lastname 全名 from table
12.select day1 day2 合并后 from table where day1 - day2 > 10
13.or not and
集合
1.union从两个表中查询,并且返回不重复的.
select name from table1
union
select name from table2
2.union all包括重复的.
3.交集intersect
select name from table1
intersect
select name from table2
4.存在于第一个表,不在第二个表(minus)
select name from table1
minus
select name from table2
5.范围in  between
select name from table
where
name in('tree','crane','zhpch');
6.
select * from table
where
age between 20 and 30;
第四天
对数据的进一步处理
P62
1.count,sum,avg,max,min基于列的运算.
2.方差variance(price)
3.日期操作
select add_months(ddate,6) from table
where
add_months(ddate,5) > enddate;
4.last_day(enddate)指定月份的最后一天.
5.闰年
select last_day('2008-01-02') non_leap,last_day('2008-01-02') leap from table
6.
select name months between(startDate,endDate) 之间 from table;
7.nextday,sysdate
8.数学函数,正弦,余弦.
select abs(price) 绝对值 from table;
9.字符操作函数,转换大小写
select upper(name) from table
10.扩充或取消lpad,rpad
select lpad(name,20,'*') from table
11.剪除ltrim,rtrim左剪右剪
ltrim(name,'*')剪除name左边的*
12.replace替换
select replace(name,'e','*') replaceName from table;
13.substr部分输出
select substr(name,2,3) from table;
14.translate(目标字符串,源字符串,目的字符串)
select name,translate(name,123abc,nnnaaa) from table;
这样,数字被替换为n,字母被替换为a
15.满足特定内容的位置instr
select name , instr(name,'e',2,1) from table;
16.length(name)
转换函数
1.数字转字符
select to_char(age) from table;
2.字符转数字to_number
其他函数
1.select user from table;

第五天SQL中的子句
P97
1.where , starting with ,order by ,group by,having
2.starting with与like相似
where name starting with('tr');
3.group by
select name sum(amount) from table group by name
先分组,再汇总。
select name sum(amount),count(payee) from table group by payee,remarks;
4.分组,没有清晰的概念,再研究吧。
5.having
由于用于计算的函数,不能放在where子句中。所以有了having
select team,avg(salary) from table group by team having salary<38000;
having里可以使用运算符,比如逻辑运算。

第六天:表的联合
P121
1.select * from table1,table2
2.如果往表里添加字段,是麻烦的事。所以可以再建一个表,关联起来。
3.select o.orderID,o.Name,p.partNum,p.Description from orders o,part p
4.等值联合
select * from t1,t2 where t1.id=t2.id
5.联合查询如果没有条件限制,则成了笛卡尔积,非常庞大。
6.根据给定的条件返回行数最少的表会作为驱动表--也就是基表。
除基表以外,其它表通常是向基表联合以便更有效地获得数据。
7.不等值联合
where o.num>p.num
8.外部联合与内部联合。
内部:个表内的行与本表内的数据相互进行联合。
没有where语句。
select p.num,p.description,o.name,o.num
from part p join orders o on orders.num=54
外部:表间的联合。
右联合
from part p right outer join orders o on orders.num=54
返回右边表集内的全部记录,左边没有值则补以空值。
用+号
where e.id = ep.id(+) and e.name like '%mith'
ep.id将会全部显示。
内连接,即交叉连接。外连接,相当于并集吧。大概意思。
http://topic.csdn.net/t/20050514/17/4006860.html
9.表的自我联合
select a.num,a.description,b.num,b.description
from tabel1 a,table2 b
where a.num=b.num and a.description <> b.description

第七天:子查询:内嵌的sql子句
P138
1.子查询是把一种查询的结果作为参数,返回给另一个查询的一种查询。
2.exist,any和all
3.
select * from table1 where table1.someNum =
(select someOtherNum from table2
 where someOtherNum = someValue)
4.在子查询中使用汇总函数
select o.name from orders 0 part p
where
o.num=p.num and
o.quantity * p.price >
(select avg(o.quantity*p.price)from
orders o,part p where o.num=p.num)
5.子查询,嵌套
select c.name .. from customer c
where c.name in
(select o.name from orders o,part p
 where o.num=p.num
 and
 o.quantity*p.price>(select..)
)
6.exist
select name from orders where
exits
(select * from orders where name='')
7.any与子查询中的每一行与主查询进行比较,并对子查询中的每一行返回一个true值。
where name>any()
总结
select * from 表A where exists(select * from 表B where 表B.id=表A.id)
这句相当于
select * from 表A where id in (select id from 表B)
对于表A的每一条数据,都执行select * from 表B where 表B.id=表A.id的存在性判断,如果表B中存在表A

当前行相同的id,则exists为真,该行显示,否则不显示
exits适合内小外大的查询,in适合内大外小的查询

 

P157第二周
第八天,操作数据
1.insert into table valuse ('1','2','3')
或insert into table(item1,item2,item3)
  values('1','2','3')
2.空值和零或者空格不是一回事。
3.避免重复列的方法
if not exists(select * from collection where name='tree')
insert into collection values('tree',50,'');
3.insert select
insert into table1
select * from table2
4.外部数据的导入导出
几乎所有的数据库系统都可以导入或导出ASCLL码的文本文件。

 

第九天
P176创建和操作表
1.create database my#db
2.建立数据字典
大多数提供数据字典的工具包。
3.主关键字和外关键字
主关键字保证每一条记录都是唯一的。
外关键字,是在其它关系中可用作匹配字段的一种关键字。
P179不好的数据库设计与好的数据库设计
1.有一点,就是重复数据过多。
2.create table tablename(name char(30),amount number,account_id number);
create table bills(
name char(30) not null)
3.alert table
4.drop table

 

第十天
P193创建视图和索引
create view
create index
都是通过对数据预排序和预定义显著提高表的工作性能。
1.create view envelope(company,mailing_address)as
select name,address+''+city+','+state
from company
2.创建了视图,还可以对视图应用查询。
select * from my_view1 where count>500
3.视图限制
不能使用union操作
不能使用order by但有相同的操作。
4.对于单表视图,可以执行update或delete操作
5.多表不可以执行delete操作。
除非底层表的所有非空列都已经在视图中出现,否则你不能使用insert语句。
6.create view view1 as
select * from table1 where name in
(select * from table2)
7.索引
使用unique关键字时强制性地保证数据的完整性
可以容易地用索引字段或其他字段进行排序
提高查询的执行速度
8.
create index index_name
on
table(column1,column2);
9.索引与order by的区别
order by每次都要排序
索引是建立在数据库中的物理对象
10.复合索引
11.避免重复
create unique index unique_id_name
on
bills(account_id,name)
go
select * from bills
go
12.create index desc_amount
on
bills(amount desc);
13.索引与归并
14.群集(簇)的使用

 

第十一天
P223事件处理控制

事务是指在逻辑上必须完成的一命令序列的单位。
整个过程,要么全部终止,要么每件事都是正确的。

set transaction read only --锁定记录集
select * from customers
where name='Bill';
commit;

select
lock table
set role
alter session
alter system

Sybase语法
begin transaction new_account
insert customers values('tee','eee')
if exist(select * from customers where name='tree')
begin
  begin transaction
  insert balances values(123.3,34)
end
else
rollback transaction
if exists(select * from balances where account_id=8)
begin
  begin transaction
  insert accounts values(8,6)
end
else
rollback transaction
if exists(select * from account where account_id=8 and customer_id=6)
commit transaction
else
rollback transaction
go
上面是事务的嵌套
默认情况下set autocommit on命令自动运行。
它告诉sql自动确认你所运行的所有语句。
如果不想让这个命令自动运行,将它的参数设为no
set autocommit off
结束事务处理

保存点。
save transaction save_it
...
rollback transaction save_it
commit transaction

P238数据库安全
谁应该得到数据库管理员权限
有多少个用户需要访问数据库系统
每个用户应该得到什么析的权限与角色
当一个用户不再访问数据库时应该如何去删除它
1.用户,角色,权限
create user username identified by password
alter user username identified by newpwd
alter user username default tablespace users
drop user username
2.创建角色
角色是允许用户在数据库中执行特定功能的一个或一组权限。
grant role to user [with admin option]
revoke role from user;
orcle三种角色
Connect,Resource,DBA
grant connect to username
connect允许创建表,更新数据。等正常操作。
Resource允许创建过程,触发机制和索引。
3.用户权限
grant system_privilege to {username}
grant create view to public
grant select on table1 to username
select * from username.table1
略读
P255高级SQL
第十三天
grant授权,revoke废除,identifier标识符

P267存储过程
1.执行带参数的存储过程
create procedure pro1
@pname char(40),
@psummary char(30) output
as
select @psummary=summary from mytalbe where pname=@pname
go
执行方法
declare @return_summary char(30)
exec pro1,@return_name=@name output
print @pname
go
存储过程的嵌套,即可以互相调用。
2.创建虚表

P274触发器

 

 

 

 

 

 

 

 

抱歉!评论已关闭.