索引和主键的关系???

[复制链接]
查看11 | 回复9 | 2005-2-28 12:57:00 | 显示全部楼层 |阅读模式
请问在创建一个表的时候是不是创建了主键的同时也就创建了索引呀,它们有什么关系???
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
Oracle会自动为主键建立索引
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
看书上说一个表有了索引
存进去的数据是按索引的这个字段的大小顺序来存储的
我创建了 SQL> descxlg
Name
Null?Type
------------------------------- -------- ----
X
NOT NULL NUMBER(8)
Y
VARCHAR2(8)
这个表主键是x 我我按顺序插进去 四个数
SQL> insertinto xlgvalues(11,'oop');
1 row created.
SQL> insertinto xlgvalues(22,'ip');
1 row created.
SQL> insertinto xlgvalues(98,'jj');
1 row created.
SQL>insertinto xlgvalues(1,'hj');
但是我查询的时候
SQL> select* fromxlg;
X Y
---------- --------
11 oop
22 ip
98 jj
1 hj
查询出来的不是按 从小到大顺序出来的呀 是按我插进去的顺序出来的 请给我解释这方面的知识谢谢!!!
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
不是存进去的数据按顺序排列,而是索引按顺序排列
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
数据是按照先后存入的顺序存放的
实际上和物理位置有关
select rowid,x,y from xlg;
你可以看到rowid的顺序
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
你一定搞错啦,如果定义了主键,一定是排了序的,指的是索引数据与表数据。
但显示时却不进行排序,ORACLE不象SQL,主键表数据会排序后显示。
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
再请教大家一个问题:
我怎么知道一个表主键叫什么名字 以及包括哪些列呀
以及索引叫什么名字 是哪个列
当然在 DBAstudio 我是知道的 我现在想知道在sqlplus下怎么知道
谢谢大家对我的问题的 解答!!!
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
查:
dba_constraints
dba_cons_columns
dba_indexes
dba_ind_columns
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
primary key:
Oracle won’t allow two records to have the same primary key value.
Unique key:
Unique key constraints identify sets of columns that must be unique for each row in a table. Unique key constraints are similar to primary key constraints, and they often represent alternative primary key choices. The one difference between a unique key and a primary key is that columns in a unique key may be null.
(When you create a primary key or unique key constraint on a table, Oracle will create a unique index to enforce that constraint. The name of the index will match the name that you give the constraint.)
foreign key:
Foreign key constraints are used to link two tables that contain related information.They are most often used in parent-child relationships,
(Foreign keys must be linked to a corresponding primary or unique key.)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行