sql数据库自动增量备份_SQL自动增量
sql数据库自动增量备份Sometimes we don’t have unique identifiers in the table to create a primary key. In this case, we can take help from SQL to have a special column for the primary key, whose values will be
sql数据库自动增量备份
Sometimes we don’t have unique identifiers in the table to create a primary key. In this case, we can take help from SQL to have a special column for the primary key, whose values will be automatically incremented when a new row is inserted.
有时我们在表中没有唯一的标识符来创建主键。 在这种情况下,我们可以从SQL寻求帮助,为主键设置一个特殊的列,当插入新行时,其值将自动递增。
SQL自动增量 (SQL Auto Increment)
SQL Auto-increment generates a unique incrementing number when a new row is inserted. Usually, the incrementing number is used for the primary key.
当插入新行时,SQL自动增量生成唯一的增量号。 通常,递增编号用于主键。
We will try to understand how the auto-increment feature is used with the below databases.
我们将尝试了解自动递增功能如何与以下数据库一起使用。
- MySQL MySQL
- SQL Server SQL服务器
- PostgreSQL PostgreSQL
MySQL自动增量 (MySQL Auto Increment)
Syntax:
句法:
CREATE TABLE table_name (
column1 int NOT NULL AUTO_INCREMENT,
column2 datatype,
columnN datatype
);
In the syntax above, the keyword AUTO_INCREMENT specifies that the corresponding column value has to be incremented by 1.
在上面的语法中,关键字AUTO_INCREMENT指定相应的列值必须增加1。
In MySQL, the value is incremented by default by 1 and starts from 1. If you want to start the value from some other number instead of 1, you can use the following command.
在MySQL中,默认情况下该值增加1,从1开始。如果要从其他数字而不是1开始该值,则可以使用以下命令。
ALTER TABLE table_name AUTO_INCREMENT = new_value;
The new_value will as the new start value for auto increment. Let us try to create a table with auto increment in MySQL.
new_value将作为自动增量的新起始值。 让我们尝试在MySQL中创建一个具有自动增量的表。
CREATE TABLE Student (
stdid int NOT NULL AUTO_INCREMENT,
stdFirstName varchar(255),
stdLastName varchar(255),
Age int,
PRIMARY KEY (stdid)
);
We are creating a Student table using the above query and the primary key is auto incremented using AUTO_INCREMENT keyword.
我们正在使用上述查询创建一个Student表,并且主键是使用AUTO_INCREMENT关键字自动递增的。
Post-execution of the above mentioned, the table properties will look as shown below.
执行完上述内容后,表属性将如下所示。
MySQL Auto Increment
MySQL自动增量
If you check the “Extra” column, for stdid, auto_increment is mentioned in Extra column.
如果检查“额外”列,则对于stdid,在“额外”列中会提到auto_increment。
用于自动增量SQL Server IDENTITY (SQL Server IDENTITY for Auto Increment)
Syntax:
句法:
CREATE TABLE table_name (
column1 int IDENTITY(1,1) PRIMARY KEY,
column2 datatype,
columnN datatype
);
In the syntax above, the keyword IDENTITY specifies that the corresponding column value has to be incremented.
在上面的语法中,关键字IDENTITY指定必须增加相应的列值。
In the above-mentioned example, the count will start from 1 and will be incremented by 1.
在上述示例中,计数将从1开始并增加1。
If we want to have the sequence started from 10 and increment it by 5, following will be the syntax for the same.
如果我们想让序列从10开始并增加5,则下面是相同的语法。
IDENTITY(10,5);
Let us try to create a table with auto increment in SQL Server.
让我们尝试在SQL Server中创建具有自动增量的表。
CREATE TABLE Student (
stdid int IDENTITY(1,1) PRIMARY KEY,
stdFirstName varchar(255),
stdLastName varchar(255),
Age int
);
Insert data into the Student table using the below-mentioned query.
使用下面提到的查询将数据插入到Student表中。
INSERT INTO [dbo].[Student]
([stdFirstName]
,[stdLastName]
,[Age]) VALUES ('First1','Last1',1);
When we insert the data, we are not inserting the value for “stdid” but the value will be auto provided by SQL Server as shown in the image below.
当我们插入数据时,我们不会插入“ stdid”的值,但是该值将由SQL Server自动提供,如下图所示。
SQLServer Auto Increment
SQLServer自动增量
PostgreSQL SERIAL自动增量 (PostgreSQL SERIAL for Auto Increment)
Syntax:
句法:
CREATE TABLE table_name (
column1 datatype SERIAL PRIMARY KEY,
column2 datatype,
columnN datatype
);
In the syntax above, we can see that the keyword SERIAL is used for specifying that the corresponding column will have auto-incremented values.
在上面的语法中,我们可以看到关键字SERIAL用于指定相应的列将具有自动递增的值。
Let us try to create a table with auto increment in PostgreSQL.
让我们尝试在PostgreSQL中创建一个具有自动增量的表。
CREATE TABLE Student (
stdid SERIAL PRIMARY KEY,
stdName varchar(255)
);
After executing the above-mentioned query, “stdid” will have the auto-increment feature.
执行完上述查询后,“ stdid”将具有自动递增功能。
结论 (Conclusion)
SQL Auto Increment is a very useful feature when we don’t have primary key columns in the table data. Notice that every database vendor has their own keyword to implement auto increment feature.
当我们在表数据中没有主键列时,SQL Auto Increment是一项非常有用的功能。 请注意,每个数据库供应商都有自己的关键字来实现自动递增功能。
sql数据库自动增量备份
更多推荐




所有评论(0)