Home > Database object qualifiers
Tutorial:
EMAIL THIS LICENSING & REPRINTS

Database object qualifiers

07 Feb 2007 | McGraw-Hill

Expert advice on database development
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google

The complete name of a database object consists of four identifiers, concatenated in the following manner:

[[[server.][database].][schema].]database_object

Each of these identifiers must comply with the rules described in the previous section. Server, database, and schema are often referred to as database object qualifiers. The complete name of the object is often referred to as the fully qualified name, or four-part name.

Schema is a part of the name that is used to logically group database objects. You can also think of them as a namespace. Two database objects can have the same name as long as they belong to different namespaces. SQL Server 2005 databases by default have a couple of namespaces: dbo (as default schema), INFORMATION_SCHEMA (defined by SQL-92 Standard for system views that provide metadata information), sys (for system tables and views), and guest.

In SQL Server 2005, a user can create database objects that belong to any schema. If a user does not specify the schema, SQL Server 2005 will make it part of the default dbo schema. It is possible to define another schema and assign it as a default schema to any user. You can find more details about schemas in BOL.

NOTE
Before SQL Server 2005, schemas were tied with users that created objects. If the object was created by the user who created the database (or any member of the db_owner fixed database role or sysadmin server role), SQL Server will record the owner as dbo. In other cases, the username of whoever created the object will be assigned as the object owner. Therefore, this segment of object name used to be called owner.

When you are referencing the object, if you do not specify the name of the schema, SQL Server will first try to find the object in the sys schema. If such an object does not exist, SQL Server will try to locate it in the schema named the same as the user. If it does not exist there either, SQL Server will attempt to find it in the dbo schema. For example, to resolve the stored procedure name ap_Eq_List specified by using dsunderic without a schema name, SQL Server 2005 will try to find it as:

  • sys.ap_Eq_List
  • dsunderic.ap_Eq_List
  • dbo.ap_Eq_List

    TIP
    To avoid this waste of time, always try to explicitly specify the schema.

    Server and database are (naturally) the names of the server and the database in which the object is stored.
    You do not have to use all the qualifiers all the time. You can omit the server name and/or the database name if the database object is located in the current database and/or on the current server. You can also omit using the schema name when referencing a database object in one of the default schemas. For example, when you are connected to the Asset5 database on the SQLBox server, instead of typing
    SQLBox.Asset5.dbo.ap_Eq_List
    you can use any of the following:

    ap_Eq_List
    dbo.ap_Eq_List
    Asset5.dbo.ap_Eq_List
    Asset5..ap_Eq_List
    SQLBox.Asset5..ap_Eq_List
    SQLBox...ap_Eq_List
    SQLBox..dbo.ap_Eq_List

    NOTE
    You can also use consecutive periods to skip qualifiers.

    But the real advantage of a schema is that you can use it to group database objects such as those in the Object Browser (see Figure 3-1).


    Basic Transact-SQL Programming Constructs

     Home: Introduction
     Tip 1: T-SQL identifiers
     Tip2: Database object qualifiers
     Tip 3: Character string data types
     Tip 4: Date, time and number data types
     Tip 5: Special data types-Part 1
     Tip 6: Special data types-Part 2
     Tip 7: Local variables
     Tip 8: Global variables
     Tip 9: Table variables
     Tip 10: Flow control statements
     Tip 11: Blocks, looping and conditional statements
     Tip 12: Unconditional and scheduled execution
     Tip 13: Transact SQL cursors
     Tip 14: Cursor related statements
     Tip 15: Problems and justified use of cursors

    The previous tip is from "15 tips in 15 minutes: Basic Transact-SQL Programming Constructs," excerpted from Chapter 3, of the book "Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL & .NET" by Dejan Sunderic, courtesy of McGraw-Hill Publishing.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


    RELATED CONTENT
    SQL/Transact SQL (T-SQL)
    Physical data storage in SQL Server 2005 and 2008
    SQL Server 2008 data types: Datetime, string, user-defined and more
    SQL Server and data manipulation in T-SQL
    Enforcing data integrity in a SQL Server database
    Supertype and subtype tables in SQL Server
    Using SQL Server datetime functions GETDATE, DATENAME and DATEPART
    Ordering the results of a SQL query
    How to use SQL Server 2008 hierarchyid data type
    SQL Server data conversions from date/time values to character types
    SQL and SQL Server Tutorial and Reference Guide
    SQL/Transact SQL (T-SQL) Research

    SQL Server stored procedures
    How to use SQL Server 2008 hierarchyid data type
    SQL and SQL Server Tutorial and Reference Guide
    SQL Server stored procedures tutorial: Write, tune and get examples
    Check SQL Server database and log file size with this stored procedure
    SQL Server source code analysis and management adds database security
    Configure SQL Server Service Broker for sending stored procedure data
    Find size of SQL Server tables and other objects with stored procedure
    Track changes to SQL Server 2000 and 2005 with one simple utility
    Troubleshoot SQL Server 2005 temporary table performance problems
    Use SQL Profiler to find long running stored procedures and commands

    XML in SQL Server
    Manage traces in SQL Server 2005 Analysis Services with XMLA commands
    Create a computed column in SQL Server using XML data
    Create DDL table in SQL Server 2005 to audit DDL trigger activity
    Retrieve XML data values with XQuery in SQL Server 2005
    XML data type in SQL Server 2005 vs. VARCHAR (MAX)
    SQL Server Blog Watch
    T-SQL commands vs. XML AUTO in SQL Server
    Basic Transact-SQL programming constructs: 15 tips, 15 minutes
    T-SQL identifiers
    Special data types-Part 1

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    ACID  (SearchSQLServer.com)
    commit  (SearchSQLServer.com)
    DAO  (SearchSQLServer.com)
    fetch  (SearchSQLServer.com)
    OLE DB  (SearchSQLServer.com)
    query  (SearchSQLServer.com)
    SQL  (SearchSQLServer.com)
    T-SQL  (SearchSQLServer.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary


  • HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




    All Rights Reserved, Copyright 2005 - 2008, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts