0% found this document useful (0 votes)
17 views29 pages

SQL Server Database Management Guide

The document provides a comprehensive overview of Microsoft SQL Server, including its introduction, operation modes (OLTP and OLAP), components, tools, and the role of a database administrator. It outlines practical steps for managing databases, such as creating, deleting, and renaming databases and tables, as well as defining data types and constraints. Additionally, it includes practical examples and SQL commands for database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views29 pages

SQL Server Database Management Guide

The document provides a comprehensive overview of Microsoft SQL Server, including its introduction, operation modes (OLTP and OLAP), components, tools, and the role of a database administrator. It outlines practical steps for managing databases, such as creating, deleting, and renaming databases and tables, as well as defining data types and constraints. Additionally, it includes practical examples and SQL commands for database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATABASE MANAGEMENT

Part One: Introduction to SQL Server


Second part: Getting started with SQL Server
Third part: Practical work
DATABASE MANAGEMENT

PRESENTATION OF SQL SERVER

INTRODUCTION
Microsoft SQL Server is a DBMS developed and marketed by Microsoft.
It is software that allows for the storage of data in a coherent and organized manner.
to easily view and modify their content.
He uses TSQL (Transact SQL) for his queries: it is an implementation of
SQL that supports stored procedures as well as triggers. SQL Server
uses the TDS format (Tabular Data Stream or communication protocol between clients)
and the server)
The first version of SQL Server was released in 1986, today it is at version 19.
But in the context of this course, we will use SQL Server 2017 version 17.9.

I. OPERATION
There are 2 types of servers on which the SQL environment can be deployed:
The OLTP server (Online Transactional Processing) which is a transactional server.
that is to say a server on which a certain number of LMD type transactions
can be executed. Most of the time OLTP systems are used for the
entry of orders, financial transactions, customer relationship management
etc. these systems have a large number of users who make requests
short and returns relatively few records.
In summary, an OLTP server has the following characteristics:

Transactions involve small amounts of data.


Has a large number of users
The response times are quite fast.
Requires great availability

The OLAP server (Online Analytical Processing) is a decision support server. It


allows for consolidating data from multiple sources through an ETL
(extraction, transformation, loading)
OLAP performs a multi-dimensional analysis of the company's data and offers the
possibility of performing complex calculations. It is used in companies for management of the
performance, planning, forecasts, financial reports, etc. it provides the analyst
and manage the information they need to make decisions.
DATABASE ADMINISTRATION

In summary, an OLAP server allows:

A quick data analysis


Allows to organize and compare data
Optimize the reporting
To perform complex calculations
To manipulate a large amount of data that can reach several terabytes.

II. THE COMPONENTS OF SQL SERVER


Microsoft SQL Server is composed of several software components that run in the form of
of service. Some have graphical interfaces, others are accessible via a
The command line. SQL Server consists of 5 main services:
SQL SERVER: it is the database engine service and corresponds to a
SQL SERVER instance. This component runs as a Windows service and is
referenced under the name MS SQL SERVER for the default instance and MS SQL
SERVER $INSTANCE_NAME For a named instance. The name of an instance
is defined during the installation.
SQL SERVER INTEGRATION SERVICE (SSIS) is the SQL SERVER tool.
import-export of data transfer and transformation
SQL SERVER ANALYSIS SERVICE: (SSAS) is the ideal project for projects
decision-making and data meaning. It allows for the construction of OLAP cubes.
SQL SERVER REPORTING SERVICE (SSRS) is thanks to this component that
we can return our data from our data warehouse in the form of
the report (tables, graphs, etc.)
THE SQL AGENT: he is responsible for monitoring SQL SERVER, this component
also manages the monitoring of scheduled tasks and the follow-up of alerts. It is
directly linked to a SQL instance and is referenced by default in the manager of
Windows services under the name: SQL SERVER AGENT. And by
$SQLSERVERAGENT (instance name) in the case of a named instance.

III. THE TOOLS


The different tools that can be found in SQL Server are:
- SQL SERVER MANAGEMENT STUDIO (SSMS): it allows you to achieve all the
operations at the database engine level
THE SQL SERVER CONFIGURATION MANAGER: it allows you to manage
all services related to SQL SERVER.
SQL SERVER PROFILER: it allows you to monitor and analyze the workload
from an SQL server instance
THE DATABASE ENGINE CONFIGURATION ASSISTANT:
allows for an optimization of the mountaineer's operation
DATABASE MANAGEMENT

-SQL CMD: it allows the execution of queries, command scripts

IV. ROLE OF A SQL SERVER DATABASE ADMINISTRATOR


A database administrator must be able to:
Install, configure the server and manage the databases;
Ensure the integrity of the data stored in the databases;
Establish backup plans;
Implement solutions to maintain production continuity;
Ensure the maintenance of data access performance;
Participate in technological monitoring.

V. STRUCTURE OF A SQL SERVER DATABASE


A SQL SERVER database is composed of 2 types of files.
The data files;
The log files.

THE DATA FILES


There are two types of them:

Primary data files: which are mandatory and have the .mdf extension
contains the catalog of the database;
Secondary data files: optional and with .ndf extensions that contain the
objects and user data.

I noted well:
Each database contains a single primary data file.

THE LOG FILES


With the .ldf extension, it records all the changes to the database.
They contain the information that will be used for the restoration of the database.
Each database must have at least one log file.
DATABASE MANAGEMENT

SECOND PART: GETTING STARTED WITH SQL SERVER

1. CONNECT TO A SQL SERVER INSTANCE


-Start SQL MANAGEMENT STUDIO
AT THE LEVEL OF THE SERVER TYPE SECTION, select ENGINE OF
DATABASE;
For the server name, enter the name of your SQL instance;
In the authentication section, select Windows authentication;
Click on log in

2. CREATION OF A DATABASE
To create a database, one can either use TSQL or SSMS.
1. Use of SSMS
In the object explorer, connect to an instance of the database engine.
data;
Right-click on the database, then click on New database
data
In the new database, enter the name of your database;
-To create the database accepting all default values, click on
OK;
To modify the default values of primary files and log files,
this is done in the database file grid, click on the cell
appropriate, then enter the new value;
-To modify the database options, select the options tab, then
make the changes of your choice;
To add an extended property to the database, select property
extended

In the NAME column, enter the name of the extended property;


In the VALUE column, enter the text of the extended property.

2. USE OF TSQL
In the standard toolbar, click on new query.
USE master;
GO
CREATE DATABASE GL
ON
(NAME = GL_dat,
DATABASE ADMINISTRATION

FILENAME = 'C:\Program Files\Microsoft SQL


Server\[Link]\MSSQL\DATA\GL_dat.mdf
SIZE=10,
MAXSIZE = 50,
FILEGROWTH = 5)
LOGON
(NAME = GL_log,
FILENAME = 'C:\Program Files\Microsoft SQL
Server\[Link]\MSSQL\DATA\
GL_log.ldf
SIZE = 5 // en MO,
MAXSIZE = 25,
FILEGROUWTH = 5);
GO

3. DELETE A DATABASE

a) Utilization of SSMS
In the object explorer, connect to an object instance of the database engine
of data;
- Developed the database file, right-click on the database
data to be deleted, then click on DELETE;
Check that the correct database is selected and then click OK.

b) Use of Transact SQL (TSQL)

Delete database
USE master;
GO
DROP DATABASE GL ;
GO
DATABASE ADMINISTRATION

4. RENAME A DATABASE

a) With SSMS
Always check that there are no open connections to the database; it is necessary
So define the database in single-user mode, in order to close all the
open connections and prevent other users from connecting during the
the name of the database has been changed.

To do this:
Right-click on the database to modify, then click on
PROPERTY;
In the properties dialog box, click on OPTION
In the option restrict access, select single user;
If other users are connected to the database, a message will open.
the connections appear to apply the property and close all the others
connexions, cliqué sur oui
In the object explorer developed based on a database, click with the button
right-click on the database to rename, then click rename.
Enter the name of the database and then click OK.
b) With TSQL
USE master;
GO
ALTER DATABASE GL SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE GL MODIFY NAME=GLCOPY;
GO
ALTER DATABASE GLCOPY SET MULTI_USER;
GO

5. DELETE DATA OR LOG FILES


a) WITH SSMS (SQL SERVER MANAGEMENT SYSTEM)
Develop the database folder, right-click on the database
data on which you wish to delete the file, then click on properties;
Select file;
In the file grid, select OK to delete.
USE master;
GO
DATABASE ADMINISTRATION

ALTER DATABASE GLCOPY REMOVE FILE GL_dat;


GO
6) CREATION OF A TABLE
WITH SSMS
In the object explorer, develop the database node, then select the
database that will contain the new table
Right-click on the table node, then click on new table
Enter the column names, choose the data type, and specify if the values
NULL values are allowed for each column;
To specify property advantages for a column, click on the column and
choose the appropriate properties in the column properties tab;
To specify a column as a primary key, right-click on the
column and select to define the primary key;
To create foreign key relationships, constraints, or indexes, click with the
Right-click in the table designer pane and select an object from the list;
By default, the table is contained in the DBO schema. To specify a schema
different, right-click in the table designer pane, and selected
property.
In the dropdown list schema, select the appropriate schema
In the file menu, choose save table name
In the dialog box, choose a name for the table, and click OK.

2. THE TYPES OF DATA UNDER SQL SERVER


The exact numerical values:
Int
Small Int
TINY Int
BIG Int
DECIMAL
NUMERIC
MONEY
MORE MONEY
The numerical values:
FLOAT
DATABASE ADMINISTRATION

REAL
Date and Time
DATE
TIME
DATETIME
SMALL DATE TIME
DATE TIME 2
DATETIME OFFSET
The string type
CHAR
VARCHAR
TEXT
Unicode character strings
NVARCHAR
NCHAR
NTEXT
The binary chains
TYPE IMAGE
BINARY TYPE
TYPE VARBINARY

3. WITH TSQL
Use nom_bd;
GO
CREATE TABLE schema (DBO)
4. Delete a table
Vous ne pouvez pas supprimer une table ayant une contrainte, vous devez au préalable
remove the constraint. When a table is deleted, the rules and values associated with it are
associated ones are automatically deleted
DATABASE MANAGEMENT

The deletion of a table automatically leads to the deletion of all the relationships it has.
maintenance

UNDER SSMS
In the object explorer, expand the database node, select the database
of data, then the table node
Right-click on the table, then in the context menu click on
delete
A message asks you to confirm the deletion, click on OK.

5. RENAME A TABLE
a) UNDER SSMS
In the object explorer, right-click on the table to rename it.
Use GL ;
Go
Exec sp_rename '[Link]', 'cours';
Go

6. ADDING COLUMNS TO A TABLE


a) With SSMS
In the object explorer, expand the database node, select the database
to give then click on the table on which you want to add columns
Right-click on the column heading and then click on new column.
Enter the new columns with their type and then validate

b) With TSQL
ALTER TABLE SCHEMA.TABLE_NAME ADD COLUMN_NAME TYPES1 ….
GO
7. DELETE A COLUMN
a) UNDER SSMS
- Search for the table where you want to remove the columns, expand it to
expose the names of the different columns
Right-click on the column to delete and then click on delete
In the dialog box to delete an object, click on ok
If the column contains constraints or other dependencies, a message will be displayed.
In the dialog box, delete an object. Resolve the error by deleting the
referenced constraints
You cannot delete a column that has a check constraint, you must
first remove the constraint
DATABASE ADMINISTRATION

You cannot delete a column that has constraints, primary key or


foreign key or other dependency, unless you use the table designer

b) TSQL
ALTER TABLE [Link]
DROP COLUMN COLUMN_NAME ;
DROP CONSTRAINT COLUMN_NAME;
GO
8. RENAME A COLUMN
a) SSMS
In the object explorer, right-click on the column to rename and
choose to rename
Enter a new column name and validate
b) TSQL
USE DATABASE_NAME;

GO
EXEC SP_RENAME
SCHEMA.TABLE_NAME.OLD_COLUMN_NAME
‘NOM_NOUVELLECOLONNE’, ‘COLUMN’ ;
9. INSERTING ROWS INTO A TABLE
10. Practical case
Create a database Gest_Etudiant with 3 extensions, a primary file, a
secondary and
DATABASE ADMINISTRATION

Create the tables


Fill in this comic using the table below.
Display all the tables,
DATABASE ADMINISTRATION

Database creation
USE master ;
GO
CREATE DATABASE GEST_ETUDIANT
ON
(NAME = STUDENT_DATA_dat,
FILENAME = "C:\Program Files\Microsoft SQL"
Server\[Link]\MSSQL\DATA\GEST_ETUDIANT.mdf

SIZE=10
MAXSIZE = 50,
FILEGROWTH = 5),
DATABASE ADMINISTRATION

(NAME = GEST_ETUDIANT_dat2, FILENAME =


C:\Program Files\Microsoft SQL
Server\[Link]\MSSQL\DATA\GEST_ETUDIANT.ndf

SIZE=10
MAXSIZE = 50,
FILEGROWTH = 5)

LOG ON
(NAME = GEST_ETUDIANT_dat,
FILENAME = "C:\Program Files\Microsoft SQL"
Server\[Link]\MSSQL\DATA\GEST_ETUDIANT.ldf
SIZE = 5 ,
MAXSIZE = 25,
FILEGROWTH = 5);
GO
Continuation of the TD

/* CONNECTION TO THE GEST_ETUDIANT DATABASE */

USE STUDENT_DB;
GO
/* CREATION OF THE STUDENT TABLE IN DBO*/

CREATE TABLE [Link] (


student_number INT PRIMARY KEY IDENTITY(01234567,1)

name NVARCHAR(20) NULL


first name NVARCHAR(20) NULL

date_naiss DATE,
sect VARCHAR(20) NULL,
age INT
);
DATABASE ADMINISTRATION

/* CREATION OF THE TABLE code mat IN DBO */


CREATE TABLE DBO.Code_mat (
code_mat INT PRIMARY KEY,
name_mat NVARCHAR(20) NULL
coef INT CHECK(coef > 0),

);

/* CREATION OF THE NOTE TABLE IN DBO*/


CREATE TABLE [Link](
num_etud INT,
code_mat INT,
note FLOAT CHECK( note > 0 and note < 20)

FOREIGN KEY(num_etud) REFERENCES [Link](num_etud),


FOREIGN KEY(code_mat) REFERENCES DBO.Code_mat(code_mat)

);

GO

SELECT DISTINCT * FROM [Link]


INNER JOIN [Link]
ON
[Link].student_id = [Link].student_id

WHERE
[Link].code_mat = 12518;
DATABASE ADMINISTRATION

Display the list of students whose name has 'O'


Use Student_Management;

Go
Select * from [Link] where Name like '%[O]%'
Go
Name of the student with the highest grade?
When faced with a request, one must ask oneself what am I supposed to
make and what tables to use?
Use Student Management

Go
Select Top(1)* from [Link] e
Inner join note n on([Link]-Etud=n.num_Etud)
order by [Link] DESC;
go
or well
select name from [Link] e inner join [Link] n on (e.student_number = n.student_number)

where [Link] = (select max(note) from [Link]);


go

For each section, what is the number of enrolled students?

Use Student_Management

Go
Select section, count (*) as student_count from [Link]
Group by ([Link]);
Go

Number of students born in 1988


Use Student Management
DATABASE ADMINISTRATION

Go
Select count (*) as Nes_en_1988 from [Link] where year(date_naiss)=1988;
Go
Display the average of each student
Use Student_Management

Go
Select name, isnull(note*coef/sum(coef),0) as average from [Link] e
Left join [Link] n on(e.student_number = n.student_number)

Left join [Link] m on(n.code_mat=m.code_mat)


Group by name;
go
TD"2 Director_Gest
display the list of clients belonging to category 1 in descending order
select * from client where num_cat=1 order by nom_cl;
list of clients who have Inno as their director?

select * from [Link] C inner join [Link] D on (D.Num_directeur = C.Num_directeur)

where nom_directeur='HInaud';
go
display the list of directors,
for each sector manager the number of departments they oversee
SELECT chef_secteu, COUNT(*) AS number_of_departments

from [Link]
group by ([Link].head_sector);
go

list of directors under sector head Fisher


select * from [Link]
inner join [Link]
on
DATABASE ADMINISTRATION

[Link].num_dir = [Link].num_direct
inner join [Link]
on [Link].code_dep = [Link].code_dep
where
[Link].section_head='FISHER';
go

the list of customers who receive 2%


select * from [Link]
inner join [Link]
on
client.num_cat = categorie.num_cat
where [Link] ='2%';

go

LIST OF SECTOR HEADS WHO HAVE AT LEAST 2 DEPARTMENTS TO THEIR


HEAD

Select chef_sector, count(dep_name) from [Link] group by

Chef_secteur having count (nom_dep)> =2;

go

List of clients belonging to communities

Select nom_cl from [Link] C inner join [Link] ca

On (c.num_cat = ca.num_cat) where nom_cat='collectivity';

Go

Name of the departments whose 3ththe letter is 'L'

Select department_name from [Link] where department_name like '__ L%'


DATABASE ADMINISTRATION

Go

List of directors whose age is above the average age

Select director_name from [Link] where age > (select AVG(age) from director);

Go

Nb structure of

Select_field,function

From dbo.nom_table

Clause (Where

Order by

Group by

Having

Condition

Go

7) Delete a table

- You cannot delete a table that is referenced by a foreign key constraint.


you must first remove the constraint or the table that references it
- When a table is deleted, the rules and default values are deleted and all
the constraints associated with it are automatically removed
- The deletion of a table automatically leads to the deletion of all the relationships it has.
entertainment

A - under SSMS
- In the object explorer, expand the database node, then select the database.
data then the node table
- Right-click on the table and then click on delete in the context menu.
- A message will ask you to confirm the deletion, click OK.

b- On TSQL
Request:

Use Database Name

Go
DATABASE ADMINISTRATION

Drop table schema_name.table_name

Go

Rename a table
a. With SSMS
- Right-click on the table to rename
- Then enter a new name in the column and validate.

b. With TSQL
Use Database_Name

go

EXEC SP_Rename [schema.old_name, new_name]

9. add columns to a table

a) With SSMS
- In the object explorer, expand the database node, select the database
give then click on the table on which you want to add columns
- Right-click on new columns, enter the new columns with their types.
and validate

b) With TSQL

Alter table schema.nom_table ADD nom_colonne type;

10. delete a column

a) UNDER SSMS
- Search the table from which you want to remove the columns, expand it to expose
the names of the different columns
- Right-click on the column to be deleted and then choose delete.
- In the dialog box, click on OK
- If the content column of the constraints or other dependencies has an error message
You will be displayed in the dialog box to delete an object. Resolve the error by deleting
the referenced constraints

Note: You cannot delete a column with a check constraint; you must first...
remove the constraint

- You cannot delete a column that has a primary key constraint.


foreign key or other dependency unless you use the table designer, in which case
on the contrary, you must first remove all dependencies from the column
DATABASE ADMINISTRATION

b) UNDER TSQL

11) Rename a column

A) With SSMS
- In the object explorer, right-click on the column to rename and
choose to rename
- Enter a new name in the column and then confirm
B) WITH TSQL

- You do
USE database_name

. EXEC SP_RENAME 'SCHEMA.table_name.old_column_name',


new_column_name, column;
oGo

12) insert rows into a table

INSERT INTO DBO.table_name


Values (value (corresponding to the type of the field ))

II. Storage Procedures


A stored procedure in SQL Server is a group of one or more TSQL statements to be
reuse more easily. The storage procedures resemble instructions from another language.
programming because they can:

- Accept input parameters and return multiple values in the form of parameters.
of sorting to the calling program
- Contain programming instructions that perform operations that perform
operations in the database

Stored procedures offer the following advantages:

- They guarantee a faster response time and better system performance.


- It allows you to stop writing the same instructions.
- It is possible to give users the right to execute stored procedures without
what is the right on the objects she manipulates

a. Stored procedure types

- The Stokes procedures defined by the user


DATABASE ADMINISTRATION

- A procedure defined by the user can be created in a database defined by


this one or any other system database except for the database named
resource
The Temporary Procedures
oLes are another form of storage procedures defined by the user. There are
two types:
. Local temporary procedures
. The global temporary procedures

the first character of the name of local temporary procedures is a #. These procedures are
available only during the user's connection and are deleted as soon as the connection
is closed.
On the other hand, the OMS of the global temporary procedures starts with ##. These procedures are visible.
to all users and are deleted at the end of the last session using the procedure

The system procedures

They are included in SQL Server and are physically stored in the database.
resource data but logically appear in the SYS schema defined either by
system either by the user. The MSBD database also contains the procedures.
to enable the system in the dbo schema used to plan alerts

NB:

- Given that the stored procedures start with the prefix sp_, it is
It is recommended not to use this prefix when naming your procedures given
- If a user creates a stored procedure with the same name as a system procedure, this
the procedure will never be executed
- The programming of a stored procedure differs depending on whether this procedure does not receive any
parameter, receives input parameters, returns output parameters or returns
a value
c. Create a Stored Procedure

- Under SSMS

Connect to a database engine instance

Database developer

Then click on programmability

Right-click on the stored procedure

In the menu, click on specify the parameter values of the model

In the dialog box, enter the following values:


DATABASE ADMINISTRATION

Parameter Values
Author Your name
Date of Creation Today's date
Description Data that returns what your Procedure does

Procedure_name Stored procedure name


@param @name of param 1
@datetype_from_param1 Parameter type 1
@default_value_from_param1 Default value of your parameter 1
@param 2 //
datatype_from_param2 //
Default value from param2 //

Click OK and then in the query editor write your query. Then to test the syntax
in the menu query click on analyze. If an error sms is returned compare the
instructions with the proposed instructions and make the necessary corrections

To execute the procedure, right-click on the name of the procedure and select.
execute the stored procedure by entering the corresponding parameters.

- With TSQL
Create PROCEDURE [schema]. procedure_name
@param1 type
@param n type n
As
SET NOCOUNT ON;
// instruction TSQL
Go

Exo

Stored procedure that displays the class and level of the student based on the procedure

Use student management

Create procedure [Link]

@Mat varchar (11)


DATABASE ADMINISTRATION

As

Set Nocount on;

Select [Link], [Link]

From [Link] E, [Link] C, [Link] N, dbo.Level_and_class NIV

Where [Link] =@Mat AND

[Link] =[Link] AND C.code_class=NIV.code_class and


N.id_number=NIV.id_number ;

GO

Declaration of a variable

DECLARE @variable_name type

Assignment of a value to a variable

SELECT @VARIABLE_NAME = VALUE

OU

SET @VARIABLE_NAME = VALUE

Allocation of information

PRINT(……)

Use of alternative structures


-The If else

If condition

BEGIN

// instruction

END

ELSE

BEGIN
DATABASE ADMINISTRATION

// instruction

END

Display an error when the user enters a name that is not in the database.

Use Gest_etud

Create procedure dbo.first_name

@penom varchar(25)

DECLARE @Pre varchar(25)

AS

Set nocount on;

Select @Pre=(select first_name from [Link] where first_name=@FirstName);

If (@Pre is not null)

BEGIN

Select student_name, student_id

From [Link]

Where Prenom=@prenom,

END

Else

Begin

Print’le prenom n’existe pas

END

Go

I want to check if a student has a grade in SQL Server. If so, we display it.
student's grade if not we display 'the student does not have a grade'

Correction

Use Student Management;


DATABASE ADMINISTRATION

Go

Create procedure dbo.not_sql

@nom varchar (25), @matier varchar (25)

AS

SET NOCOUNT ON

Declare @note float

Set @note = (select note from [Link])

The CASE when condition 1 then result 1


When condition n then result n
Else result
END

Display the list of students (first name and last name with an additional gender column that
display masculine, feminine according to the student's gender

CREATE PROCEDURE [Link]


AS
SET NOCOUNT ON;
SELECT nom, prenom, ‘genre’=
CASE
WHEN sexe = 'M' THEN 'MALE'
WHEN sexe = 'F' THEN 'FEMININE'
ELSE 'OTHERS'
END

FROM [Link]

GO

EXO
Display the list of students and indicate in each type column whether they are
MINOR or MAJOR
Use
Go
DATABASE MANAGEMENT

Create procedure dbo.get_type

As

Set nocount on;

Select nom,prénom, ‘type’=

CASE

WHEN (year (getdate ()) - year (date_naiss) < 18) THEN 'MINOR'

WHEN (year (getdate()) - year (date_naiss) >= 18) THEN 'ADULT'

END

From [Link]

Go

Transaction management

A transaction allows for the execution of a group of instructions, if for any reason
if one of these instructions was not executed, the entire instruction group is canceled:
To declare a transaction, the BEGIN TRAN statement is used.
To validate the transaction and make the associated processes effective, we
use the COMMIT TRAN instruction.
To interrupt a transaction that has not yet been validated, one uses
the instruction ROLLBACK TRAN
Syntax:
BEGIN TRANSACTION

//Instruction.
COMMIT TRAN
Example:
BEGIN TRAN exple
Delete from order where numCom=5
Delete from orderLine where orderNum=5
COMMIT TRAN
DATABASE ADMINISTRATION

Error message management

When an error is encountered, the database engine associates a message with it. If
The error does not exist in the [Link] table of the master database, it receives the code
50000. The engine also adds a notion of severity (degree of seriousness of an error),
which is rated by a value:
From 0 to 9: this means that the information messages indicate errors that
are not very serious (warnings).
Severity 10: The information messages report minor errors, and the
convert a gravity of 10 into gravity 0.
Gravity 11: Indicates that the object or entity does not exist.
Severity 12: Used for requests that do not have a lock.
Severity 13: Indicates blocking errors related to transactions.
Severity 14: Indicates errors related to security, such as a denied authorization.
Severity 15: Indicates syntax errors.
Severity 16: Indicates general errors.
From 17 to 19: Indicates software errors that cannot be corrected by
the user.
From 20 to 24: Indicates a system failure, and fatal errors that can occur
damage the BD.

Using RAISERROR :f

It allows displaying an error message.


RAISERROR ({msg_id | msg_str| @local_variable},
{severité, état},
[argument [1,..n]]
[with option [1,..n]]

Message_id indicates the message number to reference an already available message.


dans la table [Link].
Message_str : Il affiche le message personnalisé par l’utilisateur.
@local_variable: Allows calling the variable initialized by the user.
Severity: Represents the level of severity of an error. Only the administrator can add.
messages with a severity level between 19 and 24.
The state: An integer between 0 and 255, identifying the source from which the error originated.
issued.
DATABASE ADMINISTRATION

These are parameters used for substituting the variables defined in the
message.
With OPTION: It is a customized option for the error.
La gestion des curseurs.

You might also like