Database development MySQL constraints and table relationships

Keywords: MySQL

First, introduce

Constraints are optional parameters, just like the width of the data type.

Function: used to ensure data consistency and consistency.

It is mainly divided into:

PRIMARY KEY (PK) identifies that the partition is the PRIMARY KEY of the table and can uniquely identify records    
FOREIGN KEY (FK) identifies the FOREIGN KEY of the table    
NOT NULL identity the identity cannot be empty    
UNIQUE KEY (UK) identifies that the value of the subdivision is unique
 Auto increment identifies the value of the subdivision to grow automatically (overall type, and primary key)    
DEFAULT sets the value for the partition    

UNSIGNED   
ZEROFILL fills with 0

2, not null and default

Whether it is nullable, null means empty, non string not null non nullable null nullable
Default value. You can specify the default value when creating a column. If you do not set it actively when inserting data, the default value will be added automatically
create table tb1(
nid int not null defalut 2,
num int not null
)
Practice:
1. Create a table 1 that can insert null values:

 create table t1(id int);

 desc t1;

 insert into t1 values();

 select * from t1;

 mysql> create table t1(id int);
 Query OK, 0 rows affected (0.84 sec)

 mysql> desc t1;
 +-------+---------+------+-----+---------+-------+
 | Field | Type    | Null | Key | Default | Extra |
 +-------+---------+------+-----+---------+-------+
 | id    | int(11) | YES  |     | NULL    |       |
 +-------+---------+------+-----+---------+-------+
 1 row in set (0.13 sec)

 mysql> insert into t1 values();
 Query OK, 1 row affected (0.22 sec)

 mysql> select * from t1;
 +------+
 | id   |
 +------+
 | NULL |
 +------+
 1 row in set (0.00 sec)

2. Create a table 2 that cannot insert null values:

 create table t2(id int not null);

 desc t2;

 insert into t2 values();

The execution result is:

 mysql> create table t2(id int not null);
 Query OK, 0 rows affected (0.63 sec)

 mysql> desc t2;
 +-------+---------+------+-----+---------+-------+
 | Field | Type    | Null | Key | Default | Extra |
 +-------+---------+------+-----+---------+-------+
 | id    | int(11) | NO   |     | NULL    |       |
 +-------+---------+------+-----+---------+-------+
 1 row in set (0.00 sec)

 mysql> insert into t2 values();
 ERROR 1364 (HY000): Field 'id' doesn't have a default value

3. Create table 3 with default values:

create table t3(id int not null default 1);

desc t3;

insert into t3 values();
insert into t3 values(2);

select * from t3;

The execution result is:

mysql> create table t3(id int not null default 1);
Query OK, 0 rows affected (0.49 sec)

mysql> desc t3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   |     | 1       |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t3 values();
Query OK, 1 row affected (0.47 sec)

mysql> insert into t3 values(2);
Query OK, 1 row affected (0.16 sec)

mysql> select * from t3;
+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)

Three, unique

Set unique constraints, which cannot be repeated after setting.
Practice:

1. Create a unique id and student id table 4:

create table t4(
	id int unique,
	name varchar(20),
	number char(10),
	unique(number));

insert into t4 values(1, "Alex", "18023300");
insert into t4 values(2, "Coco", "18023300");
insert into t4 values(2, "Coco", "19205200");

select * from t4;

The execution result is:

 mysql> create table t4(
     -> id int unique,
     -> name varchar(20),
     -> number char(10),
     -> unique(number));
 Query OK, 0 rows affected (0.93 sec)

 mysql> insert into t4 values(1, "Alex", "18023300");
 Query OK, 1 row affected (0.20 sec)

 mysql> insert into t4 values(1, "Coco", "18023300");
 ERROR 1062 (23000): Duplicate entry '1' for key 'id'
 mysql> insert into t4 values(2, "Coco", "18023300");
 ERROR 1062 (23000): Duplicate entry '1' for key 'number '
 mysql> insert into t4 values(2, "Coco", "19205200");
 Query OK, 1 row affected (0.09 sec)

 mysql> select * from t4;
 +------+------+----------+
 | id   | name | number   |
 +------+------+----------+
 |    1 | Alex | 18023300 |
 |    2 | Coco | 19205200 |
 +------+------+----------+
 2 rows in set (0.01 sec)

If some fields are not empty and unique, that is, they are not null + unique, they are defined as primary keys:

 mysql> create table t5(id int not null unique);
 Query OK, 0 rows affected (0.54 sec)

 mysql> desc t5;
 +-------+---------+------+-----+---------+-------+
 | Field | Type    | Null | Key | Default | Extra |
 +-------+---------+------+-----+---------+-------+
 | id    |  Integer (11 ) |  no   | PRI |  NULL     |        
 +-------+---------+------+-----+---------+-------+
View column details Unlock all columns now
664 original articles published, praised 152, visited 230000+
His message board follow

Posted by runei on Wed, 12 Feb 2020 05:14:48 -0800