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.
.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.
.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.
.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.
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.
相关文章推荐
- Working with tempdb in SQL Server 2005
- [原创]T-SQL Enhancement in SQL Server 2005 - Part I
- Stored procedures to implement paging for large tables or queries in SQL Server 2005 and SQL Server 2008
- Using Plan Guides in SQL Server 2005
- How to recreate the msdb database in SQL Server 2005
- 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005
- T-SQL Enhancement in SQL Server 2005[上篇]
- Using TRY/CATCH to Resolve a Deadlock in SQL Server 2005
- An Introduction to Dynamic Management Views and Functions in SQL Server 2005
- XML Enhancement in SQL Server 2005
- How to monitor blocking in SQL Server 2005 and in SQL Server 2000 [ZT-MS]
- [转载]官方视频教学《XML Support in SQL Server 2005》
- How to build an OLE DB application for SQL Server CE in Visual Studio 2005
- Index Fragmentation Report in SQL Server 2005 and 2008
- Troubleshooting Performance Problems in SQL Server 2005
- How to format datetime & date in Sql Server 2005
- Introduction to XQuery in SQL Server 2005
- Using DDL Triggers in SQL Server 2005 to Capture Schema Changes
- Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL & .NET
- Partitioned Tables and Indexes in SQL Server 2005