MySQL 之 foreign key

來源:互聯網
上載者:User

標籤:之一   解釋   流程   cat   localhost   建立   包括   password   ref   

前段回顧

create table 表名(
欄位名1 類型[(寬度) 約束條件],
欄位名2 類型[(寬度) 約束條件],
欄位名3 類型[(寬度) 約束條件]
);

#解釋:
類型:使用限制欄位必須以什麼樣的資料類型傳值
約束條件:約束條件是在類型之外添加一種額外的限制

# 注意:
1. 在同一張表中,欄位名是不能相同
2. 寬度和約束條件可選,欄位名和類型是必須的
3、最後一個欄位後不加逗號

把所有資料都存放於一張表的弊端
1、表的組織圖複雜不清晰
2、浪費空間
3、擴充性極差

補充知識:
如果要清空表,使用truncate tb1;
作用:將整張表重設(包括id)

一、foreign key (重點)

表關係之多對一
1、尋找表與表之間的關係的套路

舉例:emp表 dep表
步驟一:
part1:
1、先站在左表emp的角度
2、去找左表emp的多條記錄能否對應右表dep的一條記錄
3、翻譯2的意義:
左表emp的多條記錄==》多個員工
右表dep的一條記錄==》一個部門

最終翻譯結果:多個員工是否可以屬於一個部門?
如果是則需要進行part2的流程

part2:
1、站在右表dep的角度
2、去找右表dep的多條記錄能否對應左表emp的一條記錄
3、翻譯2的意義:
右表dep的多條記錄==》多個部門
左表emp的一條記錄==》一個員工

最終翻譯結果:多個部門是否可以包含同一個員工

如果不可以,則可以確定emp與dep的關係只一個單向的多對一
如何??
在emp表中新增一個dep_id欄位,該欄位指向dep表的id欄位


# foreign key會帶來什麼樣的效果?
#1、約束1:在建立表時,先建被關聯的表dep,才能建關聯表emp

create table dep(
id int primary key auto_increment,
dep_name char(10),
dep_comment char(60)
);

create table emp(
id int primary key auto_increment,
name char(16),
gender enum(‘male‘,‘female‘) not null default ‘male‘,
dep_id int,
foreign key(dep_id) references dep(id)
);

#2、約束2:在插入記錄時,必須先插被關聯的表dep,才能插關聯表emp
insert into dep(dep_name,dep_comment) values
(‘sb教學部‘,‘sb輔導學生學習,教授python課程‘),
(‘外交部‘,‘老男孩上海校區駐張江形象大使‘),
(‘nb技術部‘,‘nb技術能力有限部門‘);


insert into emp(name,gender,dep_id) values
(‘alex‘,‘male‘,1),
(‘egon‘,‘male‘,2),
(‘lxx‘,‘male‘,1),
(‘wxx‘,‘male‘,1),
(‘wenzhou‘,‘female‘,3);


#3、約束3:更新與刪除都需要考慮到關聯與被關聯的關係
解決方案:
1、先刪除關聯表emp,再刪除被關聯表dep,準備重建
mysql> drop table emp;
Query OK, 0 rows affected (0.11 sec)

mysql> drop table dep;
Query OK, 0 rows affected (0.04 sec)


2、重建:新增功能,同步更新,同步刪除
create table dep(
id int primary key auto_increment,
dep_name char(10),
dep_comment char(60)
);

create table emp(
id int primary key auto_increment,
name char(16),
gender enum(‘male‘,‘female‘) not null default ‘male‘,
dep_id int,
foreign key(dep_id) references dep(id)
on update cascade
on delete cascade
);
insert into dep(dep_name,dep_comment) values
(‘sb教學部‘,‘sb輔導學生學習,教授python課程‘),
(‘外交部‘,‘老男孩上海校區駐張江形象大使‘),
(‘nb技術部‘,‘nb技術能力有限部門‘);


insert into emp(name,gender,dep_id) values
(‘alex‘,‘male‘,1),
(‘egon‘,‘male‘,2),
(‘lxx‘,‘male‘,1),
(‘wxx‘,‘male‘,1),
(‘wenzhou‘,‘female‘,3);


# 同步刪除
mysql> select * from dep;
+----+------------------+------------------------------------------------------------------------------------------+
| id | dep_name | dep_comment |
+----+------------------+------------------------------------------------------------------------------------------+
| 1 | sb教學部 | sb輔導學生學習,教授python課程 |
| 2 | 外交部 | 老男孩上海校區駐張江形象大使 |
| 3 | nb技術部 | nb技術能力有限部門 |
+----+------------------+------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql> select * from emp;
+----+------------------+--------+--------+
| id | name | gender | dep_id |
+----+------------------+--------+--------+
| 1 | alex | male | 1 |
| 2 | egon | male | 2 |
| 3 | lxx | male | 1 |
| 4 | wxx | male | 1 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
5 rows in set (0.00 sec)

mysql> delete from dep where id=1;
Query OK, 1 row affected (0.02 sec)

mysql> select * from dep;
+----+------------------+------------------------------------------------------------------------------------------+
| id | dep_name | dep_comment |
+----+------------------+------------------------------------------------------------------------------------------+
| 2 | 外交部 | 老男孩上海校區駐張江形象大使 |
| 3 | nb技術部 | nb技術能力有限部門 |
+----+------------------+------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> select * from emp;
+----+------------------+--------+--------+
| id | name | gender | dep_id |
+----+------------------+--------+--------+
| 2 | egon | male | 2 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
2 rows in set (0.00 sec)

#同步更新
mysql> select * from emp;
+----+------------------+--------+--------+
| id | name | gender | dep_id |
+----+------------------+--------+--------+
| 2 | egon | male | 2 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
2 rows in set (0.00 sec)

mysql> update dep set id=200 where id =2;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from dep;
+-----+------------------+------------------------------------------------------------------------------------------+
| id | dep_name | dep_comment |
+-----+------------------+------------------------------------------------------------------------------------------+
| 3 | nb技術部 | nb技術能力有限部門 |
| 200 | 外交部 | 老男孩上海校區駐張江形象大使 |
+-----+------------------+------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> select * from emp;
+----+------------------+--------+--------+
| id | name | gender | dep_id |
+----+------------------+--------+--------+
| 2 | egon | male | 200 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
2 rows in set (0.00 sec)


表關係之一對多


1、什麼是多對多
兩張表之間是一個雙向的多對一關聯性,稱之為多對多
如何??
建立第三張表,該表中有一個欄位fk左表的id,還有一個欄位是fk右表的id


create table author(
id int primary key auto_increment,
name char(16)
);

create table book(
id int primary key auto_increment,
bname char(16),
price int
);

insert into author(name) values
(‘egon‘),
(‘alex‘),
(‘wxx‘)
;
insert into book(bname,price) values
(‘python從入門到入土‘,200),
(‘葵花寶典切割到精通‘,800),
(‘九陰真經‘,500),
(‘九陽神功‘,100)
;


create table author2book(
id int primary key auto_increment,
author_id int,
book_id int,
foreign key(author_id) references author(id)
on update cascade
on delete cascade,
foreign key(book_id) references book(id)
on update cascade
on delete cascade
);

insert into author2book(author_id,book_id) values
(1,3),
(1,4),
(2,2),
(2,4),
(3,1),
(3,2),
(3,3),
(3,4);
表關係之一對一

左表的一條記錄唯一對應右表的一條記錄,反之也一樣
本質就是在對應的儲存id的欄位加unique 來保證唯一存在
create table customer(
id int primary key auto_increment,
name char(20) not null,
qq char(10) not null,
phone char(16) not null
);

create table student(
id int primary key auto_increment,
class_name char(20) not null,
customer_id int unique, #該欄位一定要是唯一的
foreign key(customer_id) references customer(id) #外鍵的欄位一定要保證unique
on delete cascade
on update cascade
);

insert into customer(name,qq,phone) values
(‘李飛機‘,‘31811231‘,13811341220),
(‘王大炮‘,‘123123123‘,15213146809),
(‘守榴彈‘,‘283818181‘,1867141331),
(‘吳坦克‘,‘283818181‘,1851143312),
(‘贏火箭‘,‘888818181‘,1861243314),
(‘戰地雷‘,‘112312312‘,18811431230)
;


#增加學生
insert into student(class_name,customer_id) values
(‘脫產3班‘,3),
(‘周末19期‘,4),
(‘周末19期‘,5)
;


二 插入資料INSERT


1. 插入完整資料(順序插入)
文法一:
INSERT INTO 表名(欄位1,欄位2,欄位3…欄位n) VALUES(值1,值2,值3…值n);

文法二:
INSERT INTO 表名 VALUES (值1,值2,值3…值n);

2. 指定欄位插入資料
文法:
INSERT INTO 表名(欄位1,欄位2,欄位3…) VALUES (值1,值2,值3…);

3. 插入多條記錄
文法:
INSERT INTO 表名 VALUES
(值1,值2,值3…值n),
(值1,值2,值3…值n),
(值1,值2,值3…值n);

4. 插入查詢結果
文法:
INSERT INTO 表名(欄位1,欄位2,欄位3…欄位n)
SELECT (欄位1,欄位2,欄位3…欄位n) FROM 表2
WHERE …;

三 更新資料UPDATE


文法:
UPDATE 表名 SET
欄位1=值1,
欄位2=值2,
WHERE CONDITION;

樣本:
UPDATE mysql.user SET password=password(‘123’)
where user=’root’ and host=’localhost’;

四 刪除資料DELETE


文法:
DELETE FROM 表名
WHERE CONITION;

樣本:
DELETE FROM mysql.user
WHERE password=’’;

練習:
更新MySQL root使用者密碼為mysql123
刪除除從本地登入的root使用者以外的所有使用者

MySQL 之 foreign key

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.