oracle unique約束,在這里拿出來(lái)和大家分享一下,希望對(duì)大家有用。如果某個(gè)約束只作用于單獨(dú)的字段,即可以在字段級(jí)定義約束,也可以在表級(jí)定義約束,但如果某個(gè)約束作用于多個(gè)字段,必須在表級(jí)定義約束
◆在定義約束時(shí)可以通過(guò)constraint關(guān)鍵字為約束命名,如果沒(méi)有指定,oracle將自動(dòng)為約束建立默認(rèn)的名稱(chēng)定義primary key約束(單個(gè)字段)
create table employees (empno number(5) primary key,...) 指定約束名
create table employees (empno number(5) constraint emp_pk primary key,...)
sql>
sql> create table purchase (
2 product_name varchar2(25),
3 quantity number(4,2),
4 purchase_date date,
5 salesperson varchar2(3)
6 );
table created.
sql>
sql> insert into purchase values ('a', 1, '14-jul-03', 'ca');
1 row created.
sql> insert into purchase values ('b', 75, '14-jul-03', 'bb');
1 row created.
sql> insert into purchase values ('c', 2, '14-jul-03', 'ga');
1 row created.
sql> insert into purchase values ('d', 8, '15-jul-03', 'ga');
1 row created.
sql> insert into purchase values ('a', 20, '15-jul-03', 'lb');
1 row created.
sql> insert into purchase values ('b', 2, '16-jul-03', 'ca');
1 row created.
sql> insert into purchase values ('c', 25, '16-jul-03', 'lb');
1 row created.
sql> insert into purchase values ('d', 2, '17-jul-03', 'bb');
1 row created.
sql>
sql> select unique product_name
2 from purchase
3 order by product_name;
product_name
-------------------------
a
b
c
d
4 rows selected.
sql>
sql> select distinct product_name
2 from purchase
3 order by product_name;
product_name
-------------------------
a
b
c
d
4 rows selected.
sql>
sql>
sql> drop table purchase;
table dropped.
sql>
oracle 主鍵和unique索引 有什么區(qū)別
主鍵約束:建立此約束后,對(duì)指定列進(jìn)行唯一約束,并且自動(dòng)生成一個(gè)聚集的索引。外表可以通過(guò)外鍵的形式連接該列。等等...
unique唯一約束:只是對(duì)指定列進(jìn)行唯一的約束。
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄