European Windows 2019 Hosting BLOG

BLOG about Windows 2019 Hosting and SQL 2019 Hosting - Dedicated to European Windows Hosting Customer

European SQL Server 2022 Hosting :: Installing SQL Server 2022

clock May 22, 2023 07:21 by author Peter

In this article, I will detail the installation process for SQL Server 2022 Developer Edition and SQL Server Management Studio (SSMS). The most recent version of SQL Server is 2022, and the most recent iteration of SQL Server Management Studio (SSMS) was on March 13, 2023. Developer Edition is complimentary.

Microsoft SQL Server Editions

There are four distinct edition variants available for SQL Server 2022.
    Enterprise - This edition has more features than any other
        It is the premium edition.
        Production utilization
        License cost
    Standard – Use in production
        License cost
        Enterprise Express – Free Software/Free to Download and Install has more features.
        It has a size limit of 10 GB and is only used for minor applications.
    Developer - Free to obtain and install software
        Developer is identical to Enterprise, but for non-production use.
        It is utilized to learn, construct, and test.

I'm utilizing SQL Server 2022 Developer Edition because it's a free download and installation.
How can SQL Server 2022 Developer Edition be installed?

Step 1
Downloading SQL Server 2022 from https://www.microsoft.com/en-in/sql-server/sql-server-downloads is required for installation.

Step 2
Click the "Download" button to acquire the executable file for SQL Server 2022.

Step 3
Open the system's download path and locate the executable file. SQL2022-SSEI-Dev.exe


Step 4
Double-click the SQL2022-SSEI-Dev.exe file to begin the installation.

Step 5
After a double-click, the system will request permission: "Do you wish to permit the following to make changes to this computer? To continue installing SQL Server 2022, click agree. Or select "Yes" when prompted about security.

Step 6
Choose the Basic installation type when prompted by the installer to select the installation type.

Step 7
Accept Microsoft license terms, then click the Accept button.


Step 8
Click the Install button. The installer automatically downloads and installs SQL Server.

Step 9
After loading packages, the progress bar will be shown. Wait for a few minutes while the installer downloads and installs packages.

Installation completed for Database Engine.

Congratulations!!! You have successfully installed SQL Server 2022 Developer Edition.

HostForLIFEASP.NET SQL Server 2022 Hosting



European SQL Server 2019 Hosting :: Magic Tables in SQL Server

clock May 17, 2023 08:42 by author Peter

Magic tables are the logical temporary tables created by the SQL server internally to recover recently inserted, deleted, and updated data into the SQL server. They are created during DML trigger execution. If you want to know more about DML triggers, you may refer to my previous article on DML Triggers.

Three types of Magic tables are created at the time of insert/update/delete in the SQL server.

    INSERTED Magic tables
    DELETED Magic tables
    UPDATED Magic tables

Magic tables are stored in temp DB just as a temporary internal table, and we can see them with the help of triggers. We can retrieve the information or the impacted records using these Magic tables.

Let’s see how this works with the use of a trigger.
    When we perform the insert operation, the inserted magic table will have a recently inserted record showing on top of the table.
    When we perform the delete operation, the deleted magic table will have a recently deleted record showing on top of the table.
    When we perform the update operation, the inserted magic table will have a recently updated record showing on top of the table.

Let’s consider the below table to see how this work.
SELECT * FROM StudentsReport;

Inserted Magic Table
Let’s create a trigger on the StudentsReport table to see if the values are inserted on the StudentsReport table and see if a virtual table or temp table (Magic table) is created with recently inserted records.
CREATE TRIGGER  TR_StudentsReport_InsertedMagic ON StudentsReport
FOR INSERT
AS
BEGIN
    SELECT * FROM INSERTED
END


Now when we insert the records in the StudentsReport table, at the same time inserted magic table will be created along with recently inserted records.

Now execute the below queries together.
INSERT INTO StudentsReport VALUES (6, 'Peter', 'English', 90);

SELECT * FROM StudentsReport;


In the above screenshot, we can see that while updating the record in the StudentsReport table, it’s also showing a recently updated record in the temp table, and that temp table is an updated magic table.

Conclusion
Magic tables are one of the features of DML Trigger and can be useful when we want to know the list of impacted records during trigger execution. In this article, first, we have gone through the overview of Magic tables. We have covered the three types of magic tables in detail.

I hope you have liked the article. Please let me know your inputs/feedback in the comments section below.

HostForLIFEASP.NET SQL Server 2019 Hosting



European SQL Server 2019 Hosting :: SQL Where Statement

clock May 10, 2023 10:59 by author Peter

In this article, we will learn about the WHERE statement in SQL. This statement is used to filter the results of a SELECT, UPDATE, or DELETE statement based on specified conditions in SQL.

The syntax of the WHERE clause is as follows
[ WHERE <search_condition> ]

Statement 1. Finding a row by using a simple equality
How to find a specific row in a database table using a simple equality comparison. To do this, you will need to know the name of the table and the name of the column that you want to compare against. For example, if you have a table called "OrderDetails" with columns for "OrderName," "orderAddress," and "OrderDate," you could find a specific customer by searching for their OrderName like this.
SELECT OrderName, orderAddress,OrderDate
FROM OrderDetails
WHERE OrderName  = 'Apple' ;


The SQL query you provided selects the columns 'OrderName,' 'orderAddress,' and 'OrderDate' from the 'OrderDetails' table where the 'OrderName' is equal to 'Apple.' Assuming that the 'OrderName' column contains the name of the product being ordered and the 'orderAddress' column contains the address where the order is being delivered, this query would return the 'OrderName,' 'orderAddress,' and 'OrderDate' information for all orders of the product 'Apple.'

Note. that the syntax and exact query result would depend on the specific schema and data in the 'OrderDetails' table.

Output

Statement 2. Finding rows by using a comparison operator
Using simple equality, you can use the following SQL query to find a row in a database table.
SELECT OrderName, OrderAddress
FROM OrderDetails
WHERE OrderId <= 5;


The SQL query you provided selects the OrderName and OrderAddress columns from the OrderDetails table where the OrderId is less than or equal to 5. The WHERE clause filters the rows of the table based on a specified condition. In this case, the condition is OrderId <= 5, which means that only rows where the OrderId column has a value less than or equal to 5 will be returned.

The result of this query will be a table containing the OrderName and OrderAddress values for the rows where the OrderId is less than or equal to 5. The number of rows returned will depend on how many rows in the OrderDetails table satisfy the condition.

Output

Statement 4. Finding rows that must meet several conditions
To find rows that must meet several conditions in SQL, you can use the AND operator in the WHERE clause. The AND operator allows you to combine two or more conditions that must be true for a row to be included in the query result.
SELECT OrderName, orderAddress
FROM OrderDetails
WHERE OrderId <= 1 AND OrderName LIKE '%App%' AND orderAddress LIKE '%N%';

This SQL query selects the OrderName and orderAddress columns from the OrderDetails table, where the OrderId is less than or equal to 1, the OrderName contains the substring "App," and the orderAddress contains the letter "N". In other words, it will return the order details for any orders with an OrderId of 1 or less, a name containing "App," and an address containing the letter "N". Please note that the syntax and functionality of this query may vary depending on the specific SQL implementation being used.

Output

Statement 5. Finding rows that are in a list of values  
You can use a SQL query with the IN operator to find rows in a list of values. Here's an example:

Suppose you have a table called OrderDetails with columns OrderName and Address, and you want to find all rows where the name is in a list of values ('Apple,' 'Mango,' 'Banna'). You can use the following query.
SELECT OrderName, orderAddress
FROM OrderDetails
WHERE OrderName IN ('Apple', 'Mango', 'Banna');

SQL query that selects the "OrderName" and "orderAddress" columns from the "OrderDetails" table, but only for orders with an "OrderName" of 'Apple,' 'Mango,' or 'Banna.' Here is the revised code with the corrected spelling for "Banana."

Output 

This query will return a result set containing the "OrderName" and "orderAddress" columns for all orders with an "OrderName" of 'Apple,' 'Mango,' or 'Banana.'
Conclusion  

This article taught us learned the basics of the SQL Where Statement.  

FAQs   

Q- What is the WHERE statement in SQL?

A- The WHERE statement is a clause in SQL that is used to filter data based on a specific condition. It is used in conjunction with the SELECT statement to retrieve data from one or more tables in a database.

Q- What are some common operators used in the WHERE statement?

A- Some common operators used in the WHERE statement include

    Equals (=)
    Not equals (<> or !=)
    Less than (<)
    Greater than (>)
    Less than or equal to (<=)
    Greater than or equal to (>=)
    LIKE (used for pattern matching)
    IN (used to check if a value is in a list)  

Q- Can you use multiple conditions in a WHERE statement?

A- Yes, you can use multiple conditions in a WHERE statement by using logical operators such as AND, OR, and NOT.

Q- What is the order of precedence for logical operators in a WHERE statement?

A- The order of precedence for logical operators in a WHERE statement is as follows

    NOT
    AND
    OR

HostForLIFEASP.NET SQL Server 2019 Hosting



European SQL Server 2019 Hosting :: LOGON Triggers in SQL Server

clock May 9, 2023 08:30 by author Peter

In the previous article (DDL Trigger), we have gone through the DDL trigger in detail. In this article, we shall cover Logon Triggers. So, let’s begin.

Logon Trigger

Logon trigger is a special kind of stored procedure that fires automatically when a LOGON event is detected, or a new database connection is established. Logon triggers are similar to DDL triggers and are created at the server level.

Need of Logon Trigger
There are a few use cases as follows where you may need to have a logon trigger.
    Tracking logon activity.
    Restrict connections to the SQL Server.
    Limit the number of sessions for a particular login.

Logon Trigger Syntax
CREATE TRIGGER trigger_name
ON { ALL SERVER }
FOR LOGON
AS
    BEGIN
        {sql_statement}
    END
GO

Now let’s create a logon trigger to understand more.
CREATE TRIGGER OPS_LOGON
   ON ALL SERVER
   AFTER LOGON
   AS
   BEGIN
      PRINT SUSER_SNAME() + 'HAS JUST LOGGED IN TO '+UPPER(LTRIM(@@SERVERNAME))+ 'SQL SERVER AT '+LTRIM(GETDATE())
   END
   GO

As you can see above that logon trigger “OPS_LOGON” is created.

Creating a LOGON Trigger restricts a user from simultaneously opening more than one connection with the SQL Server.

Let’s take another example.

Limit the number of sessions for a particular login.
CREATE TRIGGER limitingnumberofsessions
ON ALL SERVER WITH EXECUTE AS 'sa'
FOR LOGON
AS
BEGIN
IF ORIGINAL_LOGIN() <> 'sa'
AND
( SELECT COUNT(*)
FROM sys.dm_exec_sessions
WHERE Is_User_Process = 1 AND
Original_Login_Name = ORIGINAL_LOGIN()
) > 2
ROLLBACK
END

 

As you can see above the logon trigger “limitingnumberofsessions” is created.
All the LOGON triggers are created under the Server Objects -> Triggers folder, as shown above.

Now let’s go over some useful SQL commands used to manage triggers.

Following Syntax is to Disable Triggers in SQL Server
DISABLE TRIGGER [schema_name].[trigger_name] ON [object_name | DATABASE | ALL SERVER];

Example
DISABLE TRIGGER dbo.insertupdatedelete_trigger ON Employees;

Following Syntax is to Enable Triggers in SQL Server
ENABLE TRIGGER [schema_name.][trigger_name] ON [object_name | DATABASE | ALL SERVER];

Example
ENABLE TRIGGER dbo.insertupdatedelete_trigger ON dbo.Employees;

Following Syntax is to remove/drop DML Triggers
DROP TRIGGER [IF EXISTS] [schema_name.]trigger1, trigger2, ... ];

Example
DROP TRIGGER dbo.insertupdatedelete_trigger;

Following Syntax is to remove/drop DDL or LOGON Triggers
DROP TRIGGER [ IF EXISTS ] trigger_name [ ,...n ]   ON { DATABASE | ALL SERVER };

Example (DDL Trigger)
DROP TRIGGER IF EXISTS insertupdatedelete_trigger ON DATABASE;

Example (LOGON Trigger)
DROP TRIGGER IF EXISTS insertupdatedelete_trigger ON ALL Server;

List all the Triggers in SQL Server
Below query lists all the triggers available in the database
SELECT * FROM sys.triggers WHERE type = 'TR';

Advantages of Triggers

Below are the most common advantages of triggers
    Triggers are easy to write as they are similar to stored procedures.
    They allow us to build a basic auditing system.
    We can call other stored procedures and functions inside a trigger.

Disadvantages of Triggers
While creating triggers brings some useful benefits in tracking database events and activities. Below are the most common cons of using triggers.
    Triggers add additional overhead and slow down to DML statements a bit.
    Having a lot of nested triggers and recursive triggers can be difficult to debug and troubleshoot.
    Triggers are a bit difficult to locate as they work in the background.

Summary
Trigger is a very useful database object to keep track of database events. In this article, first, we have gone through the Logon trigger in detail. Post that, we have covered some of the useful queries related to triggers and pros/cons of them.

HostForLIFEASP.NET SQL Server 2019 Hosting



European SQL Server 2019 Hosting :: SQL Server Constraints: Types and Usage

clock May 5, 2023 10:04 by author Peter

SQL Server is a popular relational database management system used by businesses to store and manage their data. In SQL Server, constraints are used to define rules that ensure data integrity and consistency within a database. In this article, we will explore the different types of SQL Server constraints and how they can be used in database design.

Types of SQL Server Constraints

There are several types of constraints that can be defined for a table.

Here are the most common types of constraints in SQL Server.

Primary Key Constraint

A primary key constraint is used to enforce the uniqueness of a column or a combination of columns. This constraint is essential for ensuring data integrity and is often used to join tables. It ensures that each row in a table has a unique identifier. For example, a primary key constraint could be used to ensure that each customer in a database has a unique customer ID.

CREATE TABLE your_table_name (
  column1 datatype PRIMARY KEY,
  column2 datatype,
  column3 datatype,
  ...
);

The PRIMARY KEY keyword is used to define the primary key constraint on a column. The column specified as the primary key will uniquely identify each row in the table. For example, to create a primary key constraint on the "CustomerId" column of a table named "Customers".
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Email VARCHAR(50)
);


We can also modify an existing table to add a primary key constraint using the ALTER TABLE statement. For example,
ALTER TABLE Customers
ADD PRIMARY KEY (CustomerId);

Here are some benefits of using a primary key constraint in SQL Server,
Uniqueness- A primary key constraint ensures that each row in the table has a unique identifier value. This means that no two rows in the table can have the same primary key value. This helps prevent data duplication and ensures data integrity.

Referential integrity- A primary key constraint is often used as a reference point for other tables in a database. By creating a foreign key constraint that references the primary key of another table, you can ensure that the data in both tables is consistent and accurate.

Indexing- SQL Server automatically creates a clustered index on the primary key column(s) of a table. This can improve query performance by allowing the database engine to quickly locate specific rows based on their primary key value.

Data modification- A primary key constraint can help prevent unintended changes to data in a table. For example, if you try inserting a row into a table with the same primary key value as an existing row, the database engine will raise an error.

Foreign Key Constraint

A foreign key constraint is used to create a relationship between two tables. It ensures that a value in one table matches a value in another table. This constraint is used to maintain referential integrity between tables. For example, a foreign key constraint could be used to ensure that each order in a database is associated with a valid customer.
CREATE TABLE child_table (
    column1 datatype,
    column2 datatype,
    column3 datatype,
    ...
    FOREIGN KEY (column1)
      REFERENCES parent_table (parent_column)
);


The FOREIGN KEY keyword is used to define the foreign key constraint on a column in the child table. The REFERENCES keyword is used to specify the referenced table and column in the parent table. For example, to create a foreign key constraint on the "CustomerId" column of a table named "Orders", which references the "CustimerId" column of a table named "Customers".
CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);


We can also modify an existing table to add a foreign key constraint using the ALTER TABLE statement. For example:
ALTER TABLE Orders
  ADD FOREIGN KEY (CustomrrId)
    REFERENCES Customers (CustomerId);


Here are some benefits of using a foreign key constraint in SQL Server,
Referential integrity- A foreign key constraint ensures that data in related tables is consistent and accurate. By creating a foreign key constraint that references the primary key of another table, you can ensure that data in the referencing table is always linked to valid data in the referenced table.
Cascading updates and deletes- When a foreign key constraint is defined with cascade options, SQL Server can automatically update or delete related data in child tables when the parent table is modified. This can help maintain data consistency and simplify database maintenance.
Query performance- When a foreign key constraint is defined, SQL Server automatically creates an index on the referencing column(s). This can improve query performance by allowing the database engine to quickly locate related rows based on their foreign key value.
Data modification- A foreign key constraint can help prevent unintended changes to data in related tables. For example, if you try to delete a row from a table referenced by a foreign key in another table, the database engine will raise an error.

Unique Constraint
A unique constraint ensures that a column or a combination of columns has unique values. Unlike a primary key constraint, a unique constraint allows for null values. This constraint is often used to enforce business rules, such as ensuring that each product in a database has a unique product code.

Syntax
CREATE TABLE table_name (
    column1 datatype UNIQUE,
    column2 datatype,
    column3 datatype,
    ...
);

The UNIQUE keyword is used to define the unique key constraint on a column. The column specified as the unique key will have a unique value for each row in the table. For example, to create a unique key constraint on the "ProductCode" column of a table named "Products".
CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductCode VARCHAR(50) UNIQUE,
    ProductName VARCHAR(50),
    Price DECIMAL(10,2)
);


We can also modify an existing table to add a unique key constraint using the ALTER TABLE statement.

For example
ALTER TABLE Products
ADD CONSTRAINT Products_ProductCode UNIQUE (ProductCode);


Here are some benefits of using a unique constraint in SQL Server.
Data integrity- A unique constraint ensures that data in a table is consistent and accurate. By preventing duplicate values in specified columns, you can avoid data duplication and ensure that each row in the table has a unique identity.
Query performance- When a unique constraint is defined, SQL Server automatically creates a non-clustered index on the specified column(s). This can improve query performance by allowing the database engine to quickly locate specific rows based on their unique values.
Simplified database management- A unique constraint can help simplify database management by reducing the need for manual checks for duplicate data. By enforcing uniqueness at the database level, you can avoid the need for additional code to ensure data consistency.
Flexible constraints- Unlike primary key constraints, a unique constraint can be defined on columns that allow null values. This can be useful in scenarios where you want to ensure that no two rows have the same value but also allow null values in the specified column(s).

Check Constraint
A check constraint is used to enforce a condition on a column. It ensures that the values in a column meet a specified condition. This constraint is often used to enforce business rules, such as ensuring that a product's price is greater than zero.

CREATE TABLE table_name (
  column1 datatype,
  column2 datatype,
  column3 datatype,
  ...
  CONSTRAINT constraint_name CHECK (condition)
);

The CONSTRAINT keyword is used to define the check constraint on a column or set of columns. The CHECK keyword is used to specify the condition that must be met for the constraint to be satisfied. For example, create a check constraint on the "Age" column of a table named "Users", which ensures that the age is greater than or equal to 18.
CREATE TABLE Users (
  Id INT PRIMARY KEY,
  Name VARCHAR(50),
  Age INT CONSTRAINT CHK_Users_Age CHECK (Age >= 18),
  Email VARCHAR(100)
);

We can also modify an existing table to add a check constraint using the ALTER TABLE statement.

For example
ALTER TABLE Users
ADD CONSTRAINT CHK_Users_Age CHECK (Age >= 18);


Here are some benefits of using a check constraint in an SQL Server,

  • Data integrity- A check constraint ensures that data in a table meets specific criteria. By preventing invalid data from being inserted or updated in the table, you can ensure data consistency and accuracy.
  • Simplified database management- A check constraint can help simplify database management by reducing the need for additional code to ensure data consistency. By enforcing data validation at the database level, you can avoid the need for additional application codes to check data validity.
  • Flexible constraints- A check constraint can be defined on a single column or multiple columns and can use a wide range of conditions to validate data. This flexibility allows you to create custom validation rules that meet specific business requirements.
  • Improved performance- By ensuring that only valid data is stored in a table, a check constraint can improve query performance. This is because the database engine doesn't have to spend time searching for and filtering out invalid data when executing queries.

Default Constraint
A default constraint is used to provide a default value for a column. It ensures a column has a value even when not specified. This constraint is often used to provide default values for optional columns.

CREATE TABLE table_name (
    column1 datatype DEFAULT default_value,
    column2 datatype,
    column3 datatype,
    ...
);


The DEFAULT keyword is used to define the default constraint on a column. The default_value specified will be used as the default value for the column when a new row is inserted, and no value is specified for that column. For example, to create a default constraint on the "Role" column of a table named "Users" with a default value of 'User'.
CREATE TABLE Users (
    Id INT PRIMARY KEY,
    Name VARCHAR(50),
    Role VARCHAR(50) DEFAULT 'User',
    Email VARCHAR(100),
    Age INT
);

We can also modify an existing table to add a default constraint using the ALTER TABLE statement.

For example
ALTER TABLE Users
ADD CONSTRAINT DF_Users_Role DEFAULT 'User' FOR Role;

Here are some benefits of using a default constraint in SQL Server.
Data consistency- A default constraint ensures that a default value is used consistently across all rows in a table when no other value is specified. This can help ensure that data is consistent and accurate and that the data in the column is always populated with a value.
Simplified database management- A default constraint can help simplify database management by automatically populating columns with default values. This can reduce the need for additional application code to populate columns with default values.
Improved performance- By automatically populating columns with default values, a default constraint can improve performance by reducing the amount of data that needs to be updated or inserted. This can help minimize the amount of time needed to process data and improve overall database performance.
Flexibility- A default constraint can be defined on a single column or multiple columns and can use a wide range of default values to populate the column(s). This flexibility allows you to create custom default values that meet specific business requirements.

Conclusion
SQL Server constraints are essential for ensuring data integrity and consistency. They help to enforce rules and restrictions on data stored in tables. The different constraints in SQL Server can be used in various scenarios to ensure data is correctly stored and maintained. Using constraints, we can ensure that your database is accurate, reliable, and secure.

 

HostForLIFEASP.NET SQL Server 2019 Hosting



About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Month List

Tag cloud

Sign in