Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create view with primary key?

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?

like image 503
AliRıza Adıyahşi Avatar asked Jul 11 '12 17:07

AliRıza Adıyahşi


People also ask

Can we create a view with primary key?

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.

Can we create primary key on view in Oracle?

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.

Can a view have a primary key Postgres?

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.

Can you create a view without primary key?

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.


1 Answers

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.

like image 91
Kevin Aenmey Avatar answered Oct 05 '22 03:10

Kevin Aenmey