sql 显示有关如何创建其ID随插入的每个新记录而增加的表的语法和注释。代码取自:http://www.w3schoo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 显示有关如何创建其ID随插入的每个新记录而增加的表的语法和注释。代码取自:http://www.w3schoo相关的知识,希望对你有一定的参考价值。

--Syntax for MySQL:
--Keyword: AUTO_INCREMENT

CREATE TABLE Persons
(
ID INT NOT NULL AUTO_INCREMENT,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255),
PRIMARY KEY (ID)
)

--The default starting value is 1, to make the autoincrement start with another value,use ALTER:

ALTER TABLE Persons AUTO_INCREMENT=100

--To insert a new record, we don't need to specify a value for the ID column:

INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')

------------------------------------------------------------------------------
--Syntax for SQL Server:
--Keyword: IDENTITY(starting_value,increment)

(
ID INT IDENTITY(1,1) PRIMARY KEY,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
)

--To insert a new value in the table, we don't need to specify a value for the ID column.

INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')

------------------------------------------------------------------------------

--Syntax for Access
--Keyword: AUTOINCREMENT

CREATE TABLE Persons
(
ID Integer PRIMARY KEY AUTOINCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

--The default starting value is 1. The default increment is 1 
--To change the default starting value and increment, we can use AUTOINCREMENT(10,5)
--To insert a new value in the table, we don't need to specify a value for the ID column.

INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')

---------------------------------------------------------------------------------
--Syntax for Oracle
--We have to create an auto-increment field with the sequence object (this object generates a number sequence).

CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10

--To insert a new record into the "Persons" table, we will have to use the nextval function (this function retrieves the next value from seq_person sequence):

INSERT INTO Persons (ID,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen')

以上是关于sql 显示有关如何创建其ID随插入的每个新记录而增加的表的语法和注释。代码取自:http://www.w3schoo的主要内容,如果未能解决你的问题,请参考以下文章

MySQL:如何在 SQL 查询中为每个结果插入一条记录?

如何在pl sql中跟踪异常终止

如何在 SQL Server 中使用子查询而不是视图

如何在sql中插入记录时返回id(id为自动增长)

SQL查询更新1000条记录

mysql 数据库创建表 id 字段的 sql 设置