Table constraints and table design
1. Table constraints
1. Constraint Type
NOT NULL | Not-null constraint, specifying that a column cannot have NULL values |
---|---|
UNIQUE | Unique constraint, which ensures that each row of a column can only have a unique value |
PRIMARY KEY | Primary key constraint, a table can only create one primary key, but the primary key can be one column (one field) or multiple columns (multiple fields) Advantages: Quickly find specific data in the table |
FOREIGN KEY | Foreign key constraints, which correspond to primary keys, ensure referential integrity of data in one table matching values in another table |
CHECK | Check constraints to ensure that the values in the column meet the specified conditions |
AUTO_INCREMENT | Self-increment constraint, this field will increase the stored value by itself, the default value starts from 1, and increments by 1 each time. |
DEAFAULT | Default value constraint, which specifies the default value when no value is assigned to the column |
1.1 Not Null Constraint
create table table_name(id int not null, ,);
field name field type constraint...
1.2 Unique constraint (no repetition)
字段名 字段类型 unique;
There can be multiple unique constraints in a table
:
a unique constraint is set for a field, no non-null constraint is specified
1. Can this field insert a null value?
Can or can repeatedly insert null
null and 'null' are not the same
2. Can this field insert a null value?
You can
note that null and empty ('') and 'null' are completely different in MYSQL
View Unique Constraints
1.3 Primary key constraints
The primary key can be used to represent the representative credentials of a certain piece of data in a table.
Primary key characteristics:
1. Non-null and unique
2. The primary key can be composed of multiple fields or a single field
single field primary key
字段 字段类型 primary key;
Multiple fields or a single field
create table table_name(
id int,
name varchar(250),
primary key(id,name)
);
The primary key formed by the union of multiple fields, so if multiple fields are not repeated, the data can be inserted successfully.
Note:
Even if the primary key is a character type, null values cannot be inserted, which is different from the not null constraint.
1.4 Foreign key constraints
foreign key (字段名) references 主表 (列);
Primary table (with primary key) Secondary table (with foreign key, associated with primary key of primary table)
Foreign key can insert null and multiple nulls can be inserted, but if data is specified (specific value is specified), then this value must be It has to be effective!
1.5 Check Constraints
Guarantees that the values in the column meet the specified criteria
check(约束内容);
1.6 auto_increment
1.6.1 Notes
1. auto_increment must be used with Key, primary key, foreign key and unique are all acceptable
2. auto_increment self-increment constraint must be used with integer value
3. Multiple auto_increment is not allowed in a table
4. Manually specify self-increment
5. Manually Modify modify sub-value-added: (new)
1.6 insert… select…
2. Table Design
1. The three paradigms of database design
1.1 First Normal Form:
The columns of the table are guaranteed to be the smallest indivisible atomic value
1.1.1 The benefits of conforming to the first normal form:
- Reduced data redundancy
- Easier to maintain and update data
1.2 Second Normal Form:
All data in a table must be associated with the primary key
1.2.1 The benefits of conforming to the second normal form:
- Easy to maintain and modify
- good for display
1.3 Third normal form:
Make sure that each column in the table is directly related to the primary key and not indirectly related
1.3.1 The benefits of conforming to the third normal form:
- Implement decoupling of columns and non-primary keys. (one field does not affect other fields)
3. Table relationship
one-on-one
one-to-many
A class with multiple students
many-to-many
A student can take multiple courses
A course can also be selected by multiple students There
must be three tables, one of which is the middle table