1. 程式人生 > 資料庫 >MySql新增主鍵約束、新增外檢約束

MySql新增主鍵約束、新增外檢約束

新增主鍵約束

直接在建立表的時候新增主鍵約束:

create table author (author_id int(11) primary key, name varchar(45),email varchar(45));

新增外來鍵約束方式

當主鍵表已經存在,直接在建立外來鍵表的時候新增外來鍵約束

create table book (  
book_id int primary key, 
title nvarchar(128), 
description nvarchar(512),
published DATE , 
author_id int, 
price float, 
rating int,
constraint book_author_fk foreign key(author_id) references author(author_id));