1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > mysql主键约束(primary key)

mysql主键约束(primary key)

时间:2021-06-17 04:48:04

相关推荐

mysql主键约束(primary key)

primary key:

1.给某个字段添加主键约束之后,该字段既不能为0,也不能重复,效果和not null unique 相同,但本质不同。其除了可以做到not null unique之外,主键字段还默认添加索引-index。

2.一张表应该有主键字段,如果没有,则表示该张表是无效的。主键值是当前行数据的唯一标识,是当前行数据的身份证号。即使表中的两行数据是完全相同的,但只要主键值不同,即可认为是完全不同的数据。

3.给一个字段添加一个主键约束,称为单一主键;给多个字段联合添加一个主键约束,称为复合主键,无论是单一还是复合,一张标只能有一个主键约束.

4.列级主键约束:

mysql> drop table if exists t_user;

Query OK, 0 rows affected (0.00 sec)

mysql> create table t_user(

->id int(10) primary key,

-> name varchar(32));

Query OK, 0 rows affected (0.01 sec)

mysql> desc t_user;

±------±------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±------------±-----±----±--------±------+

| id | int(10) | NO | PRI | NULL | |

| name | varchar(32) | YES | | NULL | |

±------±------------±-----±----±--------±------+

2 rows in set (0.00 sec)

表级主键约束:

mysql> drop table if exists t_user;

Query OK, 0 rows affected (0.01 sec)

mysql> create table t_user(

-> id int(10),

-> name varchar(32),

-> primary key (id));

Query OK, 0 rows affected (0.01 sec)

mysql> desc t_user;

±------±------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±------------±-----±----±--------±------+

| id | int(10) | NO | PRI | NULL | |

| name | varchar(32) | YES | | NULL | |

±------±------------±-----±----±--------±------+

2 rows in set (0.00 sec)

给约束起名:

mysql> create table t_user(

-> id int(10),

-> name varchar(32),

-> constraint t_user_id_pk primary key(id));

Query OK, 0 rows affected (0.02 sec)

mysql> desc t_user;

±------±------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±------------±-----±----±--------±------+

| id | int(10) | NO | PRI | NULL | |

| name | varchar(32) | YES | | NULL | |

±------±------------±-----±----±--------±------+

2 rows in set (0.00 sec)

5.复合主键:(大于2个字段一起作为“1个”主键,即作为主键的字段同时相同时才会认为相同)

mysql> drop table if exists t_user;

Query OK, 0 rows affected (0.00 sec)

mysql> create table t_user(

-> id int(10),

-> name varchar(32),

-> primary key(id ,name));

Query OK, 0 rows affected (0.02 sec)

mysql> desc t_user;

±------±------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±------------±-----±----±--------±------+

| id | int(10) | NO | PRI | NULL | |

| name | varchar(32) | NO | PRI | NULL | |

±------±------------±-----±----±--------±------+

2 rows in set (0.00 sec)

mysql> drop table if exists t_user;

Query OK, 0 rows affected (0.01 sec)

6.给复合主键起名:

mysql> create table t_user(

-> id int(10),

-> name varchar(128),

-> constraint t_user_id_name_pk primary key(id ,name));

Query OK, 0 rows affected (0.02 sec)

mysql> desc t_user;

±------±-------------±-----±----±--------±------+

| Field | Type | Null | Key | Default | Extra |

±------±-------------±-----±----±--------±------+

| id | int(10) | NO | PRI | NULL | |

| name | varchar(128) | NO | PRI | NULL | |

±------±-------------±-----±----±--------±------+

2 rows in set (0.00 sec)

在mysql数据管理系统中提供了一个自增的数字,专门用来生成主键值,主键值不需要用户维护,也不需要用户提供,自动增加,这个值默认从1开始,以1增加。

eg:

mysql> create table t_user(

-> id int(10) primary key auto_increment,

-> name varchar(32));

Query OK, 0 rows affected (0.02 sec)

mysql> insert into t_user(name) values(‘jack’);

Query OK, 1 row affected (0.01 sec)

mysql> insert into t_user(name) values(‘jack’);

Query OK, 1 row affected (0.01 sec)

mysql> insert into t_user(name) values(‘jack’);

Query OK, 1 row affected (0.00 sec)

mysql> insert into t_user(name) values(‘jack’);

Query OK, 1 row affected (0.00 sec)

mysql> insert into t_user(name) values(‘jack’);

Query OK, 1 row affected (0.00 sec)

mysql> insert into t_user(name) values(‘jack’);

Query OK, 1 row affected (0.00 sec)

mysql> desc t_user;

±------±------------±-----±----±--------±---------------+

| Field | Type | Null | Key | Default | Extra |

±------±------------±-----±----±--------±---------------+

| id | int(10) | NO | PRI | NULL | auto_increment |

| name | varchar(32) | YES | | NULL | |

±------±------------±-----±----±--------±---------------+

2 rows in set (0.00 sec)

mysql> select * from t_user;

±—±-----+

| id | name |

±—±-----+

| 1 | jack |

| 2 | jack |

| 3 | jack |

| 4 | jack |

| 5 | jack |

| 6 | jack |

±—±-----+

6 rows in set (0.01 sec)

mysql> delete from t_user where id=3;

Query OK, 1 row affected (0.00 sec)

mysql> desc t_user;

±------±------------±-----±----±--------±---------------+

| Field | Type | Null | Key | Default | Extra |

±------±------------±-----±----±--------±---------------+

| id | int(10) | NO | PRI | NULL | auto_increment |

| name | varchar(32) | YES | | NULL | |

±------±------------±-----±----±--------±---------------+

2 rows in set (0.00 sec)

mysql> select * from t_user;

±—±-----+

| id | name |

±—±-----+

| 1 | jack |

| 2 | jack |

| 4 | jack |

| 5 | jack |

| 6 | jack |

±—±-----+

5 rows in set (0.00 sec)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。