search
HomeDatabaseMysql Tutorialoracle创建表并添加主键,设置主键自增长

oracle创建表并添加主键,设置主键自增长

Jun 07, 2016 pm 02:51 PM
oracleprimary keycreateincreasesequenceAdd toset up

oracle序列详解和创建自增主键 Oracle序列主键 序列: 是oacle提供的用于产生一系列唯一数字的数据库对象。 l 自动提供唯一的数值 l 共享对象 l 主要用于提供主键值 l 将序列值装入内存可以提高访问效率 1.首先创建序列,Oracle序列的语法格式为: CREATE SEQ

oracle序列详解和创建自增主键

Oracle序列主键
序列: 是oacle提供的用于产生一系列唯一数字的数据库对象。
l 自动提供唯一的数值
l 共享对象
l 主要用于提供主键值
l 将序列值装入内存可以提高访问效率

1.首先创建序列,Oracle序列的语法格式为:

<code class=" hljs css">       <span class="hljs-tag">CREATE</span> <span class="hljs-tag">SEQUENCE</span> 序列名
         <span class="hljs-attr_selector">[INCREMENT BY n]</span>
         <span class="hljs-attr_selector">[START WITH n]</span>
         <span class="hljs-attr_selector">[{MAXVALUE/ MINVALUEn|NOMAXVALUE}]</span>
         <span class="hljs-attr_selector">[{CYCLE|NOCYCLE}]</span>
         <span class="hljs-attr_selector">[{CACHE n|NOCACHE}]</span>;</code>

1)INCREMENT BY用于定义序列的步长,如果省略,则默认为1,
如果出现负值,则代表Oracle序列的值是按照此步长递减的。
2)START WITH 定义序列的初始值(即产生的第一个值),默认为1。
3)MAXVALUE 定义序列生成器能产生的最大值。选项NOMAXVALUE是默认选项,代表没有最大值定义,
这时对于递增Oracle序列,系统能够产生的最大值是10的27次方;对于递减序列,最大值是-1。
4)MINVALUE定义序列生成器能产生的最小值。选项NOMAXVALUE是默认选项,
代表没有最小值定义,这时对于递减序列,系统能够产生的最小值是?10的26次方;对于递增序列,最小值是1。
5)CYCLE 和NOCYCLE 表示当序列生成器的值达到限制值后是否循环。CYCLE代表循环,NOCYCLE代表不循环。 如果循环,则当递增序列达到最大值时,循环到最小值;对于递减序列达到最小值时,循环到最大值。如果不循环,达到限制值后,继续产生新值就会发生错误。
6)CACHE(缓冲)定义存放序列的内存块的大小,默认为20。NOCACHE表示不对序列进行内存缓冲。
对序列进行内存缓冲,可以改善序列的性能。

2.删除Oracle序列的语法是DROP SEQUENCE 序列名;

使用序列会产生裂缝
l 序列在下列情况下出现裂缝:
? 回滚
? 系统异常

实例应用:

1。先sqlplus登录了:
SQL> conn scott/tiger;
2。建表:
drop table users ;

<code>   create table users(
       id number(6) ,
      name varchar2(30),
      constraint pk_id primary key(id)
   );
</code>

3。建序列:
create sequence aq1
start with 1
increment by 1
minvalue 1
maxvalue 9999999
nocycle
nocache
noorder;
/或者
drop sequence sq1;
create sequence sq1;
4。建触发器:

create or replace trigger pn_trigger
before insert on users
for each row
begin
select sq1.nextval into:new.id from sys.dual;
end;

5。好了,insert一条记录测试一下喽。。。。

insert into users( name) values(‘zhsan’);
select * from users;

创建触发器

<code class=" hljs sql"><span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">replace</span> <span class="hljs-keyword">trigger</span> 触发器名
<span class="hljs-keyword">before</span> <span class="hljs-keyword">insert</span> <span class="hljs-keyword">on</span> 表名
<span class="hljs-keyword">for</span> <span class="hljs-keyword">each</span> <span class="hljs-keyword">row</span>
<span class="hljs-keyword">begin</span>
    <span class="hljs-keyword">select</span> 序列名.nextval <span class="hljs-keyword">into</span>:new.主键 <span class="hljs-keyword">from</span> dual;</span>
<span class="hljs-operator"><span class="hljs-keyword">end</span> 触发器名;</span></code>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
BLOB Data Type in MySQL: A Detailed Overview for DevelopersBLOB Data Type in MySQL: A Detailed Overview for DevelopersMay 07, 2025 pm 05:41 PM

BlobdatatypesinmysqlareusedforvoringLargebinarydatalikeImagesoraudio.1) Useblobtypes (tinyblobtolongblob) Basedondatasizeneeds. 2) Storeblobsin Perplate Petooptimize Performance.3) ConsidersxterNal Storage Forel Blob Romana DatabasesizerIndimprovebackupupe

How to Add Users to MySQL from the Command LineHow to Add Users to MySQL from the Command LineMay 07, 2025 pm 05:01 PM

ToadduserstoMySQLfromthecommandline,loginasroot,thenuseCREATEUSER'username'@'host'IDENTIFIEDBY'password';tocreateanewuser.GrantpermissionswithGRANTALLPRIVILEGESONdatabase.*TO'username'@'host';anduseFLUSHPRIVILEGES;toapplychanges.Alwaysusestrongpasswo

What Are the Different String Data Types in MySQL? A Detailed OverviewWhat Are the Different String Data Types in MySQL? A Detailed OverviewMay 07, 2025 pm 03:33 PM

MySQLofferseightstringdatatypes:CHAR,VARCHAR,BINARY,VARBINARY,BLOB,TEXT,ENUM,andSET.1)CHARisfixed-length,idealforconsistentdatalikecountrycodes.2)VARCHARisvariable-length,efficientforvaryingdatalikenames.3)BINARYandVARBINARYstorebinarydata,similartoC

The Ultimate Guide to Adding Users in MySQLThe Ultimate Guide to Adding Users in MySQLMay 07, 2025 pm 03:29 PM

ToaddauserinMySQL,usetheCREATEUSERstatement.1)UseCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';tocreateauser.2)Enforcestrongpasswordpolicieswithvalidate_passwordpluginsettings.3)GrantspecificprivilegesusingGRANTstatement.4)Forremoteaccess,use

What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.