Difference between database and schema

Sql ServerDatabaseSql Server-2005Database Schema

Sql Server Problem Overview


What's the difference between a Database and a Schema in SQL Server? Both are the containers of tables and data.

If a Schema is deleted, then are all the tables contained in that schema also deleted automatically or are they deleted when the Database is deleted?

Sql Server Solutions


Solution 1 - Sql Server

A database is the main container, it contains the data and log files, and all the schemas within it. You always back up a database, it is a discrete unit on its own.

Schemas are like folders within a database, and are mainly used to group logical objects together, which leads to ease of setting permissions by schema.

EDIT for additional question
drop schema test1

>Msg 3729, Level 16, State 1, Line 1
Cannot drop schema 'test1' because it is being referenced by object 'copyme'.

You cannot drop a schema when it is in use. You have to first remove all objects from the schema.

Related reading:

  1. https://stackoverflow.com/questions/529142/what-good-are-sql-server-schemas
  2. MSDN: User-Schema Separation

Solution 2 - Sql Server

Schema is a way of categorising the objects in a database. It can be useful if you have several applications share a single database and while there is some common set of data that all application accesses.

Solution 3 - Sql Server

Database is like container of data with schema, and schemas is layout of the tables there data types, relations and stuff

Solution 4 - Sql Server

Schema says what tables are in database, what columns they have and how they are related. Each database has its own schema.

Solution 5 - Sql Server

Schema in SQL Server is an object that conceptually holds definitions for other database objects such as tables,views,stored procedures etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsqlchildView Question on Stackoverflow
Solution 1 - Sql ServerRichardTheKiwiView Answer on Stackoverflow
Solution 2 - Sql ServercethView Answer on Stackoverflow
Solution 3 - Sql ServerSaghir A. KhatriView Answer on Stackoverflow
Solution 4 - Sql ServerRobertView Answer on Stackoverflow
Solution 5 - Sql ServerHardik MishraView Answer on Stackoverflow