SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Services
  • Product
    🏋️ GyManage 📱 More to come
Contact
  1. SQL
  2. SQL Constraints

Text copied!

« Previous
Next »

SQL Constraints

Dec. 24, 2022, 6:49 p.m. under SQL

  • Hi! You should check SQL comments post first.

    SQL constraint is a rule applied to the 'column(s)', that restricts the values that can be entered into that column(s).

    Constraints can be defined during the table creation by using "CREATE TABLE statement" or added to an existing table using "ALTER TABLE statement". They remain in place until explicitly removed or modified.

    Here's an example of SQL constraints :
    CREATE TABLE Employee (
       [Id] INT PRIMARY KEY,
       [First_Name] VARCHAR(50) NOT NULL,
       [Last_Name] VARCHAR(50) NOT NULL,
       [Email] VARCHAR(255) UNIQUE,
       [Age] INT CHECK (age >= 18),
       [Salary] DECIMAL(10,2) DEFAULT 0,
       [Department_Id] INT REFERENCES Department(id) ON DELETE CASCADE
    );
            
    SQL Constraints
    Common types of SQL constraints include :
    PRIMARY KEY :

    PRIMARY KEY constraint on [Id] column to ensure that each employee has a unique ID.

    NOT NULL :

    NOT NULL constraints on the [FirstName] and [LastName] columns to ensure that these fields cannot be left empty.

    UNIQUE :

    UNIQUE constraint on the [Email] column to ensure that each email address is unique.

    CHECK :

    CHECK constraint on the [Age] column to ensure that employee are at least 18 years old.

    DEFAULT :

    DEFAULT constraint on the [Salary] column to set the default value to the [Salary] if no value is specified during an insert operation.

    FOREIGN KEY :

    FOREIGN KEY constraint is used to establish a relationship between two tables. The FOREIGN KEY constraint is added to a column in one table that references the PRIMARY KEY column of another table. This constraint ensures that the values in the referencing column must match the values in the referenced column or be null. It can also enable the use of cascading updates and deletes, which can automatically update or delete related records in the referenced table when a record in the referencing table is modified or deleted.

    Constraints are vital for ensuring data quality control as they maintain accuracy and consistency in the database.

    Frequently Asked Questions :

    What are 5 constraints in SQL?

    Five constraints in SQL include: Primary Key, Foreign Key, Unique, Not Null, and Check constraints.

    What is constraints in Rdbms?

    Constraints in RDBMS enforce rules or conditions on data stored in tables to maintain data integrity.

    How many constraints are there in SQL?

    SQL supports multiple constraints, including the ones mentioned earlier, but the exact number varies based on the database system.

    How to query constraints in SQL Server?

    To query constraints in SQL Server, you can use system tables like sys.check_constraints and sys.foreign_keys to retrieve information about existing constraints in a database.
    Thank You! You should check SQL Joins post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India

    © 2024 Copyright | All Rights Reserved | Legal