# Centron SQL Server Database Conventions This document outlines the standardized conventions for SQL Server database tables in the Centron system. These conventions ensure consistency across the database schema and enable efficient data management. ## 1. Table Structure ### Schema Convention - All database objects must be created in the `dbo` schema - Always reference tables and other objects using the `[dbo]` schema prefix - Example: `[dbo].[TableName]` ### Primary Key Convention Every table must include: - A primary key column named `I3D` (ID 3develop) - Defined as `int IDENTITY(1,1) NOT NULL` - Configured as a clustered primary key index Example: ```sql CREATE TABLE [dbo].[TableName]( [I3D] [int] IDENTITY(1,1) NOT NULL, -- other columns CONSTRAINT [PK_TableName] PRIMARY KEY CLUSTERED ( [I3D] ASC ) ) ``` ### Foreign Key Convention - Foreign key columns must end with the suffix `I3D` - The prefix before `I3D` identifies the referenced table - Example: `AccountI3D` references the `Account` table's `I3D` column ## 2. Standard Tracking Columns ### Creation Tracking Include in all tables: - `CreatedByI3D` [int] NOT NULL - References the Personal.I3D of the user who created the record - `CreatedDate` [datetime2(2)] NOT NULL - Timestamp when the record was created ### Modification Tracking Include in all tables: - `ChangedByI3D` [int] NOT NULL - References the Personal.I3D of the user who last modified the record - `ChangedDate` [datetime2(2)] NOT NULL - Timestamp when the record was last modified ### Deletion Tracking Implement soft delete pattern with: - `IsDeleted` [bit] NOT NULL - Flag indicating if the record is deleted (true) or active (false) - `DeletedByI3D` [int] NULL - References the Personal.I3D of the user who deleted the record - `DeletedDate` [datetime2(2)] NULL - Timestamp when the record was deleted ## 3. Data Types ### Text Fields - Use `nvarchar` instead of `varchar` to support Unicode characters - Specify appropriate length limits: - Short names/codes: `nvarchar(60)` to `nvarchar(256)` - Medium text: `nvarchar(512)` to `nvarchar(2000)` - Long text/descriptions: `nvarchar(max)` ### Date and Time - Use `datetime2(2)` for standard date/time fields - Use `datetime2(0)` for date/time fields without milliseconds ### Boolean Values - Use `bit` data type for boolean values ## 4. Naming Conventions ### Language Considerations - Historical tables and columns may use German names - All new tables and columns must use English names - Do not rename existing German table/column names to maintain compatibility ### Table Naming - Use PascalCase for table names - Use plural form for entity collection tables (e.g., `Accounts`, `Employees`) - Use singular form for lookup tables (e.g., `Status`, `Category`) ### Parent-Child Relationships - Name child tables to reflect their relationship to the parent - Example: `AccountDevices` and `AccountDeviceUris` ## 5. Indexing Guidelines ### Clustered Indexes - Each table must have exactly one clustered index on the primary key (`I3D`) ### Non-Clustered Indexes - Create non-clustered indexes on foreign key columns and frequently queried columns - Follow naming pattern: `IX_TableName_Column1_Column2` - Example: ```sql CREATE NONCLUSTERED INDEX [IX_AccountDevices_AccountI3D] ON [dbo].[AccountDevices]([AccountI3D] ASC) ``` ## 6. Example Tables ### Parent Table Example ```sql CREATE TABLE [dbo].[AccountDevices]( [I3D] [int] IDENTITY(1,1) NOT NULL, [AccountI3D] [int] NOT NULL, [ShortName] [nvarchar](256) NOT NULL, [DescriptionRTF] [nvarchar](max) NULL, [Description] [nvarchar](max) NOT NULL, [DeviceId] [nvarchar](256) NULL, [WarrantyExpiryDate] [datetime2](0) NULL, [CreatedDate] [datetime2](2) NOT NULL, [CreatedByI3D] [int] NOT NULL, [ChangedDate] [datetime2](2) NOT NULL, [ChangedByI3D] [int] NOT NULL, [SerialNumber] [nvarchar](60) NULL, [Model] [nvarchar](512) NOT NULL, [Manufacturer] [nvarchar](255) NOT NULL, [Location] [nvarchar](255) NOT NULL, [IsDeleted] [bit] NOT NULL, [DeletedDate] [datetime2](2) NULL, [DeletedByI3D] [int] NULL, [OriginKind] [int] NOT NULL, [BranchI3D] [int] NOT NULL, CONSTRAINT [PK_AccountDevices] PRIMARY KEY CLUSTERED ( [I3D] ASC ) ) ``` ### Child Table Example ```sql CREATE TABLE [dbo].[AccountDeviceUris]( [I3D] [int] IDENTITY(1,1) NOT NULL, [AccountDeviceI3D] [int] NOT NULL, [Kind] [int] NOT NULL, [Uri] [nvarchar](2000) NOT NULL, CONSTRAINT [PK_AccountDeviceUris] PRIMARY KEY CLUSTERED ( [I3D] ASC ) ) ```