Using the AUTO_INCREMENT keyword in SQL can realize field decrement, that is, when inserting a new record, the integer field specified as decrement will automatically increment. Notes include: Decrementing fields must be unique primary keys or unique indexes, cannot be used with DEFAULT or NOT NULL constraints, and can only be used in INSERT operations.
Syntax for field auto-decrement in SQL
In SQL, you can use AUTO_INCREMENT
Keyword to implement field auto-decrement.
Syntax
<code>CREATE TABLE table_name ( id INT(11) NOT NULL AUTO_INCREMENT, ... PRIMARY KEY (id) );</code>
How to use
AUTO_INCREMENT
keyword to mark a field as auto-incrementing. Example
Consider the following table:
<code>CREATE TABLE products ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY (id) );</code>
After inserting two records, the value of the "id" field will automatically increment to 1 And 2:
<code>INSERT INTO products (name) VALUES ('Product 1'); INSERT INTO products (name) VALUES ('Product 2');</code>
Notes
DEFAULT
or NOT NULL
constraints. The above is the detailed content of What syntax is used to decrement fields in sql. For more information, please follow other related articles on the PHP Chinese website!