原文鏈接http://zhhll.icu/2021/01/02/%E6%95%B0%E6%8D%AE%E5%BA%93/%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E7%BA%A6%E6%9D%9F/
MySQL約束
資料庫有六大約束,分別為
- NOT NULL 非空約束,用于保證這個欄位不能為空
- DEFAULT 默認約束,用于保證該欄位有默認值
- PRIMARY KEY 主鍵約束,用于保證該欄位可以唯一表示該行記錄,唯一且非空
- UNIQUE 唯一約束,用于保證該欄位的唯一性,可以為空
- CHECK 檢查約束(MySQL不支持該約束)
- FOREIGN KEY 外鍵約束,用于限制兩個表的關系,用于保證該欄位必須來自于主表關聯列的值(在從表中添加外鍵約束,用于參考主表中某列的值)
示例:
create table class(
id int primary key, #主鍵約束
name varchar(20) not null #非空約束
);
create table student(
id int,
name varchar(20) not null,
classid int,
sex int not null,
brith date,
constraint pk primary key(id), #主鍵約束
constraint fk_student_class foreign key(classid) references class(id) #外鍵
);
由于本身的博客百度沒有收錄,博客地址http://zhhll.icu
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/258799.html
標籤:其他
上一篇:SQL練習57:使用含有關鍵字exists查找未分配具體部門的員工的所有資訊
下一篇:node操作mysql
