Skip to main content

MYSQL Constraints

 PK - Primary Key: Uniquely identifies each record in a table.

NN - Not Null: Ensures a column cannot have a NULL value.

UQ - Unique: Ensures all values in a column are different.

B - Binary: Stores binary byte strings.

UN - Unsigned: For numeric types, allows only non-negative values.

ZF - Zero Fill: Pads numeric values with zeros to the left.

AI - Auto Increment: Automatically generates a unique number for new records.

G - Generated Column: Value is computed from an expression.


  • PK - Primary Key
  • A primary key uniquely identifies each record in a table. It must contain unique values and cannot have NULL values. 
    Example:
    CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(50), Age INT );
    Here, StudentID is the primary key.
  • NN - Not Null 
  • This constraint ensures that a column cannot have NULL values. 
    Example:
    CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(50) NOT NULL, Email VARCHAR(100) );
    The Name column cannot be left empty when inserting or updating records.

  • UQ - Unique 
  • This constraint ensures all values in a column are different. 
    Example:

    CREATE TABLE Users ( UserID INT PRIMARY KEY, Username VARCHAR(50) UNIQUE, Password VARCHAR(50) );
    The Username column must have unique values for all records.

  • B - Binary 
  • This is used to store binary byte strings. 
    Example:

    CREATE TABLE Files ( FileID INT PRIMARY KEY, FileName VARCHAR(100), FileContent BINARY(1000) );
    FileContent can store binary data like images or documents.

  • UN - Unsigned 
  • For numeric types, this allows only non-negative values. 
    Example:

    CREATE TABLE Products ( ProductID INT PRIMARY KEY, Quantity INT UNSIGNED );
    Quantity can only store values >= 0.
  • ZF - Zero Fill 
  • This pads numeric values with zeros to the left. 
    Example:

    CREATE TABLE Orders ( OrderID INT PRIMARY KEY, OrderNumber INT(6) ZEROFILL );
    If OrderNumber is 123, it will be displayed as 000123.

  • AI - Auto Increment 
  • This automatically generates a unique number for new records. Example:

    CREATE TABLE Customers ( CustomerID INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(50), Email VARCHAR(100) );
    CustomerID will automatically increment for each new record.

  • G - Generated Column 
  • The value is computed from an expression. 
    Example:
    CREATE TABLE Rectangle ( ID INT PRIMARY KEY, Width INT, Height INT, Area INT GENERATED ALWAYS AS (Width * Height) );
    The Area column is automatically calculated based on Width and Height.

    Difference Between INT and INT(5) data type
    Size of INT data type: An INT in MySQL is a 4-byte integer that can store values from -2,147,483,648 to 2,147,483,647 (for signed integers) or 0 to 4,294,967,295 (for unsigned integers).

    Difference between INT and INT(5): The number in parentheses, like in INT(5), is often misunderstood. It does not affect the storage size or range of values that can be stored. Here's what it actually means:

    1. Display width: INT(5) specifies a display width of 5 digits. This only affects how the data is displayed in some contexts, not how it's stored.
    2. Padding: When used with ZEROFILL, it determines how many zeros to pad on the left side of the number when displaying it.
    3. No constraint: It doesn't constrain the number of digits that can be stored. You can still store numbers larger than 5 digits in an INT(5) column.
    Example:

    CREATE TABLE example (
        a INT,
        b INT(5) ZEROFILL
    );

    INSERT INTO example VALUES (123, 123), (12345, 12345), (1234567, 1234567);

    SELECT * FROM example;

    a               b
    123         00123
    12345     12345
    1234567 1234567

    Key points:

    • Both 'a' and 'b' can store the same range of values.
    • 'b' will display with leading zeros if the number has fewer than 5 digits.
    • Numbers larger than 5 digits are displayed normally in 'b'.

    In modern MySQL usage, specifying a display width for integer data types is deprecated, and it's recommended to use INT without the parentheses unless you specifically need ZEROFILL functionality.

    Comments

    Popular posts from this blog

    Python OOPs Concepts: Using Variables and Methods

      Types of Variables in OOPs Python   Instance Variable Static Variable Local Variable   Object Level Variables Class Level Variables Method Level Variables When to use: For Every Object if you want Separate copy, use Instance Variables For all object one copy is required, use static variables Inside method, Just used for temporary requirement Where to Declare Inside the constructor method (in general) Within the class directly, outside of methods (in general)   Within the method only. How to Declare Within the constructor: Instance variables can be declared within the constructor method using the self .   Using default values : Instance variables can be assigned default values during initialization.   Outside the class: use object name.   ·          Within the class directly

    Is Li-Fi Better than Wi-Fi?

    Li-Fi  ( light fidelity )  is a bidirectional wireless system that transmit data to the devices like mobiles, laptop, etc., via infrared light or LED. The device has a receiver to pick up light signals and a transmitter to send light signal back to the lamp using infrared light or LED. It was first unveiled in 2011 and, unlike Wi-Fi, which uses radio frequency, Li-Fi technology only needs a light source with a chip to transmit an internet signal through light waves. Light fidelity (LiFi) is a faster, more secure and efficient wireless connection that uses light waves to transmit data Li-Fi technology still has a long way to go before worldwide adoption but every year, we are getting nearer to enjoying it for ourselves. The future surely looks bright with LiFi. How LiFi Works? LiFi makes use of visible light through overhead lighting for the transmission of data. This is possible through the use of a Visible Light Communications (VLC) system for data transmission. A VLC system has two q

    Polymorphism: Method Overloading vs Method Overriding

      Method Overloading In object-oriented programming languages, method overloading enables a class to have several methods with the same name but different parameters. However, in Python, method overloading is not directly supported as opposed to languages such as Java or C++. This is because Python allows developers to define default arguments for their methods and pass arguments of any type to a method. This flexibility allows a single method to handle various types of arguments, eliminating the need for overloading.   However, there is a way to simulate method overloading in Python by using default argument values or variable length arguments and conditional statements. Here's an example: Program using default arguments:       Program using variable length arguments:   Multiple methods with Same Name: When we define multiple methods with same name, Python will consider the last defined method only. Python will not support method overloading. ( Why? Method overlo