0% found this document useful (0 votes)
5 views78 pages

Unit I DBMS

The document provides an overview of Database Management Systems (DBMS), detailing their purpose, advantages, and disadvantages, as well as the various levels of data abstraction and data models. It explains the architecture of DBMS, including 1-tier, 2-tier, and 3-tier architectures, and introduces the concept of Relational Database Management Systems (RDBMS) along with its history and key terminologies. Additionally, it covers data integrity, the relational model, and the properties of relations, rows, columns, and attributes within a database.

Uploaded by

jayapriya kce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views78 pages

Unit I DBMS

The document provides an overview of Database Management Systems (DBMS), detailing their purpose, advantages, and disadvantages, as well as the various levels of data abstraction and data models. It explains the architecture of DBMS, including 1-tier, 2-tier, and 3-tier architectures, and introduces the concept of Relational Database Management Systems (RDBMS) along with its history and key terminologies. Additionally, it covers data integrity, the relational model, and the properties of relations, rows, columns, and attributes within a database.

Uploaded by

jayapriya kce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 78

UNIT I

Jayapriya K N
AP/CSE
Purpose of Database System in DBMS
• A database is a collection of data that is
organized, which is also called structured data.
It can be accessed or stored in a computer
system.
• DBMS is the most suitable technology that
provides services like storing, organizing, and
manipulating huge amounts of data.
Disadvantages of DBMS in File Processing

• The following are the main disadvantages of


DBMS in File Processing:
• Data redundancy and inconsistency.
• Difficult in accessing data.
• Data isolation.
• Data integrity.
• Concurrency is not possible.
• Security Problems.
Purpose of Database System
• Efficient storage and retrieval are allowed by
structured organization of data.
• DBMS maintains the reliability and accuracy of the
information.
• Protecting confidential data is crucial.
• Confidentiality and privacy of data are maintained by
utilizing resilient security measures within database
systems.
• Collaboration made easy with DBMS.
• Data backups and transaction management are
mechanisms provided by database systems to ensure
data durability.
DIGRAMMATIC REPRESENTATION
Views of Data
• Data abstraction is the procedure of
concealing irrelevant or unwanted data from
the end user.
• In DBMS, data abstraction is performed in
layers which means there are levels of data
abstraction.
• In DBMS, there are three levels of data
abstraction.
Levels of Abstraction
1. Physical or Internal Level:
• It is the layer that defines how data is actually stored
in the database.
• It defines methods to access the data in the database.
• It defines complex data structures in detail, so it is
very complex to understand, which is why it is kept
hidden from the end user.
• The Data Administrator (DBA) is the person whose
role is to manage the data in the database at the
physical or internal level.
• There is a data center that securely stores the raw
data in detail on hard drives at this level.
2. Logical or Conceptual Level:
• It explains what data is going to be stored in the
database and what the relationship is between
them.
• It describes the structure of the entire data in the
form of tables. The logical level or conceptual
level is less complex than the physical level.
• 3. View or External Level
• This level is for the end-user interaction; at this
level, end users can access the data based on
their queries.
Advantages of data abstraction in DBMS
• Users can easily access the data based on their
queries.
• It provides security to the data stored in the
database.
• Database systems work efficiently because of
data abstraction.
Data Models in DBMS

• Data Models provide us with a transparent


picture of data which helps us in creating an
actual database.
• It shows us from the design of the data to its
proper implementation of data.

• Types of Relational Models


• Conceptual Data Model
• Representational Data Model
• Physical Data Model
1. Conceptual Data Model

• It is this model, that is used in the


requirement-gathering process i.e. before the
Database Designers start making a particular
database. One such popular model is the
entity/relationship model (ER model).
• The E/R model specializes in entities,
relationships, and even attributes that are
used by database designers.
Entity-Relationship Model
• Entity-Relationship Model( ER Model): It is a high-level data
model which is used to define th
• Entity: An entity is referred to as a real-world object. It can
be a name, place, object, class, etc. These are represented
by a rectangle in an ER Diagram.
• Attributes: An attribute can be defined as the description of
the entity. These are represented by Ellipse in an ER
Diagram. It can be Age, Roll Number, or Marks for a Student.
• Relationship: Relationships are used to define relations
among different entities. Diamonds and Rhombus are used
to show Relationships.
• e data and the relationships between them.
2. Representational Data Model
• This type of data model is used to represent only
the logical part of the database and does not
represent the physical structure of the database.
• The relational Model consists of
Relational Algebra and Relational Calculus. In the
Relational Model, we basically use tables to
represent our data and the relationships
between them. It is a theoretical concept whose
practical implementation is done in Physical Data
Model.
3. Physical Data Model
• All data in a database is stored physically on a
secondary storage device such as discs and tapes.
• This is stored in the form of files, records, and
certain other data structures.
• It has all the information on the format in which
the files are present and the structure of the
databases, the presence of external data
structures, and their relation to each other.
• Structured Query Language (SQL) is used to
practically implement Relational Algebra.
Other Data Models
• 1. Hierarchical Model
• The hierarchical Model is one of the oldest
models in the data model which was
developed by IBM, in the 1950s.
• In a hierarchical model, data are viewed as a
collection of tables, or we can say segments
that form a hierarchical relation.
• In this, the data is organized into a tree-like
structure where each record consists of one
parent record and many children.
2. Network Model
• The Network Model was formalized by the Database Task
group in the 1960s.
• This model is the generalization of the hierarchical model.
• This model can consist of multiple parent segments and these
segments are grouped as levels but there exists a logical
association between the segments belonging to any level.
• 3. Object-Oriented Data Model
• In the Object-Oriented Data Model, data and their
relationships are contained in a single structure which is
referred to as an object in this data model.
• In this, real-world problems are represented as objects with
different attributes.
• All objects have multiple relationships between them.
4. Float Data Model
• The float data model basically consists of a two-
dimensional array of data models that do not
contain any duplicate elements in the array.
• 5. Context Data Model
• The Context data model is simply a data model
which consists of more than one data model.
• 6. Semi-Structured Data Model
• Semi-Structured data models deal with the data in
a flexible way. Some entities may have extra
attributes and some entities may have some
missing attributes.
Advantages of Data Models
• Data Models help us in representing data
accurately.
• It helps us in finding the missing data and also in
minimizing Data Redundancy.
• Data Model provides data security in a better way.
• The data model should be detailed enough to be
used for building the physical database.
• The information in the data model can be used for
defining the relationship between tables, primary
and foreign keys, and stored procedures.
DBMS Architecture 1-level, 2-Level, 3-Level

• We choose database architecture depending


on several factors like the size of the database,
number of users, and relationships between
the users.
• There are two types of database models that
we generally use, logical model and physical
model.
• Several types of architecture are there in the
database.
1-Tier Architecture
• In 1-Tier Architecture the database is directly
available to the user, the user can directly sit
on the DBMS and use it that is, the client,
server, and Database are all present on the
same machine.
• For Example: to learn SQL we set up an SQL
server and the database on the local system.
This enables us to directly interact with the
relational database and execute operations.
1-Tier Architecture
2-Tier Architecture
• The 2-tier architecture is similar to a basic
client-server model.
• The application at the client end directly communicates
with the database on the server side.
• APIs like ODBC and JDBC are used for this interaction.
• The server side is responsible for providing query
processing and transaction management functionalities.
• On the client side, the user interfaces and application
programs are run.
• The application on the client side establishes a
connection with the server side to communicate with
the DBMS.
2-Tier Architecture
3-Tier Architecture
• In 3-Tier Architecture, there is another layer between
the client and the server.
• The client does not directly communicate with the
server. Instead, it interacts with an application server
which further communicates with the database
system and then the query processing and transaction
management takes place.
• This intermediate layer acts as a medium for the
exchange of partially processed data between the
server and the client.
• This type of architecture is used in the case of large
web applications.
3-Tier Architecture
Architecture
• The two important components of database architecture
are - Query processor and storage manager.
• Query processor:
• The interactive query processor helps the database
system to simplify and facilitate access to data. It consists
of DDL interpreter, DML compiler and query evaluation
engine.
• With the following components of query processor,
various functionalities are performed -
• i) DDL interpreter: This is basically a translator which
interprets the DDL statements in data dictionaries.
Architecture
• ii) DML compiler: It translates DML statements query
language into an evaluation plan. This plan consists of the
instructions which query evaluation engine understands.
• iii) Query evaluation engine: It executes the low-level
instructions generated by the DML compiler.
• When a user issues a query, the parsed query is presented
to a query optimizer, which uses information about how
the data is stored to produce an efficient execution plan
for evaluating the query. An execution plan is a blueprint
for evaluating a query. It is evaluated by query evaluation
engine.
Storage manager:
• Storage manager is the component of database system that
provides interface between the low level data stored in the
database and the application programs and queries
submitted to the system.
• The storage manager is responsible for storing, retrieving,
and updating data in the database. The storage manager
components include -
• i) Authorization and integrity manager: Validates the users
who want to access the data and tests for integrity
constraints.
• ii) Transaction manager: Ensures that the database remains
in consistent despite of system failures and concurrent
transaction execution proceeds without conflicting.
Storage manager:
• iii) File manager: Manages allocation of space on disk
storage and representation of the information on disk.
• iv) Buffer manager: Manages the fetching of data from disk
storage into main memory. The buffer manager also
decides what data to cache in main memory. Buffer
manager is a crucial part of database system.
• Storage manager implements several data structures such
as -
• i) Data files: Used for storing database itself.
• ii) Data dictionary: Used for storing metadata, particularly
schema of database.
• iii) Indices: Indices are used to provide fast access to data
items present in the database
Database users and Administrator
• Naïve users-unsophisticated user
• Application programmers-RAD
• Sophisticated users-DML query
• Specialized users-new specific database
application
Database Administrator
Schema definition, Physical organization
modification,Granting of authorization for
data access, Routine maintenance.
RDBMS
• What is RDBMS (Relational Database Management
System)
• RDBMS stands for Relational Database
Management System.
• All modern database management systems like SQL,
MS SQL Server, IBM DB2, ORACLE, My-SQL, and
Microsoft Access are based on RDBMS.
• It is called Relational Database Management System
(RDBMS) because it is based on the relational model
introduced by E.F. Codd.
RDBMS
• Data is represented in terms of tuples (rows)
in RDBMS.
• A relational database is the most commonly
used database. It contains several tables, and
each table has its primary key.
• Due to a collection of an organized set of
tables, data can be accessed easily in RDBMS.
Brief History of RDBMS

• From 1970 to 1972, E.F. Codd published a


paper to propose using a relational database
model.
• RDBMS is originally based on E.F. Codd's
relational model invention.
Following are the various terminologies of RDBMS:
What is table/Relation?
• Everything in a relational database is stored in the
form of relations.
• The RDBMS database uses tables to store data.
• A table is a collection of related data entries and
contains rows and columns to store data.
• Each table represents some real-world objects
such as person, place, or event about which
information is collected.
• The organized collection of data into a relational
table is known as the logical view of the database.
Properties of a Relation:
• Each relation has a unique name by which it is
identified in the database.
• Relation does not contain duplicate tuples.
• The tuples of a relation have no specific order.
• All attributes in a relation are atomic, i.e., each
cell of a relation contains exactly one value.
What is a row or record?
• A row of a table is also called a record or tuple. It
contains the specific information of each entry in the
table. It is a horizontal entity in the table. For
example, The above table contains 5 records.
• Properties of a row:
• No two tuples are identical to each other in all their
entries.
• All tuples of the relation have the same format and
the same number of entries.
• The order of the tuple is irrelevant. They are
identified by their content, not by their position.
What is a column/attribute?

• A column is a vertical entity in the table which


contains all information associated with a
specific field in a table. For example, "name" is
a column in the above table which contains all
information about a student's name.
Properties of an Attribute:

• Every attribute of a relation must have a


name.
• Null values are permitted for the attributes.
• Default values can be specified for an attribute
automatically inserted if no other value is
specified for an attribute.
• Attributes that uniquely identify each tuple of
a relation are the primary key.
What is data item/Cells?
• The smallest unit of data in the table is the individual data
item. It is stored at the intersection of tuples and attributes.
• Properties of data items:
• Data items are atomic.
• The data items for an attribute should be drawn from the same
domain.
• Degree:
• The total number of attributes that comprise a relation is
known as the degree of the table.
• Cardinality:
• The total number of tuples at any one time in a relation is
known as the table's cardinality. The relation whose cardinality
is 0 is called an empty table.
Domain:
• The domain refers to the possible values each
attribute can contain. It can be specified using
standard data types such as integers, floating
numbers, etc. For example, An attribute entitled
Marital_Status may be limited to married or
unmarried values.
• NULL Values
• The NULL value of the table specifies that the
field has been left blank during record creation.
It is different from the value filled with zero or a
field that contains space.
Data Integrity
• Entity integrity: It specifies that there should be no
duplicate rows in a table.
• Domain integrity: It enforces valid entries for a
given column by restricting the type, the format, or
the range of values.
• Referential integrity specifies that rows cannot be
deleted, which are used by other records.
• User-defined integrity: It enforces some specific
business rules defined by users. These rules are
different from the entity, domain, or referential
integrity.
What is the Relational Model?
• The relational model represents how data is
stored in Relational Databases. A relational
database consists of a collection of tables,
each of which is assigned a unique name.
• Domain Constraints
• These are attribute-level constraints. An
attribute can only take values that lie inside
the domain range. e.g.; If a constraint AGE>0
is applied to STUDENT relation, inserting a
negative value of AGE will result in failure.
Key Integrity

• Every relation in the database should have at


least one set of attributes that defines a tuple
uniquely. Those set of attributes is called keys.
e.g.; ROLL_NO in STUDENT is key. No two
students can have the same roll number. So a
key has two properties:
• It should be unique for all tuples.
• It can’t have NULL values.
Referential Integrity

• When one attribute of a relation can only


take values from another attribute of the
same relation or any other relation, it is
called referential integrity.
• The relation which is referencing another
relation is called REFERENCING RELATION
(STUDENT in this case) and the relation to
which other relations refer is called
REFERENCED RELATION (BRANCH in this case).
Anomalies in the Relational Model
• An anomaly is an irregularity or something which
deviates from the expected or normal state.
When designing databases, we identify three types
of anomalies: Insert, Update, and Delete.
• Insertion Anomaly in Referencing Relation
• We can’t insert a row in REFERENCING RELATION if
referencing attribute’s value is not present in the
referenced attribute value. e.g.; Insertion of a
student with BRANCH_CODE ‘ME’ in STUDENT
relation will result in an error because ‘ME’ is not
present in BRANCH_CODE of BRANCH.
Deletion/ Updation Anomaly in Referenced Relation:

• We can’t delete or update a row from


REFERENCED RELATION if the value of
REFERENCED ATTRIBUTE is used in the value
of REFERENCING ATTRIBUTE. e.g; if we try to
delete a tuple from BRANCH having
BRANCH_CODE ‘CS’, it will result in an error
because ‘CS’ is referenced by BRANCH_CODE
of STUDENT
On Delete Cascade

• It will delete the tuples from REFERENCING


RELATION if the value used by REFERENCING
ATTRIBUTE is deleted from REFERENCED
RELATION. e.g.; For, if we delete a row from
BRANCH with BRANCH_CODE ‘CS’, the rows in
STUDENT relation with BRANCH_CODE CS
(ROLL_NO 1 and 2 in this case) will be
deleted.
On Update Cascade

• It will update the REFERENCING ATTRIBUTE in


REFERENCING RELATION if the attribute value
used by REFERENCING ATTRIBUTE is updated
in REFERENCED RELATION. e.g;, if we update a
row from BRANCH with BRANCH_CODE ‘CS’ to
‘CSE’, the rows in STUDENT relation with
BRANCH_CODE CS (ROLL_NO 1 and 2 in this
case) will be updated with BRANCH_CODE
‘CSE’.
Super Keys

• Any set of attributes that allows us to identify


unique rows (tuples) in a given relationship is
known as super keys. Out of these super keys,
we can always choose a proper subset among
these that can be used as a primary key. Such
keys are known as Candidate keys. If there is a
combination of two or more attributes that
are being used as the primary key then we
call it a Composite key.
Advantages of the Relational Model
• Simple model: Relational Model is simple and easy to
use in comparison to other languages.
• Flexible: Relational Model is more flexible than any other
relational model present.
• Secure: Relational Model is more secure than any other
relational model.
• Data Accuracy: Data is more accurate in the relational
data model.
• Data Integrity: The integrity of the data is maintained in
the relational model.
• Operations can be Applied Easily: It is better to perform
operations in the relational model.
Disadvantages of the Relational Model

• Relational Database Model is not very good


for large databases.
• Sometimes, it becomes difficult to find the
relation between tables.
• Because of the complex structure, the
response time for queries is high.
Characteristics of the Relational Model

• Data is represented in rows and columns called relations.


• Data is stored in tables having relationships between them
called the Relational model.
• The relational model supports the operations like Data
definition, Data manipulation, and Transaction
management.
• Each column has a distinct name and they are representing
attributes.
• Each row represents a single entity.
Keys
• Keys play an important role in the relational
database.
• It is used to uniquely identify any record or row
of data from the table. It is also used to establish
and identify relationships between tables.
• For example, ID is used as a key in the Student
table because it is unique for each student. In
the PERSON table, passport_number,
license_number, SSN are keys since they are
unique for each person.
1. Primary key

• It is the first key used to identify one and only one instance
of an entity uniquely. An entity can contain multiple keys,
as we saw in the PERSON table. The key which is most
suitable from those lists becomes a primary key.
• In the EMPLOYEE table, ID can be the primary key since it
is unique for each employee. In the EMPLOYEE table, we
can even select License_Number and Passport_Number as
primary keys since they are also unique.
• For each entity, the primary key selection is based on
requirements and developers.
2. Candidate key

• A candidate key is an attribute or set of attributes


that can uniquely identify a tuple.
• Except for the primary key, the remaining attributes
are considered a candidate key. The candidate keys
are as strong as the primary key.
• For example: In the EMPLOYEE table, id is best
suited for the primary key. The rest of the
attributes, like SSN, Passport_Number,
License_Number, etc., are considered a candidate
key.
3. Super Key
• Super key is an attribute set that can uniquely identify
a tuple. A super key is a superset of a candidate key.
• For example: In the above EMPLOYEE table,
for(EMPLOEE_ID, EMPLOYEE_NAME), the name of
two employees can be the same, but their
EMPLYEE_ID can't be the same. Hence, this
combination can also be a key.
• The super key would be EMPLOYEE-ID (EMPLOYEE_ID,
EMPLOYEE-NAME), etc.
4. Foreign key

• Foreign keys are the column of the table used to point to


the primary key of another table.
• Every employee works in a specific department in a
company, and employee and department are two different
entities. So we can't store the department's information in
the employee table. That's why we link these two tables
through the primary key of one table.
• We add the primary key of the DEPARTMENT table,
Department_Id, as a new attribute in the EMPLOYEE table.
• In the EMPLOYEE table, Department_Id is the foreign key,
and both the tables are related.
5. Alternate key
• There may be one or more attributes or a combination of
attributes that uniquely identify each tuple in a relation. These
attributes or combinations of the attributes are called the
candidate keys. One key is chosen as the primary key from
these candidate keys, and the remaining candidate key, if it
exists, is termed the alternate key. In other words, the total
number of the alternate keys is the total number of candidate
keys minus the primary key. The alternate key may or may not
exist. If there is only one candidate key in a relation, it does not
have an alternate key.
• For example, employee relation has two attributes,
Employee_Id and PAN_No, that act as candidate keys. In this
relation, Employee_Id is chosen as the primary key, so the
other candidate key, PAN_No, acts as the Alternate key.
6. Composite key
• Whenever a primary key consists of more than one
attribute, it is known as a composite key. This key is also
known as Concatenated Key.
• For example, in employee relations, we assume that an
employee may be assigned multiple roles, and an
employee may work on multiple projects simultaneously.
So the primary key will be composed of all three
attributes, namely Emp_ID, Emp_role, and Proj_ID in
combination. So these attributes act as a composite key
since the primary key comprises more than one attribute.
7. Artificial key
• The key created using arbitrarily assigned data are
known as artificial keys. These keys are created
when a primary key is large and complex and has
no relationship with many other relations. The
data values of the artificial keys are usually
numbered in a serial order.
• For example, the primary key, which is composed
of Emp_ID, Emp_role, and Proj_ID, is large in
employee relations. So it would be better to add a
new virtual attribute to identify each tuple in the
relation uniquely.
Introduction of Relational Algebra in DBMS

• Relational Algebra is a procedural query language.


• The main purpose of using Relational Algebra is to define operators
that transform one or more input relations into an output relation.
• Fundamental Operators
• These are the basic/fundamental operators used in Relational Algebra.
• Selection(σ)
• Projection(π)
• Union(U)
• Set Difference(-)
• Set Intersection(∩)
• Rename(ρ)
• Cartesian Product(X)
Fundamental Operators
• Selection(σ)
• σ(c>3)R will select the tuples which have c
more than 3.
• Projection(π)
• π(B,C)R
• Union(U)
• Set Difference(-)
• π(Student_Name)FRENCH -
π(Student_Name)GERMAN
Continuation
• Set Intersection(∩)
• Rename(ρ)
• ρ(a/b)R will rename the attribute 'b' of the
relation by 'a'.
• Cross Product(X)
• Let’s say A and B, so the cross product
between A X B will result in all the attributes
of A followed by each attribute of B. Each
record of A will pair with every record of B.
Derived Operators

• These are some of the derived operators, which


are derived from the fundamental operators.
• Natural Join(⋈)
• Conditional Join
• 1. Natural Join(⋈): Natural join is a binary
operator. Natural join between two or more
relations will result in a set of all combinations
of tuples where they have an equal common
attribute.
Continuation
• Conditional Join: Conditional join works
similarly to natural join. In natural join, by
default condition is equal between common
attributes while in conditional join we can
specify any condition such as greater than,
less than, or not equal.
Built in functions
• https://www.cs.utexas.edu/users/mitra/
csSummer2023/hsra/lectures/sqlFunc.html
SQL Views
• View defines a customized query that
retrieves data from one or more tables, and
represents the data as if it was coming from a
single source.
• Syntax
• CREATE VIEW view_name AS SELECT column1,
column2..... FROM table_name WHERE
condition;
LISTING ALL VIEWS IN A DATABASE

• We can list View using the SHOW FULL


TABLES statement or using
the information_schema table. A View can be
created from a single table or multiple tables.
• Syntax
• USE "database_name"; SHOW FULL TABLES
WHERE table_type LIKE "%VIEW";
DELETE VIEWS in SQL
• SQL allows us to delete an existing View. We can
delete or drop View using the DROP statement.
• Syntax
• DROP VIEW view_name;
• UPDATE VIEW in SQL
• If you want to update the existing data within the
view, use the UPDATE statement.
• Syntax
• UPDATE view_name SET column1 = value1,
column2 = value2...., columnN = valueN WHERE
[condition];
Aggregate Functions

• In database management an aggregate function is a


function where the values of multiple rows are grouped
together as input on certain criteria to form a single
value of more significant meaning.
• SUM(): Calculates the sum of values in a column.
• AVG(): Computes the average of values in a column.
• COUNT(): Returns the number of rows or non-null
values in a column.
• MIN(): Finds the minimum value in a column.
• MAX(): Retrieves the maximum value in a column.
Conditional Functions

• CASE WHEN: Allows conditional logic to be


applied in the SELECT statement.
• COALESCE(): Returns the first non-null value in
a list.
• NULLIF(): Compares two expressions and
returns null if they are equal; otherwise,
returns the first expression.
Mathematical Functions

• Mathematical functions are present in SQL which


can be used to perform mathematical
calculations. Some commonly used mathematical
functions are given below:
• ABS(): Returns the absolute value of a number.
• ROUND(): Rounds a number to a specified
number of decimal places.
• POWER(): Raises a number to a specified power.
• SQRT(): Calculates the square root of a number.
Dynamic SQL
• Dynamic SQL is a programming technique that could be used to write
SQL queries during runtime. Dynamic SQL could be used to create
general and flexible SQL queries.
• Syntax for dynamic SQL is to make it string as below :

• 'SELECT statement';To run a dynamic SQL statement, run the stored


procedure sp_executesql as shown below :

• EXEC sp_executesql N'SELECT statement';Use prefix N with the


sp_executesql to use dynamic SQL as a Unicode string.
Steps to use Dynamic SQL :
• Declare two variables, @var1 for holding the name of the table
and @var 2 for holding the dynamic SQL :
• DECLARE
• @var1 NVARCHAR(MAX),
• @var2 NVARCHAR(MAX);
• Set the value of the @var1 variable to table_name :
• SET @var1 = N'table_name';
• Create the dynamic SQL by adding the SELECT statement to the
table name parameter :
• SET @var2= N'SELECT * FROM ' + @var1;
• Run the sp_executesql stored procedure by using the @var2
parameter :
• EXEC sp_executesql @var2;

You might also like