oracle must manually set the foreign key index

navicat for mysql index types 1. Normal 2. Unique 3. Full Text

A foreign key is a foreign key definition table column values must appear in another table.
Referring to the table is called the parent table (parent table), create a table called the child table (child table) foreign key. The foreign key in the child table associated with the primary key in the parent table.
General definition of the foreign key when creating a table

Under normal circumstances, the primary key of the table is necessary, there is no primary key table can be said not meet the design specifications of
a table can have only one PRIMARY KEY, KEY but can have multiple UNIQUE
Primary Key of (single notes, or multiple columns as the primary key) must NOT NULL (oracle mysql and apply, i.e., the primary key is provided as a plurality)

And a unique primary key constraint constraint (or a column), the Oracle will automatically add a unique constraint index in the corresponding column,
i.e. you add a primary key / unique constraint in keys, see directly under Indexes 
( for navicat for mysql not see the primary key column in the index has been set at the index, you do not see does not mean it does not exist).

But for the foreign key constraint, either mysql or oracle, are not automatically indexed (oracle-pl / sql in the indexes can not see, navicat for mysql column in the index can not see),
you must manually set to the next indexes index (for navicat for mysql will create good foreign keys when you save, you are prompted to add the necessary indexes for foreign keys, click OK to jump to the index settings tab,
add an index on it), in short, no matter mysql or oracle You must manually add foreign key index, which is common sense.
As our pm_templet_id foreign key table templet_id is a foreign key, be sure to add a unique index.

 

navicat for mysql only need to create a primary key index fields are listed in the Index Settings tab, set alone

 

The following is a pl / sql oracle keys is disposed in the indexes

keys of 3 primary keys Foreign Foreign key Primary Key Unique unique index 


There are three general index in the index Unique Normal Bitmap bitmap index unique index

Oracle establish increment primary key (sequence and trigger (trigger is applied to the trigger of a table) to be used for,)

首先,你要有一张表!
CREATE TABLE example(
ID Number(4) NOT NULL PRIMARY KEY,
NAME VARCHAR(25),
PHONE VARCHAR(10),
ADDRESS VARCHAR(50));
如果对于以上的建表语句还有疑问的话,建议您不要继续了!有那么些时间您还不如去看看金庸读读琼瑶!
然后,你需要一个自定义的sequence
CREATE SEQUENCE emp_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
NOCACHE -- 不建缓冲区
以上代码完成了一个序列(sequence)的建立过程,名称为emp_sequence,范围是从1开始到无限大(无限大的程度是由你机器决定的),nocycle 是决定不循环,如果你设置了最大值那么你可以用cycle 会使seq到最大之后循环.

对于nocache顺便说一下如果你给出了cache值那么系统将自动读取你的cache值大小个seq
,这样在反复操作时会加快运行速度,但如果遭遇意外情况如当机了或oracle死了,则下次取出的seq值将和上次的不连贯.(如果连不连贯无所谓建议用cache,因为时间就是金钱呀!跑题了!)


书接上文,你只有了表和序列还不够,还需要一个触发器来执行它!代码如下:
CREATE TRIGGER "触发器名称" BEFORE
INSERT ON example FOR EACH ROW WHEN (new.id is null)
begin
select emp_sequence.nextval into: new.id from dual;
end;
打完收工!下面你就试试插入数据吧!
INSERT INTO example(Name,phone,address) Values('Cao','56498543','Heibei');

发布了78 篇原创文章 · 获赞 12 · 访问量 12万+

Guess you like

Origin blog.csdn.net/qq_29883183/article/details/88655675