如何在VIEW中拥有主键(不依赖于源表的键) [英] How can I have a primary key in a VIEW (a key that doesn't depend on source tables)

查看:381
本文介绍了如何在VIEW中拥有主键(不依赖于源表的键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从3个表中创建一个VIEW.每个表都有它的主键.但是,在VIEW中,为了拥有主键,我将不得不使用复合主键(3个表的主键的组合).

I'm creating a VIEW out of 3 TABLES. Each TABLE has it's primary key. However, in the VIEW in order to have a primary key, I'll have to use a composite primary key (combination of primary keys of 3 TABLES).

但是,我想在VIEW中添加一列作为仅用于VIEW的主键.作为VIEW定义的一部分,它应该是UNIQUE(自动递增,因为它主要是INT).我怎样才能做到这一点?

I would however like to add a column in the VIEW as a primary key that is created just for the purpose of the VIEW. As part of the VIEW definition, it should be UNIQUE(autoincrement since it would be mostly an INT). How can I achieve this?

我正在使用MySQL 5.1

I am using MySQL 5.1

推荐答案

您可以使用各种方法向视图数据插入唯一ID,例如:

you could use various methods to insert a unique ID to the view data, such as:

SELECT @rownum:=@rownum+1 as id, mytable.*
FROM (SELECT @rownum:=0) r, mytable;

但这不是不是主键,它不是一致的,并且会在数据更改时更改.

However this is not a primary key, it is not consistant and will change when the data changes.

您到底需要什么钥匙?

这篇关于如何在VIEW中拥有主键(不依赖于源表的键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆