USE [warehouse] GO /****** Object: Table [dbo].[库存] Script Date: 2023/5/30 9:34:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[库存]( [货物编号] [char](5) NOT NULL, [仓库编号] [char](5) NOT NULL, [库存量] [int] NULL, PRIMARY KEY CLUSTERED ( [货物编号] ASC, [仓库编号] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[库存] ADD DEFAULT ((0)) FOR [库存量] GO ALTER TABLE [dbo].[库存] WITH CHECK ADD FOREIGN KEY([仓库编号]) REFERENCES [dbo].[仓库] ([仓库编号]) GO 如何分辨这个表中的主键和外键
时间: 2023-06-30 15:19:26 浏览: 119
Python库 | fio_sale_data_warehouse-4.0.0.3.tar.gz
这个表中的主键是由货物编号和仓库编号组成的联合主键,即 PRIMARY KEY CLUSTERED ( [货物编号] ASC, [仓库编号] ASC )。而外键是仓库编号,它引用了仓库表中的仓库编号字段,通过以下代码进行了添加约束:
```
ALTER TABLE [dbo].[库存] WITH CHECK ADD FOREIGN KEY([仓库编号]) REFERENCES [dbo].[仓库] ([仓库编号])
```
其中,WITH CHECK 用于指定在添加外键约束时检查是否所有的行都满足约束条件,如果不满足则不允许添加该约束。这样可以保证数据的完整性和一致性。
阅读全文