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.   · ...

    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...

    Practical 1: Getting Started with MYSQL

     Getting Started with MySQL Introduction to MySQL Definition: MySQL is an open-source relational database management system (RDBMS) Uses: Web applications, data warehousing, e-commerce, logging applications Key features: Speed, reliability, scalability, and ease of use Installing MySQL Download MySQL Community Server from official website Follow installation wizard for your operating system Set root password during installation Verify installation: mysql --version MySQL Command-line Client Accessing MySQL: mysql -u root -p Basic commands: SHOW DATABASES ; CREATE DATABASE mydb ; USE mydb ; SHOW TABLES ; MySQL Workbench Introduction: Visual tool for database design and management Key features: SQL development Data modeling Server administration Example: Creating a new connection New Connection > Enter details (hostname, username, password) PHPMyAdmin Web-based MySQL administration tool Often comes pre-installed with web hosting packag...