您的位置:首页 > 数据库

Synonyms in SQL Server 2005

2007-08-28 10:00 471 查看
http://nayyeri.net/archive/2007/08/27/synonyms-in-sql-server-2005.aspx

Naming conventions are always a big concern for developers whether they're an application developer or database developers! Naming tables, columns, views, stored procedures and other objects in a database sometimes gets harder than what we think, especially when we're working on multiple objects with same name from two different databases, tables or ...
SQL Server 2005 introduced a new feature for synonyms that come handy to assign alternative names for a database object in order to work with it easier. CREATE SYNONYM is a new statement that lets you assign these alternative names.
You can define a synonym on a two-part, three-part or four-part object name. The scope of synonyms is limited to the database where they're defined in! Synonyms can be defined for some objects like:

Table
View
Stored Procedure
Function
Replication filter procedure
Extended stored procedure

Let me give an example. Suppose that I have a database named MyDB with a table named MyTable. This table contains three columns: ID, Name and Age. I create some sample data in this table.
Now I can assign an alternative name to MyTable like AltMyTable as you see.
CREATE SYNONYM dbo.AltMyTable FOR MyTable
SELECT * FROM AltMyTable

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This simply returns data from MyTable.



As another example, I also create a simple stored procedure to select data from MyTable and name it SelectData.
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'SelectData')
BEGIN
DROP Procedure Stored_Procedure_Name
END
GO
CREATE Procedure SelectData
AS
SELECT * FROM MyTable
GO

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Now I assign a new name like SelectMyData to this stored procedure.
CREATE SYNONYM dbo.SelectMyData FOR SelectData
EXECUTE SelectMyData

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }



You can also check your synonyms from the explorer easily.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: