SQL Server - Database Objects
Last Updated :
08 Oct, 2024
In SQL Server, database objects are essential components that allow to store and manage data effectively. These objects can range from tables and views to stored procedures and indexes.
Understanding the various types of database objects is important for database design, management, and optimization. This article will provide an overview of the key database objects in SQL Server.
SQL Server - Database Objects
A database object in SQL Server refers to the defined structure that contains data or metadata. These objects can be created, modified, and deleted as needed to support the database operations and apply the business rules of an application.
Types of Database Objects
1. Tables
Tables are the fundamental building blocks of a database. They store data in rows and columns, where each row represents a record and each column represents a field in that record.
Query:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(100),
DepartmentID INT,
Salary DECIMAL(10, 2)
);
INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID, Salary)
VALUES
(1, 'John Smith', 101, 50000.00),
(2, 'Jane Doe', 102, 60000.00),
(3, 'Michael Johnson', 101, 55000.00),
(4, 'Emily Davis', 103, 70000.00),
(5, 'David Wilson', 102, 48000.00);
Output:
EmployeeID | EmployeeName | DepartmentID | Salary |
---|
1 | John Smith | 101 | 50000.00 |
2 | Jane Doe | 102 | 60000.00 |
3 | Michael Johnson | 101 | 55000.00 |
4 | Emily Davis | 103 | 70000.00 |
5 | David Wilson | 102 | 48000.00 |
2. Views
Views are virtual tables that provide a way to present data from one or more tables. They can simplify complex queries, provide data security by restricting access to specific columns, and present data in a specific format.
Query:
CREATE VIEW EmployeeDetails AS
SELECT EmployeeName, Salary
FROM Employees
WHERE Salary > 50000;
Output:
EmployeeName | Salary |
---|
Jane Doe | 60000.00 |
Michael Johnson | 55000.00 |
Emily Davis | 70000.00 |
Explanation:
The EmployeeDetails
table shows only the employees whose salary is greater than 50,000:
- John Smith and David Wilson are excluded since their salaries are 50,000 and 48,000, respectively.
- Jane Doe, Michael Johnson, and Emily Davis are included because their salaries exceed 50,000.
3. Stored Procedures
Stored procedures are precompiled SQL statements that can be executed to perform a specific task. They allow for reusable code, contain business logic, and can accept parameters.
Query:
CREATE PROCEDURE GetEmployeeByID
@EmployeeID INT
AS
BEGIN
SELECT * FROM Employees WHERE EmployeeID = @EmployeeID;
END;
Executing the stored procedure:
EXEC GetEmployeeByID @EmployeeID = 101;
Output:
EmployeeID | EmployeeName | DepartmentID | Salary |
---|
101 | John Doe | 1 | 60000.00 |
Explanation:
This case assumes that the Employees
table has an employee with EmployeeID = 101.
If the employee does not exist, the result would be an empty set.
4. Functions
Functions are similar to stored procedures but are used primarily for calculations and can return a value. They can be scalar (return a single value) or table-valued (return a table).
Query:
CREATE FUNCTION GetEmployeeCount()
RETURNS INT
AS
BEGIN
DECLARE @Count INT;
SELECT @Count = COUNT(*) FROM Employees;
RETURN @Count;
END;
Executing the function:
SELECT dbo.GetEmployeeCount() AS TotalEmployees;
Output:
Explanation:
- Function Purpose: The
GetEmployeeCount
function calculates and returns the total number of employees in the Employees
table.
- Return Value: The function returns an integer (
INT
) representing the total count.
- Execution Result: The result from the
SELECT
statement displays the total number of employees as TotalEmployees
. In this case the value would be 5
reflecting the five employees inserted earlier.
5. Indexes
Indexes improve the performance of queries by allowing SQL Server to find rows more quickly. They can be created on one or more columns of a table.
Example:
CREATE INDEX IDX_EmployeeName ON Employees (EmployeeName);
Output:
IndexName | ColumnName |
---|
IDX_EmployeeName | EmployeeName |
Explanation:
This output confirms that the index IDX_EmployeeName
exists on the EmployeeName
column of the Employees
table. While creating an index it does not produce a direct output table. It enhances query performance and can be verified through system catalog queries, helping us to understand how the index impacts data retrieval.
6. Triggers
Triggers are special types of stored procedures that automatically execute in response to specific events on a table, such as INSERT
, UPDATE
, or DELETE
operations. They are useful for applying business rules or maintaining data integrity.
Query:
CREATE TRIGGER trgAfterInsert
ON Employees
AFTER INSERT
AS
BEGIN
PRINT 'New employee added.';
END;
Inserting a new employee to trigger the message:
INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID, Salary)
VALUES (104, 'Alice Brown', 1, 70000);
Output:
EmployeeID | EmployeeName | DepartmentID | Salary |
---|
1 | John Smith | 101 | 50000.00 |
2 | Jane Doe | 102 | 60000.00 |
3 | Michael Johnson | 101 | 55000.00 |
4 | Emily Davis | 103 | 70000.00 |
5 | David Wilson | 102 | 48000.00 |
104 | Alice Brown | 1 | 70000.00 |
Explanation:
- The trigger
trgAfterInsert
executes after a new employee is added, printing a message to the output.
- The
Employees
table is updated to include the new employee, confirming the successful insertion.
Conclusion
Understanding database objects in SQL Server is crucial for effective database management and design. Each type of database object has a unique purpose and can work together to build strong and efficient databases.
By using these objects effectively, we can make sure that data is stored, accessed, and managed according to the need of the applicant.
Similar Reads
SQL Server Show/List Databases
Listing all databases in SQL Server is a common task for database administrators and developers. SQL Server provides two main methods to solve this such as using SQL commands and using SQL Server Management Studio (SSMS). In this article, we will learn about how to Show/List the SQL Server Databases
4 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
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
6 min read
How to Open a Database in SQL Server?
Opening a database in SQL Server is a fundamental task for database administrators and developers. It involves establishing a connection to the server instance and selecting a database to work with. In this article, we will explore two methods to open a database in SQL Server such as using SQL Serve
3 min read
SQL Select Database
The USE DATABASE statement is a command in certain SQL-based database management systems that allows users to select and set a specific database as the default for the current session. By selecting a database, subsequent queries are executed within the context of that database, making it easier to i
4 min read
SQL - Show Databases
In the dynamic scene of database management, having a good insight into the available databases for effective administration and development tasks. SHOW DATABASES command is designed to present all databases located on the server. The purpose of exploring the SQL SHOW DATABASES command is to give da
3 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
How to Import and Export SQL Server Database?
Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the d
3 min read
SQL Server sp depends
In SQL Server, making changes to any existing database objects like deleting a table or changing the name of a table or a table column can affect other database objects like views, procedures, and functions where the updated or deleted database object was referenced. So before updating or deleting a
4 min read
SQL USE Database Statement
SQL(Structured Query Language) is a standard Database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. It is flexible and user-friendly. In SQL, to interact with the database, the users have to type queries that have certain syntax, a
2 min read