[mysql basic series four] field attributes

Field attributes are also called column attributes. There are 6 column attributes in mysql: null, default, comment, primary key, unique key, and auto_increment.

simple property

1. null: Whether the field can be empty.

2. default (default value): When the field allows default, if no data is inserted, it can be filled with the default value, usually null.

Field name field type default value -- basic use 1 (for definition)
insert into values ​​(value1,default,value2...); -- basically use 2 (for assignment)

3. comment (field description): comment function. Can only be viewed using a table create statement

show create table 表名;

primary key

The primary key, in a table, has one and only one field (or a combination of multiple fields) with unique values.

1. Create a primary key

Field name Field type primary key -- Syntax 1: When creating a table
primary key (list of field names) -- syntax 2: when creating a table
alter table table name add primary key (field name list); -- syntax 3: add field attributes

2. View the primary key

        Option 1: View the table structure.

        Option 2: View the creation statement of the table.

3. Delete the primary key

alter table 表名 drop primary key;

Note: After the primary key is deleted, its non-null properties remain.

4. Composite primary key

        Multiple fields work together to achieve data uniqueness.

5, the primary key constraint: can not be empty, can not have duplicates.

6. Primary key classification: business primary key (with practical significance), logical primary key (self-increasing)

Self Growth

auto_increment: After use, when the column data does not provide definite data, the system will automatically increase the data according to the existing data before filling in the data. Typically used for logical primary keys.

1. Principle: A set of data is maintained in the system to save the fields that currently use the auto-growth attribute, remember the current corresponding data value, and give a specified step size. When data is inserted, if no value is given, the system will add a step size to the original value to become new data. Once self-growth is triggered, an option will be automatically added to the table options to ensure that a table can only have at most one self-growth.

2. Basic syntax: field name field type auto_increment

3. View self-growth: Use the create statement to view the table.

4. Modify the self-growth step size: It is realized by modifying the table structure.

alter table table name auto_increment step size;

5. Delete auto-increment: that is, the auto_increment is no longer retained after the field attribute, that is, the field attribute can be modified.

alter table table name modify field name field type;

6. View the auto-increment initial variable

show variables like 'auto_increment%';

unique key

unique key, used to ensure data uniqueness. There can be multiple unique keys in a table. The field data is allowed to be null, and there can be multiple nulls.

1. The operation of the unique key is roughly the same as that of the primary key.

2. Delete the unique key

alter table table name drop index unique key name;

Note: When creating a unique key, the system will generate a unique key name for it, which is the same as the field name by default. Index represents an index, and a unique key name is essentially a type of index.

3. The composite unique key is the same as the composite primary key.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325437570&siteId=291194637