Oracle创建主键自增表(sql语句实现)及触发器应用
主键自增在插入数据的时候是很实用的,可以获取并操作返回的插入记录的ID,接下来介绍Oracle如何创建主键自增表,感兴趣的你可以了解下,就当是巩固知识,希望此文对你有所帮助
1、创建表代码如下:
createtableTest_Increase(
useridnumber(10)NOTNULLprimarykey,/*主键,自动增加*/
usernamevarchar2(20)
);
2、创建自动增长序列
代码如下:
CREATESEQUENCETestIncrease_Sequence
INCREMENTBY1--每次加几个
STARTWITH1--从1开始计数
NOMAXVALUE--不设置最大值,设置最大值:maxvalue9999
NOCYCLE--一直累加,不循环
CACHE10;
3、创建触发器
代码如下:
CREATETRIGGERTest_IncreaseBEFORE
insertONTest_IncreaseFOREACHROW/*对每一行都检测是否触发*/
begin
selectTestIncrease_Sequence.nextvalinto:New.useridfromdual;
end;
//*退出sqlplus行编辑*/
4、提交
代码如下:
commit;
5、测试
insertinto
代码如下:
Test_Increase(Username)values('test');

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)
