search
HomeDatabaseMysql TutorialMySQL&Oracle数据库创建自动递增字段实操

MySQL&Oracle数据库创建自动递增字段实操

Jun 07, 2016 pm 04:13 PM
ampmysqloraclecreatedatabaseautomaticIncrement

以下的文章教会你如何在MySQLOracle数据库中创建自动递增字段的实际操作方案,我们大家都知道Oracle(大型网站数据库平台)数据库初学者所必须知道的100个问题如以下所示,望你看完会有所收获。 Oracle(大型网站数据库平台) PL/SQL语言基础 Oracle(大型网站数

以下的文章教会你如何在MySQL&Oracle数据库中创建自动递增字段的实际操作方案,我们大家都知道Oracle(大型网站数据库平台)数据库初学者所必须知道的100个问题如以下所示,望你看完会有所收获。

Oracle(大型网站数据库平台) PL/SQL语言基础

Oracle(大型网站数据库平台)架构的基础知识(入门级)

简单的Oracle(大型网站数据库平台)存储过程的创建方法(菜

Oracle(大型网站数据库平台)中的各种数据类型详细的介绍

Oracle(大型网站数据库平台)触发器详细介绍

简单的Oracle(大型网站数据库平台)存储过程的创建方法

Oracle(大型网站数据库平台)的初学者入门心得

Oracle(大型网站数据库平台)的nvl函数的用法

Oracle(大型网站数据库平台)开发中序列的使用

如何在MySQL&Oracle(大型网站数据库平台)下创建自动递增字段

在MySQL(和PHP搭配之最佳组合)下创建自动递增字段:

create table article

先创建一个表。

(

id int Prima(最完善的虚拟主机管理系统)ry key auto_increment,

设置该字段为自动递增字段。

<ol class="dp-xml">
<li class="alt"><span><span>title varchar(255)  </span></span></li>
<li><span>);  </span></li>
<li class="alt"><span>insert into article values (null,'a');   </span></li>
</ol>

向数据库中插入数据。

select * from article; 结果如下:

<ol class="dp-xml">
<li class="alt"><span><span>Id  </span></span></li>
<li><span>Title   </span></li>
<li class="alt"><span>1  </span></li>
<li><span>a  </span></li>
<li class="alt"><span>insert into article values (null,’b’);  </span></li>
<li><span>insert into article values (null,'c');  </span></li>
<li class="alt"><span>insert into article (title) values ('d');  </span></li>
</ol>

select * from article; 结果如下:

<ol class="dp-xml">
<li class="alt"><span><span>Id  </span></span></li>
<li><span>Title   </span></li>
<li class="alt"><span>1  </span></li>
<li><span>a  </span></li>
<li class="alt"><span>2  </span></li>
<li><span>b  </span></li>
<li class="alt"><span>3   </span></li>
<li><span>c  </span></li>
<li class="alt"><span>4  </span></li>
<li><span>d </span></li>
</ol>

但是Oracle(大型网站数据库平台)没有这样的功能,但是通过触发器(trigger)和序列(sequence)可以实现。

假设关键字段为id,建一个序列,代码为:

<ol class="dp-xml">
<li class="alt"><span><span>create sequence seq_test_ids  </span></span></li>
<li><span>minvalue 1  </span></li>
<li class="alt"><span>maxvalue 99999999  </span></li>
<li><span>start with 1  </span></li>
<li class="alt"><span>increment by 1  </span></li>
<li><span>nocache  </span></li>
<li class="alt"><span>order;  </span></li>
<li>
<span class="comments"><!--[if !supportLineBreakNewLine]--></span><span> </span>
</li>
<li class="alt">
<span class="comments"><!--[endif]--></span><span> </span>
</li>
</ol>

建解发器代码为:

<ol class="dp-xml">
<li class="alt"><span><span>create or replace trigger tri_test_id  </span></span></li>
<li><span>before insert on test_table   </span></li>
<li class="alt"><span>for each row  </span></li>
<li><span>declare  </span></li>
<li class="alt"><span>nextid number;  </span></li>
<li><span>begin  </span></li>
<li class="alt">
<span>IF :new.id IS NULLor </span><span class="attribute">:new.id</span><span>=</span><span class="attribute-value">0</span><span> THEN  </span>
</li>
<li><span>select seq_test_id.nextval  </span></li>
<li class="alt"><span>into nextid  </span></li>
<li><span>from sys.dual;  </span></li>
<li class="alt">
<span class="attribute">:new.id:</span><span>=</span><span class="attribute-value">nextid</span><span>;  </span>
</li>
<li><span>end if;  </span></li>
<li class="alt"><span>end tri_test_id; </span></li>
</ol>

OK,上面的代码就可以实现自动递增的功能了。

以上的相关内容就是对在MySQL&Oracle创建自动递增字段的介绍,望你能有所收获。


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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

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 Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)