Unlocking the World of Business Intelligence with SQLBI

Image
Introduction : ·         In the current data-centric world, Business Intelligence (BI) is integral to transforming raw data into actionable insights, guiding organizations toward informed decision-making.  ·         Among the prominent educational platforms for mastering BI,  SQLBI  stands out for its focus on Microsoft technologies like Power BI, DAX (Data Analysis Expressions), and SSAS Tabular.  ·         This guide delves deep into how SQLBI can serve as an invaluable educational resource, helping both educators and learners build practical and theoretical knowledge of BI. What is SQLBI? ·         SQLBI is an educational platform dedicated to the study and application of Business Intelligence, particularly focused on Microsoft technologies. ·         Founded by renowned experts M...

Types of Constraints in DBMS

What is Constraints?

  • In DBMS (Database Management Systems), a constraint is a rule applied to a column or a set of columns in a table to enforce data integrity and consistency. 
  • Constraints ensure that only valid data is entered into a database, preventing errors and maintaining the reliability of the data. 
  • They can limit the type of data that can go into a table and define relationships between tables.
  • Types of Constraints:
    • NOT NULL Constraint
    • UNIQUE Constraint:
    • PRIMARY KEY Constraint:
    • FOREIGN KEY Constraint:
    • CHECK Constraint:
    • DEFAULT Constraint:
  • Ley's Check all the Constraints one by one

  • NOT NULL CONSTRAINT

    • The NOT NULL constraint in SQL is used to ensure that a column cannot contain a NULL value. 
    • It enforces that every row in the table must have a value in the column where this constraint is applied. 
    • This is useful when you want to ensure that a certain field must always have data, and it cannot be left empty.

    • Purpose:

      • To prevent null (empty) values in a column.
      • Ensures that a column will always have a valid value, improving data integrity.

    • Syntax:

CREATE TABLE table_name ( 
column_name datatype NOT NULL 
);

    • Example:

    • Applying NOT NULL on a Column:

CREATE TABLE Employees ( 
 EmployeeID INT NOT NULL
 Name VARCHAR(100) NOT NULL
 Department VARCHAR(50
);
      • In this example, the EmployeeID and Name columns are set as NOT NULL, meaning every record must have values for these two columns. However, the Department column can accept NULL values.

    • Inserting Data into a NOT NULL Column:

INSERT INTO Employees (EmployeeID, Name, Department) 
VALUES (1, 'John Doe', 'IT');
      • This works fine because all NOT NULL columns (EmployeeID and Name) have values.

    • Violation of NOT NULL Constraint:

INSERT INTO Employees (EmployeeID, Name) 
VALUES (2, NULL);
      • This would generate an error because the Name column is defined as NOT NULL, and we are trying to insert a NULL value into it.

    • Important Points:

      • Mandatory Data: Any column with a NOT NULL constraint must always be provided with a value when inserting or updating data.
      • Data Integrity: By ensuring that critical columns (like EmployeeID or Name) always have values, the NOT NULL constraint helps maintain the integrity of the database.
      • Primary Key: The NOT NULL constraint is often used with Primary Keys, as primary keys must have unique and non-null values.

  • UNIQUE CONSTRAINT

    • The UNIQUE constraint in SQL ensures that all the values in a column (or a group of columns) are different from each other. 
    • This means no two rows can have the same value in the column where the UNIQUE constraint is applied. 
    • The UNIQUE constraint allows for better data integrity by preventing duplicate entries, ensuring the distinctiveness of data.

    • Purpose:

      • To prevent duplicate values in a column or a combination of columns.
      • Ensures the uniqueness of specific data, such as email addresses, phone numbers, or product codes.

    • Syntax:

CREATE TABLE table_name ( 
 column_name datatype UNIQUE 
);

    • Example:

    • Applying UNIQUE on a Single Column:

CREATE TABLE Employees ( 
 EmployeeID INT UNIQUE
 Email VARCHAR(100) UNIQUE
 Name VARCHAR(100
);
      • In this example, both the EmployeeID and Email columns have the UNIQUE constraint. This ensures that each employee will have a unique ID and email address.

    • Inserting Data with UNIQUE Constraint:

INSERT INTO Employees (EmployeeID, Email, Name) 
VALUES (1, 'john@example.com', 'John Doe');
      • This works because the EmployeeID and Email values are unique.

    • Violation of UNIQUE Constraint:

INSERT INTO Employees (EmployeeID, Email, Name) 
VALUES (1, 'jane@example.com', 'Jane Doe');
      • This would result in an error because the EmployeeID of 1 is already in use, and it must remain unique.

    • Important Notes:

      • Multiple UNIQUE Constraints: A table can have multiple UNIQUE constraints, each applying to different columns.
      • Null Values: Unlike the PRIMARY KEY constraint, columns with a UNIQUE constraint can accept NULL values, but each null is treated as unique (i.e., multiple NULL values are allowed in a UNIQUE column).
      • Difference from Primary Key: A PRIMARY KEY constraint is also unique, but a table can have only one primary key, whereas it can have multiple UNIQUE constraints

  • PRIMARY KEY CONSTRAINT

    • A Primary Key in SQL is a column (or a set of columns) that uniquely identifies each record in a table. 
    • It ensures that every row in the table has a unique value in the primary key column(s), and it does not allow NULL values. 
    • The Primary Key constraint combines the properties of both the UNIQUE and NOT NULL constraints.

    • Purpose:

      • To uniquely identify each record in a table.
      • Ensures that no duplicate or null values exist in the primary key column.
      • Often used to establish relationships between tables (e.g., via foreign keys).

    • Syntax:

CREATE TABLE table_name ( 
 column_name datatype PRIMARY KEY 
);

    • Example:

    • Single-column Primary Key:

CREATE TABLE Employees ( 
 EmployeeID INT PRIMARY KEY, 
 Name VARCHAR(100), 
 Department VARCHAR(50
);
      • In this example, EmployeeID is the primary key, meaning each employee must have a unique EmployeeID, and it cannot be NULL.

    • Inserting Data into a Table with Primary Key:

INSERT INTO Employees (EmployeeID, Name, Department) 
VALUES (1, 'John Doe', 'HR');
      • This works because EmployeeID is unique and not null.

    • Violation of Primary Key Constraint:

INSERT INTO Employees (EmployeeID, Name, Department) 
VALUES (1, 'Jane Doe', 'IT');
    • This would result in an error because EmployeeID = 1 already exists, and the primary key must be unique.

    • Key Features:

      • Uniqueness: Every value in the primary key column(s) must be unique.
      • NOT NULL: A primary key column cannot contain NULL values.
      • Single or Composite: A primary key can consist of one or more columns (composite key) to uniquely identify records.
      • One Per Table: Each table can have only one primary key, but it can be a single or composite key.

    • Primary Key vs. Unique Key:

      • Primary Key does not allow NULL values, while Unique Key can allow NULL values.
      • A table can have only one Primary Key, but it can have multiple Unique Keys.

  • FOREIGN KEY CONSTRAINT

    • A Foreign Key in SQL is a column (or a group of columns) in a table that creates a relationship between two tables. 
    • It is a field in one table that refers to the Primary Key in another table. 
    • The Foreign Key constraint ensures referential integrity between the two tables by enforcing that the value in the foreign key column corresponds to an existing value in the referenced primary key column.

    • Purpose:

      • To establish a relationship between two tables.
      • Ensures that the value in the foreign key column exists in the referenced table.
      • Maintains data integrity by preventing actions that would break the link between tables (e.g., deleting a record that is being referenced).

    • Syntax:

CREATE TABLE child_table ( 
 column_name datatype, 
FOREIGN KEY (column_name) REFERENCES parent_table (parent_column) 
);

    • Example:

    • Creating a Foreign Key Relationship:

CREATE TABLE Departments ( 
 DepartmentID INT PRIMARY KEY, 
 DepartmentName VARCHAR(100
); 

CREATE TABLE Employees ( 
 EmployeeID INT PRIMARY KEY, 
 Name VARCHAR(100), 
 DepartmentID INT
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) 
);
      • In this example, DepartmentID in the Employees table is a foreign key that references the DepartmentID in the Departments table. This creates a relationship between the two tables where each employee is associated with a department.

    • Inserting Data into Related Tables:

INSERT INTO Departments (DepartmentID, DepartmentName) 
VALUES (1, 'HR'), (2, 'IT'); 

INSERT INTO Employees (EmployeeID, Name, DepartmentID) 
VALUES (101, 'John Doe', 1), (102, 'Jane Smith', 2);
      • This works because the values in the DepartmentID column of the Employees table (1 and 2) match existing DepartmentID values in the Departments table.

    • Violation of Foreign Key Constraint:

INSERT INTO Employees (EmployeeID, Name, DepartmentID) 
VALUES (103, 'Michael Johnson', 3);
      • This would generate an error because DepartmentID = 3 does not exist in the Departments table, violating the foreign key constraint.

    • Foreign Key vs. Primary Key:

      • The Primary Key uniquely identifies each record in a table and cannot be null.
      • The Foreign Key references the primary key in another table to create a relationship. It can have duplicate values and can be null (in some cases, when allowed).

  • CHECK CONSTRAINT

    • The CHECK constraint in SQL is used to limit the values that can be inserted into a column. 
    • It allows you to specify a condition that must be met for each record in the table. If the data being inserted or updated violates the condition defined by the CHECK constraint, SQL will throw an error and prevent the operation.

    • Purpose:

      • To enforce specific rules on data in a column.
      • Ensures that only values satisfying the given condition are accepted.
      • Helps maintain data integrity by restricting invalid or out-of-range values.

    • Syntax:

CREATE TABLE table_name ( 
 column_name datatype CHECK (condition
);

    • Example:

    • Single Column CHECK:

CREATE TABLE Employees ( 
 EmployeeID INT PRIMARY KEY, 
 Name VARCHAR(100), 
 Age INT CHECK (Age >= 18
);
      • In this example, the CHECK constraint ensures that the Age column must always have a value of 18 or greater. Any attempt to insert a value less than 18 will result in an error.

    • Violation of CHECK Constraint:

INSERT INTO Employees (EmployeeID, Name, Age) 
VALUES (1, 'John Doe', 16);
      • This will result in an error because the value 16 for Age does not satisfy the CHECK constraint (Age >= 18).

    • Important Notes:

      • CHECK constraints can be used on multiple columns but typically evaluate conditions on a single row of data.
      • The database management system (DBMS) ensures that every data modification operation (such as INSERT or UPDATE) complies with the CHECK condition.
      • NULL Values: If the column allows NULL values, the CHECK constraint will not evaluate rows where the column contains NULL.

  • DEFAULT CONSTRAINT

    • The DEFAULT constraint in SQL is used to provide a default value for a column when no explicit value is specified during an INSERT operation. 
    • If a user does not provide a value for the column, the DEFAULT value will automatically be inserted into the column. 
    • This is particularly useful for ensuring that specific fields always have a meaningful value without requiring user input every time.

    • Purpose:

      • To automatically assign a default value to a column when no value is provided.
      • Ensures data consistency by avoiding NULL values in certain columns.
      • Simplifies INSERT operations by eliminating the need to always provide values for specific fields.

    • Syntax:

CREATE TABLE table_name ( 
 column_name datatype DEFAULT default_value );

    • Example:

    • Applying DEFAULT on a Column:

CREATE TABLE Employees ( 
 EmployeeID INT PRIMARY KEY, 
 Name VARCHAR(100), 
 Department VARCHAR(50) DEFAULT 'General' );
      • In this example, if a new employee record is inserted without specifying a department, the Department column will automatically be set to 'General'.

    • Inserting Data without Providing a Value for the DEFAULT Column:

INSERT INTO Employees (EmployeeID, Name) 
VALUES (1, 'John Doe');
      • Since no value is provided for Department, the default value 'General' will be automatically inserted for that row.

    • Inserting Data by Overriding the DEFAULT Value:

INSERT INTO Employees (EmployeeID, Name, Department) 
VALUES (2, 'Jane Smith', 'HR');
      • In this case, the specified value 'HR' for the Department column will override the default value 'General'.

    • Important Notes:

      • A DEFAULT value can be any constant value or function (such as CURRENT_DATE for date fields).
      • DEFAULT constraints apply only when a value is missing in an INSERT statement. If a value is provided, the DEFAULT is ignored.
      • NULL Values: If a column is nullable, the user can explicitly insert NULL even when a DEFAULT value is set.

Comments

Popular posts from this blog

ASP.Net Fundamentals

ASP.net Server Controls Part-1

Disk Operating System