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.
Here, StudentID is the primary key.CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(50), Age INT );
The Name column cannot be left empty when inserting or updating records.CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(50) NOT NULL, Email VARCHAR(100) );
The Username column must have unique values for all records.CREATE TABLE Users ( UserID INT PRIMARY KEY, Username VARCHAR(50) UNIQUE, Password VARCHAR(50) );
FileContent can store binary data like images or documents.CREATE TABLE Files ( FileID INT PRIMARY KEY, FileName VARCHAR(100), FileContent BINARY(1000) );
Quantity can only store values >= 0.CREATE TABLE Products ( ProductID INT PRIMARY KEY, Quantity INT UNSIGNED );
If OrderNumber is 123, it will be displayed as 000123.CREATE TABLE Orders ( OrderID INT PRIMARY KEY, OrderNumber INT(6) ZEROFILL );
CustomerID will automatically increment for each new record.CREATE TABLE Customers ( CustomerID INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(50), Email VARCHAR(100) );
Example:
The Area column is automatically calculated based on Width and Height.CREATE TABLE Rectangle ( ID INT PRIMARY KEY, Width INT, Height INT, Area INT GENERATED ALWAYS AS (Width * Height) );
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:
- 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.
- Padding: When used with ZEROFILL, it determines how many zeros to pad on the left side of the number when displaying it.
- 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.
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
Post a Comment