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

    ORACLE Express Edition: Getting Started

    1. Introduction to Oracle Database 21c Express Edition (XE) - Free, lightweight version of Oracle Database - Ideal for learning and small-scale applications - Limited to 12GB of user data and uses up to 2GB of RAM 2. Installation and Setup 2.1 Installing Oracle 21c XE 1. Download Oracle 21c XE from: https://www.oracle.com/database/technologies/xe-downloads.html 2. Run the installer:    - Windows: Double-click the .exe file    - Linux: Use `rpm` or `yum` command 3. Follow the installation wizard:  Accept the license agreement Choose an installation location (default is usually fine) Set a password for the SYS, SYSTEM, and PDBADMIN accounts (write this down!) Select the option to start the database service automatically (recommended)  4. Complete the installation: Wait for the installation process to finish Note down the database connection details provided at the end The default container database (CDB) name is XE The default pluggable database (PDB) nam...

    Types of DBMS

     DBMS Types Hierarchical DBMS: Structure: Data organized in a tree-like structure with parent-child relationships. Example: IBM's Information Management System (IMS) Use case: Organizational charts, file systems Imagine a family tree: Grandparent Parent 1 Child 1 Child 2 Parent 2 Child 3 Network DBMS: Structure: Data organized in a graph-like structure allowing many-to-many relationships. Example: Integrated Data Store (IDS) Use case: Complex relationship modeling Think of a social network: Person A is friends with Person B and Person C Person B is friends with Person A and Person D Person C is friends with Person A and Person D Relational DBMS: Structure: Data organized in tables with rows and columns, using relationships between tables. Examples: MySQL, Oracle, PostgreSQL Use case: Most common type, used in various applications Imagine a library system: Books table: (ID, Title, Author, ISBN) Members table: (ID, Name, Address, Phone) Loans table...