Schema In SQL Server

This article explains schemas in SQL Server. A SQL schema in a database is a collection of logical structures of data.

Introduction

 
A schema in a SQL database is a collection of logical structures of data. The schema is owned by a database user and has the same name as the database user. From SQL Server 2005, a schema is an independent entity (container of objects) different from the user who creates that object. In other words, schemas are very similar to separate namespaces or containers that are used to store database objects. Security permissions can be applied to schemas hence schemas are an important tool for separating and protecting database objects on the basis of user access rights. It improves flexibility for security-related administration of the database.
 

User schema separation

 
Before SQL Server 2005, database object owners and users were the same things and database objects (table, index, view and so on) were owned by the user. In other words database objects were directly linked to the user and the user could not delete them without removing the database object that were associated with the user. In SQL Server 2005, a schema separation is introduced, now the database object is no longer owned by a user, group or role. The schema can be owned by the user, group or role. The schema can have multiple owners. The schema ownership is transferrable. Database objects are created within the schema. Now the user can be dropped without the dropping of the database object owned by the user. But the schema cannot be deleted if it contains a database object.
 
The following are advantages of user schema separation:
  • The schema ownership is transferrable.
  • Database objects can be moved among the schemas.
  • A single schema can be shared among multiple users.
  • A user can be dropped without dropping the database objects associated with the user.
  • Provides more control of access and level of access.

Default schema

 
The default schema is the first schema searched when resolving object names. The user can be defined within the default schema. Using the "SCHEMA_NAME" function we can determine the default schema for the database.
 
result
 
The schema can be made the default for the user by defining DEFAULT_SCHEMA with CREATE USER or ALTER USER. If there is no default schema defined then SQL will assume "DBO" as the default schema. Note that there is no default schema associated with a user if the user is authenticated as a member of the group in the Windows operating system. In this case a new schema will be created and the name is the same as the user name.
 

Advantages of using Schema

  • Act as object protection tool: A schema can be a very effective object projection tool combined with the appropriate level of user permissions. A DBA can maintain control access to an object that would be very crucial.
  • Managing a logical group of database objects within a database: Schemas allow database objects to be organized into a logical group. This would be advantagous when multiple teams are working on the same database application and the design team wants to maintain integrity of the database tables.
  • Easy to maintain the database: A schema allows a logical grouping of the database objects, so the schema can help us in situations where the database object name is the same but falls in a different logical group.

Other Advantages

  • A single schema can be shared among multiple databases and database users.
  • A database user can be dropped without dropping database objects.
  • Manipulation of and access to the object is now very complex and more secure. The schema acts as an additional layer of security.
  • Database objects can be moved among schemas.
  • The ownership of schemas is transferable.

Conclusion

 
A schema is a very useful database concept and helps us to separate database users from the database object owners and also helps to create a logical grouping of database objects.