SQL Delete cascade & Update Cascade in SQL Server with foreign key

SQL-DELETE-CASCADE
SQL Delete Cascade

SQL Delete Cascade

In this post, we will look at SQL DELETE CASCADE and UPDATE CASCADE rules in SQL Server foreign keys using multiple examples.

DELETE CASCADE: When we create a foreign key using delete cascade, it deletes the referencing rows in the child table when the referenced row in the parent table with a primary key is deleted.

This means that if a Parent row is deleted, no orphan row in the Child table will be left. All of the children`s rows that refer to the parent’s rows should also be deleted (removed). If any of these children have grandchildren (through another foreign key in another table) and ON DELETE CASCADE is defined, they must be deleted as well.

Read more >>

Primary Key Constraint In SQL Server

The  PRIMARY KEY  constraint in SQL Server ensures that each entry in a table is uniquely recognized.
Primary keys must have UNIQUE values and cannot have NULL values.
In SQL Server, a table can only have one primary key, which can be made up of one or more columns (fields).

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.

Read more >>

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.

Read more >>

5 Types of SQL Commands: DML, DDL, DCL, TCL, DQL with Query Example

There are five SQL commands supported in the SQL programming language to perform various database operations such as DML, DDL, DCL, TCL, and DQL commands.

  • SQL commands are instructions used for database communication in order to perform specific tasks, functions, and data queries.

  • SQL can perform various tasks like creating a table, adding data to tables, dropping the table, modifying the table, setting permissions for users, and much more.

Types of SQL Commands

The following is the list of 5 types of SQL Commands: DDL, DML, DCL, TCL, and DQL.

image-types of SQL Commands
Types of SQL Commands

Read more >>