mysql设置主键有什么用


当前第2页 返回上一页

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

[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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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设置主键有什么用的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

mysql中blob字段过多怎么办

必看!php开发者常犯的10个mysql错误

mysql 存储引擎和索引相关知识总结

mysql优化总结-查询总条数

mysql学习笔记之数据的增、删、改实现方法

从mongodb开始浅谈数据库的知识

实例详解mysql存储过程

介绍提高mysql性能的7个技巧

访问mysql需要权限怎么办

为什么我的mysql服务没了?

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

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

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

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

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...