您好,欢迎访问一九零五行业门户网

mysql设置主键有什么用

mysql设置主键的作用是:1、唯一地标识表中的每一行,通过它可强制表的实体完整性;2、主要是用于其他表的外键关联,以及本记录的修改与删除。
【相关学习推荐:mysql教程】
mysql设置主键的作用是:
1、什么是主键
 数据库主键,指的是一个列或多列的组合,其值能唯一地标识表中的每一行,通过它可强制表的实体完整性。主键主要是用于其他表的外键关联,以及本记录的修改与删除。
2、主键的作用
主键是能确定一条记录的唯一标识,主键字段必须唯一,必须非空,一个表中只能有一个主键,主键可以包含一个或多个字段。
打个比方,一条记录包括身份正号,姓名,年龄,学校,国籍,性别等。身份证号是唯一能确定你这个人的,其他都可能有重复,所以,身份证号是主键。 
3、mysql创建一张没有主键的表
[root@node110 ~]# mysql -uroot -pyinzhengjiemysql: [warning] using a password on the command line interface can be insecure.welcome to the mysql monitor. commands end with ; or \g.your mysql connection id is 13server version: 8.0.14 mysql community server - gplcopyright (c) 2000, 2019, oracle and/or its affiliates. all rights reserved.oracle is a registered trademark of oracle corporation and/or itsaffiliates. other names may be trademarks of their respectiveowners.type 'help;' or '\h' for help. type '\c' to clear the current input statement.mysql> mysql> mysql> create database devops character set = utf8;query ok, 1 row affected, 1 warning (0.00 sec)mysql> mysql> mysql> use devopsdatabase changedmysql> show tables;empty set (0.00 sec)mysql> mysql> create table students(stu_id int(11),stu_name varchar(50),gender int(11));#首先,我们这里只是创建了一张极为普通的表。query ok, 0 rows affected (0.01 sec)mysql> mysql> insert into students values(1,'jason',10);#插入第一条数据query ok, 1 row affected (0.00 sec)mysql> mysql> insert into students values(2,'danny',20);#插入第二条数据query ok, 1 row affected (0.01 sec)mysql> mysql> insert into students values(1,'jenny',30); #插入第三条数据,注意!这个id和第一条插入的数据是相同的,别问我为什么这么干,我是故意这样搞的!query ok, 1 row affected (0.00 sec)mysql>mysql> select * from students;#我们查询咱们刚刚插入的三条数据+--------+----------+--------+| stu_id | stu_name | gender |+--------+----------+--------+| 1 | jason | 10 || 2 | danny | 20 || 1 | jenny | 30 |+--------+----------+--------+3 rows in set (0.00 sec)mysql>
4、创建一张含有主键的表(student_primary)
mysql> create table student_primary(stu_id int(11) primary key auto_increment,stu_name varchar(50),gender int(11));#仔细一下这个建表语句,除了和上面的表名不同,我还为stu_id字段加了主键属性,以及自动增长的属性!query ok, 0 rows affected (0.01 sec)mysql> mysql> insert into student_primary values(1,'json',10); #这里我们插入第一条数据query ok, 1 row affected (0.00 sec)mysql> mysql> insert into student_primary values(2,'danny',20);#这里我们插入第二条数据query ok, 1 row affected (0.01 sec)mysql> mysql> insert into student_primary values(1,'jenny',30); #这里我们插入第三条数据时,报错啦!提示主键重复!error 1062 (23000): duplicate entry '1' for key 'primary'mysql> mysql> mysql> select * from student_primary;#我们查看表中的数据,果不其然,只有两条数据!第三条数据没有被插入进来,因为它不符合我们定义的主键规则!主键必须唯一且非空!+--------+----------+--------+| stu_id | stu_name | gender |+--------+----------+--------+| 1 | json | 10 || 2 | danny | 20 |+--------+----------+--------+2 rows in set (0.00 sec)mysql>
5、在创建一张含有主键的表(course)
mysql> create table course(id int(11) primary key auto_increment,course_name varchar(30));query ok, 0 rows affected (0.02 sec)mysql> mysql> insert into course values(1,'chinese'); #插入第一条数据query ok, 1 row affected (0.01 sec)mysql> mysql> insert into course values(2,'english'); query ok, 1 row affected (0.00 sec)mysql> insert into course values(3,'mathematics'),(4,'physics'),(5,'chemistry'),(6,'biology');#咱们可以同时插入多条数据query ok, 4 rows affected (0.01 sec)records: 4 duplicates: 0 warnings: 0mysql> mysql> select * from course;+----+-------------+| id | course_name |+----+-------------+| 1 | chinese || 2 | english || 3 | mathematics || 4 | physics || 5 | chemistry || 6 | biology |+----+-------------+6 rows in set (0.00 sec)mysql>
以上就是mysql设置主键有什么用的详细内容。
其它类似信息

推荐信息