百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>数据库> mysql设定主键有什么用
分享文章到:

mysql设定主键有什么用

发布时间:09/01 来源:未知 浏览: 关键词:
mysql设定主键的作用是:1、独一地标识表中的每一行,通过它可强迫表的实体完备性;2、主如果用于其他表的外键关联,以及本记载的修改与删除。

mysql设定主键的作用是:1、独一地标识表中的每一行,通过它可强迫表的实体完备性;2、主如果用于其他表的外键关联,以及本记载的修改与删除。

【相干学习举荐:mysql教程】

mysql设定主键的作用是:

1、什么是主键

  数据库主键,指的是一个列或多列的组合,其值能独一地标识表中的每一行,通过它可强迫表的实体完备性。主键主如果用于其他表的外键关联,以及本记载的修改与删除。

2、主键的作用

  主键是能肯定一笔记录的独一标识,主键字段必需独一,必需非空,一个表中只能有一个主键,主键可以包括一个或多个字段。

  打个比如,一笔记录包含身份正号,姓名,年龄,学校,国籍,性别等。身份证号是独一能肯定你这个人的,其他都可能有反复,所以,身份证号是主键。

3、MySQL新建一张没有主键的表

[root@node110 ~]# mysql -uroot -pyinzhengjie
mysql: [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 13
Server version: 8.0.14 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
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 devops
Database changed
mysql> 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: 0
mysql> 
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设定主键有什么用的细致内容,更多请关注 百分百源码网 其它相干文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有152人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板