SQL SERVER 2005中同义词实例
2007-11-11 12:29:02 来源:WEB开发网在sql server(WINDOWS平台上强大的数据库平台) 2005中,终于出现了同义词了,大大方便了使用。下面举个小例子说明
同义词是用来实现下列用途的数据库对象:
为本地或远程服务器上的另一个数据库对象(称为“基对象”)提供备选名称。
提供一个提取层,该层防止客户端应用程序的基对象的名称或位置被更改。
例如,名为 Server1 的服务器上有 Adventure Works 的 Employee 表。若要从另一台服务器 Server2 引用此表,客户端应用程序必须使用由四个部分构成的名称 Server1.AdventureWorks.Person.Employee。另外,如果更改了表的位置(例如,更改到另一台服务器上),那么需要修改客户端应用程序以反映所做的更改。 之前在http://www.cnblogs.com/jackyrong/archive/2006/06/15/426304.html中已经归纳了一些特性,现在在举出例子,例子来自老外的。
首先建立两个数据库
Create Database RiverResearch
go
Use RiverResearch
go
然后分别为两个数据库建立模式以及表,存储过程如下
Use RainbowResearch
go
Create Schema Rain
go
Create Schema Snow
go
Create table Rainbowresearch.Rain.Cities (id int, City varchar(200))
go
insert into Rainbowresearch.Rain.Cities select 1, 'Hongkong'
insert into Rainbowresearch.Rain.Cities select 2, 'Tokyo'
insert into Rainbowresearch.Rain.Cities select 3, 'Beijing'
insert into Rainbowresearch.Rain.Cities select 4, 'Taipei'
insert into Rainbowresearch.Rain.Cities select 5, 'Seoul'
insert into Rainbowresearch.Rain.Cities select 6, 'Mumbai'
go
Create procedure Rain.DisplayCities @id int
as
Select City from Rainbowresearch.Rain.Cities where id=@id
go
Create table Rainbowresearch.Snow.Cities (id int, City varchar(200))
go
insert into Rainbowresearch.Snow.Cities select 1, 'Tokyo'
insert into Rainbowresearch.Snow.Cities select 2, 'Seoul'
insert into Rainbowresearch.Snow.Cities select 3, 'Moscow'
insert into Rainbowresearch.Snow.Cities select 4, 'NewYork'
go
Create procedure Snow.DisplayCities @id int
as
Select City from Rainbowresearch.Snow.Cities where id=@id
go
Use RiverResearch
go
Create Schema River
go
go
Create table RiverResearch.River.Cities (id int, City varchar(200))
go
insert into RiverResearch.River.Cities select 1, 'Hongkong'
insert into RiverResearch.River.Cities select 2, 'Tokyo'
insert into RiverResearch.River.Cities select 3, 'Beijing'
insert into RiverResearch.River.Cities select 4, 'Taipei'
insert into RiverResearch.River.Cities select 5, 'Seoul'
go
Create procedure River.DisplayCities @id int
as
Select City from RiverResearch.River.Cities where id=@id
Go
这里,在RainbowResearch数据库里建立了两个模式,rain,snow,再分别在这两个模式下建立了city表,而在
riversearch数据库里也建立了个模式river,也建立了表cities,就这么简单,不详细表述。
之后,我们查询刚才建好的内容
select * from RiverResearch.River.Cities
go
select * from Rainbowresearch.Rain.Cities
go
select * from Rainbowresearch.Snow.Cities
go
最后,我们建立同义词
use RainbowResearch
go
create SYNONYM RiverCities for RiverResearch.River.Cities
go
create SYNONYM RainCities for Rainbowresearch.Rain.Cities
go
create SYNONYM SnowCities for Rainbowresearch.Snow.Cities
go
use RiverResearch
go
create SYNONYM RiverCities for RiverResearch.River.Cities
go
create SYNONYM RainCities for Rainbowresearch.Rain.Cities
go
create SYNONYM SnowCities for Rainbowresearch.Snow.Cities
go
看到了么,简单许多了
再来查询一次,这次简单多了
Use RiverResearch
go
Select * from RiverCities
Select * from RainCities
Select * from SnowCities
Go
Use RainbowResearch
go
Select * from RiverCities
go
Select * from RainCities
go
Select * from SnowCities
go
http://www.cnblogs.com/jackyrong/archive/2006/11/15/561287.html
- ››SQL Server 2008 R2 下如何清理数据库日志文件
- ››sqlite 存取中文的解决方法
- ››SQL2005、2008、2000 清空删除日志
- ››SQL Server 2005和SQL Server 2000数据的相互导入...
- ››sql server 2008 在安装了活动目录以后无法启动服...
- ››sqlserver 每30分自动生成一次
- ››sqlite 数据库 对 BOOL型 数据的插入处理正确用法...
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
更多精彩
赞助商链接