溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql設(shè)置主鍵的作用是什么

發(fā)布時(shí)間:2020-08-26 10:00:45 來源:億速云 閱讀:145 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章給大家分享的是有關(guān)mysql設(shè)置主鍵的作用是什么的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。

mysql設(shè)置主鍵的作用是:1、唯一地標(biāo)識(shí)表中的每一行,通過它可強(qiáng)制表的實(shí)體完整性;2、主要是用于其他表的外鍵關(guān)聯(lián),以及本記錄的修改與刪除。

mysql設(shè)置主鍵的作用是什么

mysql設(shè)置主鍵的作用是:

1、什么是主鍵

  數(shù)據(jù)庫主鍵,指的是一個(gè)列或多列的組合,其值能唯一地標(biāo)識(shí)表中的每一行,通過它可強(qiáng)制表的實(shí)體完整性。主鍵主要是用于其他表的外鍵關(guān)聯(lián),以及本記錄的修改與刪除。

2、主鍵的作用

  主鍵是能確定一條記錄的唯一標(biāo)識(shí),主鍵字段必須唯一,必須非空,一個(gè)表中只能有一個(gè)主鍵,主鍵可以包含一個(gè)或多個(gè)字段。

  打個(gè)比方,一條記錄包括身份正號(hào),姓名,年齡,學(xué)校,國籍,性別等。身份證號(hào)是唯一能確定你這個(gè)人的,其他都可能有重復(fù),所以,身份證號(hào)是主鍵。

3、MySQL創(chuàng)建一張沒有主鍵的表

[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));      #首先,我們這里只是創(chuàng)建了一張極為普通的表。
Query OK, 0 rows affected (0.01 sec)
mysql> 
mysql> INSERT INTO students VALUES(1,'jason',10);                          #插入第一條數(shù)據(jù)
Query OK, 1 row affected (0.00 sec)
mysql> 
mysql> INSERT INTO students VALUES(2,'danny',20);                          #插入第二條數(shù)據(jù)
Query OK, 1 row affected (0.01 sec)
mysql> 
mysql> INSERT INTO students VALUES(1,'jenny',30);                           #插入第三條數(shù)據(jù),注意!這個(gè)id和第一條插入的數(shù)據(jù)是相同的,別問我為什么這么干,我是故意這樣搞的!
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> SELECT * FROM students;                                      #我們查詢咱們剛剛插入的三條數(shù)據(jù)
+--------+----------+--------+
| stu_id | stu_name | gender |
+--------+----------+--------+
|      1 | jason    |     10 |
|      2 | danny    |     20 |
|      1 | jenny    |     30 |
+--------+----------+--------+
3 rows in set (0.00 sec)
mysql>

4、創(chuàng)建一張含有主鍵的表(student_primary)

mysql> CREATE TABLE student_primary(stu_id INT(11) PRIMARY KEY AUTO_INCREMENT,stu_name VARCHAR(50),gender INT(11));        #仔細(xì)一下這個(gè)建表語句,除了和上面的表名不同,我還為stu_id字段加了主鍵屬性,以及自動(dòng)增長的屬性!
Query OK, 0 rows affected (0.01 sec)
mysql> 
mysql> INSERT INTO student_primary VALUES(1,'json',10);                 #這里我們插入第一條數(shù)據(jù)
Query OK, 1 row affected (0.00 sec)
mysql> 
mysql> INSERT INTO student_primary VALUES(2,'danny',20);                #這里我們插入第二條數(shù)據(jù)
Query OK, 1 row affected (0.01 sec)
mysql> 
mysql> INSERT INTO student_primary VALUES(1,'jenny',30);                  #這里我們插入第三條數(shù)據(jù)時(shí),報(bào)錯(cuò)啦!提示主鍵重復(fù)!
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> 
mysql> 
mysql> SELECT * FROM student_primary;                            #我們查看表中的數(shù)據(jù),果不其然,只有兩條數(shù)據(jù)!第三條數(shù)據(jù)沒有被插入進(jìn)來,因?yàn)樗环衔覀兌x的主鍵規(guī)則!主鍵必須唯一且非空!
+--------+----------+--------+
| stu_id | stu_name | gender |
+--------+----------+--------+
|      1 | json     |     10 |
|      2 | danny    |     20 |
+--------+----------+--------+
2 rows in set (0.00 sec)
mysql>

5、在創(chuàng)建一張含有主鍵的表(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');                                     #插入第一條數(shù)據(jù)
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');        #咱們可以同時(shí)插入多條數(shù)據(jù)
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>

感謝各位的閱讀!關(guān)于mysql設(shè)置主鍵的作用是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI