CREATE SCHEMA in SQL Server
Last Updated :
14 May, 2024
A schema is a collection of database objects like tables, triggers, stored procedures, etc. A schema is connected with a user which is known as the schema owner. The database may have one or more schema.
To create a schema in SQL Server use the 'CREATE SCHEMA' Statement.
SQL CREATE SCHEMA
CREATE SCHEMA statement used to create a new schema in the current database.
The CREATE SCHEMA statement can also create tables and views within the new schema and set GRANT, DENY, or permissions on those objects
It is useful to enhance the structure, security, and manageability of SQL Server databases.
Syntax
CREATE SCHEMA syntax is:
CREATE SCHEMA schemaname
[AUTHORIZATION ownername]GO
In this Syntax,
- Mention the name of the schema that you want to create in the CREATE SCHEMA clause.
- Define the owner of the schema after the AUTHORISATION keyword.
Note:
SQL Server has some built-in schema, for example, dbo, guest, sys, and INFORMATION_SCHEMA.
dbo is the default schema for a new database, owned by dbo user. While creating a new user with CREATE USER command, the user will take dbo as its default schema.
SQL CREATE SCHEMA Example
Let's look at some examples on how to create schema in SQL server using CREATE SCHEMA command.
Creating Schema in SQL Server Example
In this example, we are creating a schema named geeks_sch.
Query:
CREATE SCHEMA geeks_sch;
GO
View All SQL Server SCHEMA
To list all schema in the current database, use the query as shown below :
SELECT *
FROM sys.schemas
Result
name | schema_id | principal_id |
---|
dbo | 1 | 1 |
guest | 2 | 2 |
INFORMATION_SCHEMA | 3 | 4 |
sys | 4 | 4 |
db_owner | 16384 | 16384 |
db_accessadmin | 16385 | 16385 |
db_securityadmin | 16386 | 16386 |
db_ddladmin | 16387 | 16387 |
db_backupoperator | 16389 | 16389 |
db_datareader | 16390 | 16390 |
db_datawriter | 16391 | 16391 |
db_denydatareader | 16392 | 16392 |
db_denydatawriter | 16393 | 16393 |
Create Objects for the Schema
To create objects for a schema in SQL Server, you can use the CREATE TABLE statement with the schema name specified.
Syntax
CREATE TABLE schemaname.tablename( values... );
Create Objects for the Schema Example
To create a new table named Geektab in the geeks_sch schema:
CREATE TABLE geeks_sch.Geektab(
G_id INT PRIMARY KEY IDENTITY,
Name VARCHAR(200),
DOJ DATETIME2 NOT NULL
);
Similar Reads
Create login in SQL Server Creating a login in SQL Server is a crucial step in securing your database and controlling access to it. A login is an account that allows a user to connect to the SQL Server instance and access the database. In this article, we will explore the process of creating a login in SQL Server and the vari
2 min read
How to Rename SQL Server Schema? In SQL, we cannot RENAME a SCHEMA. To achieve this, we need to create a new SCHEMA, transfer all the contents(objects) from the old schema to new schema and then finally delete the old schema using the DROP command. The same is depicted in the below article. For this article, we will be using the Mi
3 min read
Create Database in MS SQL Server Databases in Microsoft SQL Server are crucial for managing data, categorized into system databases, which are auto-created and user databases, created by users. In this article, We will learn about the basics of system and user databases along with methods for creating and managing them using T-SQL
5 min read
Create, Alter and Drop schema in MS SQL Server Schema management in MS SQL Server involves creating, altering, and dropping database schema elements such as tables, views, stored procedures, and indexes. It ensures that the database structure is optimized for data storage and retrieval. In this article, we will be discussing schema and how to cr
4 min read
SQL Server Architecture Microsoft SQL Server is a widely used relational database management system (RDBMS) that organizations around the world rely on for managing and processing their data. It provides a scalable and reliable platform for managing large volumes of data, supporting a wide range of applications from small-
5 min read
List All Databases in SQL Server In SQL Server, databases are crucial for storing and managing data efficiently. Whether we are managing a large enterprise system or a small application, understanding how to list all the databases on our SQL Server is essential. In this article, we will write SQL queries that help us to retrieve al
3 min read
Delete Database in MS SQL Server Prerequisite â Introduction of MS SQL Server and Create Database in MS SQL Server System databases can't be deleted, only user databases could be deleted. Data and log files will automatically be deleted from disk with database deletion. To delete a database, the below methods could be used â SQL Se
1 min read
How to Create a Composite Primary Key in SQL Server? In this article, We will learn what is a Composite Primary Key and How will create a Composite Primary Key. As We know a Primary key is the Candidate key that is selected to uniquely identify a row in a table. A And Primary Key does not allow NULL value. Composite Primary KeyWhen two or more Columns
3 min read
Introduction of MS SQL Server Data is a collection of facts and figures and we have humungous data available to the users via the internet and other sources. To manipulate the data, Structured Query Language (SQL) in short has been introduced years ago. There are different versions of SQL available in the market provided by diff
2 min read
SQL CREATE DATABASE Creating a database is one of the fundamental tasks in SQL and is the first step in structuring your data for efficient management. Whether you're a developer or a database administrator, understanding the CREATE DATABASE statement is essential. Understanding how to use this command effectively is c
6 min read