2017-04-04 82 views
0

可以创建具有不同列和列长度的2个或更多表的视图。我正在使用MS SQL。构造具有不同列和列长度的表的视图

例如,我有下面的表和列结构

USAAddress (door no, street char(30), city, zipcode (50)) 
GBPAddress (door no, addressline1 char(30), addressline2 char(30), 
addressline3 city, country, postcode char(30), phoneno char(30)) 

是否可以创建具有不同列和数据类型大小以上两代表的视图?

i) street would be mapped to addressline1 
ii) zipcode would be mapped to postcode (zipcode is char(50) while postcode is char(30)) 
iii)phoneno is missing in USAAddress table, while it is present in GBPAddress table. I view should have phoneno but should show as empty in USAAddress. 
+0

你试过了吗?对的,这是可能的。 –

回答

0

使用视图类似下面的查询应该做的伎俩:

select addressline1, postcode, phoneno 
from GBPAddress 

union all 

select street, left(zipcode,30), null 
from USAAddress 

一点题外话,它更常见的是使用VARCHAR对于其中长度变化长文本字段。而不是在数据的长度相当固定时使用CHAR。

相关问题