首頁 > 軟體

MySQL範例講解資料庫約束以及表的設計

2022-06-16 14:02:20

資料庫約束

not null

指定某列的儲存不能為null值

create table student (id int not null,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

unique

保證某列必須有唯一的值,插入重複的值就會報錯

default

規定給列賦值時的預設值

create table student(id int,name varchar(20) default '匿名');

primary key 主鍵

主鍵約束,是not null 與unique的結合,確保某列的賦值不能為null,並且是唯一的

auto_increment 自增特點:

1.如果表中沒有記錄,自增從1開始

2.如果有資料,從上一條記錄往下自增

3.插入再刪掉資料,自增的值不會重複利用,會按刪掉的那條開始自增

create table student (id int primary key auto_increment,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into student values(null,'張三');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+--------+
| id | name   |
+----+--------+
|  1 | 張三   |
+----+--------+
1 row in set (0.00 sec)

foreign key 外來鍵

外來鍵約束,在表一中的資料必須在表二中存在,要參照完整性準則

外來鍵約束描述的是兩張表的兩個列之間的“依賴關係”

外來鍵約束會影響表的刪除,例如下面的範例的class表被關聯,所以它不能被輕易刪除

mysql> create table class (
    -> id int primary key,
    -> name varchar(20) not null
    -> );
Query OK, 0 rows affected (0.04 sec)
mysql> create table student (
    -> id int primary key,
    -> name varchar(20) not null,
    -> email varchar(20) default 'unknow',
    -> QQ varchar(20) unique,
    -> classId int , foreign key (classId) references class(id)
    -> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(20) | NO   |     | NULL    |       |
| email   | varchar(20) | YES  |     | unknow  |       |
| QQ      | varchar(20) | YES  | UNI | NULL    |       |
| classId | int(11)     | YES  | MUL | NULL    |       |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

check

指定一個條件,通過條件來對值進行判定

但是mysql並不支援

create table test_user (
   id int,
   name varchar(20),
   sex varchar(1),
   check (sex ='男' or sex='女')
);

表的設計

一對一

一對一設計表就比如學生表和賬戶表,一個賬戶對應到一個學生,一個學生也只有一個賬戶

表示方法

1.可以把這兩個實體用一張表來表示

2.可以用兩張表來表示,其中一張表包含了另一個表的id

一對多

一個學生應該處於一個班級中,一個班級可以包含多個學生

表示方法:

1.在班級表中,新增一列,表示這個班級裡的學生id都有啥(mysql沒有陣列型別,redis可以)

2.班級表不變,學生表中,新增一列classId

多對多

多對多設計表就好比學生表和課程表,一個學生可以選多個課程,一個課程也可以被多個學生選擇

表示方法 :

使用一個關聯表,來表示兩個實體之間的關係

多對多建表範例

-- 學生表
mysql> create table test_student (
    -> id int primary key,
    -> name varchar(10) default 'unknow'
    -> );
Query OK, 0 rows affected (0.03 sec)
-- 選課表
mysql> create table test_course (
    -> id int primary key,
    -> name varchar(20) default 'unknow'
    -> );
Query OK, 0 rows affected (0.02 sec)
-- 成績表
mysql> create table test_score (
    -> studentId int,
    -> courseId int,
    -> score int,
    -> foreign key (studentId) references test_student(id),
    -> foreign key (courseId) references test_course(id)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> desc test_student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(10) | YES  |     | unknow  |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc test_coures;
ERROR 1146 (42S02): Table 'java_5_27.test_coures' doesn't exist
mysql> desc test_course;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | unknow  |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc test_score;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| studentId | int(11) | YES  | MUL | NULL    |       |
| courseId  | int(11) | YES  | MUL | NULL    |       |
| score     | int(11) | YES  |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

插入資料到範例實現多對多

mysql> insert into test_student values (1, 'listen');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_course values (1, '數學');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_student values (2, 'Faker');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_course values (2, '數學');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_score values(1, 1, 90);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_score values (1, 2, 99);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_score values (2, 1, 50);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_score values (2, 2, 60);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test_student;
+----+--------+
| id | name   |
+----+--------+
|  1 | listen |
|  2 | Faker  |
+----+--------+
2 rows in set (0.00 sec)

mysql> select * from test_course;
+----+--------+
| id | name   |
+----+--------+
|  1 | 數學   |
|  2 | 語文   |
+----+--------+
2 rows in set (0.00 sec)

mysql> select * from test_score;
+-----------+----------+-------+
| studentId | courseId | score |
+-----------+----------+-------+
|         1 |        1 |    90 |
|         1 |        2 |    99 |
|         2 |        1 |    50 |
|         2 |        2 |    60 |
+-----------+----------+-------+
4 rows in set (0.00 sec)

到此這篇關於MySQL範例講解資料庫約束以及表的設計的文章就介紹到這了,更多相關MySQL資料庫約束內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com