Magic Tables in SQL Server
Last Updated :
11 Jun, 2021
Magic tables are the temporary logical tables that are created by the SQL server whenever there are insertion or deletion or update( D.M.L) operations. The recently performed operation on the rows gets stored in magic tables automatically. These are not physical table but they are just temporary internal tables. These magic tables can't be retrieved directly, we need to use triggers to access these magic tables to get the deleted and inserted rows.
When the following operations are done :
- INSERT -
The recently inserted row gets added to the INSERTED magic table.
- DELETE -
The recently deleted row gets added to the DELETED magic table.
- UPDATE -
The updated row gets stored in INSERTED magic table and the old row or previous row gets stored in the DELETED magic table.
Let us see how this works by using MSSQL as a server:
Creating a database :
Creating a database GeeksForGeeks by using the following SQL query as follows.
CREATE DATABASE GeeksForGeeks;
Using the database :
Using the database student using the following SQL query as follows.
USE GeeksForGeeks;
Creating table students with SQL query as follows:
CREATE TABLE students
(
stu_id varchar(10),
stu_name varchar(20),
branch varchar(20)
);
Verifying the database :
To view the description of the table in the database GeeksForGeeks using the following SQL query as follows.
EXEC sp_columns students;
Inserting data into the table :
Inserting rows into students table using the following SQL query as follows:
INSERT INTO students VALUES
('1901401','DEVA','C.S'),
('1901402','HARSH','C.S'),
('1901403','ABHISHEK','C.S'),
('1901404','GARVIT','C.S'),
('1901405','SAMPATH','C.S');
Verifying the inserted data :
Viewing the table after inserting rows by using the following SQL query as follows.
SELECT * FROM students;
Creating a trigger T1 on insert operation :
CREATE TRIGGER T1 ON students
AFTER INSERT
AS
BEGIN
SELECT * FROM INSERTED
END
Inserting entries to check how trigger retrieves INSERTED magic table :
INSERT INTO students VALUES
('1901406','PRADEEP','C.S'),
('1901407','DEVESH','C.S');
SELECT* FROM students ;
Creating a trigger T2 on delete operation :
CREATE TRIGGER T2 ON students
AFTER DELETE
AS
BEGIN
SELECT * FROM DELETED
END
Deleting entry to check how trigger retrieves DELETED magic table :
DELETE FROM students
WHERE stu_name = 'PRADEEP';
SELECT* FROM students ;
Creating a trigger T3 on update operation :
CREATE TRIGGER T3 ON students
AFTER UPDATE
AS
BEGIN
SELECT * FROM DELETED
SELECT* FROM INSERTED
END
Updating entry to check how trigger retrieves DELETED, INSERTED magic tables since we find an old entry in a DELETED and updated entry in the INSERTED magic table :
UPDATE students SET stu_name= 'DEVANSH'
WHERE stu_id = '1901401'
SELECT* FROM students
Similar Reads
Table operations in MS SQL Server
In a relational database, the data is stored in the form of tables and each table is referred to as a relation. A table can store a maximum of 1000 rows. Tables are a preferred choice as: Tables are arranged in an organized manner. We can segregate the data according to our preferences in the form o
2 min read
CREATE TABLE in SQL Server
SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
SQL Server Describe Table
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. When working with databases in SQL Server it is essential to understand the schema of the tables present in the database. Describing a table means getting informati
4 min read
Reverse PIVOT Table in SQL Server
In SQL Server, the PIVOT operation is a powerful feature that allows you to transform rows into columns, providing a convenient way to structure and analyze data. However, there are situations where we may need to reverse this operation, converting columns back into rows. This process is commonly kn
7 min read
Global Variables in MS SQL Server
Global variables are pre-defined system variables. It starts with @@. It provides information about the present user environment for SQL Server. SQL Server provides multiple global variables, which are very effective to use in Transact-SQL. The following are some frequently used global variables - @
2 min read
Select Statement in MS SQL Server
The SELECT statement in SQL Server is a foundational SQL command used for querying and retrieving data from one or more tables within a database. This command allows users to specify which columns and rows to retrieve and apply filters to focus on specific data and perform various operations to mani
4 min read
Select top in MS SQL Server
Prerequisite - Select in MS SQL Server Suppose that a user wants to extract the top students from the whole institution but has to use some complex queries to extract the data. To avoid complexity, the user can use 'Select Top'. 'Select Top' extracts the limited number of rows. This results in accur
2 min read
SQL Server UPDATE JOIN
In the expansive realm of SQL Server, the UPDATE JOIN operation emerges as a potent tool for modifying data within tables by combining information from multiple sources. Unlike a traditional UPDATE statement that modifies a single table, UPDATE JOIN enables the modification of records based on condi
6 min read
Dynamic SQL in SQL Server
In SQL Server, at times the SQL Queries need to be dynamic and not static, meaning the complete SQL query may be built dynamically at run time as a string using the user inputs and any specific application logic. This can be done in queries run from back-end applications or inside stored procedures.
6 min read
SQL Server Common Table Expressions
SQL Server is a relational database management system (RDBMS) that is used to handle complex data and maintain it in of tabular manner. With the help of SQL Server, one can easily protect their data as it provides various security features. In this article, we are going to explore SQL server's CTE a
8 min read