Column attributes: The constraints of data types are very simple, and some additional constraints are needed to ensure the validity of data.
:NULL/NOT NULL ,default,primary key ,unique key,auto_increment,comment
Empty attribute: Two values: NULL and NOT NULL Column Description: comment, description, no actual meaning, is specifically used to describe the field, will create statements according to the table to save: To give data managers an understanding Default values: Users can selectively use default values Default keywords: default create table my_default( name varchar(20) not null, age tinyint unsigned default 0, gender enum('male','female','confidential') default'male' )charset utf8; Field properties: Primary key, unique key and self-growth Primary key: primary key, the main key, a table can only have one field can use the corresponding key, used to uniquely constrain the data in the field, can not be repeated A table can have at most one primary key Add primary key: 1. When creating a table, the primary key is not allowed to be empty directly after the field, following the primary key keyword. Advantages: Very direct; Disadvantages: Only one field can be used as the primary key 2. When creating a table, after all the fields, use primary key (primary keyword list) to create the primary key (if there are multiple fields as the primary key, it can be a composite primary key). 3. Once the table has been created, add the primary key again: you can modify the table field properties, or you can add it directly. alter table table table name add primary (field list) alter table table name mod field primary key; Data in fields corresponding to primary keys is not allowed to be duplicated: once duplicated, data operations fail Update Primary Key - Delete Primary Key: Primary Key must be deleted before it can be updated alter table table table name drop primary key; Primary key classification: In the actual process of creating tables, real business data is seldom used as primary key fields (business primary keys, such as snowflakes, course numbers). Most of the time, logical fields are used (fields have no business meaning, and it doesn't matter what the value is). This field primary key is called logical primary key. Automatic growth: When the corresponding field is not given value, or NULL, the system will get a new field from the maximum value in the current field in the + 1 operation. Self-growth is usually matched with the primary key. View the variables corresponding to self-growth: show variables like'auto_incremrnt%'; Features: auto_increment 1. For any field to grow by itself, it must be an index (the key column has value). 2. Self-growing fields must be numbers (integers) 3. A table can only grow by itself at most. create table my_auto( id int primary key auto_increment comment'self-growth', name varchar(10) )charset utf8; Self-growth Use: Self-growth defaults to 1 for the first time, plus 1 for each time. If the corresponding field inputs a value, then self-growth fails, but the next time it can correctly self-growth (i.e. input value + 1) Modify self-growth: If field changes are involved, you must first delete the self-growth and then increase it. Modify the existing value of current self-growth: Modification can only be greater than the maximum value of current self-growth, not smaller. alter table table name auto_increment = value; Delete self-growth: alter table table name modify field type; Unique key: There are many fields in a table that need uniqueness and data cannot be duplicated, but a table can only have one primary key. The unique key can be solved Add unique key: 1. When creating a table, the field is followed directly by unique. 2. Add unique key (field list) after all fields -- compound unique key 3. Add unique keys after table creation Delete the unique key: alter table table table name drop index index index name; - - unique key is indexed by field name by default Unique key constraints: Like primary keys, the only difference is that unique keys are allowed to be null and that multiple keys are null.
-- Create table
create table my_teacher(
name varchar(20) not null comment 'Full name',
money decimal(10,2) not null comment 'wages'
)charset utf8;
-- Default value
create table my_default(
name varchar(20) not null,
age tinyint unsigned default 0,
gender enum('male','female','secrecy') default 'male'
)charset utf8;
--insert data
insert into my_default (name) values ('Marr');
insert into my_default values ('Arry',default,default);
-- Add primary key
create table my_pril(
name varchar(20) not null comment 'Full name',
number char(10) primary key comment 'Student ID:0000,Can not repeat'
)charset utf8;
-- composite primary key
create table my_pri2(
number char(10) comment 'Student ID',
course char(10) comment 'Course code',
score tinyint unsigned default 60 comment 'achievement',
-- Increase primary key restriction:Student number and course number should correspond.,Uniqueness
primary key (number,course)
)charset utf8;
-- Supplemental primary key
create table my_pri3(
course char(10) not null comment 'Course number',
name varchar(10) not null comment 'Course name'
)charset utf8;
alter table my_pri3 modify course char(10) primary key;
alter table my_pri3 add primary key(course) ;
-- drop primary key
alter table my_pri3 drop primary key;
-- Self growth
create table my_auto(
id int primary key auto_increment comment 'Self growth',
name varchar(10)
)charset utf8;
-- Triggering self-growth
insert into my_auto(name) values('Han');
insert into my_auto values(null,'Mary');
insert into my_auto values(default,'Grr');
insert into my_auto values(6,'Sarry');
insert into my_auto values(null,'Kaat');
-- Modify self-growth
alter table my_auto auto_increment = 8;
-- View self-growth variables
show variables like 'auto_increment%';
-- Delete self-growth
alter table my_auto modify id int;
-- Unique key
create table my_unique1(
number char(10) unique comment 'Learn:Unique key',
name varchar(20) not null
)charset utf8;
------------
create table my_unique2(
number char(10) unique comment 'Learn:Unique key',
name varchar(20) not null,
unique key(number)
)charset utf8;
------------ Added unique key
create table my_unique3(
id int primary key auto_increment,
number char(10) not null,
name varchar(20) not null
)charset utf8;
alter table my_unique3 add unique key (number);
-- Delete the unique key
alter table my_unique3 drop index number;