Adding in a primary key to an SQL view(向 SQL 视图添加主键)
问题描述
我在 SQL Server 数据库中创建了一个视图,它只是两个表的连接.
I have created a view in a SQL Server database which is just a join of two tables.
有什么办法可以在这个视图的行中插入一个唯一的主键……或者我不确定如何将其中一个列名指定为主键……有什么想法吗?
Is there any way I can insert a unique primary key into the rows of this view ...or I'm not sure how I can specify one of the column names to be a primary key...any ideas?
谢谢
推荐答案
您必须创建物化(索引)视图才能添加唯一索引.但是你不能创建PK约束.
You would have to create materialized (indexed) view in order to be able to add unique index. But you can't create PK constraint.
CREATE VIEW v_test
WITH SCHEMABINDING --optional
AS
SELECT id from table
GO
CREATE UNIQUE CLUSTERED INDEX idx_id
ON v_test (id)
GO
这篇关于向 SQL 视图添加主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向 SQL 视图添加主键


基础教程推荐
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01