I create a view with following codes
SELECT CONVERT(NVARCHAR, YEAR(okuma_tarihi)) + 'T1' AS sno, YEAR(okuma_tarihi) AS Yillar, SUM(toplam_kullanim_T1) AS TotalUsageValue, 'T1' AS UsageType FROM TblSayacOkumalari GROUP BY CONVERT(NVARCHAR, YEAR(okuma_tarihi)) + 'T1', YEAR(okuma_tarihi) UNION ALL SELECT CONVERT(NVARCHAR, YEAR(okuma_tarihi)) + 'T2' AS sno, YEAR(okuma_tarihi) AS Yillar, SUM(toplam_kullanim_T2) AS TotalUsageValue, 'T2' AS UsageType FROM TblSayacOkumalari GROUP BY CONVERT(NVARCHAR, YEAR(okuma_tarihi)) + 'T1', YEAR(okuma_tarihi) UNION ALL SELECT CONVERT(NVARCHAR, YEAR(okuma_tarihi)) + 'T3' AS sno, YEAR(okuma_tarihi) AS Yillar, SUM(toplam_kullanim_T3) AS TotalUsageValue, 'T3' AS UsageType FROM TblSayacOkumalari GROUP BY CONVERT(NVARCHAR, YEAR(okuma_tarihi)) + 'T1', YEAR(okuma_tarihi)
I want to define CONVERT(nvarchar, YEAR(okuma_tarihi)) + 'T1' AS sno
as a primary key is that possible? If is this possible how can I do?
You cannot create a primary key on a view. In SQL Server you can create an index on a view but that is different to creating a primary key.
You can specify only unique, primary key, and foreign key constraints on views. However, you can define the view using the WITH CHECK OPTION clause, which is equivalent to specifying a check constraint for the view. View constraints are supported only in DISABLE NOVALIDATE mode. You cannot specify any other mode.
Views in Postgresql can't have primary keys. You can specify only unique, primary key, and foreign key constraints on views, and they are supported only in DISABLE NOVALIDATE mode.
There is, however, a way to get a view added as a "read only entity" sans primary key if you use database-first Entity Framework. You even get a neat error when you add a view without an obvious key that says EF is forcing your entity to be a keyless read-only entity.
You cannot create a primary key on a view. In SQL Server you can create an index on a view but that is different to creating a primary key.
If you give us more information as to why you want a key on your view, perhaps we can help with that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With