当前位置:Gxlcms > mysql > oracleUNIQUE/DISTINCT用法

oracleUNIQUE/DISTINCT用法

时间:2021-07-01 10:21:17 帮助过:87人阅读

oracle unique约束,在这里拿出来和大家分享一下,希望对大家有用。如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但如果某个约束作用于多个字段,必须在表级定义约束

◆在定义约束时可以通过constraint关键字为约束命名,如果没有指定,oracle将自动为约束建立默认的名称定义primary key约束(单个字段)

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> 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索引 有什么区别
主键约束:建立此约束后,对指定列进行唯一约束,并且自动生成一个聚集的索引。外表可以通过外键的形式连接该列。等等...

unique唯一约束:只是对指定列进行唯一的约束。

人气教程排行