Sql-Server

某些sql server表中沒有主鍵

  • January 6, 2019

我已將 WorldWideImporters 數據庫下載為標準數據庫(Microsoft Sample Database for SQL Server)。在這個數據庫中,一些表沒有主鍵,但可以看到聚集索引。誰能告訴我為什麼有些表在沒有 PK 的情況下工作?是什麼原因?

任何意見將不勝感激!

您可以執行以下查詢來查看沒有主鍵的表。

SELECT [schema] = SCHEMA_NAME(schema_id),
      name,
      temporal_type_desc
FROM   sys.tables
WHERE  object_id NOT IN (SELECT parent_object_id
                        FROM   sys.key_constraints
                        WHERE  type = 'PK');

這將返回以下內容

+-------------+------------------------------+--------------------+
|   schema    |             name             | temporal_type_desc |
+-------------+------------------------------+--------------------+
| Warehouse   | ColdRoomTemperatures_Archive | HISTORY_TABLE      |
| Warehouse   | Colors_Archive               | HISTORY_TABLE      |
| Warehouse   | PackageTypes_Archive         | HISTORY_TABLE      |
| Warehouse   | StockGroups_Archive          | HISTORY_TABLE      |
| Warehouse   | StockItems_Archive           | HISTORY_TABLE      |
| Application | Cities_Archive               | HISTORY_TABLE      |
| Application | Countries_Archive            | HISTORY_TABLE      |
| Application | DeliveryMethods_Archive      | HISTORY_TABLE      |
| Application | PaymentMethods_Archive       | HISTORY_TABLE      |
| Application | People_Archive               | HISTORY_TABLE      |
| Application | StateProvinces_Archive       | HISTORY_TABLE      |
| Application | TransactionTypes_Archive     | HISTORY_TABLE      |
| Purchasing  | SupplierCategories_Archive   | HISTORY_TABLE      |
| Purchasing  | Suppliers_Archive            | HISTORY_TABLE      |
| Sales       | BuyingGroups_Archive         | HISTORY_TABLE      |
| Sales       | CustomerCategories_Archive   | HISTORY_TABLE      |
| Sales       | Customers_Archive            | HISTORY_TABLE      |
+-------------+------------------------------+--------------------+

您將看到其中 100% 是時態表對的歷史表組件。

因此,根據文件,解釋非常簡單。

歷史表不能有約束(主鍵、外鍵、表或列約束)。

引用自:https://dba.stackexchange.com/questions/226396