当前位置:网站首页>MySQL - 表字段的自增约束
MySQL - 表字段的自增约束
2022-07-18 01:03:00 【骆驼胡杨】
设置表字段值自动增加(AUTO_INCREMENT)
AUTO_INCREMENT是MySQL唯一扩展的完整性约束,当向数据库表中插入新记录时,字段上的值会自动生成唯一的ID。在具体设置AUTO_INCREMENT约束时,一个数据库表中只能有一个字段使用该约束,该字段的数据类型必须是整数类型。
由于设置AUTO_INCREMENT约束后的字段会生成唯一的ID,因此该字段也经常会同时设置成PK主键。
设置表中某字段值的自动增加约束非常简单,可以在MySQL数据库管理系统中通过SQL语句AUTO_INCREMENT来实现,其语法形式如下:
CREATE TABLE tablename(
propName propType AUTO_INCREMENT, ......
);
在上述语句中,tablename参数表示所要设置非空约束的字段名字,propName参数为属性名,propType为属性类型,propName字段要设置自动增加约束。默认情况下,字段propName的值从1开始增加,每增加一条记录,记录中该字段的值就会在前一条记录(或已存在的最大值(包括曾经存在的))的基础上加1。
mysql> create table class4(id int primary key AUTO_INCREMENT, name varchar(64), teacher varchar(64)); #创建表class4,设置id列为主键/自增长
mysql> desc class4; #查询表class4约束信息

注意: mysql8 中,AUTO_INCREMENT 必须设为键(主键、外键和唯一键均可)! 否则会报错!
边栏推荐
- 云想衣裳花想容,春风拂槛露华浓(浪漫代码实现)
- JS capture window closing event
- The first person to accept the prime theorem
- JS捕获窗口关闭事件
- Liquibase learning 1 - installation, simple use
- Advanced C language - custom type (1): struct
- [MCU simulation project] LED flashing light (Proteus schematic +keil code)
- Vscode下Latex 图片引用时 编译报错Recipe terminated with error. Retry building the project.
- Lecture 21 of project practice: platform commodity Library
- 2022/07 CSI TOOL 安装过程记录
猜你喜欢
![Codeforce:b. difference array [optimization of prefix 0]](/img/2d/b505b0a1c3b0926db8d50a27efe410.png)
Codeforce:b. difference array [optimization of prefix 0]

JVM personal learning notes

在线摇骰子色子工具

A few lines of code, let black and white old photos regain their vitality!

Hash table related knowledge

A powerful mock data generation tool

WPF effect article 192 treeview supports multiple selections
uni-app进阶之自定义【day13】

在线SQL转文本工具

ArrayList源码解析一
随机推荐
CoCon: A Self-Supervised Approach for Controlled Text Generation | ICLR 2021
Open3d library installation, CONDA common instructions, importing open3d times this error solving environment: failed with initial frozen solve Retrying w
A powerful mock data generation tool
Liquibase learning 2 - Introduction to the extension plug-in liquibase data
C语言力扣第34题之在排序数组中查找元素的第一个和最后一个位置。两种方法
Offline installation of MariaDB
Detailed explanation of the life cycle of arkui development framework components
Excel文件数据导入到MySQL数据库
codeforce:B. Difference Array【前缀0的优化】
解决MATLAB安装后出现 “License Manager Error -8”(亲测有效)
ArrayList源码解析一
初识convolution(卷积)+matlab对离散卷积过程的直观认识
Liquibase learning - problem solving: start error reporting
Reading notes: self cultivation of programmers - Chapter 2
【单片机仿真项目】广告灯(proteus原理图+keil代码)
WPF effect article 192 treeview supports multiple selections
Learning notes of JUC source code 2 - AQS sharing and semaphore, countdownlatch
Interference signal radiated from motor
Import excel file data into MySQL database
阿米的思考