SQL Delete Cascade – SQL query to delete parent-child referenced rows

In this article, we will learn DELETE CASCADE rules in SQL Server with foreign key constraint having multiple examples.

OVERVIEW – SQL DELETE CASCADE

1.  DELETE CASCADE : In the SQL server, when we create a foreign key using the delete cascade option, it deletes all the related rows from the child table when the referenced row is deleted from the parent table having primary key constraint.

SQL-DELETE-CASCADE
SQL-Delete cascade rule

The following are the SQL scripts that create a parent (Departments), child (Employees) table, and a foreign key on the child table without the delete cascade rule.

Continue reading “SQL Delete Cascade – SQL query to delete parent-child referenced rows”

SQL Comparison Operators (Less than, Greater than, Equal, Not Equal operators)

 Introduction  : In this SQL Operators article, we will learn all about SQL Comparison operators in details with multiple examples.

SQL Server Comparison Operators

In SQL Server, the comparison operators are useful to compare one expression with another expression using mathematical operators.
Comparison operators return  true  or  false  based on the comparison.
The following are the list of different type of comparison operators available in SQL.

Consider X is the variable and the value assigned to X = 5 ;

OperatorDescriptionExample
<Less Than (<) OperatorX < 10 (returns true)
>Greater Than (>) OperatorX > 10 (returns false)
<=Less Than or Equal To (<=) OperatorX <= 10 (returns true)
>=Greater Than or Equal To (>=) OperatorX >=5 (returns true)
=Equal (=) OperatorX = 5 (returns true)
!=Not Equal (!=) OperatorX != 5 (returns false)
<>Not Equal (<>) OperatorX <>5 (return false)
SQL Comparison operators

The following are the examples of the above SQL comparison operators.

Continue reading “SQL Comparison Operators (Less than, Greater than, Equal, Not Equal operators)”

Primary Key Constraint In SQL Server

What is the primary key in SQL Server?

In SQL Server, a  Primary Key  is a field or column which uniquely identifies each and every record in a table.

    • A primary key column doesn’t allow to insert  null  or duplicate values.
    • A Primary key can consist of up to 16 columns.
    • A table can have only one primary key which can be applied on single or multiple columns.
    • The primary key consist of multiple columns or fields is known as the Composite primary key.
    • A Primary Key constraint can be applied to the columns having any data types.
    • A primary key by default creates a clustered index on the column.

A primary key can be created at the column or table level.

Primary Key Constraint  in SQL Server
Primary Key Constraint in SQL Server

Syntax to create a primary key

The following are the syntax to create a primary key in SQL server.

Continue reading “Primary Key Constraint In SQL Server”

Stored Procedure in SQL Server With Examples

What Is a Stored Procedure In SQL Server?

 Stored Procedure : A stored procedure in SQL Server is a precompiled collection of SQL statements that get cached and stored in the database in order to reused over and over again whenever required.

Advantage of using Stored Procedure

Following are the advantage of using stored procedure in SQL server.

    • A stored procedure (SP) provides the reusability of SQL code as we can simply call the stored procedure Instead of writing the same SQL statements over and over again.
    • It allows reducing the network traffic by executing the SQL statements at the server-side rather than sending hundreds lines of SQL queries over the network.
    • A stored procedure can be used as a security mechanism by granting a user to execute only the stored procedure instead of allowing permission on the table.
    • It allows faster execution of SQL statements in order to enhance the performance.

Continue reading “Stored Procedure in SQL Server With Examples”

CREATE, ALTER, AND DROP DATABASE IN SQL SERVER

In this article, we will discuss how to Create, Alter, and Drop a database in SQL Server.
In my previous article, we have discussed all about the Types of SQL Commands.

Database Introduction

 Database  : In the SQL server a database is a place to store the data in an electronic format. It is an organized collection of database objects such as tables, views, functions, stored procedures, triggers and so on.
It allows a user to easily access or manipulate the data.

SQL server database

Create Database In SQL Server

In SQL Server, there are two ways to create a user-defined database, either by using the Transact-SQL statements or by using SQL Server Management Studio.

Continue reading “CREATE, ALTER, AND DROP DATABASE IN SQL SERVER”