2012-11-02 21 views

回答

2

unique約束創建一個表:

create table t (id integer primary key, 
       building text not null, 
       room text, 
       unique (building, room)); 

插入數據:

insert into t (building, room) values ("B1", "R1"); 
insert into t (building, room) values ("B1", "R2"); 
insert into t (building, room) values ("B1", null); 
insert into t (building, room) values ("B1", "R3"); 
insert into t (building, room) values ("B1", "R1"); 
-- Error: columns building, room are not unique 
insert into t (building, room) values ("B1", null); 
-- Note: This last insert does not violate the constraint! 

視圖中的數據:

select * from t; 
1|B1|R1 
2|B1|R2 
3|B1| 
4|B1|R3 
5|B1| 
+0

@詹姆斯,如果你想(building,NULL)也是唯一的,你可以將房間定義爲NOT NULL,當房間名稱不重要時,可以使用空字符串或0。 –

+0

@LawrenceBarsanti可以這樣做。但是要注意'null'不具有與'''或'0相同的語義。但只有詹姆斯知道他需要什麼。 – 2012-11-02 13:10:51

相關問題