首頁  >  文章  >  資料庫  >  MySQL資料庫約束及表格的設計實例分析

MySQL資料庫約束及表格的設計實例分析

WBOY
WBOY轉載
2023-05-29 22:40:051063瀏覽

資料庫約束

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資料庫約束及表格的設計實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除