An Azure relational database service.
The published ledger documentation does not explicitly guarantee that PRIMARY KEY, UNIQUE, FOREIGN KEY, or CHECK constraints are supported on append-only ledger tables, so this design should not treat the generic Azure SQL constraint documentation as a ledger-specific contract without obtaining confirmation from Microsoft Support.
Constraints on append-only ledger tables
The documentation establishes these separate facts:
- An append-only ledger table permits
INSERT, but notUPDATEorDELETE; it cannot later be converted to a non-ledger table.^1^ - Azure SQL Database ordinarily enforces:
- The normal Azure SQL
ALTER TABLEgrammar supportsPRIMARY KEY,UNIQUE,FOREIGN KEY, andCHECK; primary and unique constraints generate indexes, while foreign keys do not automatically create indexes.^5^
However, none of those pages explicitly says that all these constraints are valid on a table created with:
WITH (LEDGER = ON (APPEND_ONLY = ON))
The ledger limitations page documents supported schema changes such as adding nullable columns, restricted column alterations, and soft deletion of ledger columns/tables, but the supplied text does not address adding, dropping, disabling, or enforcing relational constraints.^6^
Consequently, the following remain undocumented in the available official material:
- whether an append-only ledger table can be both sides of an FK;
- whether a self-referencing FK is accepted;
- whether constraints can be added after ledger-table creation;
- whether every normal index type is supported;
- whether a constraint may be disabled and re-enabled.
A deployment-time test can identify current engine behavior, but it is not a substitute for a documented compatibility guarantee:
CREATE TABLE dbo.LedgerEntry
(
EntryId bigint NOT NULL,
PreviousId bigint NULL,
Amount decimal(19,4) NOT NULL,
CONSTRAINT PK_LedgerEntry PRIMARY KEY (EntryId),
CONSTRAINT UQ_LedgerEntry_Previous UNIQUE (PreviousId),
CONSTRAINT CK_LedgerEntry_Amount CHECK (Amount >= 0),
CONSTRAINT FK_LedgerEntry_Previous
FOREIGN KEY (PreviousId) REFERENCES dbo.LedgerEntry(EntryId)
)
WITH (LEDGER = ON (APPEND_ONLY = ON));
Run this against the intended Azure SQL Database configuration and open a Microsoft support request for a ledger-specific contractual answer before making these constraints part of the audit security model.
Dropping or changing constraints
For an ordinary Azure SQL table, ALTER TABLE ... DROP CONSTRAINT removes a named constraint, and ALTER TABLE supports adding, dropping, enabling, and disabling constraints.^7^ Creating or modifying a foreign key requires ALTER permission on the table; creating a unique constraint likewise requires ALTER.^8^
Therefore, a runtime principal granted only object-level SELECT and INSERT, with no ownership, role-derived DDL rights, ALTER, or broader implied permission, cannot use ALTER TABLE ... DROP CONSTRAINT. Check indirect grants and role membership as well: database CONTROL implies ALTER, and broader permissions can imply database-level rights.^9^
Ledger immutability does not mean that all schema objects are indestructible. Ledger tables themselves can be dropped using soft-delete semantics: the table and dependent ledger objects remain physically represented for verification but are renamed and marked as dropped.^10^
Concurrent conflicting inserts
If the constraints are accepted and enforced on the ledger table, SQL transaction consistency requires completed transactions to satisfy integrity rules, and an unsuccessful transaction is rolled back.^11^ Thus two transactions cannot both commit duplicate values protected by an enforced primary key or unique constraint.
For a foreign key, the outcome depends on the operations:
- An insert whose referenced key does not exist cannot commit under an enforced FK.
- Concurrent insertion of a parent and child is sensitive to transaction ordering and isolation; do not assume that an uncommitted parent in another transaction satisfies the child’s FK.
- A self-referencing row can reference itself only if the engine’s statement-level FK validation accepts the row being inserted; this exact ledger case is not documented in the supplied sources.
Applications should handle constraint failures and deadlocks as transaction failures rather than depend on one particular error code or which session loses.
Primary versus readable-secondary reads
Connections that require immediate cross-session visibility should use the primary:
Server=tcp:<server>.database.windows.net;
Database=<database>;
ApplicationIntent=ReadWrite;
Encrypt=True;
ReadWrite is the default and routes to the read-write replica; ReadOnly routes to a readable replica when read scale-out applies. Read-only replicas are transactionally consistent within a session, but asynchronous relative to the primary and can lag without a fixed upper bound. Microsoft explicitly directs workloads requiring guaranteed cross-session consistency or immediate visibility to the primary.^12^
Verify the current connection with:
SELECT DATABASEPROPERTYEX(DB_NAME(), 'Updateability') AS Updateability;
For an absolute service-configuration guarantee regardless of connection-string intent, disable read scale-out. Otherwise, omit ApplicationIntent or set it explicitly to ReadWrite and reject a connection if the query returns READ_ONLY.^12^
Database and logical-server deletion
These are two different authorization planes:
- T-SQL database deletion: In Azure SQL Database, only the provisioning server-level principal or members of the
dbmanagerrole can executeDROP DATABASE.^13^ - Azure SQL logical database management: Members of
##MS_DatabaseManager##can create and delete databases.^14^ - Azure resource deletion: Azure RBAC roles are independent of SQL permissions. SQL DB Contributor can delete databases but cannot manage the parent logical server; SQL Server Contributor can manage SQL servers and databases.^15^ Resource owners and custom roles may also possess the relevant management-plane delete action.
Deleting a logical server deletes its associated databases. If logical-server soft-delete retention is not configured, deletion is permanent with no server recovery option; server restore through soft delete is currently documented as preview.^16^
Finally, Azure SQL Database point-in-time restore does not overwrite the source. It always creates a new database on the same logical server; the restored database can subsequently replace the original through rename/delete operations.^17^
References
- Append-only ledger tables
- Primary and foreign key constraints
- Create unique constraints
- Unique constraints and check constraints
- ALTER TABLE table_constraint (Transact-SQL)
- Ledger considerations and limitations
- ALTER TABLE (Transact-SQL)
- Modify Foreign Key Relationships
- REVOKE Database Permissions (Transact-SQL)
- DROP TABLE (Transact-SQL)
- Transaction locking and row versioning guide
- Use read-only replicas to offload read-only query workloads
- DROP DATABASE (Transact-SQL)
- Azure SQL Database server roles for permission management
- Azure built-in roles for Databases
- Restore a deleted logical server in Azure SQL Database (preview)
- Restore a database from a backup in Azure SQL Database